Skip to content

Commit bab7bd2

Browse files
authored
Update CodPTACParityRunner.java
1 parent 04bb97e commit bab7bd2

1 file changed

Lines changed: 189 additions & 72 deletions

File tree

src/main/java/cod/runner/CodPTACParityRunner.java

Lines changed: 189 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,104 +9,174 @@
99
import cod.ptac.CodPTACArtifact;
1010
import cod.ptac.CodPTACExecutor;
1111
import cod.ptac.CodPTACOptions;
12-
1312
import java.io.ByteArrayOutputStream;
1413
import java.io.File;
1514
import java.io.PrintStream;
1615
import java.util.ArrayList;
1716
import java.util.List;
17+
import java.util.Scanner;
1818

1919
public final class CodPTACParityRunner extends BaseRunner {
20-
private static final String[] DEFAULT_CASES = new String[] {
21-
"src/main/cod/src/main/test/Basic.cod",
22-
"src/main/cod/src/main/test/Loop.cod",
23-
"src/main/cod/src/main/test/LazyLoop.cod",
24-
"src/main/cod/src/main/test/Lambda.cod",
25-
"src/main/cod/src/main/test/TailCallOptimization.cod",
26-
"src/main/cod/src/main/test/Import.cod",
27-
"src/main/cod/src/main/test/LinearRecurrenceOptimization.cod"
20+
21+
private final String androidPath = "/storage/emulated/0";
22+
private final String baseTestPath = "/JavaNIDE/Programming-Language/Coderive/app/src/main/cod/src/main/test/";
23+
24+
private final String[] DEFAULT_CASES = new String[] {
25+
androidPath + baseTestPath + "Basic.cod",
26+
androidPath + baseTestPath + "Loop.cod",
27+
androidPath + baseTestPath + "LazyLoop.cod",
28+
androidPath + baseTestPath + "Lambda.cod",
29+
androidPath + baseTestPath + "TailCallOptimization.cod",
30+
androidPath + baseTestPath + "Import.cod",
31+
androidPath + baseTestPath + "LinearRecurrenceOptimization.cod"
2832
};
2933

3034
@Override
3135
public void run(String[] args) throws Exception {
3236
List<String> files = new ArrayList<String>();
37+
3338
if (args != null && args.length > 0) {
3439
for (String arg : args) {
35-
if (arg != null && !arg.trim().isEmpty()) {
36-
files.add(resolvePath(arg));
40+
if (arg != null && !arg.trim().isEmpty() && !arg.startsWith("-")) {
41+
files.add(arg);
3742
}
3843
}
39-
} else {
40-
for (String file : DEFAULT_CASES) files.add(resolvePath(file));
44+
}
45+
46+
if (files.isEmpty()) {
47+
// Check if Android default path exists
48+
File lazyLoop = new File(androidPath + baseTestPath + "LazyLoop.cod");
49+
if (lazyLoop.exists()) {
50+
System.out.println("Found Android test directory at: " + androidPath + baseTestPath);
51+
System.out.print("Use Android default tests? (y/n): ");
52+
System.out.flush();
53+
54+
Scanner scanner = new Scanner(System.in);
55+
String response = scanner.nextLine().trim().toLowerCase();
56+
57+
if (response.equals("y") || response.equals("yes")) {
58+
for (String file : DEFAULT_CASES) {
59+
File f = new File(file);
60+
if (f.exists()) {
61+
files.add(file);
62+
}
63+
}
64+
} else {
65+
System.out.print("Enter test file path: ");
66+
String userFile = scanner.nextLine().trim();
67+
if (!userFile.isEmpty()) {
68+
files.add(userFile);
69+
}
70+
}
71+
} else {
72+
System.out.println("No Android test directory found at: " + androidPath + baseTestPath);
73+
System.out.print("Enter test file path: ");
74+
System.out.flush();
75+
76+
Scanner scanner = new Scanner(System.in);
77+
String userFile = scanner.nextLine().trim();
78+
if (!userFile.isEmpty()) {
79+
files.add(userFile);
80+
}
81+
}
82+
}
83+
84+
if (files.isEmpty()) {
85+
System.out.println("No test files specified.");
86+
return;
4187
}
4288

4389
int passed = 0;
44-
for (String file : files) {
45-
String astOut = runAstPath(file);
46-
String ptacOut = runCodPTACPath(file);
47-
if (!normalize(astOut).equals(normalize(ptacOut))) {
48-
throw new RuntimeException(
49-
"CodP-TAC parity mismatch for " + file + "\nAST:\n" + astOut + "\nPTAC:\n" + ptacOut);
90+
int failed = 0;
91+
List<String> failures = new ArrayList<String>();
92+
93+
System.out.println();
94+
System.out.println("CodP-TAC Parity Validation");
95+
System.out.println("=========================");
96+
System.out.println("Running " + files.size() + " test(s)...");
97+
System.out.println();
98+
99+
for (int i = 0; i < files.size(); i++) {
100+
String file = files.get(i);
101+
String shortName = new File(file).getName();
102+
103+
System.out.print("[" + (i + 1) + "/" + files.size() + "] Testing " + shortName + "... ");
104+
System.out.flush();
105+
106+
try {
107+
String astOut = runAstPath(file);
108+
String ptacOut = runCodPTACPath(file);
109+
110+
if (normalize(astOut).equals(normalize(ptacOut))) {
111+
System.out.println("PASSED");
112+
passed++;
113+
} else {
114+
System.out.println("FAILED");
115+
failed++;
116+
failures.add(shortName);
117+
}
118+
} catch (Exception e) {
119+
System.out.println("ERROR: " + e.getMessage());
120+
failed++;
121+
failures.add(shortName + " - " + e.getMessage());
50122
}
51-
passed++;
123+
124+
System.out.flush();
52125
}
53126

54-
System.out.println("CodP-TAC parity passed: " + passed + " case(s)");
127+
System.out.println();
128+
System.out.println("=========================");
129+
System.out.println("Results: " + passed + " passed, " + failed + " failed, " + files.size() + " total");
130+
131+
if (!failures.isEmpty()) {
132+
System.out.println();
133+
System.out.println("Failed tests:");
134+
for (String failure : failures) {
135+
System.out.println(" - " + failure);
136+
}
137+
}
138+
139+
System.out.println();
140+
System.out.println("Parity check complete.");
55141
}
56142

57143
private String runAstPath(String file) throws Exception {
58-
final Interpreter interpreter = new Interpreter();
144+
Interpreter interpreter = new Interpreter();
59145
interpreter.setFilePath(file);
60-
final Program ast = parse(file, interpreter);
61-
return captureOutput(new Runnable() {
62-
@Override
63-
public void run() {
64-
interpreter.run(ast);
65-
}
66-
});
146+
Program ast = parse(file, interpreter);
147+
return captureOutput(interpreter, ast);
67148
}
68149

69150
private String runCodPTACPath(String file) throws Exception {
70-
final Interpreter interpreter = new Interpreter();
151+
Interpreter interpreter = new Interpreter();
71152
interpreter.setFilePath(file);
72-
final Program ast = parse(file, interpreter);
153+
Program ast = parse(file, interpreter);
154+
73155
if (ast == null || ast.unit == null || ast.unit.types == null || ast.unit.types.isEmpty()) {
74-
return captureOutput(new Runnable() {
75-
@Override
76-
public void run() {
77-
interpreter.run(ast);
78-
}
79-
});
156+
return captureOutput(interpreter, ast);
80157
}
81158

82159
String projectRoot = Index.getProjectRoot();
83160
if (projectRoot == null) {
84161
projectRoot = new File(".").getAbsoluteFile().getAbsolutePath();
85162
}
86-
final IRManager manager = new IRManager(projectRoot);
87-
final String unitName = ast.unit.name;
88-
final Type entryType = findMainType(ast);
163+
164+
IRManager manager = new IRManager(projectRoot);
165+
String unitName = ast.unit.name;
166+
Type entryType = findMainType(ast);
167+
89168
if (entryType == null) {
90-
return captureOutput(new Runnable() {
91-
@Override
92-
public void run() {
93-
interpreter.run(ast);
94-
}
95-
});
169+
return captureOutput(interpreter, ast);
96170
}
171+
97172
manager.save(unitName, entryType);
98-
final CodPTACArtifact artifact = manager.loadArtifact(unitName, entryType.name);
173+
CodPTACArtifact artifact = manager.loadArtifact(unitName, entryType.name);
174+
99175
if (artifact == null) {
100-
throw new RuntimeException("Failed to load CodP-TAC artifact for parity: " + file);
176+
throw new Exception("Failed to load CodP-TAC artifact for: " + file);
101177
}
102178

103-
return captureOutput(new Runnable() {
104-
@Override
105-
public void run() {
106-
new CodPTACExecutor(CodPTACOptions.compileExecuteWithFallback(true))
107-
.execute(artifact, interpreter);
108-
}
109-
});
179+
return captureOutputPTAC(artifact, interpreter);
110180
}
111181

112182
private Type findMainType(Program ast) {
@@ -125,38 +195,85 @@ private Type findMainType(Program ast) {
125195
return ast.unit.types.get(0);
126196
}
127197

128-
private String captureOutput(Runnable runnable) {
198+
private String captureOutput(Interpreter interpreter, Program ast) {
129199
PrintStream oldOut = System.out;
130-
ByteArrayOutputStream output = new ByteArrayOutputStream();
131-
PrintStream replacement = new PrintStream(output);
200+
PrintStream oldErr = System.err;
201+
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
202+
ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
203+
PrintStream outReplacement = new PrintStream(outBuffer);
204+
PrintStream errReplacement = new PrintStream(errBuffer);
205+
132206
try {
133-
System.setOut(replacement);
134-
runnable.run();
207+
System.setOut(outReplacement);
208+
System.setErr(errReplacement);
209+
interpreter.run(ast);
210+
outReplacement.flush();
211+
errReplacement.flush();
212+
213+
String output = outBuffer.toString();
214+
String error = errBuffer.toString();
215+
216+
if (error != null && !error.trim().isEmpty()) {
217+
output = output + "\n[STDERR]\n" + error;
218+
}
219+
return output;
135220
} finally {
136-
replacement.flush();
137-
replacement.close();
221+
outReplacement.close();
222+
errReplacement.close();
138223
System.setOut(oldOut);
224+
System.setErr(oldErr);
139225
}
140-
return output.toString();
141226
}
142227

143-
private String normalize(String text) {
144-
if (text == null) return "";
145-
return text.replace("\r", "").trim();
228+
private String captureOutputPTAC(CodPTACArtifact artifact, Interpreter interpreter) {
229+
PrintStream oldOut = System.out;
230+
PrintStream oldErr = System.err;
231+
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
232+
ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
233+
PrintStream outReplacement = new PrintStream(outBuffer);
234+
PrintStream errReplacement = new PrintStream(errBuffer);
235+
236+
try {
237+
System.setOut(outReplacement);
238+
System.setErr(errReplacement);
239+
new CodPTACExecutor(CodPTACOptions.compileExecuteWithFallback(true))
240+
.execute(artifact, interpreter);
241+
outReplacement.flush();
242+
errReplacement.flush();
243+
244+
String output = outBuffer.toString();
245+
String error = errBuffer.toString();
246+
247+
if (error != null && !error.trim().isEmpty()) {
248+
output = output + "\n[STDERR]\n" + error;
249+
}
250+
return output;
251+
} finally {
252+
outReplacement.close();
253+
errReplacement.close();
254+
System.setOut(oldOut);
255+
System.setErr(oldErr);
256+
}
146257
}
147258

148-
private String resolvePath(String path) {
149-
if (path == null) return null;
150-
File file = new File(path);
151-
return file.isAbsolute() ? file.getAbsolutePath() : file.getAbsoluteFile().getAbsolutePath();
259+
private String normalize(String text) {
260+
if (text == null) return "";
261+
String normalized = text.replace("\r\n", "\n").replace("\r", "\n");
262+
String[] lines = normalized.split("\n");
263+
StringBuilder sb = new StringBuilder();
264+
for (String line : lines) {
265+
sb.append(line.trim()).append("\n");
266+
}
267+
return sb.toString().trim();
152268
}
153269

154270
public static void main(String[] args) {
271+
CodPTACParityRunner runner = new CodPTACParityRunner();
155272
try {
156-
new CodPTACParityRunner().run(args);
273+
runner.run(args);
157274
} catch (Exception e) {
275+
System.err.println("Error: " + e.getMessage());
158276
e.printStackTrace();
159-
System.exit(1);
160277
}
161278
}
162279
}

0 commit comments

Comments
 (0)