-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added cpp executor and cpp searcher (#143)
* added cpp executor and cpp searcher * improved regular expression for cpp_searcher.java * Update CppExecutor.java reformat compilation command
- Loading branch information
1 parent
94a1452
commit 833d52c
Showing
3 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.hyperskill.hstest.testing.execution.process; | ||
|
||
import org.hyperskill.hstest.testing.execution.ProcessExecutor; | ||
import org.hyperskill.hstest.testing.execution.searcher.CppSearcher; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hyperskill.hstest.common.FileUtils.abspath; | ||
import static org.hyperskill.hstest.common.OsUtils.isWindows; | ||
|
||
/** | ||
* Executes C++ runnable files | ||
* (files with main function) | ||
* in the given directory. | ||
* | ||
*/ | ||
public class CppExecutor extends ProcessExecutor { | ||
private final String executable; | ||
private final String filename; | ||
|
||
public CppExecutor(String sourceName) { | ||
super(new CppSearcher().find(sourceName)); | ||
|
||
var fileName = runnable.getFile().getName(); | ||
|
||
var withoutCpp = fileName | ||
.substring(0, fileName.length() - new CppSearcher().extension().length()); | ||
|
||
if (isWindows()) { | ||
executable = withoutCpp; | ||
filename = executable + ".exe"; | ||
} else { | ||
executable = "./" + withoutCpp; | ||
filename = withoutCpp; | ||
} | ||
} | ||
|
||
@Override | ||
protected List<String> compilationCommand() { | ||
return List.of("g++", "-std", "c++20", "-pipe", "-O2", "-static", "-o", filename, runnable.getFile().getName()); | ||
} | ||
|
||
@Override | ||
protected String filterCompilationError(String error) { | ||
// Adapt error filtering if needed | ||
return error; | ||
} | ||
|
||
@Override | ||
protected List<String> executionCommand(List<String> args) { | ||
List<String> fullArgs = new ArrayList<>(); | ||
fullArgs.add(executable); | ||
fullArgs.addAll(args); | ||
|
||
return fullArgs; | ||
} | ||
|
||
@Override | ||
protected void cleanup() { | ||
try { | ||
Files.deleteIfExists(Paths.get(abspath(filename))); | ||
} catch (IOException ignored) { } | ||
} | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
src/main/java/org/hyperskill/hstest/testing/execution/searcher/CppSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.hyperskill.hstest.testing.execution.searcher; | ||
|
||
import org.hyperskill.hstest.testing.execution.runnable.RunnableFile; | ||
|
||
/** | ||
* Searches for C++ runnable files | ||
* (files with main function) | ||
* in the given directory | ||
* and returns the first one found. | ||
*/ | ||
public class CppSearcher extends BaseSearcher { | ||
@Override | ||
public String extension() { | ||
return ".cpp"; | ||
} | ||
|
||
@Override | ||
public RunnableFile search(String whereToSearch) { | ||
return simpleSearch(whereToSearch, | ||
"int main()", | ||
"(^|\\n)\\s*int\\s+main\\s*\\(.*\\)" | ||
); | ||
} | ||
} |