Skip to content

Commit 6919ae6

Browse files
test: add 56 unit tests for semantic, slab_alloc, and ast_profile modules
Add focused unit tests for three previously untested modules: - tests/test_semantic.c (32 tests): cbm_sem_tokenize (camel/snake/dot/abbrev), cbm_sem_cosine (identical/orthogonal/zero/negative/null), cbm_sem_normalize, cbm_sem_vec_add_scaled, cbm_sem_random_index (determinism), cbm_sem_proximity, cbm_sem_diffuse, corpus lifecycle, IDF, config - tests/test_slab_alloc.c (14 tests): slab malloc/free (≤64B and >64B), calloc zeroing/overflow, realloc (shrink/promote/same-size), free-list cycling, pointer uniqueness, thread reset - tests/test_ast_profile.c (10 tests): to_str/from_str round-trip (25 fields), null/empty/invalid inputs, to_vector range/saturation All 56 tests pass with ASan+UBSan. Registered in Makefile.cbm and test_main.c. Signed-off-by: Raul Acosta <raulacosta@users.noreply.github.com> Signed-off-by: Raul Acosta <raul.acosta.mmm@gmail.com>
1 parent 9169f46 commit 6919ae6

5 files changed

Lines changed: 759 additions & 1 deletion

File tree

‎Makefile.cbm‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ TEST_SECURITY_SRCS = tests/test_security.c
385385

386386
TEST_YAML_SRCS = tests/test_yaml.c
387387

388+
TEST_SEMANTIC_SRCS = tests/test_semantic.c
389+
TEST_AST_PROFILE_SRCS = tests/test_ast_profile.c
390+
TEST_SLAB_ALLOC_SRCS = tests/test_slab_alloc.c
391+
388392
TEST_SIMHASH_SRCS = tests/test_simhash.c
389393

390394
TEST_STACK_OVERFLOW_SRCS = tests/test_stack_overflow.c
@@ -447,7 +451,7 @@ TEST_REPRO_SRCS = \
447451
tests/repro/repro_lsp_java_cs.c \
448452
tests/repro/repro_lsp_kt_php_rust.c
449453

450-
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
454+
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SEMANTIC_SRCS) $(TEST_AST_PROFILE_SRCS) $(TEST_SLAB_ALLOC_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
451455

452456

453457
# ── Build directories ────────────────────────────────────────────

‎tests/test_ast_profile.c‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* test_ast_profile.c — Unit tests for ast_profile.c (serialization/deserialization).
3+
*
4+
* Covers: to_str, from_str round-trip, to_vector, edge cases.
5+
*/
6+
#include "test_framework.h"
7+
#include <semantic/ast_profile.h>
8+
9+
#include <string.h>
10+
11+
/* ── Helper ──────────────────────────────────────────────────────── */
12+
13+
static cbm_ast_profile_t make_profile(void) {
14+
cbm_ast_profile_t p;
15+
memset(&p, 0, sizeof(p));
16+
p.if_count = 5;
17+
p.for_count = 3;
18+
p.while_count = 1;
19+
p.switch_count = 2;
20+
p.try_count = 0;
21+
p.return_count = 4;
22+
p.max_nesting_depth = 3;
23+
p.avg_nesting_depth_x10 = 15;
24+
p.comparison_ops = 10;
25+
p.arithmetic_ops = 8;
26+
p.logical_ops = 2;
27+
p.assignment_count = 7;
28+
p.string_literals = 3;
29+
p.number_literals = 5;
30+
p.bool_literals = 1;
31+
p.param_count = 4;
32+
p.params_in_returns = 2;
33+
p.params_in_conditions = 1;
34+
p.variable_reassigns = 3;
35+
p.unique_operators = 12;
36+
p.unique_operands = 20;
37+
p.total_operators = 45;
38+
p.total_operands = 60;
39+
p.body_lines = 30;
40+
p.body_tokens = 200;
41+
return p;
42+
}
43+
44+
/* ── to_str + from_str round-trip ────────────────────────────────── */
45+
46+
TEST(ast_profile_roundtrip) {
47+
cbm_ast_profile_t original = make_profile();
48+
char buf[200];
49+
cbm_ast_profile_to_str(&original, buf, sizeof(buf));
50+
51+
cbm_ast_profile_t decoded;
52+
ASSERT_TRUE(cbm_ast_profile_from_str(buf, &decoded));
53+
54+
ASSERT_EQ(original.if_count, decoded.if_count);
55+
ASSERT_EQ(original.for_count, decoded.for_count);
56+
ASSERT_EQ(original.while_count, decoded.while_count);
57+
ASSERT_EQ(original.switch_count, decoded.switch_count);
58+
ASSERT_EQ(original.try_count, decoded.try_count);
59+
ASSERT_EQ(original.return_count, decoded.return_count);
60+
ASSERT_EQ(original.max_nesting_depth, decoded.max_nesting_depth);
61+
ASSERT_EQ(original.avg_nesting_depth_x10, decoded.avg_nesting_depth_x10);
62+
ASSERT_EQ(original.comparison_ops, decoded.comparison_ops);
63+
ASSERT_EQ(original.arithmetic_ops, decoded.arithmetic_ops);
64+
ASSERT_EQ(original.logical_ops, decoded.logical_ops);
65+
ASSERT_EQ(original.assignment_count, decoded.assignment_count);
66+
ASSERT_EQ(original.string_literals, decoded.string_literals);
67+
ASSERT_EQ(original.number_literals, decoded.number_literals);
68+
ASSERT_EQ(original.bool_literals, decoded.bool_literals);
69+
ASSERT_EQ(original.param_count, decoded.param_count);
70+
ASSERT_EQ(original.params_in_returns, decoded.params_in_returns);
71+
ASSERT_EQ(original.params_in_conditions, decoded.params_in_conditions);
72+
ASSERT_EQ(original.variable_reassigns, decoded.variable_reassigns);
73+
ASSERT_EQ(original.unique_operators, decoded.unique_operators);
74+
ASSERT_EQ(original.unique_operands, decoded.unique_operands);
75+
ASSERT_EQ(original.total_operators, decoded.total_operators);
76+
ASSERT_EQ(original.total_operands, decoded.total_operands);
77+
ASSERT_EQ(original.body_lines, decoded.body_lines);
78+
ASSERT_EQ(original.body_tokens, decoded.body_tokens);
79+
PASS();
80+
}
81+
82+
TEST(ast_profile_to_str_null) {
83+
char buf[200];
84+
cbm_ast_profile_to_str(NULL, buf, sizeof(buf)); /* should not crash */
85+
PASS();
86+
}
87+
88+
TEST(ast_profile_to_str_small_buf) {
89+
cbm_ast_profile_t p = make_profile();
90+
/* buf too small: should write empty string */
91+
char buf[1] = "X";
92+
cbm_ast_profile_to_str(&p, buf, 0);
93+
/* 0-length buffer: function should handle gracefully */
94+
PASS();
95+
}
96+
97+
TEST(ast_profile_from_str_null) {
98+
ASSERT_FALSE(cbm_ast_profile_from_str(NULL, NULL));
99+
PASS();
100+
}
101+
102+
TEST(ast_profile_from_str_invalid) {
103+
cbm_ast_profile_t out;
104+
ASSERT_FALSE(cbm_ast_profile_from_str("not,a,valid,string", &out));
105+
ASSERT_FALSE(cbm_ast_profile_from_str("", &out));
106+
PASS();
107+
}
108+
109+
TEST(ast_profile_from_str_too_few_fields) {
110+
cbm_ast_profile_t out;
111+
/* Only 5 fields instead of 25 */
112+
ASSERT_FALSE(cbm_ast_profile_from_str("1,2,3,4,5", &out));
113+
PASS();
114+
}
115+
116+
/* ── to_vector ───────────────────────────────────────────────────── */
117+
118+
TEST(ast_profile_to_vector_range) {
119+
cbm_ast_profile_t p = make_profile();
120+
float vec[25];
121+
cbm_ast_profile_to_vector(&p, vec);
122+
/* All values should be in [0, 1] range (normalized) */
123+
for (int i = 0; i < 25; i++) {
124+
ASSERT_GTE(vec[i], 0.0f);
125+
ASSERT_LTE(vec[i], 1.0f);
126+
}
127+
PASS();
128+
}
129+
130+
TEST(ast_profile_to_vector_zero) {
131+
cbm_ast_profile_t p;
132+
memset(&p, 0, sizeof(p));
133+
float vec[25];
134+
cbm_ast_profile_to_vector(&p, vec);
135+
/* All zeros should produce all-zero vector */
136+
for (int i = 0; i < 25; i++) {
137+
ASSERT_FLOAT_EQ(vec[i], 0.0f, 0.001f);
138+
}
139+
PASS();
140+
}
141+
142+
TEST(ast_profile_to_vector_null) {
143+
float vec[25];
144+
memset(vec, 0xFF, sizeof(vec));
145+
cbm_ast_profile_to_vector(NULL, vec); /* should not crash or write */
146+
PASS();
147+
}
148+
149+
/* ── to_vector extremes (saturation) ─────────────────────────────── */
150+
151+
TEST(ast_profile_to_vector_saturates) {
152+
cbm_ast_profile_t p;
153+
memset(&p, 0, sizeof(p));
154+
/* count / MAX_COUNT (100) → can exceed 1.0; body_tokens / MAX_TOKENS (2000) = 2.5 */
155+
p.if_count = 500;
156+
p.body_tokens = 5000;
157+
float vec[25];
158+
cbm_ast_profile_to_vector(&p, vec);
159+
ASSERT_TRUE(vec[0] > 1.0f);
160+
ASSERT_TRUE(vec[24] > 1.0f);
161+
PASS();
162+
}
163+
164+
/* ── Suite ───────────────────────────────────────────────────────── */
165+
166+
SUITE(ast_profile) {
167+
RUN_TEST(ast_profile_roundtrip);
168+
RUN_TEST(ast_profile_to_str_null);
169+
RUN_TEST(ast_profile_to_str_small_buf);
170+
RUN_TEST(ast_profile_from_str_null);
171+
RUN_TEST(ast_profile_from_str_invalid);
172+
RUN_TEST(ast_profile_from_str_too_few_fields);
173+
RUN_TEST(ast_profile_to_vector_range);
174+
RUN_TEST(ast_profile_to_vector_zero);
175+
RUN_TEST(ast_profile_to_vector_null);
176+
RUN_TEST(ast_profile_to_vector_saturates);
177+
}

‎tests/test_main.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ extern void suite_grammar_probe_e(void);
120120
extern void suite_grammar_probe_f(void);
121121
extern void suite_grammar_probe_g(void);
122122
extern void suite_incremental(void);
123+
extern void suite_semantic(void);
124+
extern void suite_ast_profile(void);
125+
extern void suite_slab_alloc(void);
123126
extern void suite_simhash(void);
124127
extern void suite_stack_overflow(void);
125128
extern void suite_dump_verify(void);
@@ -239,6 +242,7 @@ int main(int argc, char **argv) {
239242
RUN_SELECTED_SUITE(parallel);
240243

241244
/* mem + arena + slab integration */
245+
RUN_SELECTED_SUITE(slab_alloc);
242246
RUN_SELECTED_SUITE(mem);
243247

244248
/* UI (config, embedded assets, layout) */
@@ -254,6 +258,8 @@ int main(int argc, char **argv) {
254258
RUN_SELECTED_SUITE(yaml);
255259

256260
/* SimHash / SIMILAR_TO */
261+
RUN_SELECTED_SUITE(semantic);
262+
RUN_SELECTED_SUITE(ast_profile);
257263
RUN_SELECTED_SUITE(simhash);
258264

259265
/* Stack overflow regression (GitHub #199) */

0 commit comments

Comments
 (0)