Skip to content

Commit 82361e3

Browse files
authored
Expand bytecode integration test coverage (#4231)
1 parent ad7c4ff commit 82361e3

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

vm/tests/src/test/java/com/codename1/tools/translator/BytecodeInstructionIntegrationTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ void translatesOptimizedBytecodeToLLVMExecutable() throws Exception {
7070
assertTrue(generatedCode.contains("CustomJump */"), "Optimized comparisons should emit CustomJump code");
7171
assertTrue(generatedCode.contains("BC_IINC"), "Increment operations should translate to BC_IINC macro");
7272
assertTrue(generatedCode.contains("VarOp.assignFrom"), "Optimized stores should rely on CustomIntruction output");
73+
assertTrue(generatedCode.contains("switch((*SP).data.i)"), "SwitchInstruction should emit a native switch statement");
74+
assertTrue(generatedCode.contains("BC_DUP(); /* DUP */"), "DupExpression should translate DUP operations to BC_DUP");
7375

7476
CleanTargetIntegrationTest.replaceLibraryWithExecutableTarget(cmakeLists, srcRoot.getFileName().toString());
7577

@@ -88,7 +90,7 @@ void translatesOptimizedBytecodeToLLVMExecutable() throws Exception {
8890

8991
Path executable = buildDir.resolve("CustomBytecodeApp");
9092
String output = CleanTargetIntegrationTest.runCommand(Arrays.asList(executable.toString()), buildDir);
91-
assertTrue(output.contains("RESULT=14"), "Compiled program should print the expected arithmetic result");
93+
assertTrue(output.contains("RESULT=54"), "Compiled program should print the expected arithmetic result");
9294
}
9395

9496
private Path findGeneratedSource(Path srcRoot) throws Exception {
@@ -116,10 +118,30 @@ private String appSource() {
116118
" int result = min + counter;\n" +
117119
" return result;\n" +
118120
" }\n" +
121+
" private static int switchComputation(int value) {\n" +
122+
" switch (value) {\n" +
123+
" case 4:\n" +
124+
" return value + 10;\n" +
125+
" case 10:\n" +
126+
" return value + 5;\n" +
127+
" default:\n" +
128+
" return value - 1;\n" +
129+
" }\n" +
130+
" }\n" +
131+
" private static int synchronizedIncrement(int base) {\n" +
132+
" Object lock = new Object();\n" +
133+
" int result = base;\n" +
134+
" synchronized (lock) {\n" +
135+
" result++;\n" +
136+
" }\n" +
137+
" return result;\n" +
138+
" }\n" +
119139
" public static void main(String[] args) {\n" +
120140
" int first = optimizedComputation(1, 3);\n" +
121141
" int second = optimizedComputation(5, 2);\n" +
122-
" report(first + second);\n" +
142+
" int switched = switchComputation(first) + switchComputation(second);\n" +
143+
" int synchronizedValue = synchronizedIncrement(second);\n" +
144+
" report(first + second + switched + synchronizedValue);\n" +
123145
" }\n" +
124146
"}\n";
125147
}

vm/tests/src/test/java/com/codename1/tools/translator/CleanTargetIntegrationTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ static void patchCn1Globals(Path srcRoot) throws IOException {
171171
content = content.replace("#ifdef __OBJC__\n", "#ifdef __OBJC__\n@class NSString;\n");
172172
Files.write(cn1Globals, content.getBytes(StandardCharsets.UTF_8));
173173
}
174+
if (!content.contains("#include <string.h>")) {
175+
content = content.replace("#include <stdlib.h>\n", "#include <stdlib.h>\n#include <string.h>\n");
176+
Files.write(cn1Globals, content.getBytes(StandardCharsets.UTF_8));
177+
}
174178
}
175179

176180
static void writeRuntimeStubs(Path srcRoot) throws IOException {
@@ -187,11 +191,12 @@ static void writeRuntimeStubs(Path srcRoot) throws IOException {
187191
"\n" +
188192
"static void initThreadState() {\n" +
189193
" memset(&globalThreadData, 0, sizeof(globalThreadData));\n" +
190-
" globalThreadData.threadObjectStack = calloc(64, sizeof(struct elementStruct));\n" +
191-
" globalThreadData.pendingHeapAllocations = calloc(64, sizeof(void*));\n" +
192-
" globalThreadData.callStackClass = calloc(64, sizeof(int));\n" +
193-
" globalThreadData.callStackLine = calloc(64, sizeof(int));\n" +
194-
" globalThreadData.callStackMethod = calloc(64, sizeof(int));\n" +
194+
" globalThreadData.blocks = calloc(CN1_MAX_STACK_CALL_DEPTH, sizeof(struct TryBlock));\n" +
195+
" globalThreadData.threadObjectStack = calloc(CN1_MAX_OBJECT_STACK_DEPTH, sizeof(struct elementStruct));\n" +
196+
" globalThreadData.pendingHeapAllocations = calloc(CN1_MAX_OBJECT_STACK_DEPTH, sizeof(void*));\n" +
197+
" globalThreadData.callStackClass = calloc(CN1_MAX_STACK_CALL_DEPTH, sizeof(int));\n" +
198+
" globalThreadData.callStackLine = calloc(CN1_MAX_STACK_CALL_DEPTH, sizeof(int));\n" +
199+
" globalThreadData.callStackMethod = calloc(CN1_MAX_STACK_CALL_DEPTH, sizeof(int));\n" +
195200
"}\n" +
196201
"\n" +
197202
"struct ThreadLocalData* getThreadLocalData() {\n" +
@@ -237,6 +242,22 @@ static void writeRuntimeStubs(Path srcRoot) throws IOException {
237242
"\n" +
238243
"void monitorExit(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT obj) { (void)obj; }\n" +
239244
"\n" +
245+
"struct elementStruct* pop(struct elementStruct** sp) {\n" +
246+
" (*sp)--;\n" +
247+
" return *sp;\n" +
248+
"}\n" +
249+
"\n" +
250+
"void popMany(CODENAME_ONE_THREAD_STATE, int count, struct elementStruct** sp) {\n" +
251+
" while (count-- > 0) {\n" +
252+
" (*sp)--;\n" +
253+
" }\n" +
254+
"}\n" +
255+
"\n" +
256+
"void throwException(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT obj) {\n" +
257+
" (void)obj;\n" +
258+
" exit(1);\n" +
259+
"}\n" +
260+
"\n" +
240261
"struct clazz class__java_lang_Class = {0};\n" +
241262
"int currentGcMarkValue = 1;\n";
242263

0 commit comments

Comments
 (0)