decy-core 2.2.0

Core transpilation pipeline for C-to-Rust conversion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! Popperian Falsification Test Suite for Decy C-to-Rust Transpiler
//!
//! C1476-C1500: Physics Simulation patterns -- the kind of C code found in
//! physics engines, game engines, and scientific simulation software.
//! Tests are APPEND-ONLY per Popperian methodology.
//! Falsified tests are marked #[ignore = "FALSIFIED: reason"].
//!
//! These tests exercise real-world physics simulation patterns commonly
//! found in Box2D, Bullet, PhysX, SPH solvers, cloth simulators, and
//! spatial acceleration structures -- all expressed as valid C99.
//!
//! Organization:
//! - C1476-C1480: Particle systems (init/step, emitter, force accumulator, collision, lifespan)
//! - C1481-C1485: Rigid body dynamics (state, force/torque, AABB collision, impulse, constraint)
//! - C1486-C1490: Fluid simulation (SPH density, pressure, viscosity, advection, boundary)
//! - C1491-C1495: Cloth/soft body (spring-mass, distance constraint, bending, self-collision, wind)
//! - C1496-C1500: Spatial structures (uniform grid, BVH, k-d tree, octree, broad phase)
//!
//! Results: 25 passing, 0 falsified (100.0% pass rate)

// ============================================================================
// C1476-C1480: Particle Systems
// ============================================================================

#[test]
fn c1476_particle_init_step() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float mass; int alive; } phys_particle_t;

void phys_particle_init(phys_particle_t *p, float x, float y) {
    p->x = x; p->y = y;
    p->vx = 0.0f; p->vy = 0.0f;
    p->mass = 1.0f; p->alive = 1;
}

void phys_particle_step(phys_particle_t *p, float dt) {
    if (!p->alive) return;
    p->x += p->vx * dt;
    p->y += p->vy * dt;
}

void phys_particle_apply_gravity(phys_particle_t *p, float g, float dt) {
    if (!p->alive) return;
    p->vy += g * dt;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1476 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1476: empty output");
    assert!(code.contains("fn phys_particle_init"), "C1476: should contain phys_particle_init");
}

#[test]
fn c1477_particle_emitter() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; int alive; } phys_part_t;
typedef struct { phys_part_t particles[256]; int count; int max_count; float emit_rate; float timer; } phys_emitter_t;

void phys_emitter_init(phys_emitter_t *e, int max) {
    e->count = 0;
    e->max_count = max;
    e->emit_rate = 10.0f;
    e->timer = 0.0f;
}

void phys_emitter_emit(phys_emitter_t *e, float x, float y, float vx, float vy) {
    if (e->count >= e->max_count) return;
    phys_part_t *p = &e->particles[e->count];
    p->x = x; p->y = y;
    p->vx = vx; p->vy = vy;
    p->alive = 1;
    e->count++;
}

void phys_emitter_update(phys_emitter_t *e, float dt) {
    e->timer += dt;
    while (e->timer >= 1.0f / e->emit_rate) {
        phys_emitter_emit(e, 0.0f, 0.0f, 0.0f, 1.0f);
        e->timer -= 1.0f / e->emit_rate;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1477 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1477: empty output");
    assert!(code.contains("fn phys_emitter_init"), "C1477: should contain phys_emitter_init");
}

#[test]
fn c1478_force_accumulator() {
    let c_code = r#"
typedef struct { float fx; float fy; float fz; } phys_vec3_t;
typedef struct { phys_vec3_t forces[16]; int count; } phys_force_accum_t;

void phys_accum_reset(phys_force_accum_t *a) {
    a->count = 0;
}

void phys_accum_add(phys_force_accum_t *a, float fx, float fy, float fz) {
    if (a->count >= 16) return;
    a->forces[a->count].fx = fx;
    a->forces[a->count].fy = fy;
    a->forces[a->count].fz = fz;
    a->count++;
}

phys_vec3_t phys_accum_total(const phys_force_accum_t *a) {
    phys_vec3_t total;
    total.fx = 0.0f; total.fy = 0.0f; total.fz = 0.0f;
    int i;
    for (i = 0; i < a->count; i++) {
        total.fx += a->forces[i].fx;
        total.fy += a->forces[i].fy;
        total.fz += a->forces[i].fz;
    }
    return total;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1478 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1478: empty output");
    assert!(code.contains("fn phys_accum_total"), "C1478: should contain phys_accum_total");
}

#[test]
fn c1479_particle_collision() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float radius; } phys_ball_t;

float phys_distance_sq(float x1, float y1, float x2, float y2) {
    float dx = x2 - x1;
    float dy = y2 - y1;
    return dx * dx + dy * dy;
}

int phys_balls_collide(const phys_ball_t *a, const phys_ball_t *b) {
    float r = a->radius + b->radius;
    return phys_distance_sq(a->x, a->y, b->x, b->y) < r * r;
}

void phys_resolve_collision(phys_ball_t *a, phys_ball_t *b) {
    if (!phys_balls_collide(a, b)) return;
    float tmpvx = a->vx; float tmpvy = a->vy;
    a->vx = b->vx; a->vy = b->vy;
    b->vx = tmpvx; b->vy = tmpvy;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1479 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1479: empty output");
    assert!(code.contains("fn phys_balls_collide"), "C1479: should contain phys_balls_collide");
}

#[test]
fn c1480_particle_lifespan() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float life; float max_life; int active; } phys_lpart_t;

void phys_lpart_spawn(phys_lpart_t *p, float x, float y, float max_life) {
    p->x = x; p->y = y;
    p->vx = 0.0f; p->vy = 0.0f;
    p->life = 0.0f;
    p->max_life = max_life;
    p->active = 1;
}

void phys_lpart_tick(phys_lpart_t *p, float dt) {
    if (!p->active) return;
    p->life += dt;
    if (p->life >= p->max_life) { p->active = 0; return; }
    p->x += p->vx * dt;
    p->y += p->vy * dt;
}

float phys_lpart_alpha(const phys_lpart_t *p) {
    if (!p->active || p->max_life <= 0.0f) return 0.0f;
    return 1.0f - p->life / p->max_life;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1480 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1480: empty output");
    assert!(code.contains("fn phys_lpart_alpha"), "C1480: should contain phys_lpart_alpha");
}

// ============================================================================
// C1481-C1485: Rigid Body Dynamics
// ============================================================================

#[test]
fn c1481_rigid_body_state() {
    let c_code = r#"
typedef struct { float x; float y; float angle; float vx; float vy; float omega; float mass; float inertia; } phys_rbody_t;

void phys_rbody_init(phys_rbody_t *b, float x, float y, float mass) {
    b->x = x; b->y = y;
    b->angle = 0.0f;
    b->vx = 0.0f; b->vy = 0.0f;
    b->omega = 0.0f;
    b->mass = mass;
    b->inertia = mass * 0.5f;
}

void phys_rbody_integrate(phys_rbody_t *b, float dt) {
    b->x += b->vx * dt;
    b->y += b->vy * dt;
    b->angle += b->omega * dt;
}

float phys_rbody_kinetic_energy(const phys_rbody_t *b) {
    float lin = 0.5f * b->mass * (b->vx * b->vx + b->vy * b->vy);
    float rot = 0.5f * b->inertia * b->omega * b->omega;
    return lin + rot;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1481 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1481: empty output");
    assert!(code.contains("fn phys_rbody_init"), "C1481: should contain phys_rbody_init");
}

#[test]
fn c1482_force_and_torque() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float omega; float mass; float inertia; float fx; float fy; float torque; } phys_body2d_t;

void phys_body2d_clear_forces(phys_body2d_t *b) {
    b->fx = 0.0f; b->fy = 0.0f; b->torque = 0.0f;
}

void phys_body2d_apply_force(phys_body2d_t *b, float fx, float fy) {
    b->fx += fx;
    b->fy += fy;
}

void phys_body2d_apply_torque_at(phys_body2d_t *b, float fx, float fy, float rx, float ry) {
    b->fx += fx;
    b->fy += fy;
    b->torque += rx * fy - ry * fx;
}

void phys_body2d_step(phys_body2d_t *b, float dt) {
    float inv_m = 1.0f / b->mass;
    b->vx += b->fx * inv_m * dt;
    b->vy += b->fy * inv_m * dt;
    b->omega += b->torque / b->inertia * dt;
    b->x += b->vx * dt;
    b->y += b->vy * dt;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1482 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1482: empty output");
    assert!(
        code.contains("fn phys_body2d_apply_torque_at"),
        "C1482: should contain phys_body2d_apply_torque_at"
    );
}

#[test]
fn c1483_aabb_collision() {
    let c_code = r#"
typedef struct { float min_x; float min_y; float max_x; float max_y; } phys_aabb_t;

int phys_aabb_overlap(const phys_aabb_t *a, const phys_aabb_t *b) {
    if (a->max_x < b->min_x || b->max_x < a->min_x) return 0;
    if (a->max_y < b->min_y || b->max_y < a->min_y) return 0;
    return 1;
}

phys_aabb_t phys_aabb_merge(const phys_aabb_t *a, const phys_aabb_t *b) {
    phys_aabb_t r;
    r.min_x = a->min_x < b->min_x ? a->min_x : b->min_x;
    r.min_y = a->min_y < b->min_y ? a->min_y : b->min_y;
    r.max_x = a->max_x > b->max_x ? a->max_x : b->max_x;
    r.max_y = a->max_y > b->max_y ? a->max_y : b->max_y;
    return r;
}

float phys_aabb_area(const phys_aabb_t *a) {
    return (a->max_x - a->min_x) * (a->max_y - a->min_y);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1483 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1483: empty output");
    assert!(code.contains("fn phys_aabb_overlap"), "C1483: should contain phys_aabb_overlap");
}

#[test]
fn c1484_impulse_response() {
    let c_code = r#"
typedef struct { float vx; float vy; float mass; float restitution; } phys_impbody_t;

void phys_impulse_apply(phys_impbody_t *a, phys_impbody_t *b, float nx, float ny) {
    float rel_vx = b->vx - a->vx;
    float rel_vy = b->vy - a->vy;
    float vel_along = rel_vx * nx + rel_vy * ny;
    if (vel_along > 0.0f) return;
    float e = a->restitution < b->restitution ? a->restitution : b->restitution;
    float j = -(1.0f + e) * vel_along;
    float inv_sum = 1.0f / a->mass + 1.0f / b->mass;
    j = j / inv_sum;
    a->vx -= j / a->mass * nx;
    a->vy -= j / a->mass * ny;
    b->vx += j / b->mass * nx;
    b->vy += j / b->mass * ny;
}

float phys_impulse_magnitude(float v_rel, float e, float inv_mass_sum) {
    return -(1.0f + e) * v_rel / inv_mass_sum;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1484 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1484: empty output");
    assert!(code.contains("fn phys_impulse_apply"), "C1484: should contain phys_impulse_apply");
}

#[test]
fn c1485_constraint_solver() {
    let c_code = r#"
typedef struct { float x; float y; float inv_mass; } phys_cpoint_t;
typedef struct { int a; int b; float rest_length; } phys_constraint_t;

void phys_solve_distance(phys_cpoint_t *pts, const phys_constraint_t *c) {
    float dx = pts[c->b].x - pts[c->a].x;
    float dy = pts[c->b].y - pts[c->a].y;
    float dist_sq = dx * dx + dy * dy;
    if (dist_sq < 0.0001f) return;
    float dist = dist_sq;
    int i;
    for (i = 0; i < 5; i++) dist = 0.5f * (dist + dist_sq / dist);
    float diff = (dist - c->rest_length) / dist;
    float w = pts[c->a].inv_mass + pts[c->b].inv_mass;
    if (w < 0.0001f) return;
    float s1 = pts[c->a].inv_mass / w * diff;
    float s2 = pts[c->b].inv_mass / w * diff;
    pts[c->a].x += dx * s1;
    pts[c->a].y += dy * s1;
    pts[c->b].x -= dx * s2;
    pts[c->b].y -= dy * s2;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1485 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1485: empty output");
    assert!(code.contains("fn phys_solve_distance"), "C1485: should contain phys_solve_distance");
}

// ============================================================================
// C1486-C1490: Fluid Simulation
// ============================================================================

#[test]
fn c1486_sph_density() {
    let c_code = r#"
typedef struct { float x; float y; float density; float pressure; float mass; } phys_sph_t;

float phys_sph_kernel(float r, float h) {
    if (r >= h) return 0.0f;
    float q = 1.0f - r / h;
    return 315.0f / (64.0f * 3.14159f * h * h) * q * q * q;
}

void phys_sph_compute_density(phys_sph_t *particles, int n, float h) {
    int i, j;
    for (i = 0; i < n; i++) {
        particles[i].density = 0.0f;
        for (j = 0; j < n; j++) {
            float dx = particles[i].x - particles[j].x;
            float dy = particles[i].y - particles[j].y;
            float r = dx * dx + dy * dy;
            int k; float sq = r;
            for (k = 0; k < 5; k++) sq = 0.5f * (sq + r / sq);
            particles[i].density += particles[j].mass * phys_sph_kernel(sq, h);
        }
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1486 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1486: empty output");
    assert!(code.contains("fn phys_sph_kernel"), "C1486: should contain phys_sph_kernel");
}

#[test]
fn c1487_pressure_force() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float density; float pressure; float mass; } phys_fluid_t;

void phys_compute_pressure(phys_fluid_t *p, int n, float k, float rho0) {
    int i;
    for (i = 0; i < n; i++) {
        p[i].pressure = k * (p[i].density - rho0);
    }
}

void phys_pressure_force(phys_fluid_t *particles, int n, float h) {
    int i, j;
    for (i = 0; i < n; i++) {
        float fpx = 0.0f, fpy = 0.0f;
        for (j = 0; j < n; j++) {
            if (i == j) continue;
            float dx = particles[j].x - particles[i].x;
            float dy = particles[j].y - particles[i].y;
            float r2 = dx * dx + dy * dy;
            if (r2 > h * h || r2 < 0.0001f) continue;
            float avg_p = (particles[i].pressure + particles[j].pressure) * 0.5f;
            float w = particles[j].mass / particles[j].density;
            fpx += avg_p * w * dx;
            fpy += avg_p * w * dy;
        }
        particles[i].vx += fpx / particles[i].density;
        particles[i].vy += fpy / particles[i].density;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1487 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1487: empty output");
    assert!(code.contains("fn phys_pressure_force"), "C1487: should contain phys_pressure_force");
}

#[test]
fn c1488_viscosity() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float density; float mass; } phys_visc_t;

float phys_visc_kernel(float r, float h) {
    if (r >= h) return 0.0f;
    return 45.0f / (3.14159f * h * h * h * h * h * h) * (h - r);
}

void phys_apply_viscosity(phys_visc_t *p, int n, float h, float mu) {
    int i, j;
    for (i = 0; i < n; i++) {
        float ax = 0.0f, ay = 0.0f;
        for (j = 0; j < n; j++) {
            if (i == j) continue;
            float dx = p[j].x - p[i].x;
            float dy = p[j].y - p[i].y;
            float r2 = dx * dx + dy * dy;
            if (r2 > h * h) continue;
            float r = r2; int k;
            for (k = 0; k < 5; k++) r = 0.5f * (r + r2 / r);
            float w = phys_visc_kernel(r, h) * p[j].mass / p[j].density;
            ax += (p[j].vx - p[i].vx) * w;
            ay += (p[j].vy - p[i].vy) * w;
        }
        p[i].vx += mu * ax; p[i].vy += mu * ay;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1488 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1488: empty output");
    assert!(code.contains("fn phys_apply_viscosity"), "C1488: should contain phys_apply_viscosity");
}

#[test]
fn c1489_grid_advection() {
    let c_code = r#"
typedef struct { float u[64]; float v[64]; int nx; int ny; } phys_grid_t;

void phys_grid_init(phys_grid_t *g, int nx, int ny) {
    g->nx = nx; g->ny = ny;
    int i;
    for (i = 0; i < 64; i++) { g->u[i] = 0.0f; g->v[i] = 0.0f; }
}

float phys_grid_sample(const float *field, int nx, int ny, float x, float y) {
    int ix = (int)x; int iy = (int)y;
    if (ix < 0) ix = 0; if (ix >= nx - 1) ix = nx - 2;
    if (iy < 0) iy = 0; if (iy >= ny - 1) iy = ny - 2;
    float fx = x - ix; float fy = y - iy;
    float v00 = field[iy * nx + ix];
    float v10 = field[iy * nx + ix + 1];
    float v01 = field[(iy + 1) * nx + ix];
    float v11 = field[(iy + 1) * nx + ix + 1];
    return v00 * (1 - fx) * (1 - fy) + v10 * fx * (1 - fy) + v01 * (1 - fx) * fy + v11 * fx * fy;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1489 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1489: empty output");
    assert!(code.contains("fn phys_grid_sample"), "C1489: should contain phys_grid_sample");
}

#[test]
fn c1490_boundary_conditions() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; } phys_bpart_t;
typedef struct { float xmin; float ymin; float xmax; float ymax; float damping; } phys_bounds_t;

void phys_bounds_init(phys_bounds_t *b, float w, float h) {
    b->xmin = 0.0f; b->ymin = 0.0f;
    b->xmax = w; b->ymax = h;
    b->damping = 0.8f;
}

void phys_enforce_bounds(phys_bpart_t *p, const phys_bounds_t *b) {
    if (p->x < b->xmin) { p->x = b->xmin; p->vx = -p->vx * b->damping; }
    if (p->x > b->xmax) { p->x = b->xmax; p->vx = -p->vx * b->damping; }
    if (p->y < b->ymin) { p->y = b->ymin; p->vy = -p->vy * b->damping; }
    if (p->y > b->ymax) { p->y = b->ymax; p->vy = -p->vy * b->damping; }
}

int phys_is_in_bounds(const phys_bpart_t *p, const phys_bounds_t *b) {
    return p->x >= b->xmin && p->x <= b->xmax && p->y >= b->ymin && p->y <= b->ymax;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1490 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1490: empty output");
    assert!(code.contains("fn phys_enforce_bounds"), "C1490: should contain phys_enforce_bounds");
}

// ============================================================================
// C1491-C1495: Cloth / Soft Body Simulation
// ============================================================================

#[test]
fn c1491_spring_mass_system() {
    let c_code = r#"
typedef struct { float x; float y; float ox; float oy; float mass; int pinned; } phys_node_t;

void phys_node_init(phys_node_t *n, float x, float y) {
    n->x = x; n->y = y;
    n->ox = x; n->oy = y;
    n->mass = 1.0f; n->pinned = 0;
}

void phys_verlet_step(phys_node_t *n, float gx, float gy, float dt) {
    if (n->pinned) return;
    float nx = 2.0f * n->x - n->ox + gx * dt * dt;
    float ny = 2.0f * n->y - n->oy + gy * dt * dt;
    n->ox = n->x; n->oy = n->y;
    n->x = nx; n->y = ny;
}

void phys_spring_step(phys_node_t *a, phys_node_t *b, float rest) {
    float dx = b->x - a->x;
    float dy = b->y - a->y;
    float d2 = dx * dx + dy * dy;
    if (d2 < 0.0001f) return;
    float d = d2; int i;
    for (i = 0; i < 5; i++) d = 0.5f * (d + d2 / d);
    float diff = (d - rest) / d * 0.5f;
    if (!a->pinned) { a->x += dx * diff; a->y += dy * diff; }
    if (!b->pinned) { b->x -= dx * diff; b->y -= dy * diff; }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1491 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1491: empty output");
    assert!(code.contains("fn phys_verlet_step"), "C1491: should contain phys_verlet_step");
}

#[test]
fn c1492_distance_constraint() {
    let c_code = r#"
typedef struct { float x; float y; } phys_dpt_t;
typedef struct { int i0; int i1; float rest; float stiffness; } phys_dconstraint_t;

void phys_solve_dconstraint(phys_dpt_t *pts, const phys_dconstraint_t *c) {
    float dx = pts[c->i1].x - pts[c->i0].x;
    float dy = pts[c->i1].y - pts[c->i0].y;
    float d2 = dx * dx + dy * dy;
    if (d2 < 0.0001f) return;
    float d = d2; int k;
    for (k = 0; k < 5; k++) d = 0.5f * (d + d2 / d);
    float correction = c->stiffness * (d - c->rest) / d;
    float hc = correction * 0.5f;
    pts[c->i0].x += dx * hc;
    pts[c->i0].y += dy * hc;
    pts[c->i1].x -= dx * hc;
    pts[c->i1].y -= dy * hc;
}

void phys_solve_iterations(phys_dpt_t *pts, const phys_dconstraint_t *constraints, int nc, int iters) {
    int it, c;
    for (it = 0; it < iters; it++) {
        for (c = 0; c < nc; c++) {
            phys_solve_dconstraint(pts, &constraints[c]);
        }
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1492 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1492: empty output");
    assert!(
        code.contains("fn phys_solve_dconstraint"),
        "C1492: should contain phys_solve_dconstraint"
    );
}

#[test]
fn c1493_bending_constraint() {
    let c_code = r#"
typedef struct { float x; float y; } phys_bpt_t;

float phys_triangle_area(const phys_bpt_t *a, const phys_bpt_t *b, const phys_bpt_t *c) {
    float ax = b->x - a->x, ay = b->y - a->y;
    float bx = c->x - a->x, by = c->y - a->y;
    float cross = ax * by - ay * bx;
    return cross < 0 ? -cross * 0.5f : cross * 0.5f;
}

void phys_bend_constraint(phys_bpt_t *p0, phys_bpt_t *p1, phys_bpt_t *p2, float rest_angle, float stiffness) {
    float d1x = p1->x - p0->x, d1y = p1->y - p0->y;
    float d2x = p2->x - p0->x, d2y = p2->y - p0->y;
    float dot = d1x * d2x + d1y * d2y;
    float cross = d1x * d2y - d1y * d2x;
    float angle = 0.0f;
    if (dot > 0.0001f || dot < -0.0001f) {
        angle = cross / dot;
    }
    float diff = stiffness * (angle - rest_angle);
    p1->x -= diff * d1y * 0.5f;
    p1->y += diff * d1x * 0.5f;
    p2->x -= diff * d2y * 0.5f;
    p2->y += diff * d2x * 0.5f;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1493 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1493: empty output");
    assert!(code.contains("fn phys_bend_constraint"), "C1493: should contain phys_bend_constraint");
}

#[test]
fn c1494_self_collision() {
    let c_code = r#"
typedef struct { float x; float y; float radius; } phys_snode_t;

int phys_self_collide_check(const phys_snode_t *a, const phys_snode_t *b) {
    float dx = b->x - a->x;
    float dy = b->y - a->y;
    float min_d = a->radius + b->radius;
    return (dx * dx + dy * dy) < (min_d * min_d);
}

void phys_self_collide_resolve(phys_snode_t *a, phys_snode_t *b) {
    float dx = b->x - a->x;
    float dy = b->y - a->y;
    float d2 = dx * dx + dy * dy;
    float min_d = a->radius + b->radius;
    if (d2 >= min_d * min_d || d2 < 0.0001f) return;
    float d = d2; int k;
    for (k = 0; k < 5; k++) d = 0.5f * (d + d2 / d);
    float overlap = (min_d - d) * 0.5f / d;
    a->x -= dx * overlap;
    a->y -= dy * overlap;
    b->x += dx * overlap;
    b->y += dy * overlap;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1494 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1494: empty output");
    assert!(
        code.contains("fn phys_self_collide_resolve"),
        "C1494: should contain phys_self_collide_resolve"
    );
}

#[test]
fn c1495_wind_force() {
    let c_code = r#"
typedef struct { float x; float y; float vx; float vy; float area; } phys_sail_t;

void phys_wind_apply(phys_sail_t *s, float wx, float wy, float drag) {
    float rel_x = wx - s->vx;
    float rel_y = wy - s->vy;
    float speed2 = rel_x * rel_x + rel_y * rel_y;
    float force = drag * s->area * speed2;
    float sp = speed2; int i;
    for (i = 0; i < 5; i++) sp = 0.5f * (sp + speed2 / sp);
    if (sp < 0.0001f) return;
    s->vx += force * rel_x / sp;
    s->vy += force * rel_y / sp;
}

void phys_turbulent_wind(float *wx, float *wy, float base_x, float base_y, float t) {
    float phase = t * 2.0f;
    float variation = 0.3f;
    *wx = base_x + variation * (phase - (int)phase);
    *wy = base_y + variation * (1.0f - (phase - (int)phase));
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1495 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1495: empty output");
    assert!(code.contains("fn phys_wind_apply"), "C1495: should contain phys_wind_apply");
}

// ============================================================================
// C1496-C1500: Spatial Acceleration Structures
// ============================================================================

#[test]
fn c1496_uniform_grid() {
    let c_code = r#"
typedef struct { int cells[256]; int counts[64]; int nx; int ny; float cell_size; } phys_ugrid_t;

void phys_ugrid_init(phys_ugrid_t *g, int nx, int ny, float cs) {
    g->nx = nx; g->ny = ny; g->cell_size = cs;
    int i;
    for (i = 0; i < 64; i++) g->counts[i] = 0;
    for (i = 0; i < 256; i++) g->cells[i] = -1;
}

int phys_ugrid_cell(const phys_ugrid_t *g, float x, float y) {
    int cx = (int)(x / g->cell_size);
    int cy = (int)(y / g->cell_size);
    if (cx < 0) cx = 0; if (cx >= g->nx) cx = g->nx - 1;
    if (cy < 0) cy = 0; if (cy >= g->ny) cy = g->ny - 1;
    return cy * g->nx + cx;
}

void phys_ugrid_insert(phys_ugrid_t *g, float x, float y, int id) {
    int cell = phys_ugrid_cell(g, x, y);
    int base = cell * 4;
    if (g->counts[cell] >= 4) return;
    g->cells[base + g->counts[cell]] = id;
    g->counts[cell]++;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1496 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1496: empty output");
    assert!(code.contains("fn phys_ugrid_insert"), "C1496: should contain phys_ugrid_insert");
}

#[test]
fn c1497_bvh_construction() {
    let c_code = r#"
typedef struct { float min_x; float min_y; float max_x; float max_y; } phys_bbox_t;
typedef struct { phys_bbox_t bounds; int left; int right; int obj_id; } phys_bvh_node_t;

phys_bbox_t phys_bbox_union(const phys_bbox_t *a, const phys_bbox_t *b) {
    phys_bbox_t r;
    r.min_x = a->min_x < b->min_x ? a->min_x : b->min_x;
    r.min_y = a->min_y < b->min_y ? a->min_y : b->min_y;
    r.max_x = a->max_x > b->max_x ? a->max_x : b->max_x;
    r.max_y = a->max_y > b->max_y ? a->max_y : b->max_y;
    return r;
}

float phys_bbox_cost(const phys_bbox_t *b) {
    return (b->max_x - b->min_x) * (b->max_y - b->min_y);
}

int phys_bvh_is_leaf(const phys_bvh_node_t *n) {
    return n->left == -1 && n->right == -1;
}

int phys_bbox_overlaps(const phys_bbox_t *a, const phys_bbox_t *b) {
    if (a->max_x < b->min_x || b->max_x < a->min_x) return 0;
    if (a->max_y < b->min_y || b->max_y < a->min_y) return 0;
    return 1;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1497 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1497: empty output");
    assert!(code.contains("fn phys_bvh_is_leaf"), "C1497: should contain phys_bvh_is_leaf");
}

#[test]
fn c1498_kd_tree_nearest() {
    let c_code = r#"
typedef struct { float x; float y; int left; int right; int axis; } phys_kd_node_t;

float phys_kd_dist2(float x1, float y1, float x2, float y2) {
    float dx = x2 - x1; float dy = y2 - y1;
    return dx * dx + dy * dy;
}

int phys_kd_closer(const phys_kd_node_t *nodes, int a, int b, float qx, float qy) {
    if (a == -1) return b;
    if (b == -1) return a;
    float da = phys_kd_dist2(nodes[a].x, nodes[a].y, qx, qy);
    float db = phys_kd_dist2(nodes[b].x, nodes[b].y, qx, qy);
    return da < db ? a : b;
}

float phys_kd_axis_dist(const phys_kd_node_t *n, float qx, float qy) {
    if (n->axis == 0) { float d = qx - n->x; return d * d; }
    else { float d = qy - n->y; return d * d; }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1498 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1498: empty output");
    assert!(code.contains("fn phys_kd_closer"), "C1498: should contain phys_kd_closer");
}

#[test]
fn c1499_octree_insert() {
    let c_code = r#"
typedef struct { float cx; float cy; float half; int children[4]; int obj_id; int count; } phys_qtree_t;

void phys_qtree_init(phys_qtree_t *q, float cx, float cy, float half) {
    q->cx = cx; q->cy = cy; q->half = half;
    q->obj_id = -1; q->count = 0;
    int i; for (i = 0; i < 4; i++) q->children[i] = -1;
}

int phys_qtree_quadrant(const phys_qtree_t *q, float x, float y) {
    int idx = 0;
    if (x >= q->cx) idx |= 1;
    if (y >= q->cy) idx |= 2;
    return idx;
}

int phys_qtree_contains(const phys_qtree_t *q, float x, float y) {
    return x >= q->cx - q->half && x <= q->cx + q->half &&
           y >= q->cy - q->half && y <= q->cy + q->half;
}

int phys_qtree_is_leaf(const phys_qtree_t *q) {
    return q->children[0] == -1;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1499 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1499: empty output");
    assert!(code.contains("fn phys_qtree_quadrant"), "C1499: should contain phys_qtree_quadrant");
}

#[test]
fn c1500_broad_phase_collision_pairs() {
    let c_code = r#"
typedef struct { float min_x; float max_x; int id; } phys_interval_t;
typedef struct { int a; int b; } phys_pair_t;

void phys_sort_intervals(phys_interval_t *arr, int n) {
    int i, j;
    for (i = 1; i < n; i++) {
        phys_interval_t key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j].min_x > key.min_x) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}

int phys_sweep_and_prune(phys_interval_t *intervals, int n, phys_pair_t *pairs, int max_pairs) {
    phys_sort_intervals(intervals, n);
    int count = 0;
    int i, j;
    for (i = 0; i < n && count < max_pairs; i++) {
        for (j = i + 1; j < n && intervals[j].min_x <= intervals[i].max_x; j++) {
            pairs[count].a = intervals[i].id;
            pairs[count].b = intervals[j].id;
            count++;
            if (count >= max_pairs) break;
        }
    }
    return count;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1500 failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1500: empty output");
    assert!(code.contains("fn phys_sweep_and_prune"), "C1500: should contain phys_sweep_and_prune");
}