Skip to content

test_structs_v1.tc

test_structs_v1.tc — probe what works in the IDE's existing struct

Source on GitHub

// =================================================================
// test_structs_v1.tc — probe what works in the IDE's existing struct
// scaffolding. Tests all v1 use cases; failures show us what's missing.
// =================================================================

// Test 1: simple struct definition + local var + field access
struct Point {
    int x;
    int y;
}

// Test 2: bigger struct with mixed types
struct WriteLog {
    int  addr;
    int  val;
    int  ms;
    char src;
}

// Test 3: char-array field
struct Sample {
    int  duration_ms;
    float ratio;
    char  label[16];
}

// Test 4: nested struct
struct Rect {
    Point tl;
    Point br;
}

WriteLog g_w;
Point    g_pts[5];        // array of struct, global

void test_basic() {
    Point p;
    p.x = 10;
    p.y = 20;
    int sum = p.x + p.y;
    addLog("test_basic: x=%d y=%d sum=%d", p.x, p.y, sum);
}

void test_local_struct_with_init() {
    Point q = {3, 4};
    addLog("test_init: q.x=%d q.y=%d", q.x, q.y);
}

void test_global() {
    g_w.addr = 0x40;
    g_w.val  = 0xFF00;
    g_w.ms   = millis();
    g_w.src  = 'O';
    addLog("test_global: addr=%d val=%d src=%c", g_w.addr, g_w.val, g_w.src);
}

void test_array_of_struct() {
    for (int i = 0; i < 5; i = i + 1) {
        g_pts[i].x = i * 10;
        g_pts[i].y = i * 20;
    }
    addLog("test_array: g_pts[3]=(%d,%d)", g_pts[3].x, g_pts[3].y);
}

void test_char_array_field() {
    Sample s;
    s.duration_ms = 1234;
    s.ratio = 0.75;
    strcpy(s.label, "boost");
    addLog("test_chararr: duration=%d ratio=%.2f label=%s", s.duration_ms, s.ratio, s.label);
}

void test_nested_struct() {
    Rect r;
    r.tl.x = 0;
    r.tl.y = 0;
    r.br.x = 100;
    r.br.y = 200;
    addLog("test_nested: tl=(%d,%d) br=(%d,%d)", r.tl.x, r.tl.y, r.br.x, r.br.y);
}

void test_whole_assign_var_to_var() {
    Point a;
    a.x = 5;
    a.y = 7;
    Point b;
    b = a;
    addLog("test_assign: b=(%d,%d) (expect 5,7)", b.x, b.y);
}

void test_whole_assign_arr_to_var() {
    g_pts[2].x = 100;
    g_pts[2].y = 200;
    Point c;
    c = g_pts[2];
    addLog("test_a2v: c=(%d,%d) (expect 100,200)", c.x, c.y);
}

void test_whole_assign_var_to_arr() {
    Point d;
    d.x = 33;
    d.y = 44;
    g_pts[4] = d;
    addLog("test_v2a: g_pts[4]=(%d,%d) (expect 33,44)", g_pts[4].x, g_pts[4].y);
}

void greet(Point p) {
    addLog("test_param: got (%d,%d) (expect 99,11)", p.x, p.y);
}

void test_struct_param() {
    Point r;
    r.x = 99;
    r.y = 11;
    greet(r);
}

Point make_point(int xx, int yy) {
    Point p;
    p.x = xx;
    p.y = yy;
    return p;
}

void test_struct_return() {
    Point z = make_point(42, 84);
    addLog("test_return: z=(%d,%d) (expect 42,84)", z.x, z.y);
}

void test_sizeof() {
    int sp = sizeof(Point);          // 2 slots
    int sw = sizeof(WriteLog);       // 4 slots (3 ints + 1 char)
    int ss = sizeof(Sample);         // 18 slots (1 int + 1 float + char[16])
    int sr = sizeof(Rect);           // 4 slots (2× Point = 2 + 2)
    addLog("test_sizeof: Point=%d WriteLog=%d Sample=%d Rect=%d", sp, sw, ss, sr);
}

int main() {
    addLog("=== struct v1 probe start ===");
    test_basic();
    test_local_struct_with_init();
    test_global();
    test_array_of_struct();
    test_char_array_field();
    test_nested_struct();
    test_whole_assign_var_to_var();
    test_whole_assign_arr_to_var();
    test_whole_assign_var_to_arr();
    test_struct_param();
    test_struct_return();
    test_sizeof();
    addLog("=== struct v1 probe end ===");
    return 0;
}