Skip to content

Commit c474d88

Browse files
committed
add tests
1 parent feca618 commit c474d88

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/test_harness_builder.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <assert.h>
2+
#include <stdint.h>
23
#include <stdio.h>
34

45
#include "builder.h"
@@ -29,6 +30,86 @@ static void test_assign_and_add(void)
2930
destroy_cj_ctx(cj);
3031
}
3132

33+
static void test_scratch_helpers(void)
34+
{
35+
cj_ctx *cj = create_cj_ctx();
36+
cj_builder_frame frame;
37+
cj_builder_fn_prologue(cj, 0, &frame);
38+
39+
cj_builder_scratch scratch;
40+
cj_builder_scratch_init(&scratch);
41+
42+
cj_operand acc = cj_builder_scratch_acquire(&scratch);
43+
cj_operand tmp = cj_builder_scratch_acquire(&scratch);
44+
45+
cj_builder_assign(cj, acc, cj_builder_arg_int(cj, 0));
46+
cj_builder_assign(cj, tmp, cj_make_constant(7));
47+
cj_builder_add_assign(cj, acc, tmp);
48+
49+
cj_builder_scratch_release(&scratch); // release tmp
50+
51+
cj_operand adjust = cj_builder_scratch_acquire(&scratch);
52+
cj_builder_assign(cj, adjust, cj_make_constant(3));
53+
cj_builder_sub_assign(cj, acc, adjust);
54+
55+
cj_builder_scratch_release(&scratch); // release adjust
56+
57+
cj_builder_return_value(cj, &frame, acc);
58+
cj_builder_scratch_release(&scratch); // release acc
59+
60+
fn1_t fn = (fn1_t)create_cj_fn(cj);
61+
assert(fn);
62+
assert(fn(0) == 4);
63+
assert(fn(5) == 9);
64+
65+
destroy_cj_fn(cj, (cj_fn)fn);
66+
destroy_cj_ctx(cj);
67+
}
68+
69+
static void test_call_helper(void)
70+
{
71+
cj_ctx *cj = create_cj_ctx();
72+
cj_label entry = cj_create_label(cj);
73+
cj_label callee = cj_create_label(cj);
74+
75+
// Main function
76+
cj_mark_label(cj, entry);
77+
cj_builder_frame main_frame;
78+
cj_builder_fn_prologue_with_link_save(cj, 0, &main_frame);
79+
cj_builder_scratch scratch;
80+
cj_builder_scratch_init(&scratch);
81+
82+
cj_operand arg = cj_builder_scratch_acquire(&scratch);
83+
cj_builder_assign(cj, arg, cj_builder_arg_int(cj, 0));
84+
cj_builder_add_assign(cj, arg, cj_make_constant(2));
85+
86+
cj_operand call_result = cj_builder_call_unary(cj, &scratch, callee, arg);
87+
cj_builder_return_value(cj, &main_frame, call_result);
88+
cj_builder_scratch_release(&scratch);
89+
90+
// Callee function: returns x + 1
91+
cj_mark_label(cj, callee);
92+
cj_builder_frame callee_frame;
93+
cj_builder_fn_prologue(cj, 0, &callee_frame);
94+
cj_operand callee_arg = cj_builder_arg_int(cj, 0);
95+
cj_operand tmp = cj_builder_scratch_reg(0);
96+
cj_builder_assign(cj, tmp, callee_arg);
97+
cj_builder_add_assign(cj, tmp, cj_make_constant(1));
98+
cj_builder_return_value(cj, &callee_frame, tmp);
99+
100+
cj_fn module = create_cj_fn(cj);
101+
assert(module);
102+
103+
size_t entry_offset = cj->label_positions[entry.id];
104+
fn1_t fn = (fn1_t)((uint8_t *)(void *)module + entry_offset);
105+
assert(fn);
106+
assert(fn(10) == 13);
107+
assert(fn(-4) == -1);
108+
109+
destroy_cj_fn(cj, module);
110+
destroy_cj_ctx(cj);
111+
}
112+
32113
static void test_for_loop_sum(void)
33114
{
34115
cj_ctx *cj = create_cj_ctx();
@@ -95,6 +176,8 @@ static void test_if_else(void)
95176
int main(void)
96177
{
97178
test_assign_and_add();
179+
test_scratch_helpers();
180+
test_call_helper();
98181
test_for_loop_sum();
99182
test_if_else();
100183
puts("builder harness OK");

0 commit comments

Comments
 (0)