Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions frontend/src/api/Language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ enum Language {
Python = 'PYTHON',
// Ruby = 'RUBY',
// Swift = 'SWIFT',
// CPP = 'CPP',
CPP = 'CPP',
// PHP = 'PHP',
// C = 'C',
Java = 'JAVA',
Expand Down Expand Up @@ -31,8 +31,8 @@ export const languageToEditorLanguage = (key: Language): string => {
return 'python';
// case Language.Ruby:
// return 'ruby';
// case Language.CPP:
// return 'c++';
case Language.CPP:
return 'c++';
// case Language.PHP:
// return 'php';
// case Language.C:
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<optional>true</optional>
</dependency>
<!-- Encryption for database configuration -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum ProblemError implements ApiError {
INCORRECT_INPUT_COUNT(HttpStatus.BAD_REQUEST, "Please specify the correct number of parameters for this problem."),
INVALID_INPUT(HttpStatus.BAD_REQUEST, "Please ensure each line of test case input/output is valid and is of the correct type."),
INVALID_NUMBER_REQUEST(HttpStatus.BAD_REQUEST, "Please request a valid number of problems (between 1-10)."),
INVALID_VARIABLE_NAME(HttpStatus.BAD_REQUEST, "Please ensure all variable names are valid for Java and Python."),
INVALID_VARIABLE_NAME(HttpStatus.BAD_REQUEST, "Please ensure all variable names are valid for Java, Python, and C++."),
EMPTY_FIELD(HttpStatus.BAD_REQUEST, "Please enter a value for each required field."),
INTERNAL_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "An internal error occurred when attempting to find a problem."),
NOT_ENOUGH_FOUND(HttpStatus.NOT_FOUND, "Not enough problems could be found with the given criteria."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.codejoust.main.service.generators;

import java.util.List;

import com.codejoust.main.exception.ProblemError;
import com.codejoust.main.exception.api.ApiException;
import com.codejoust.main.model.problem.ProblemIOType;
import com.codejoust.main.model.problem.ProblemInput;
import com.codejoust.main.model.report.CodeLanguage;

import org.springframework.stereotype.Service;

@Service
public class CppDefaultCodeGeneratorService implements DefaultCodeGeneratorService {

@Override
public String getDefaultCode(List<ProblemInput> problemInputs, ProblemIOType outputType) {
boolean needStringImport = false;
boolean needVectorImport = false;

if (outputType == ProblemIOType.ARRAY_STRING) {
needStringImport = true;
needVectorImport = true;
} else if (outputType == ProblemIOType.STRING) {
needStringImport = true;
} else if (outputType.getClassType().isArray()) {
needVectorImport = true;
}

// Initialize method line StringBuilder with the output type.
StringBuilder methodLineBuilder = new StringBuilder();
methodLineBuilder.append(String.format("\t\t%s solve(", typeInstantiationToString(outputType)));

// Add all of the method inputs and names.
String prefix = "";
for (ProblemInput problemInput : problemInputs) {
if (problemInput.getType() == ProblemIOType.ARRAY_STRING) {
needStringImport = true;
needVectorImport = true;
} else if (problemInput.getType() == ProblemIOType.STRING) {
needStringImport = true;
} else if (problemInput.getType().getClassType().isArray()) {
needVectorImport = true;
}

methodLineBuilder.append(prefix);
prefix = ", ";
methodLineBuilder.append(String.format("%s %s",
typeInstantiationToString(problemInput.getType()),
problemInput.getName()
));
}
methodLineBuilder.append(") {");

StringBuilder importLineBuilder = new StringBuilder();
if (needStringImport) {
importLineBuilder.append("#include <string>\n");
} else if (needVectorImport) {
importLineBuilder.append("#include <vector>\n");
}

return String.join("\n",
importLineBuilder.toString(),
"using namespace std;",
"",
"class Solution {",
"\tpublic:",
methodLineBuilder.toString(),
"\t\t\t",
"\t\t}",
"}",
""
);
}

@Override
public String typeInstantiationToString(ProblemIOType ioType) {
if (ioType == null) {
throw new ApiException(ProblemError.BAD_IOTYPE);
}

switch (ioType) {
case STRING:
return "string";
case INTEGER:
return "int";
case DOUBLE:
return "double";
case CHARACTER:
return "char";
case BOOLEAN:
return "bool";
case ARRAY_STRING:
return "vector<string>";
case ARRAY_INTEGER:
return "vector<int>";
case ARRAY_DOUBLE:
return "vector<double>";
case ARRAY_CHARACTER:
return "vector<char>";
case ARRAY_BOOLEAN:
return "vector<bool>";
default:
throw new ApiException(ProblemError.BAD_IOTYPE);
}
}

@Override
public CodeLanguage getLanguage() {
return CodeLanguage.CPP;
}
}
17 changes: 16 additions & 1 deletion src/test/java/com/codejoust/main/api/ProblemTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class ProblemTests {
"\t\t"
).replaceAll("\t", " ");

public static final String cppDefaultCode = String.join("\n",
"#include <vector>",
"",
"using namespace std;",
"",
"class Solution {",
"\tpublic:",
"\t\tvector<int> solve(vector<int> nums) {",
"\t\t\t",
"\t\t}",
"}",
""
).replaceAll("\t", " ");

@Test
public void getProblemNonExistent() throws Exception {
ApiError ERROR = ProblemError.NOT_FOUND;
Expand Down Expand Up @@ -379,7 +393,8 @@ public void getDefaultCodeSuccess() throws Exception {

assertEquals(javaDefaultCode, actual.get(CodeLanguage.JAVA));
assertEquals(pythonDefaultCode, actual.get(CodeLanguage.PYTHON));
assertEquals(2, actual.size());
assertEquals(cppDefaultCode, actual.get(CodeLanguage.CPP));
assertEquals(3, actual.size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class DefaultCodeGeneratorServiceTests {
@InjectMocks
private PythonDefaultCodeGeneratorService pythonDefaultCodeGeneratorService;

@Spy
@InjectMocks
private CppDefaultCodeGeneratorService cppDefaultCodeGeneratorService;

private static final String javaDefaultCode = String.join("\n",
"import java.util.*;",
"",
Expand All @@ -43,6 +47,20 @@ public class DefaultCodeGeneratorServiceTests {
"\t\t"
);

private static final String cppDefaultCode = String.join("\n",
"#include <vector>",
"",
"using namespace std;",
"",
"class Solution {",
"\tpublic:",
"\t\tvector<int> solve(vector<int> nums) {",
"\t\t\t",
"\t\t}",
"}",
""
);

/**
* Helper method to test the "getDefaultCode" method across languages.
*
Expand All @@ -65,4 +83,9 @@ public void getDefaultCodeJava() {
public void getDefaultCodePython() {
getDefaultCodeSetupMethod(pythonDefaultCodeGeneratorService, pythonDefaultCode);
}

@Test
public void getDefaultCodeCpp() {
getDefaultCodeSetupMethod(cppDefaultCodeGeneratorService, cppDefaultCode);
}
}