|
| 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 | +} |
0 commit comments