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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
//! Popperian Falsification Test Suite for Decy C-to-Rust Transpiler
//!
//! C1851-C1875: Queue & Message Queue Implementations -- circular buffers,
//! priority queues, message queues, batch operations, and queue applications.
//! Tests are APPEND-ONLY per Popperian methodology.
//! Falsified tests are marked #[ignore = "FALSIFIED: reason"].
//!
//! These tests exercise real-world queue patterns commonly found in operating
//! systems, message brokers, event systems, and task schedulers -- all expressed
//! as valid C99 without #include directives.
//!
//! Organization:
//! - C1851-C1855: Basic queues (circular buffer, double-ended, bounded, multi-queue, ring buffer)
//! - C1856-C1860: Priority queues (min-priority, max-priority, stable priority, indexed priority, multi-level)
//! - C1861-C1865: Message queues (producer-consumer, broadcast, filtered, delayed, reliable)
//! - C1866-C1870: Queue operations (batch enqueue, drain, peek many, queue merging, queue splitting)
//! - C1871-C1875: Queue applications (task queue, event queue, command queue, undo queue, log queue)

// ============================================================================
// C1851-C1855: Basic Queues
// ============================================================================

/// C1851: Circular buffer queue with wrap-around
#[test]
fn c1851_circular_buffer_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_CIRC_CAP 128

typedef struct {
    int data[QUE_CIRC_CAP];
    int head;
    int tail;
    int count;
} que_circ_t;

void que_circ_init(que_circ_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
    int i;
    for (i = 0; i < QUE_CIRC_CAP; i++) {
        q->data[i] = 0;
    }
}

int que_circ_enqueue(que_circ_t *q, int val) {
    if (q->count >= QUE_CIRC_CAP) return -1;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % QUE_CIRC_CAP;
    q->count = q->count + 1;
    return 0;
}

int que_circ_dequeue(que_circ_t *q, int *out) {
    if (q->count <= 0) return -1;
    *out = q->data[q->head];
    q->head = (q->head + 1) % QUE_CIRC_CAP;
    q->count = q->count - 1;
    return 0;
}

int que_circ_peek(const que_circ_t *q) {
    if (q->count <= 0) return -1;
    return q->data[q->head];
}

int que_circ_is_full(const que_circ_t *q) {
    return q->count >= QUE_CIRC_CAP;
}

int que_circ_test(void) {
    que_circ_t q;
    que_circ_init(&q);
    que_circ_enqueue(&q, 10);
    que_circ_enqueue(&q, 20);
    que_circ_enqueue(&q, 30);
    if (que_circ_peek(&q) != 10) return -1;
    int val = 0;
    que_circ_dequeue(&q, &val);
    if (val != 10) return -2;
    if (q.count != 2) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1851: Circular buffer queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1851: Output should not be empty");
    assert!(code.contains("fn que_circ_init"), "C1851: Should contain que_circ_init");
    assert!(code.contains("fn que_circ_enqueue"), "C1851: Should contain que_circ_enqueue");
    assert!(code.contains("fn que_circ_dequeue"), "C1851: Should contain que_circ_dequeue");
    Ok(())
}

/// C1852: Double-ended queue (deque) with push/pop front and back
#[test]
fn c1852_double_ended_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_DEQUE_CAP 64

typedef struct {
    int data[QUE_DEQUE_CAP];
    int front;
    int back;
    int count;
} que_deque_t;

void que_deque_init(que_deque_t *dq) {
    dq->front = QUE_DEQUE_CAP / 2;
    dq->back = QUE_DEQUE_CAP / 2;
    dq->count = 0;
}

int que_deque_push_back(que_deque_t *dq, int val) {
    if (dq->count >= QUE_DEQUE_CAP) return -1;
    dq->data[dq->back] = val;
    dq->back = (dq->back + 1) % QUE_DEQUE_CAP;
    dq->count = dq->count + 1;
    return 0;
}

int que_deque_push_front(que_deque_t *dq, int val) {
    if (dq->count >= QUE_DEQUE_CAP) return -1;
    dq->front = (dq->front - 1 + QUE_DEQUE_CAP) % QUE_DEQUE_CAP;
    dq->data[dq->front] = val;
    dq->count = dq->count + 1;
    return 0;
}

int que_deque_pop_front(que_deque_t *dq, int *out) {
    if (dq->count <= 0) return -1;
    *out = dq->data[dq->front];
    dq->front = (dq->front + 1) % QUE_DEQUE_CAP;
    dq->count = dq->count - 1;
    return 0;
}

int que_deque_pop_back(que_deque_t *dq, int *out) {
    if (dq->count <= 0) return -1;
    dq->back = (dq->back - 1 + QUE_DEQUE_CAP) % QUE_DEQUE_CAP;
    *out = dq->data[dq->back];
    dq->count = dq->count - 1;
    return 0;
}

int que_deque_test(void) {
    que_deque_t dq;
    que_deque_init(&dq);
    que_deque_push_back(&dq, 10);
    que_deque_push_front(&dq, 5);
    que_deque_push_back(&dq, 20);
    int val = 0;
    que_deque_pop_front(&dq, &val);
    if (val != 5) return -1;
    que_deque_pop_back(&dq, &val);
    if (val != 20) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1852: Double-ended queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1852: Output should not be empty");
    assert!(code.contains("fn que_deque_init"), "C1852: Should contain que_deque_init");
    assert!(code.contains("fn que_deque_push_back"), "C1852: Should contain que_deque_push_back");
    assert!(code.contains("fn que_deque_pop_front"), "C1852: Should contain que_deque_pop_front");
    Ok(())
}

/// C1853: Bounded queue with high/low watermark flow control
#[test]
fn c1853_bounded_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_BOUND_CAP 256

typedef struct {
    int data[QUE_BOUND_CAP];
    int head;
    int tail;
    int count;
    int high_watermark;
    int low_watermark;
    int flow_blocked;
} que_bounded_t;

void que_bounded_init(que_bounded_t *q, int high, int low) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
    q->high_watermark = high;
    q->low_watermark = low;
    q->flow_blocked = 0;
}

int que_bounded_enqueue(que_bounded_t *q, int val) {
    if (q->count >= QUE_BOUND_CAP) return -1;
    if (q->flow_blocked) return -2;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % QUE_BOUND_CAP;
    q->count = q->count + 1;
    if (q->count >= q->high_watermark) {
        q->flow_blocked = 1;
    }
    return 0;
}

int que_bounded_dequeue(que_bounded_t *q, int *out) {
    if (q->count <= 0) return -1;
    *out = q->data[q->head];
    q->head = (q->head + 1) % QUE_BOUND_CAP;
    q->count = q->count - 1;
    if (q->flow_blocked && q->count <= q->low_watermark) {
        q->flow_blocked = 0;
    }
    return 0;
}

int que_bounded_is_blocked(const que_bounded_t *q) {
    return q->flow_blocked;
}

int que_bounded_test(void) {
    que_bounded_t q;
    que_bounded_init(&q, 5, 2);
    int i;
    for (i = 0; i < 5; i++) {
        que_bounded_enqueue(&q, i * 10);
    }
    if (!que_bounded_is_blocked(&q)) return -1;
    int val = 0;
    for (i = 0; i < 3; i++) {
        que_bounded_dequeue(&q, &val);
    }
    if (que_bounded_is_blocked(&q)) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1853: Bounded queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1853: Output should not be empty");
    assert!(code.contains("fn que_bounded_init"), "C1853: Should contain que_bounded_init");
    assert!(code.contains("fn que_bounded_enqueue"), "C1853: Should contain que_bounded_enqueue");
    assert!(code.contains("fn que_bounded_dequeue"), "C1853: Should contain que_bounded_dequeue");
    Ok(())
}

/// C1854: Multi-queue with round-robin dispatch across lanes
#[test]
fn c1854_multi_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_MULTI_LANES 4
#define QUE_MULTI_LANE_CAP 64

typedef struct {
    int data[QUE_MULTI_LANE_CAP];
    int head;
    int tail;
    int count;
} que_lane_t;

typedef struct {
    que_lane_t lanes[QUE_MULTI_LANES];
    int num_lanes;
    int dispatch_idx;
    int total_items;
} que_multi_t;

void que_multi_init(que_multi_t *mq) {
    int i;
    mq->num_lanes = QUE_MULTI_LANES;
    mq->dispatch_idx = 0;
    mq->total_items = 0;
    for (i = 0; i < QUE_MULTI_LANES; i++) {
        mq->lanes[i].head = 0;
        mq->lanes[i].tail = 0;
        mq->lanes[i].count = 0;
    }
}

int que_multi_enqueue(que_multi_t *mq, int val) {
    int lane = mq->dispatch_idx;
    que_lane_t *l = &mq->lanes[lane];
    if (l->count >= QUE_MULTI_LANE_CAP) return -1;
    l->data[l->tail] = val;
    l->tail = (l->tail + 1) % QUE_MULTI_LANE_CAP;
    l->count = l->count + 1;
    mq->total_items = mq->total_items + 1;
    mq->dispatch_idx = (mq->dispatch_idx + 1) % mq->num_lanes;
    return lane;
}

int que_multi_dequeue(que_multi_t *mq, int lane, int *out) {
    if (lane < 0 || lane >= mq->num_lanes) return -1;
    que_lane_t *l = &mq->lanes[lane];
    if (l->count <= 0) return -2;
    *out = l->data[l->head];
    l->head = (l->head + 1) % QUE_MULTI_LANE_CAP;
    l->count = l->count - 1;
    mq->total_items = mq->total_items - 1;
    return 0;
}

int que_multi_lane_size(const que_multi_t *mq, int lane) {
    if (lane < 0 || lane >= mq->num_lanes) return -1;
    return mq->lanes[lane].count;
}

int que_multi_test(void) {
    que_multi_t mq;
    que_multi_init(&mq);
    que_multi_enqueue(&mq, 100);
    que_multi_enqueue(&mq, 200);
    que_multi_enqueue(&mq, 300);
    que_multi_enqueue(&mq, 400);
    if (que_multi_lane_size(&mq, 0) != 1) return -1;
    if (que_multi_lane_size(&mq, 1) != 1) return -2;
    if (mq.total_items != 4) return -3;
    int val = 0;
    que_multi_dequeue(&mq, 0, &val);
    if (val != 100) return -4;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1854: Multi-queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1854: Output should not be empty");
    assert!(code.contains("fn que_multi_init"), "C1854: Should contain que_multi_init");
    assert!(code.contains("fn que_multi_enqueue"), "C1854: Should contain que_multi_enqueue");
    assert!(code.contains("fn que_multi_dequeue"), "C1854: Should contain que_multi_dequeue");
    Ok(())
}

/// C1855: Ring buffer queue with overwrite-on-full semantics
#[test]
fn c1855_ring_buffer_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_RING_CAP 32

typedef struct {
    int data[QUE_RING_CAP];
    int write_pos;
    int read_pos;
    int count;
    int overwrite_count;
} que_ring_t;

void que_ring_init(que_ring_t *r) {
    r->write_pos = 0;
    r->read_pos = 0;
    r->count = 0;
    r->overwrite_count = 0;
    int i;
    for (i = 0; i < QUE_RING_CAP; i++) {
        r->data[i] = 0;
    }
}

void que_ring_write(que_ring_t *r, int val) {
    if (r->count >= QUE_RING_CAP) {
        r->read_pos = (r->read_pos + 1) % QUE_RING_CAP;
        r->overwrite_count = r->overwrite_count + 1;
    } else {
        r->count = r->count + 1;
    }
    r->data[r->write_pos] = val;
    r->write_pos = (r->write_pos + 1) % QUE_RING_CAP;
}

int que_ring_read(que_ring_t *r, int *out) {
    if (r->count <= 0) return -1;
    *out = r->data[r->read_pos];
    r->read_pos = (r->read_pos + 1) % QUE_RING_CAP;
    r->count = r->count - 1;
    return 0;
}

int que_ring_available(const que_ring_t *r) {
    return r->count;
}

int que_ring_test(void) {
    que_ring_t r;
    que_ring_init(&r);
    int i;
    for (i = 0; i < 40; i++) {
        que_ring_write(&r, i);
    }
    if (que_ring_available(&r) != QUE_RING_CAP) return -1;
    if (r.overwrite_count != 8) return -2;
    int val = 0;
    que_ring_read(&r, &val);
    if (val != 8) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1855: Ring buffer queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1855: Output should not be empty");
    assert!(code.contains("fn que_ring_init"), "C1855: Should contain que_ring_init");
    assert!(code.contains("fn que_ring_write"), "C1855: Should contain que_ring_write");
    assert!(code.contains("fn que_ring_read"), "C1855: Should contain que_ring_read");
    Ok(())
}

// ============================================================================
// C1856-C1860: Priority Queues
// ============================================================================

/// C1856: Min-priority queue using sorted insertion
#[test]
fn c1856_min_priority_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_MINPQ_CAP 128

typedef struct {
    int key;
    int value;
} que_minpq_entry_t;

typedef struct {
    que_minpq_entry_t entries[QUE_MINPQ_CAP];
    int count;
} que_minpq_t;

void que_minpq_init(que_minpq_t *pq) {
    pq->count = 0;
}

static void que_minpq_swap(que_minpq_entry_t *a, que_minpq_entry_t *b) {
    que_minpq_entry_t tmp;
    tmp.key = a->key;
    tmp.value = a->value;
    a->key = b->key;
    a->value = b->value;
    b->key = tmp.key;
    b->value = tmp.value;
}

static void que_minpq_sift_up(que_minpq_t *pq, int idx) {
    while (idx > 0) {
        int parent = (idx - 1) / 2;
        if (pq->entries[parent].key > pq->entries[idx].key) {
            que_minpq_swap(&pq->entries[parent], &pq->entries[idx]);
            idx = parent;
        } else {
            break;
        }
    }
}

static void que_minpq_sift_down(que_minpq_t *pq, int idx) {
    while (1) {
        int smallest = idx;
        int left = 2 * idx + 1;
        int right = 2 * idx + 2;
        if (left < pq->count && pq->entries[left].key < pq->entries[smallest].key)
            smallest = left;
        if (right < pq->count && pq->entries[right].key < pq->entries[smallest].key)
            smallest = right;
        if (smallest != idx) {
            que_minpq_swap(&pq->entries[idx], &pq->entries[smallest]);
            idx = smallest;
        } else {
            break;
        }
    }
}

int que_minpq_insert(que_minpq_t *pq, int key, int value) {
    if (pq->count >= QUE_MINPQ_CAP) return -1;
    pq->entries[pq->count].key = key;
    pq->entries[pq->count].value = value;
    que_minpq_sift_up(pq, pq->count);
    pq->count = pq->count + 1;
    return 0;
}

int que_minpq_extract(que_minpq_t *pq, int *key_out, int *val_out) {
    if (pq->count <= 0) return -1;
    *key_out = pq->entries[0].key;
    *val_out = pq->entries[0].value;
    pq->count = pq->count - 1;
    pq->entries[0] = pq->entries[pq->count];
    que_minpq_sift_down(pq, 0);
    return 0;
}

int que_minpq_test(void) {
    que_minpq_t pq;
    que_minpq_init(&pq);
    que_minpq_insert(&pq, 30, 300);
    que_minpq_insert(&pq, 10, 100);
    que_minpq_insert(&pq, 20, 200);
    int k = 0, v = 0;
    que_minpq_extract(&pq, &k, &v);
    if (k != 10 || v != 100) return -1;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1856: Min-priority queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1856: Output should not be empty");
    assert!(code.contains("fn que_minpq_init"), "C1856: Should contain que_minpq_init");
    assert!(code.contains("fn que_minpq_insert"), "C1856: Should contain que_minpq_insert");
    assert!(code.contains("fn que_minpq_extract"), "C1856: Should contain que_minpq_extract");
    Ok(())
}

/// C1857: Max-priority queue with peek and size
#[test]
fn c1857_max_priority_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_MAXPQ_CAP 128

typedef struct {
    int priority;
    int data;
} que_maxpq_entry_t;

typedef struct {
    que_maxpq_entry_t heap[QUE_MAXPQ_CAP];
    int size;
} que_maxpq_t;

void que_maxpq_init(que_maxpq_t *pq) {
    pq->size = 0;
}

static void que_maxpq_swap_entries(que_maxpq_entry_t *a, que_maxpq_entry_t *b) {
    que_maxpq_entry_t tmp;
    tmp.priority = a->priority;
    tmp.data = a->data;
    a->priority = b->priority;
    a->data = b->data;
    b->priority = tmp.priority;
    b->data = tmp.data;
}

int que_maxpq_insert(que_maxpq_t *pq, int priority, int data) {
    if (pq->size >= QUE_MAXPQ_CAP) return -1;
    int idx = pq->size;
    pq->heap[idx].priority = priority;
    pq->heap[idx].data = data;
    pq->size = pq->size + 1;
    while (idx > 0) {
        int parent = (idx - 1) / 2;
        if (pq->heap[parent].priority < pq->heap[idx].priority) {
            que_maxpq_swap_entries(&pq->heap[parent], &pq->heap[idx]);
            idx = parent;
        } else {
            break;
        }
    }
    return 0;
}

int que_maxpq_extract(que_maxpq_t *pq, int *out_data) {
    if (pq->size <= 0) return -1;
    *out_data = pq->heap[0].data;
    pq->size = pq->size - 1;
    pq->heap[0] = pq->heap[pq->size];
    int idx = 0;
    while (1) {
        int largest = idx;
        int left = 2 * idx + 1;
        int right = 2 * idx + 2;
        if (left < pq->size && pq->heap[left].priority > pq->heap[largest].priority)
            largest = left;
        if (right < pq->size && pq->heap[right].priority > pq->heap[largest].priority)
            largest = right;
        if (largest != idx) {
            que_maxpq_swap_entries(&pq->heap[idx], &pq->heap[largest]);
            idx = largest;
        } else {
            break;
        }
    }
    return 0;
}

int que_maxpq_peek(const que_maxpq_t *pq) {
    if (pq->size <= 0) return -1;
    return pq->heap[0].data;
}

int que_maxpq_test(void) {
    que_maxpq_t pq;
    que_maxpq_init(&pq);
    que_maxpq_insert(&pq, 5, 50);
    que_maxpq_insert(&pq, 9, 90);
    que_maxpq_insert(&pq, 3, 30);
    if (que_maxpq_peek(&pq) != 90) return -1;
    int val = 0;
    que_maxpq_extract(&pq, &val);
    if (val != 90) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1857: Max-priority queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1857: Output should not be empty");
    assert!(code.contains("fn que_maxpq_init"), "C1857: Should contain que_maxpq_init");
    assert!(code.contains("fn que_maxpq_insert"), "C1857: Should contain que_maxpq_insert");
    assert!(code.contains("fn que_maxpq_extract"), "C1857: Should contain que_maxpq_extract");
    Ok(())
}

/// C1858: Stable priority queue preserving insertion order at same priority
#[test]
fn c1858_stable_priority_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_STABLE_CAP 128

typedef struct {
    int priority;
    int data;
    uint32_t sequence;
} que_stable_entry_t;

typedef struct {
    que_stable_entry_t entries[QUE_STABLE_CAP];
    int count;
    uint32_t next_seq;
} que_stable_pq_t;

void que_stable_init(que_stable_pq_t *pq) {
    pq->count = 0;
    pq->next_seq = 0;
}

int que_stable_insert(que_stable_pq_t *pq, int priority, int data) {
    if (pq->count >= QUE_STABLE_CAP) return -1;
    int pos = pq->count;
    pq->entries[pos].priority = priority;
    pq->entries[pos].data = data;
    pq->entries[pos].sequence = pq->next_seq;
    pq->next_seq = pq->next_seq + 1;
    pq->count = pq->count + 1;
    while (pos > 0) {
        int parent = (pos - 1) / 2;
        int swap = 0;
        if (pq->entries[parent].priority > pq->entries[pos].priority) {
            swap = 1;
        } else if (pq->entries[parent].priority == pq->entries[pos].priority &&
                   pq->entries[parent].sequence > pq->entries[pos].sequence) {
            swap = 1;
        }
        if (swap) {
            que_stable_entry_t tmp = pq->entries[parent];
            pq->entries[parent] = pq->entries[pos];
            pq->entries[pos] = tmp;
            pos = parent;
        } else {
            break;
        }
    }
    return 0;
}

int que_stable_extract(que_stable_pq_t *pq, int *out) {
    if (pq->count <= 0) return -1;
    *out = pq->entries[0].data;
    pq->count = pq->count - 1;
    pq->entries[0] = pq->entries[pq->count];
    int idx = 0;
    while (1) {
        int best = idx;
        int left = 2 * idx + 1;
        int right = 2 * idx + 2;
        if (left < pq->count) {
            if (pq->entries[left].priority < pq->entries[best].priority ||
                (pq->entries[left].priority == pq->entries[best].priority &&
                 pq->entries[left].sequence < pq->entries[best].sequence))
                best = left;
        }
        if (right < pq->count) {
            if (pq->entries[right].priority < pq->entries[best].priority ||
                (pq->entries[right].priority == pq->entries[best].priority &&
                 pq->entries[right].sequence < pq->entries[best].sequence))
                best = right;
        }
        if (best != idx) {
            que_stable_entry_t tmp = pq->entries[idx];
            pq->entries[idx] = pq->entries[best];
            pq->entries[best] = tmp;
            idx = best;
        } else {
            break;
        }
    }
    return 0;
}

int que_stable_test(void) {
    que_stable_pq_t pq;
    que_stable_init(&pq);
    que_stable_insert(&pq, 1, 10);
    que_stable_insert(&pq, 1, 20);
    que_stable_insert(&pq, 1, 30);
    int val = 0;
    que_stable_extract(&pq, &val);
    if (val != 10) return -1;
    que_stable_extract(&pq, &val);
    if (val != 20) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1858: Stable priority queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1858: Output should not be empty");
    assert!(code.contains("fn que_stable_init"), "C1858: Should contain que_stable_init");
    assert!(code.contains("fn que_stable_insert"), "C1858: Should contain que_stable_insert");
    assert!(code.contains("fn que_stable_extract"), "C1858: Should contain que_stable_extract");
    Ok(())
}

/// C1859: Indexed priority queue with decrease-key operation
#[test]
fn c1859_indexed_priority_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_IDXPQ_CAP 64

typedef struct {
    int keys[QUE_IDXPQ_CAP];
    int heap[QUE_IDXPQ_CAP];
    int pos[QUE_IDXPQ_CAP];
    int size;
    int max_id;
} que_idxpq_t;

void que_idxpq_init(que_idxpq_t *pq) {
    pq->size = 0;
    pq->max_id = QUE_IDXPQ_CAP;
    int i;
    for (i = 0; i < QUE_IDXPQ_CAP; i++) {
        pq->keys[i] = 0;
        pq->heap[i] = -1;
        pq->pos[i] = -1;
    }
}

static void que_idxpq_swap(que_idxpq_t *pq, int i, int j) {
    int hi = pq->heap[i];
    int hj = pq->heap[j];
    pq->heap[i] = hj;
    pq->heap[j] = hi;
    pq->pos[hj] = i;
    pq->pos[hi] = j;
}

static void que_idxpq_bubble_up(que_idxpq_t *pq, int idx) {
    while (idx > 0) {
        int parent = (idx - 1) / 2;
        if (pq->keys[pq->heap[idx]] < pq->keys[pq->heap[parent]]) {
            que_idxpq_swap(pq, idx, parent);
            idx = parent;
        } else {
            break;
        }
    }
}

int que_idxpq_insert(que_idxpq_t *pq, int id, int key) {
    if (id < 0 || id >= pq->max_id) return -1;
    if (pq->size >= QUE_IDXPQ_CAP) return -2;
    pq->keys[id] = key;
    pq->heap[pq->size] = id;
    pq->pos[id] = pq->size;
    pq->size = pq->size + 1;
    que_idxpq_bubble_up(pq, pq->pos[id]);
    return 0;
}

int que_idxpq_decrease_key(que_idxpq_t *pq, int id, int new_key) {
    if (id < 0 || id >= pq->max_id) return -1;
    if (pq->pos[id] < 0) return -2;
    if (new_key >= pq->keys[id]) return -3;
    pq->keys[id] = new_key;
    que_idxpq_bubble_up(pq, pq->pos[id]);
    return 0;
}

int que_idxpq_peek_id(const que_idxpq_t *pq) {
    if (pq->size <= 0) return -1;
    return pq->heap[0];
}

int que_idxpq_test(void) {
    que_idxpq_t pq;
    que_idxpq_init(&pq);
    que_idxpq_insert(&pq, 0, 50);
    que_idxpq_insert(&pq, 1, 30);
    que_idxpq_insert(&pq, 2, 70);
    if (que_idxpq_peek_id(&pq) != 1) return -1;
    que_idxpq_decrease_key(&pq, 2, 10);
    if (que_idxpq_peek_id(&pq) != 2) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1859: Indexed priority queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1859: Output should not be empty");
    assert!(code.contains("fn que_idxpq_init"), "C1859: Should contain que_idxpq_init");
    assert!(code.contains("fn que_idxpq_insert"), "C1859: Should contain que_idxpq_insert");
    assert!(
        code.contains("fn que_idxpq_decrease_key"),
        "C1859: Should contain que_idxpq_decrease_key"
    );
    Ok(())
}

/// C1860: Multi-level feedback queue with aging promotion
#[test]
fn c1860_multilevel_feedback_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_MLFQ_LEVELS 4
#define QUE_MLFQ_CAP 32

typedef struct {
    int task_id;
    int age;
    int cpu_used;
} que_mlfq_task_t;

typedef struct {
    que_mlfq_task_t tasks[QUE_MLFQ_CAP];
    int head;
    int tail;
    int count;
} que_mlfq_level_t;

typedef struct {
    que_mlfq_level_t levels[QUE_MLFQ_LEVELS];
    int age_threshold;
    int total_tasks;
} que_mlfq_t;

void que_mlfq_init(que_mlfq_t *mq, int age_thresh) {
    mq->age_threshold = age_thresh;
    mq->total_tasks = 0;
    int i;
    for (i = 0; i < QUE_MLFQ_LEVELS; i++) {
        mq->levels[i].head = 0;
        mq->levels[i].tail = 0;
        mq->levels[i].count = 0;
    }
}

int que_mlfq_add_task(que_mlfq_t *mq, int level, int task_id) {
    if (level < 0 || level >= QUE_MLFQ_LEVELS) return -1;
    que_mlfq_level_t *lv = &mq->levels[level];
    if (lv->count >= QUE_MLFQ_CAP) return -2;
    lv->tasks[lv->tail].task_id = task_id;
    lv->tasks[lv->tail].age = 0;
    lv->tasks[lv->tail].cpu_used = 0;
    lv->tail = (lv->tail + 1) % QUE_MLFQ_CAP;
    lv->count = lv->count + 1;
    mq->total_tasks = mq->total_tasks + 1;
    return 0;
}

int que_mlfq_promote_aged(que_mlfq_t *mq) {
    int promoted = 0;
    int level;
    for (level = 1; level < QUE_MLFQ_LEVELS; level++) {
        que_mlfq_level_t *lv = &mq->levels[level];
        int checked = 0;
        while (checked < lv->count) {
            int idx = (lv->head + checked) % QUE_MLFQ_CAP;
            lv->tasks[idx].age = lv->tasks[idx].age + 1;
            if (lv->tasks[idx].age >= mq->age_threshold) {
                promoted = promoted + 1;
            }
            checked = checked + 1;
        }
    }
    return promoted;
}

int que_mlfq_select_task(const que_mlfq_t *mq) {
    int level;
    for (level = 0; level < QUE_MLFQ_LEVELS; level++) {
        if (mq->levels[level].count > 0) {
            int head = mq->levels[level].head;
            return mq->levels[level].tasks[head].task_id;
        }
    }
    return -1;
}

int que_mlfq_test(void) {
    que_mlfq_t mq;
    que_mlfq_init(&mq, 5);
    que_mlfq_add_task(&mq, 0, 100);
    que_mlfq_add_task(&mq, 1, 200);
    que_mlfq_add_task(&mq, 2, 300);
    if (que_mlfq_select_task(&mq) != 100) return -1;
    if (mq.total_tasks != 3) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C1860: Multi-level feedback queue should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1860: Output should not be empty");
    assert!(code.contains("fn que_mlfq_init"), "C1860: Should contain que_mlfq_init");
    assert!(code.contains("fn que_mlfq_add_task"), "C1860: Should contain que_mlfq_add_task");
    assert!(code.contains("fn que_mlfq_select_task"), "C1860: Should contain que_mlfq_select_task");
    Ok(())
}

// ============================================================================
// C1861-C1865: Message Queues
// ============================================================================

/// C1861: Producer-consumer message queue with typed messages
#[test]
fn c1861_producer_consumer_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_MSG_CAP 64
#define QUE_MSG_DATA_LEN 32

typedef struct {
    uint32_t msg_type;
    int sender_id;
    int payload[QUE_MSG_DATA_LEN];
    int payload_len;
} que_msg_t;

typedef struct {
    que_msg_t buffer[QUE_MSG_CAP];
    int head;
    int tail;
    int count;
    int producers_active;
    int consumers_active;
} que_prodcons_t;

void que_prodcons_init(que_prodcons_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
    q->producers_active = 0;
    q->consumers_active = 0;
}

int que_prodcons_send(que_prodcons_t *q, uint32_t msg_type, int sender, const int *data, int len) {
    if (q->count >= QUE_MSG_CAP) return -1;
    if (len > QUE_MSG_DATA_LEN) len = QUE_MSG_DATA_LEN;
    que_msg_t *m = &q->buffer[q->tail];
    m->msg_type = msg_type;
    m->sender_id = sender;
    m->payload_len = len;
    int i;
    for (i = 0; i < len; i++) {
        m->payload[i] = data[i];
    }
    q->tail = (q->tail + 1) % QUE_MSG_CAP;
    q->count = q->count + 1;
    return 0;
}

int que_prodcons_recv(que_prodcons_t *q, que_msg_t *out) {
    if (q->count <= 0) return -1;
    *out = q->buffer[q->head];
    q->head = (q->head + 1) % QUE_MSG_CAP;
    q->count = q->count - 1;
    return 0;
}

int que_prodcons_pending(const que_prodcons_t *q) {
    return q->count;
}

int que_prodcons_test(void) {
    que_prodcons_t q;
    que_prodcons_init(&q);
    int data[4];
    data[0] = 1; data[1] = 2; data[2] = 3; data[3] = 4;
    que_prodcons_send(&q, 1, 0, data, 4);
    que_prodcons_send(&q, 2, 1, data, 2);
    if (que_prodcons_pending(&q) != 2) return -1;
    que_msg_t msg;
    que_prodcons_recv(&q, &msg);
    if (msg.msg_type != 1) return -2;
    if (msg.sender_id != 0) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1861: Producer-consumer queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1861: Output should not be empty");
    assert!(code.contains("fn que_prodcons_init"), "C1861: Should contain que_prodcons_init");
    assert!(code.contains("fn que_prodcons_send"), "C1861: Should contain que_prodcons_send");
    assert!(code.contains("fn que_prodcons_recv"), "C1861: Should contain que_prodcons_recv");
    Ok(())
}

/// C1862: Broadcast queue with multiple subscriber cursors
#[test]
fn c1862_broadcast_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_BCAST_CAP 64
#define QUE_BCAST_MAX_SUBS 8

typedef struct {
    int data[QUE_BCAST_CAP];
    int write_pos;
    int sub_read_pos[QUE_BCAST_MAX_SUBS];
    int sub_active[QUE_BCAST_MAX_SUBS];
    int num_subs;
    int total_written;
} que_bcast_t;

void que_bcast_init(que_bcast_t *bq) {
    bq->write_pos = 0;
    bq->num_subs = 0;
    bq->total_written = 0;
    int i;
    for (i = 0; i < QUE_BCAST_MAX_SUBS; i++) {
        bq->sub_read_pos[i] = 0;
        bq->sub_active[i] = 0;
    }
}

int que_bcast_subscribe(que_bcast_t *bq) {
    if (bq->num_subs >= QUE_BCAST_MAX_SUBS) return -1;
    int id = bq->num_subs;
    bq->sub_active[id] = 1;
    bq->sub_read_pos[id] = bq->write_pos;
    bq->num_subs = bq->num_subs + 1;
    return id;
}

int que_bcast_publish(que_bcast_t *bq, int val) {
    bq->data[bq->write_pos % QUE_BCAST_CAP] = val;
    bq->write_pos = bq->write_pos + 1;
    bq->total_written = bq->total_written + 1;
    return 0;
}

int que_bcast_consume(que_bcast_t *bq, int sub_id, int *out) {
    if (sub_id < 0 || sub_id >= bq->num_subs) return -1;
    if (!bq->sub_active[sub_id]) return -2;
    if (bq->sub_read_pos[sub_id] >= bq->write_pos) return -3;
    *out = bq->data[bq->sub_read_pos[sub_id] % QUE_BCAST_CAP];
    bq->sub_read_pos[sub_id] = bq->sub_read_pos[sub_id] + 1;
    return 0;
}

int que_bcast_test(void) {
    que_bcast_t bq;
    que_bcast_init(&bq);
    int s0 = que_bcast_subscribe(&bq);
    int s1 = que_bcast_subscribe(&bq);
    que_bcast_publish(&bq, 42);
    que_bcast_publish(&bq, 99);
    int val = 0;
    que_bcast_consume(&bq, s0, &val);
    if (val != 42) return -1;
    que_bcast_consume(&bq, s1, &val);
    if (val != 42) return -2;
    if (s0 != 0 || s1 != 1) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1862: Broadcast queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1862: Output should not be empty");
    assert!(code.contains("fn que_bcast_init"), "C1862: Should contain que_bcast_init");
    assert!(code.contains("fn que_bcast_publish"), "C1862: Should contain que_bcast_publish");
    assert!(code.contains("fn que_bcast_consume"), "C1862: Should contain que_bcast_consume");
    Ok(())
}

/// C1863: Filtered queue that only dequeues messages matching a predicate type
#[test]
fn c1863_filtered_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_FILT_CAP 64

typedef struct {
    uint32_t msg_type;
    int value;
    int processed;
} que_filt_msg_t;

typedef struct {
    que_filt_msg_t msgs[QUE_FILT_CAP];
    int count;
} que_filt_t;

void que_filt_init(que_filt_t *q) {
    q->count = 0;
    int i;
    for (i = 0; i < QUE_FILT_CAP; i++) {
        q->msgs[i].msg_type = 0;
        q->msgs[i].value = 0;
        q->msgs[i].processed = 0;
    }
}

int que_filt_push(que_filt_t *q, uint32_t msg_type, int value) {
    if (q->count >= QUE_FILT_CAP) return -1;
    q->msgs[q->count].msg_type = msg_type;
    q->msgs[q->count].value = value;
    q->msgs[q->count].processed = 0;
    q->count = q->count + 1;
    return 0;
}

int que_filt_pop_by_type(que_filt_t *q, uint32_t target_type, int *out_value) {
    int i;
    for (i = 0; i < q->count; i++) {
        if (q->msgs[i].msg_type == target_type && !q->msgs[i].processed) {
            *out_value = q->msgs[i].value;
            q->msgs[i].processed = 1;
            return 0;
        }
    }
    return -1;
}

int que_filt_count_by_type(const que_filt_t *q, uint32_t target_type) {
    int cnt = 0;
    int i;
    for (i = 0; i < q->count; i++) {
        if (q->msgs[i].msg_type == target_type && !q->msgs[i].processed) {
            cnt = cnt + 1;
        }
    }
    return cnt;
}

int que_filt_test(void) {
    que_filt_t q;
    que_filt_init(&q);
    que_filt_push(&q, 1, 100);
    que_filt_push(&q, 2, 200);
    que_filt_push(&q, 1, 300);
    que_filt_push(&q, 3, 400);
    if (que_filt_count_by_type(&q, 1) != 2) return -1;
    int val = 0;
    que_filt_pop_by_type(&q, 2, &val);
    if (val != 200) return -2;
    if (que_filt_count_by_type(&q, 2) != 0) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1863: Filtered queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1863: Output should not be empty");
    assert!(code.contains("fn que_filt_init"), "C1863: Should contain que_filt_init");
    assert!(code.contains("fn que_filt_push"), "C1863: Should contain que_filt_push");
    assert!(code.contains("fn que_filt_pop_by_type"), "C1863: Should contain que_filt_pop_by_type");
    Ok(())
}

/// C1864: Delayed queue where messages become available after a delay tick
#[test]
fn c1864_delayed_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_DELAY_CAP 64

typedef struct {
    int value;
    uint32_t ready_at;
    int active;
} que_delay_entry_t;

typedef struct {
    que_delay_entry_t entries[QUE_DELAY_CAP];
    int count;
    uint32_t current_tick;
} que_delay_t;

void que_delay_init(que_delay_t *q) {
    q->count = 0;
    q->current_tick = 0;
    int i;
    for (i = 0; i < QUE_DELAY_CAP; i++) {
        q->entries[i].active = 0;
    }
}

int que_delay_enqueue(que_delay_t *q, int value, uint32_t delay_ticks) {
    if (q->count >= QUE_DELAY_CAP) return -1;
    int i;
    for (i = 0; i < QUE_DELAY_CAP; i++) {
        if (!q->entries[i].active) {
            q->entries[i].value = value;
            q->entries[i].ready_at = q->current_tick + delay_ticks;
            q->entries[i].active = 1;
            q->count = q->count + 1;
            return 0;
        }
    }
    return -2;
}

int que_delay_try_dequeue(que_delay_t *q, int *out) {
    int i;
    int best_idx = -1;
    uint32_t best_ready = 0;
    for (i = 0; i < QUE_DELAY_CAP; i++) {
        if (q->entries[i].active && q->entries[i].ready_at <= q->current_tick) {
            if (best_idx < 0 || q->entries[i].ready_at < best_ready) {
                best_idx = i;
                best_ready = q->entries[i].ready_at;
            }
        }
    }
    if (best_idx < 0) return -1;
    *out = q->entries[best_idx].value;
    q->entries[best_idx].active = 0;
    q->count = q->count - 1;
    return 0;
}

void que_delay_tick(que_delay_t *q) {
    q->current_tick = q->current_tick + 1;
}

int que_delay_ready_count(const que_delay_t *q) {
    int cnt = 0;
    int i;
    for (i = 0; i < QUE_DELAY_CAP; i++) {
        if (q->entries[i].active && q->entries[i].ready_at <= q->current_tick) {
            cnt = cnt + 1;
        }
    }
    return cnt;
}

int que_delay_test(void) {
    que_delay_t q;
    que_delay_init(&q);
    que_delay_enqueue(&q, 100, 3);
    que_delay_enqueue(&q, 200, 1);
    if (que_delay_ready_count(&q) != 0) return -1;
    que_delay_tick(&q);
    if (que_delay_ready_count(&q) != 1) return -2;
    int val = 0;
    que_delay_try_dequeue(&q, &val);
    if (val != 200) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1864: Delayed queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1864: Output should not be empty");
    assert!(code.contains("fn que_delay_init"), "C1864: Should contain que_delay_init");
    assert!(code.contains("fn que_delay_enqueue"), "C1864: Should contain que_delay_enqueue");
    assert!(
        code.contains("fn que_delay_try_dequeue"),
        "C1864: Should contain que_delay_try_dequeue"
    );
    Ok(())
}

/// C1865: Reliable message queue with acknowledgment tracking
#[test]
fn c1865_reliable_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_REL_CAP 64
#define QUE_REL_PENDING 0
#define QUE_REL_DELIVERED 1
#define QUE_REL_ACKED 2

typedef struct {
    int value;
    uint32_t msg_id;
    int status;
    int retry_count;
} que_rel_msg_t;

typedef struct {
    que_rel_msg_t msgs[QUE_REL_CAP];
    int count;
    uint32_t next_id;
    int max_retries;
} que_rel_t;

void que_rel_init(que_rel_t *q, int max_retries) {
    q->count = 0;
    q->next_id = 1;
    q->max_retries = max_retries;
    int i;
    for (i = 0; i < QUE_REL_CAP; i++) {
        q->msgs[i].status = -1;
    }
}

uint32_t que_rel_send(que_rel_t *q, int value) {
    if (q->count >= QUE_REL_CAP) return 0;
    int i;
    for (i = 0; i < QUE_REL_CAP; i++) {
        if (q->msgs[i].status < 0) {
            q->msgs[i].value = value;
            q->msgs[i].msg_id = q->next_id;
            q->msgs[i].status = QUE_REL_PENDING;
            q->msgs[i].retry_count = 0;
            q->count = q->count + 1;
            q->next_id = q->next_id + 1;
            return q->msgs[i].msg_id;
        }
    }
    return 0;
}

int que_rel_deliver(que_rel_t *q, int *out_value, uint32_t *out_id) {
    int i;
    for (i = 0; i < QUE_REL_CAP; i++) {
        if (q->msgs[i].status == QUE_REL_PENDING) {
            *out_value = q->msgs[i].value;
            *out_id = q->msgs[i].msg_id;
            q->msgs[i].status = QUE_REL_DELIVERED;
            return 0;
        }
    }
    return -1;
}

int que_rel_ack(que_rel_t *q, uint32_t msg_id) {
    int i;
    for (i = 0; i < QUE_REL_CAP; i++) {
        if (q->msgs[i].msg_id == msg_id && q->msgs[i].status == QUE_REL_DELIVERED) {
            q->msgs[i].status = QUE_REL_ACKED;
            q->count = q->count - 1;
            return 0;
        }
    }
    return -1;
}

int que_rel_test(void) {
    que_rel_t q;
    que_rel_init(&q, 3);
    uint32_t id1 = que_rel_send(&q, 42);
    uint32_t id2 = que_rel_send(&q, 99);
    if (id1 != 1 || id2 != 2) return -1;
    int val = 0;
    uint32_t mid = 0;
    que_rel_deliver(&q, &val, &mid);
    if (val != 42 || mid != 1) return -2;
    que_rel_ack(&q, mid);
    if (q.count != 1) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1865: Reliable queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1865: Output should not be empty");
    assert!(code.contains("fn que_rel_init"), "C1865: Should contain que_rel_init");
    assert!(code.contains("fn que_rel_send"), "C1865: Should contain que_rel_send");
    assert!(code.contains("fn que_rel_ack"), "C1865: Should contain que_rel_ack");
    Ok(())
}

// ============================================================================
// C1866-C1870: Queue Operations
// ============================================================================

/// C1866: Batch enqueue operation inserting multiple items atomically
#[test]
fn c1866_batch_enqueue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_BATCH_CAP 128

typedef struct {
    int data[QUE_BATCH_CAP];
    int head;
    int tail;
    int count;
} que_batch_t;

void que_batch_init(que_batch_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
}

int que_batch_enqueue_many(que_batch_t *q, const int *items, int n) {
    if (q->count + n > QUE_BATCH_CAP) return -1;
    int i;
    for (i = 0; i < n; i++) {
        q->data[q->tail] = items[i];
        q->tail = (q->tail + 1) % QUE_BATCH_CAP;
    }
    q->count = q->count + n;
    return n;
}

int que_batch_dequeue(que_batch_t *q, int *out) {
    if (q->count <= 0) return -1;
    *out = q->data[q->head];
    q->head = (q->head + 1) % QUE_BATCH_CAP;
    q->count = q->count - 1;
    return 0;
}

int que_batch_size(const que_batch_t *q) {
    return q->count;
}

int que_batch_test(void) {
    que_batch_t q;
    que_batch_init(&q);
    int items[5];
    items[0] = 10; items[1] = 20; items[2] = 30; items[3] = 40; items[4] = 50;
    int added = que_batch_enqueue_many(&q, items, 5);
    if (added != 5) return -1;
    if (que_batch_size(&q) != 5) return -2;
    int val = 0;
    que_batch_dequeue(&q, &val);
    if (val != 10) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1866: Batch enqueue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1866: Output should not be empty");
    assert!(code.contains("fn que_batch_init"), "C1866: Should contain que_batch_init");
    assert!(
        code.contains("fn que_batch_enqueue_many"),
        "C1866: Should contain que_batch_enqueue_many"
    );
    assert!(code.contains("fn que_batch_dequeue"), "C1866: Should contain que_batch_dequeue");
    Ok(())
}

/// C1867: Queue drain operation removing all elements into a buffer
#[test]
fn c1867_queue_drain() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_DRAIN_CAP 64

typedef struct {
    int data[QUE_DRAIN_CAP];
    int head;
    int tail;
    int count;
} que_drain_t;

void que_drain_init(que_drain_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
}

int que_drain_push(que_drain_t *q, int val) {
    if (q->count >= QUE_DRAIN_CAP) return -1;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % QUE_DRAIN_CAP;
    q->count = q->count + 1;
    return 0;
}

int que_drain_all(que_drain_t *q, int *output, int max_out) {
    int drained = 0;
    while (q->count > 0 && drained < max_out) {
        output[drained] = q->data[q->head];
        q->head = (q->head + 1) % QUE_DRAIN_CAP;
        q->count = q->count - 1;
        drained = drained + 1;
    }
    return drained;
}

int que_drain_is_empty(const que_drain_t *q) {
    return q->count == 0;
}

int que_drain_test(void) {
    que_drain_t q;
    que_drain_init(&q);
    que_drain_push(&q, 1);
    que_drain_push(&q, 2);
    que_drain_push(&q, 3);
    que_drain_push(&q, 4);
    int buf[10];
    int n = que_drain_all(&q, buf, 10);
    if (n != 4) return -1;
    if (buf[0] != 1 || buf[3] != 4) return -2;
    if (!que_drain_is_empty(&q)) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1867: Queue drain should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1867: Output should not be empty");
    assert!(code.contains("fn que_drain_init"), "C1867: Should contain que_drain_init");
    assert!(code.contains("fn que_drain_push"), "C1867: Should contain que_drain_push");
    assert!(code.contains("fn que_drain_all"), "C1867: Should contain que_drain_all");
    Ok(())
}

/// C1868: Peek-many operation inspecting multiple front elements without removal
#[test]
fn c1868_peek_many() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_PEEK_CAP 64

typedef struct {
    int data[QUE_PEEK_CAP];
    int head;
    int tail;
    int count;
} que_peek_t;

void que_peek_init(que_peek_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
}

int que_peek_enqueue(que_peek_t *q, int val) {
    if (q->count >= QUE_PEEK_CAP) return -1;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % QUE_PEEK_CAP;
    q->count = q->count + 1;
    return 0;
}

int que_peek_front(const que_peek_t *q) {
    if (q->count <= 0) return -1;
    return q->data[q->head];
}

int que_peek_many(const que_peek_t *q, int *output, int n) {
    int available = q->count;
    if (n > available) n = available;
    int i;
    for (i = 0; i < n; i++) {
        int idx = (q->head + i) % QUE_PEEK_CAP;
        output[i] = q->data[idx];
    }
    return n;
}

int que_peek_dequeue(que_peek_t *q, int *out) {
    if (q->count <= 0) return -1;
    *out = q->data[q->head];
    q->head = (q->head + 1) % QUE_PEEK_CAP;
    q->count = q->count - 1;
    return 0;
}

int que_peek_test(void) {
    que_peek_t q;
    que_peek_init(&q);
    que_peek_enqueue(&q, 10);
    que_peek_enqueue(&q, 20);
    que_peek_enqueue(&q, 30);
    que_peek_enqueue(&q, 40);
    int buf[3];
    int got = que_peek_many(&q, buf, 3);
    if (got != 3) return -1;
    if (buf[0] != 10 || buf[1] != 20 || buf[2] != 30) return -2;
    if (q.count != 4) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1868: Peek-many should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1868: Output should not be empty");
    assert!(code.contains("fn que_peek_init"), "C1868: Should contain que_peek_init");
    assert!(code.contains("fn que_peek_many"), "C1868: Should contain que_peek_many");
    assert!(code.contains("fn que_peek_enqueue"), "C1868: Should contain que_peek_enqueue");
    Ok(())
}

/// C1869: Queue merging -- combine two sorted queues into one sorted result
#[test]
fn c1869_queue_merging() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_MERGE_CAP 128

typedef struct {
    int data[QUE_MERGE_CAP];
    int head;
    int tail;
    int count;
} que_merge_t;

void que_merge_init(que_merge_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
}

int que_merge_push(que_merge_t *q, int val) {
    if (q->count >= QUE_MERGE_CAP) return -1;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % QUE_MERGE_CAP;
    q->count = q->count + 1;
    return 0;
}

int que_merge_pop(que_merge_t *q, int *out) {
    if (q->count <= 0) return -1;
    *out = q->data[q->head];
    q->head = (q->head + 1) % QUE_MERGE_CAP;
    q->count = q->count - 1;
    return 0;
}

int que_merge_front(const que_merge_t *q) {
    if (q->count <= 0) return -1;
    return q->data[q->head];
}

int que_merge_two(que_merge_t *a, que_merge_t *b, que_merge_t *out) {
    que_merge_init(out);
    int merged = 0;
    while (a->count > 0 && b->count > 0) {
        int va = que_merge_front(a);
        int vb = que_merge_front(b);
        int val = 0;
        if (va <= vb) {
            que_merge_pop(a, &val);
        } else {
            que_merge_pop(b, &val);
        }
        que_merge_push(out, val);
        merged = merged + 1;
    }
    while (a->count > 0) {
        int val = 0;
        que_merge_pop(a, &val);
        que_merge_push(out, val);
        merged = merged + 1;
    }
    while (b->count > 0) {
        int val = 0;
        que_merge_pop(b, &val);
        que_merge_push(out, val);
        merged = merged + 1;
    }
    return merged;
}

int que_merge_test(void) {
    que_merge_t a, b, out;
    que_merge_init(&a);
    que_merge_init(&b);
    que_merge_push(&a, 1); que_merge_push(&a, 3); que_merge_push(&a, 5);
    que_merge_push(&b, 2); que_merge_push(&b, 4); que_merge_push(&b, 6);
    int n = que_merge_two(&a, &b, &out);
    if (n != 6) return -1;
    int val = 0;
    que_merge_pop(&out, &val);
    if (val != 1) return -2;
    que_merge_pop(&out, &val);
    if (val != 2) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1869: Queue merging should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1869: Output should not be empty");
    assert!(code.contains("fn que_merge_init"), "C1869: Should contain que_merge_init");
    assert!(code.contains("fn que_merge_two"), "C1869: Should contain que_merge_two");
    assert!(code.contains("fn que_merge_pop"), "C1869: Should contain que_merge_pop");
    Ok(())
}

/// C1870: Queue splitting -- partition a queue into two by predicate (even/odd)
#[test]
fn c1870_queue_splitting() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_SPLIT_CAP 64

typedef struct {
    int data[QUE_SPLIT_CAP];
    int head;
    int tail;
    int count;
} que_split_t;

void que_split_init(que_split_t *q) {
    q->head = 0;
    q->tail = 0;
    q->count = 0;
}

int que_split_push(que_split_t *q, int val) {
    if (q->count >= QUE_SPLIT_CAP) return -1;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % QUE_SPLIT_CAP;
    q->count = q->count + 1;
    return 0;
}

int que_split_pop(que_split_t *q, int *out) {
    if (q->count <= 0) return -1;
    *out = q->data[q->head];
    q->head = (q->head + 1) % QUE_SPLIT_CAP;
    q->count = q->count - 1;
    return 0;
}

int que_split_partition(que_split_t *src, que_split_t *evens, que_split_t *odds) {
    que_split_init(evens);
    que_split_init(odds);
    int total = 0;
    while (src->count > 0) {
        int val = 0;
        que_split_pop(src, &val);
        if (val % 2 == 0) {
            que_split_push(evens, val);
        } else {
            que_split_push(odds, val);
        }
        total = total + 1;
    }
    return total;
}

int que_split_size(const que_split_t *q) {
    return q->count;
}

int que_split_test(void) {
    que_split_t src, evens, odds;
    que_split_init(&src);
    que_split_push(&src, 1);
    que_split_push(&src, 2);
    que_split_push(&src, 3);
    que_split_push(&src, 4);
    que_split_push(&src, 5);
    que_split_push(&src, 6);
    int n = que_split_partition(&src, &evens, &odds);
    if (n != 6) return -1;
    if (que_split_size(&evens) != 3) return -2;
    if (que_split_size(&odds) != 3) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1870: Queue splitting should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1870: Output should not be empty");
    assert!(code.contains("fn que_split_init"), "C1870: Should contain que_split_init");
    assert!(code.contains("fn que_split_partition"), "C1870: Should contain que_split_partition");
    assert!(code.contains("fn que_split_pop"), "C1870: Should contain que_split_pop");
    Ok(())
}

// ============================================================================
// C1871-C1875: Queue Applications
// ============================================================================

/// C1871: Task queue with priority scheduling and completion tracking
#[test]
fn c1871_task_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_TASK_CAP 64
#define QUE_TASK_PENDING 0
#define QUE_TASK_RUNNING 1
#define QUE_TASK_DONE 2

typedef struct {
    uint32_t task_id;
    int priority;
    int status;
    int result;
} que_task_entry_t;

typedef struct {
    que_task_entry_t tasks[QUE_TASK_CAP];
    int count;
    uint32_t next_id;
    int completed;
} que_task_queue_t;

void que_task_init(que_task_queue_t *tq) {
    tq->count = 0;
    tq->next_id = 1;
    tq->completed = 0;
    int i;
    for (i = 0; i < QUE_TASK_CAP; i++) {
        tq->tasks[i].status = -1;
    }
}

uint32_t que_task_submit(que_task_queue_t *tq, int priority) {
    if (tq->count >= QUE_TASK_CAP) return 0;
    int i;
    for (i = 0; i < QUE_TASK_CAP; i++) {
        if (tq->tasks[i].status < 0) {
            tq->tasks[i].task_id = tq->next_id;
            tq->tasks[i].priority = priority;
            tq->tasks[i].status = QUE_TASK_PENDING;
            tq->tasks[i].result = 0;
            tq->next_id = tq->next_id + 1;
            tq->count = tq->count + 1;
            return tq->tasks[i].task_id;
        }
    }
    return 0;
}

int que_task_schedule(que_task_queue_t *tq) {
    int best = -1;
    int best_pri = -1;
    int i;
    for (i = 0; i < QUE_TASK_CAP; i++) {
        if (tq->tasks[i].status == QUE_TASK_PENDING) {
            if (best < 0 || tq->tasks[i].priority > best_pri) {
                best = i;
                best_pri = tq->tasks[i].priority;
            }
        }
    }
    if (best < 0) return -1;
    tq->tasks[best].status = QUE_TASK_RUNNING;
    return tq->tasks[best].task_id;
}

int que_task_complete(que_task_queue_t *tq, uint32_t task_id, int result) {
    int i;
    for (i = 0; i < QUE_TASK_CAP; i++) {
        if (tq->tasks[i].task_id == task_id && tq->tasks[i].status == QUE_TASK_RUNNING) {
            tq->tasks[i].status = QUE_TASK_DONE;
            tq->tasks[i].result = result;
            tq->completed = tq->completed + 1;
            return 0;
        }
    }
    return -1;
}

int que_task_test(void) {
    que_task_queue_t tq;
    que_task_init(&tq);
    que_task_submit(&tq, 5);
    que_task_submit(&tq, 10);
    que_task_submit(&tq, 3);
    int tid = que_task_schedule(&tq);
    if (tid <= 0) return -1;
    que_task_complete(&tq, tid, 42);
    if (tq.completed != 1) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1871: Task queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1871: Output should not be empty");
    assert!(code.contains("fn que_task_init"), "C1871: Should contain que_task_init");
    assert!(code.contains("fn que_task_submit"), "C1871: Should contain que_task_submit");
    assert!(code.contains("fn que_task_schedule"), "C1871: Should contain que_task_schedule");
    Ok(())
}

/// C1872: Event queue dispatching typed events to handlers
#[test]
fn c1872_event_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_EVT_CAP 64
#define QUE_EVT_DATA_LEN 8

typedef struct {
    uint32_t event_type;
    uint32_t timestamp;
    int data[QUE_EVT_DATA_LEN];
    int data_len;
} que_event_t;

typedef struct {
    que_event_t events[QUE_EVT_CAP];
    int head;
    int tail;
    int count;
    uint32_t events_processed;
    uint32_t events_dropped;
} que_evtqueue_t;

void que_evtqueue_init(que_evtqueue_t *eq) {
    eq->head = 0;
    eq->tail = 0;
    eq->count = 0;
    eq->events_processed = 0;
    eq->events_dropped = 0;
}

int que_evtqueue_post(que_evtqueue_t *eq, uint32_t etype, uint32_t ts, const int *data, int dlen) {
    if (eq->count >= QUE_EVT_CAP) {
        eq->events_dropped = eq->events_dropped + 1;
        return -1;
    }
    que_event_t *e = &eq->events[eq->tail];
    e->event_type = etype;
    e->timestamp = ts;
    e->data_len = dlen;
    if (dlen > QUE_EVT_DATA_LEN) dlen = QUE_EVT_DATA_LEN;
    int i;
    for (i = 0; i < dlen; i++) {
        e->data[i] = data[i];
    }
    eq->tail = (eq->tail + 1) % QUE_EVT_CAP;
    eq->count = eq->count + 1;
    return 0;
}

int que_evtqueue_dispatch(que_evtqueue_t *eq, que_event_t *out) {
    if (eq->count <= 0) return -1;
    *out = eq->events[eq->head];
    eq->head = (eq->head + 1) % QUE_EVT_CAP;
    eq->count = eq->count - 1;
    eq->events_processed = eq->events_processed + 1;
    return 0;
}

int que_evtqueue_pending(const que_evtqueue_t *eq) {
    return eq->count;
}

int que_evtqueue_test(void) {
    que_evtqueue_t eq;
    que_evtqueue_init(&eq);
    int data[2];
    data[0] = 42; data[1] = 99;
    que_evtqueue_post(&eq, 1, 1000, data, 2);
    que_evtqueue_post(&eq, 2, 1001, data, 1);
    if (que_evtqueue_pending(&eq) != 2) return -1;
    que_event_t evt;
    que_evtqueue_dispatch(&eq, &evt);
    if (evt.event_type != 1) return -2;
    if (evt.timestamp != 1000) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1872: Event queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1872: Output should not be empty");
    assert!(code.contains("fn que_evtqueue_init"), "C1872: Should contain que_evtqueue_init");
    assert!(code.contains("fn que_evtqueue_post"), "C1872: Should contain que_evtqueue_post");
    assert!(
        code.contains("fn que_evtqueue_dispatch"),
        "C1872: Should contain que_evtqueue_dispatch"
    );
    Ok(())
}

/// C1873: Command queue with execute and rollback support
#[test]
fn c1873_command_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_CMD_CAP 32
#define QUE_CMD_SET 1
#define QUE_CMD_ADD 2
#define QUE_CMD_MUL 3

typedef struct {
    int cmd_type;
    int operand;
    int prev_value;
    int executed;
} que_cmd_t;

typedef struct {
    que_cmd_t commands[QUE_CMD_CAP];
    int head;
    int tail;
    int count;
    int state_value;
} que_cmdqueue_t;

void que_cmdqueue_init(que_cmdqueue_t *cq, int initial_value) {
    cq->head = 0;
    cq->tail = 0;
    cq->count = 0;
    cq->state_value = initial_value;
}

int que_cmdqueue_enqueue(que_cmdqueue_t *cq, int cmd_type, int operand) {
    if (cq->count >= QUE_CMD_CAP) return -1;
    cq->commands[cq->tail].cmd_type = cmd_type;
    cq->commands[cq->tail].operand = operand;
    cq->commands[cq->tail].prev_value = 0;
    cq->commands[cq->tail].executed = 0;
    cq->tail = (cq->tail + 1) % QUE_CMD_CAP;
    cq->count = cq->count + 1;
    return 0;
}

int que_cmdqueue_execute_next(que_cmdqueue_t *cq) {
    if (cq->count <= 0) return -1;
    que_cmd_t *cmd = &cq->commands[cq->head];
    cmd->prev_value = cq->state_value;
    if (cmd->cmd_type == QUE_CMD_SET) {
        cq->state_value = cmd->operand;
    } else if (cmd->cmd_type == QUE_CMD_ADD) {
        cq->state_value = cq->state_value + cmd->operand;
    } else if (cmd->cmd_type == QUE_CMD_MUL) {
        cq->state_value = cq->state_value * cmd->operand;
    }
    cmd->executed = 1;
    cq->head = (cq->head + 1) % QUE_CMD_CAP;
    cq->count = cq->count - 1;
    return cq->state_value;
}

int que_cmdqueue_get_state(const que_cmdqueue_t *cq) {
    return cq->state_value;
}

int que_cmdqueue_test(void) {
    que_cmdqueue_t cq;
    que_cmdqueue_init(&cq, 10);
    que_cmdqueue_enqueue(&cq, QUE_CMD_ADD, 5);
    que_cmdqueue_enqueue(&cq, QUE_CMD_MUL, 3);
    que_cmdqueue_execute_next(&cq);
    if (que_cmdqueue_get_state(&cq) != 15) return -1;
    que_cmdqueue_execute_next(&cq);
    if (que_cmdqueue_get_state(&cq) != 45) return -2;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1873: Command queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1873: Output should not be empty");
    assert!(code.contains("fn que_cmdqueue_init"), "C1873: Should contain que_cmdqueue_init");
    assert!(code.contains("fn que_cmdqueue_enqueue"), "C1873: Should contain que_cmdqueue_enqueue");
    assert!(
        code.contains("fn que_cmdqueue_execute_next"),
        "C1873: Should contain que_cmdqueue_execute_next"
    );
    Ok(())
}

/// C1874: Undo queue recording operations for revert capability
#[test]
fn c1874_undo_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;

#define QUE_UNDO_CAP 32

typedef struct {
    int old_value;
    int new_value;
    int field_id;
} que_undo_entry_t;

typedef struct {
    que_undo_entry_t history[QUE_UNDO_CAP];
    int top;
    int redo_top;
    int values[16];
} que_undo_t;

void que_undo_init(que_undo_t *u) {
    u->top = 0;
    u->redo_top = 0;
    int i;
    for (i = 0; i < 16; i++) {
        u->values[i] = 0;
    }
}

int que_undo_set(que_undo_t *u, int field, int new_val) {
    if (field < 0 || field >= 16) return -1;
    if (u->top >= QUE_UNDO_CAP) return -2;
    u->history[u->top].field_id = field;
    u->history[u->top].old_value = u->values[field];
    u->history[u->top].new_value = new_val;
    u->top = u->top + 1;
    u->redo_top = u->top;
    u->values[field] = new_val;
    return 0;
}

int que_undo_revert(que_undo_t *u) {
    if (u->top <= 0) return -1;
    u->top = u->top - 1;
    int field = u->history[u->top].field_id;
    u->values[field] = u->history[u->top].old_value;
    return 0;
}

int que_undo_redo(que_undo_t *u) {
    if (u->top >= u->redo_top) return -1;
    int field = u->history[u->top].field_id;
    u->values[field] = u->history[u->top].new_value;
    u->top = u->top + 1;
    return 0;
}

int que_undo_get(const que_undo_t *u, int field) {
    if (field < 0 || field >= 16) return -1;
    return u->values[field];
}

int que_undo_test(void) {
    que_undo_t u;
    que_undo_init(&u);
    que_undo_set(&u, 0, 100);
    que_undo_set(&u, 0, 200);
    if (que_undo_get(&u, 0) != 200) return -1;
    que_undo_revert(&u);
    if (que_undo_get(&u, 0) != 100) return -2;
    que_undo_redo(&u);
    if (que_undo_get(&u, 0) != 200) return -3;
    que_undo_revert(&u);
    que_undo_revert(&u);
    if (que_undo_get(&u, 0) != 0) return -4;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1874: Undo queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1874: Output should not be empty");
    assert!(code.contains("fn que_undo_init"), "C1874: Should contain que_undo_init");
    assert!(code.contains("fn que_undo_set"), "C1874: Should contain que_undo_set");
    assert!(code.contains("fn que_undo_revert"), "C1874: Should contain que_undo_revert");
    Ok(())
}

/// C1875: Log queue with severity levels and circular overwrite
#[test]
fn c1875_log_queue() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define QUE_LOG_CAP 128
#define QUE_LOG_DEBUG 0
#define QUE_LOG_INFO 1
#define QUE_LOG_WARN 2
#define QUE_LOG_ERROR 3

typedef struct {
    int severity;
    uint32_t timestamp;
    int code;
    int value;
} que_log_entry_t;

typedef struct {
    que_log_entry_t entries[QUE_LOG_CAP];
    int write_pos;
    int count;
    int min_severity;
    uint32_t total_logged;
    int error_count;
} que_logqueue_t;

void que_logqueue_init(que_logqueue_t *lq, int min_sev) {
    lq->write_pos = 0;
    lq->count = 0;
    lq->min_severity = min_sev;
    lq->total_logged = 0;
    lq->error_count = 0;
}

int que_logqueue_write(que_logqueue_t *lq, int severity, uint32_t ts, int code, int value) {
    if (severity < lq->min_severity) return -1;
    que_log_entry_t *e = &lq->entries[lq->write_pos % QUE_LOG_CAP];
    e->severity = severity;
    e->timestamp = ts;
    e->code = code;
    e->value = value;
    lq->write_pos = lq->write_pos + 1;
    if (lq->count < QUE_LOG_CAP) {
        lq->count = lq->count + 1;
    }
    lq->total_logged = lq->total_logged + 1;
    if (severity == QUE_LOG_ERROR) {
        lq->error_count = lq->error_count + 1;
    }
    return 0;
}

int que_logqueue_read_recent(const que_logqueue_t *lq, int n, que_log_entry_t *out) {
    if (n > lq->count) n = lq->count;
    int i;
    for (i = 0; i < n; i++) {
        int idx = (lq->write_pos - n + i + QUE_LOG_CAP) % QUE_LOG_CAP;
        out[i] = lq->entries[idx];
    }
    return n;
}

int que_logqueue_count_by_severity(const que_logqueue_t *lq, int severity) {
    int cnt = 0;
    int start = 0;
    if (lq->count >= QUE_LOG_CAP) {
        start = lq->write_pos;
    }
    int i;
    for (i = 0; i < lq->count; i++) {
        int idx = (start + i) % QUE_LOG_CAP;
        if (lq->entries[idx].severity == severity) {
            cnt = cnt + 1;
        }
    }
    return cnt;
}

int que_logqueue_test(void) {
    que_logqueue_t lq;
    que_logqueue_init(&lq, QUE_LOG_INFO);
    que_logqueue_write(&lq, QUE_LOG_DEBUG, 1, 100, 0);
    que_logqueue_write(&lq, QUE_LOG_INFO, 2, 101, 10);
    que_logqueue_write(&lq, QUE_LOG_ERROR, 3, 102, 20);
    que_logqueue_write(&lq, QUE_LOG_WARN, 4, 103, 30);
    if (lq.count != 3) return -1;
    if (lq.error_count != 1) return -2;
    que_log_entry_t recent[2];
    int got = que_logqueue_read_recent(&lq, 2, recent);
    if (got != 2) return -3;
    return 0;
}
"##;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C1875: Log queue should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1875: Output should not be empty");
    assert!(code.contains("fn que_logqueue_init"), "C1875: Should contain que_logqueue_init");
    assert!(code.contains("fn que_logqueue_write"), "C1875: Should contain que_logqueue_write");
    assert!(
        code.contains("fn que_logqueue_read_recent"),
        "C1875: Should contain que_logqueue_read_recent"
    );
    Ok(())
}