Skip to content

Commit 94bd37f

Browse files
committed
feat(tests): add opcode round-trip and value tests; update CMake configuration
1 parent a16ae93 commit 94bd37f

7 files changed

Lines changed: 145 additions & 3 deletions

File tree

BASELINE_REPORT.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ If you want, I can:
4747
- Write `ir/opcodes.md` and an initial `TICKET-001` draft in `tickets/`.
4848
- Create a minimal benchmark harness under `tools/bench/` to start profiling.
4949

50+
Progress updates (2025-12-13)
51+
- Added GitHub Actions CI workflow and cross-platform scripts.
52+
- Created `tests/test_opcode_roundtrip.c` and `tests/test_opcode_values.c` and wired them into CMake.
53+
- Added a micro-benchmark `tools/bench/bench_simple.c` and CMake target.
54+
5055
---
5156
Generated by automation on behalf of the ProXPL modernization plan.

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -g")
77
# Include directories
88
include_directories(include)
99

10+
# Source files
1011
# Source files
1112
file(GLOB_RECURSE SOURCES "src/*.c")
1213

14+
# Core library (exposed to tests and tools)
15+
add_library(prox_core STATIC ${SOURCES})
16+
target_include_directories(prox_core PUBLIC ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src)
17+
1318
# Executable
14-
add_executable(prox ${SOURCES})
19+
add_executable(prox src/main.c)
20+
target_link_libraries(prox PRIVATE prox_core)
21+
22+
# Optional targets: tools and tests
23+
add_subdirectory(tests)
24+
add_subdirectory(tools/bench)
1525

1626
# Tests
1727
enable_testing()

tests/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
cmake_minimum_required(VERSION 3.10)
22

3-
file(GLOB_RECURSE PROX_SRC "${CMAKE_SOURCE_DIR}/src/*.c")
4-
add_library(prox_core OBJECT ${PROX_SRC})
53

64
add_executable(bytecode_tests bytecode_tests.c)
75
target_link_libraries(bytecode_tests PRIVATE prox_core)
86
target_include_directories(bytecode_tests PRIVATE ${CMAKE_SOURCE_DIR}/include)
97

108
add_test(NAME BytecodeTests COMMAND bytecode_tests)
9+
10+
add_executable(test_opcode_roundtrip test_opcode_roundtrip.c)
11+
target_link_libraries(test_opcode_roundtrip PRIVATE prox_core)
12+
target_include_directories(test_opcode_roundtrip PRIVATE ${CMAKE_SOURCE_DIR}/include)
13+
add_test(NAME OpcodeRoundTrip COMMAND test_opcode_roundtrip)
14+
15+
add_executable(test_opcode_values test_opcode_values.c)
16+
target_link_libraries(test_opcode_values PRIVATE prox_core)
17+
target_include_directories(test_opcode_values PRIVATE ${CMAKE_SOURCE_DIR}/include)
18+
add_test(NAME OpcodeValues COMMAND test_opcode_values)

tests/test_opcode_roundtrip.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* test_opcode_roundtrip.c
2+
* Verifies that a chunk with opcodes and constants round-trips through
3+
* write_chunk_to_file() and read_chunk_from_file() preserving bytes.
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <assert.h>
10+
#include "bytecode.h"
11+
12+
int main(void) {
13+
const char *tmp = "tests/tmp_opcode_rt.proxbc";
14+
15+
Chunk c;
16+
chunk_init(&c);
17+
18+
/* Add a couple constants */
19+
Value v1; v1.type = VAL_NUMBER; v1.as.number = 3.14; consttable_add(&c.constants, v1);
20+
Value v2; v2.type = VAL_STRING; v2.as.string.length = 5; v2.as.string.chars = strdup("hello"); consttable_add(&c.constants, v2);
21+
22+
/* Emit instructions */
23+
emit_opcode(&c, OP_PUSH_CONST);
24+
emit_uleb128(&c, 0);
25+
emit_opcode(&c, OP_PUSH_CONST);
26+
emit_uleb128(&c, 1);
27+
emit_opcode(&c, OP_ADD);
28+
emit_opcode(&c, OP_HALT);
29+
30+
if (write_chunk_to_file(tmp, &c) != 0) {
31+
fprintf(stderr, "Failed to write chunk to %s\n", tmp);
32+
chunk_free(&c);
33+
return 2;
34+
}
35+
36+
Chunk out;
37+
if (read_chunk_from_file(tmp, &out) != 0) {
38+
fprintf(stderr, "Failed to read chunk back\n");
39+
chunk_free(&c);
40+
return 3;
41+
}
42+
43+
/* Compare code bytes */
44+
if (out.code_len != c.code_len) {
45+
fprintf(stderr, "Code length mismatch: %zu vs %zu\n", out.code_len, c.code_len);
46+
chunk_free(&c); chunk_free(&out); return 4;
47+
}
48+
if (memcmp(out.code, c.code, c.code_len) != 0) {
49+
fprintf(stderr, "Code bytes differ\n");
50+
chunk_free(&c); chunk_free(&out); return 5;
51+
}
52+
53+
/* Compare constants count and first values */
54+
if (out.constants.count != c.constants.count) {
55+
fprintf(stderr, "Const count mismatch: %zu vs %zu\n", out.constants.count, c.constants.count);
56+
chunk_free(&c); chunk_free(&out); return 6;
57+
}
58+
if (out.constants.items[0].type != VAL_NUMBER) {
59+
fprintf(stderr, "Const type mismatch for idx 0\n"); chunk_free(&c); chunk_free(&out); return 7;
60+
}
61+
62+
/* Clean up */
63+
chunk_free(&c);
64+
chunk_free(&out);
65+
66+
/* Remove temporary file */
67+
remove(tmp);
68+
69+
printf("test_opcode_roundtrip: OK\n");
70+
return 0;
71+
}

tests/test_opcode_values.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* test_opcode_values.c
2+
* Verifies opcode enum numeric assignments match documented values
3+
*/
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <assert.h>
8+
#include "bytecode.h"
9+
10+
int main(void) {
11+
assert(OP_ADD == 0x01);
12+
assert(OP_HALT == 0xFF);
13+
assert(OP_PUSH_CONST == 0x30);
14+
assert(OP_CALL == 0x50);
15+
printf("test_opcode_values: OK\n");
16+
return 0;
17+
}

tools/bench/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# Build bench_simple
4+
add_executable(bench_simple bench_simple.c)
5+
target_include_directories(bench_simple PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src)
6+
target_link_libraries(bench_simple PRIVATE prox_core)
7+
8+
add_custom_target(run_bench_simple
9+
COMMAND bench_simple
10+
DEPENDS bench_simple
11+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
12+
)

tools/bench/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Bench Simple
2+
3+
This microbenchmark builds a CPU-bound chunk and runs it repeatedly against the VM to measure throughput.
4+
5+
Build and Run
6+
-------------
7+
1. Configure the project with CMake (from project root):
8+
9+
```bash
10+
mkdir build && cd build
11+
cmake .. -DCMAKE_BUILD_TYPE=Release
12+
cmake --build . --target bench_simple
13+
```
14+
15+
2. Run the benchmark:
16+
17+
```bash
18+
./bench_simple
19+
```

0 commit comments

Comments
 (0)