aboutsummaryrefslogtreecommitdiff
path: root/tests/test_core.py
blob: c196adf3181f6781dc584f2f188636d6fbb2bad2 (plain)
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
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
# coding=utf-8

import glob, hashlib, os, re, shutil, subprocess, sys
import tools.shared
from tools.shared import *
from runner import RunnerCore, path_from_root, checked_sanity, test_modes

class T(RunnerCore): # Short name, to make it more fun to use manually on the commandline
  def is_le32(self):
    return not ('i386-pc-linux-gnu' in COMPILER_OPTS or self.env.get('EMCC_LLVM_TARGET') == 'i386-pc-linux-gnu')

  def test_hello_world(self):
      src = '''
        #include <stdio.h>
        int main()
        {
          printf("hello, world!\\n");
          return 0;
        }
      '''
      self.do_run(src, 'hello, world!')

      assert 'EMSCRIPTEN_GENERATED_FUNCTIONS' not in open(self.in_dir('src.cpp.o.js')).read(), 'must not emit this unneeded internal thing'

  def test_intvars(self):
      if self.emcc_args == None: return self.skip('needs ta2')

      src = '''
        #include <stdio.h>
        int global = 20;
        int *far;
        int main()
        {
          int x = 5;
          int y = x+17;
          int z = (y-1)/2; // Should stay an integer after division!
          y += 1;
          int w = x*3+4;
          int k = w < 15 ? 99 : 101;
          far = &k;
          *far += global;
          int i = k > 100; // Should be an int, not a bool!
          int j = i << 6;
          j >>= 1;
          j = j ^ 5;
          int h = 1;
          h |= 0;
          int p = h;
          p &= 0;
          printf("*%d,%d,%d,%d,%d,%d,%d,%d,%d*\\n", x, y, z, w, k, i, j, h, p);

          long hash = -1;
          size_t perturb;
          int ii = 0;
          for (perturb = hash; ; perturb >>= 5) {
            printf("%d:%d", ii, perturb);
            ii++;
            if (ii == 9) break;
            printf(",");
          }
          printf("*\\n");
          printf("*%.1d,%.2d*\\n", 56, 9);

          // Fixed-point math on 64-bit ints. Tricky to support since we have no 64-bit shifts in JS
          {
            struct Fixed {
              static int Mult(int a, int b) {
                return ((long long)a * (long long)b) >> 16;
              }
            };
            printf("fixed:%d\\n", Fixed::Mult(150000, 140000));
          }

          printf("*%ld*%p\\n", (long)21, &hash); // The %p should not enter an infinite loop!
          return 0;
        }
      '''
      self.do_run(src, '*5,23,10,19,121,1,37,1,0*\n0:-1,1:134217727,2:4194303,3:131071,4:4095,5:127,6:3,7:0,8:0*\n*56,09*\nfixed:320434\n*21*')

  def test_sintvars(self):
      Settings.CORRECT_SIGNS = 1 # Relevant to this test
      src = '''
        #include <stdio.h>
        struct S {
          char *match_start;
          char *strstart;
        };
        int main()
        {
          struct S _s;
          struct S *s = &_s;
          unsigned short int sh;

          s->match_start = (char*)32522;
          s->strstart = (char*)(32780);
          printf("*%d,%d,%d*\\n", (int)s->strstart, (int)s->match_start, (int)(s->strstart - s->match_start));
          sh = s->strstart - s->match_start;
          printf("*%d,%d*\\n", sh, sh>>7);

          s->match_start = (char*)32999;
          s->strstart = (char*)(32780);
          printf("*%d,%d,%d*\\n", (int)s->strstart, (int)s->match_start, (int)(s->strstart - s->match_start));
          sh = s->strstart - s->match_start;
          printf("*%d,%d*\\n", sh, sh>>7);
        }
      '''
      output = '*32780,32522,258*\n*258,2*\n*32780,32999,-219*\n*65317,510*'
      Settings.CORRECT_OVERFLOWS = 0 # We should not need overflow correction to get this right
      self.do_run(src, output, force_c=True)

  def test_i64(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('i64 mode 1 requires ta2')

      src = '''
        #include <stdio.h>
        int main()
        {
          long long a = 0x2b00505c10;
          long long b = a >> 29;
          long long c = a >> 32;
          long long d = a >> 34;
          printf("*%Ld,%Ld,%Ld,%Ld*\\n", a, b, c, d);
          unsigned long long ua = 0x2b00505c10;
          unsigned long long ub = ua >> 29;
          unsigned long long uc = ua >> 32;
          unsigned long long ud = ua >> 34;
          printf("*%Ld,%Ld,%Ld,%Ld*\\n", ua, ub, uc, ud);

          long long x = 0x0000def123450789ULL; // any bigger than this, and we
          long long y = 0x00020ef123456089ULL; // start to run into the double precision limit!
          printf("*%Ld,%Ld,%Ld,%Ld,%Ld*\\n", x, y, x | y, x & y, x ^ y, x >> 2, y << 2);

          printf("*");
          long long z = 13;
          int n = 0;
          while (z > 1) {
            printf("%.2f,", (float)z); // these must be integers!
            z = z >> 1;
            n++;
          }
          printf("*%d*\\n", n);
          return 0;
        }
      '''
      self.do_run(src, '*184688860176,344,43,10*\n*184688860176,344,43,10*\n*245127260211081,579378795077769,808077213656969,16428841631881,791648372025088*\n*13.00,6.00,3.00,*3*')

      src = r'''
        #include <time.h>
        #include <stdio.h>
        #include <stdint.h>

        int64_t returner1() { return 0x0000def123450789ULL; }
        int64_t returner2(int test) {
          while (test > 10) test /= 2; // confuse the compiler so it doesn't eliminate this function
          return test > 5 ? 0x0000def123450123ULL : 0ULL;
        }

        void modifier1(int64_t t) {
          t |= 12;
          printf("m1: %Ld\n", t);
        }
        void modifier2(int64_t &t) {
          t |= 12;
        }

        int truthy() {
          int x = time(0);
          while (x > 10) {
            x |= 7;
            x /= 2;
          }
          return x < 3;
        }

        struct IUB {
           int c;
           long long d;
        };

        IUB iub[] = {
           { 55, 17179869201 },
           { 122, 25769803837 },
        };

        int main(int argc, char **argv)
        {
          int64_t x1 = 0x1234def123450789ULL;
          int64_t x2 = 0x1234def123450788ULL;
          int64_t x3 = 0x1234def123450789ULL;
          printf("*%Ld\n%d,%d,%d,%d,%d\n%d,%d,%d,%d,%d*\n", x1, x1==x2, x1<x2, x1<=x2, x1>x2, x1>=x2, // note: some rounding in the printing!
                                                                x1==x3, x1<x3, x1<=x3, x1>x3, x1>=x3);
          printf("*%Ld*\n", returner1());
          printf("*%Ld*\n", returner2(30));

          uint64_t maxx = -1ULL;
          printf("*%Lu*\n*%Lu*\n", maxx, maxx >> 5);

          // Make sure params are not modified if they shouldn't be
          int64_t t = 123;
          modifier1(t);
          printf("*%Ld*\n", t);
          modifier2(t);
          printf("*%Ld*\n", t);

          // global structs with i64s
          printf("*%d,%Ld*\n*%d,%Ld*\n", iub[0].c, iub[0].d, iub[1].c, iub[1].d);

          // Bitshifts
          {
            int64_t a = -1;
            int64_t b = a >> 29;
            int64_t c = a >> 32;
            int64_t d = a >> 34;
            printf("*%Ld,%Ld,%Ld,%Ld*\n", a, b, c, d);
            uint64_t ua = -1;
            int64_t ub = ua >> 29;
            int64_t uc = ua >> 32;
            int64_t ud = ua >> 34;
            printf("*%Ld,%Ld,%Ld,%Ld*\n", ua, ub, uc, ud);
          }

          // Nonconstant bitshifts
          {
            int64_t a = -1;
            int64_t b = a >> (29 - argc + 1);
            int64_t c = a >> (32 - argc + 1);
            int64_t d = a >> (34 - argc + 1);
            printf("*%Ld,%Ld,%Ld,%Ld*\n", a, b, c, d);
            uint64_t ua = -1;
            int64_t ub = ua >> (29 - argc + 1);
            int64_t uc = ua >> (32 - argc + 1);
            int64_t ud = ua >> (34 - argc + 1);
            printf("*%Ld,%Ld,%Ld,%Ld*\n", ua, ub, uc, ud);
          }

          // Math mixtures with doubles
          {
            uint64_t a = 5;
            double b = 6.8;
            uint64_t c = a * b;
            if (truthy()) printf("*%d,%d,%d*\n", (int)&a, (int)&b, (int)&c); // printing addresses prevents optimizations
            printf("*prod:%llu*\n", c);
          }

          // Basic (rounded, for now) math. Just check compilation.
          int64_t a = 0x1234def123450789ULL;
          a--; if (truthy()) a--; // confuse optimizer
          int64_t b = 0x1234000000450789ULL;
          b++; if (truthy()) b--; // confuse optimizer
          printf("*%Ld,%Ld,%Ld,%Ld*\n",   (a+b)/5000, (a-b)/5000, (a*3)/5000, (a/5)/5000);

          a -= 17; if (truthy()) a += 5; // confuse optimizer
          b -= 17; if (truthy()) b += 121; // confuse optimizer
          printf("*%Lx,%Lx,%Lx,%Lx*\n", b - a, b - a/2, b/2 - a, b - 20);

          if (truthy()) a += 5/b; // confuse optimizer
          if (truthy()) b += 121*(3+a/b); // confuse optimizer
          printf("*%Lx,%Lx,%Lx,%Lx*\n", a - b, a - b/2, a/2 - b, a - 20);

          return 0;
        }
      '''
      self.do_run(src, '*1311918518731868041\n' +
                       '0,0,0,1,1\n' +
                       '1,0,1,0,1*\n' +
                       '*245127260211081*\n' +
                       '*245127260209443*\n' +
                       '*18446744073709551615*\n' +
                       '*576460752303423487*\n' +
                       'm1: 127\n' +
                       '*123*\n' +
                       '*127*\n' +
                       '*55,17179869201*\n' +
                       '*122,25769803837*\n' +
                       '*-1,-1,-1,-1*\n' +
                       '*-1,34359738367,4294967295,1073741823*\n' +
                       '*-1,-1,-1,-1*\n' +
                       '*-1,34359738367,4294967295,1073741823*\n' +
                       '*prod:34*\n' +
                       '*524718382041609,49025451137,787151111239120,52476740749274*\n' +
                       '*ffff210edd000002,91990876ea283be,f6e5210edcdd7c45,1234000000450765*\n' +
                       '*def122fffffe,91adef1232283bb,f6e66f78915d7c42,1234def123450763*\n')

      src = r'''
        #include <stdio.h>
        #include <limits>

        int main()
        {
            long long i,j,k;

            i = 0;
            j = -1,
            k = 1;

            printf( "*\n" );
            printf( "%s\n", i > j ? "Ok": "Fail" );
            printf( "%s\n", k > i ? "Ok": "Fail" );
            printf( "%s\n", k > j ? "Ok": "Fail" );
            printf( "%s\n", i < j ? "Fail": "Ok" );
            printf( "%s\n", k < i ? "Fail": "Ok" );
            printf( "%s\n", k < j ? "Fail": "Ok" );
            printf( "%s\n", (i-j) >= k ? "Ok": "Fail" );
            printf( "%s\n", (i-j) <= k ? "Ok": "Fail" );
            printf( "%s\n", i > std::numeric_limits<long long>::min() ? "Ok": "Fail" );
            printf( "%s\n", i < std::numeric_limits<long long>::max() ? "Ok": "Fail" );
            printf( "*\n" );
        }
      '''

      self.do_run(src, '*\nOk\nOk\nOk\nOk\nOk\nOk\nOk\nOk\nOk\nOk\n*')

      # stuff that also needs sign corrections

      Settings.CORRECT_SIGNS = 1

      src = r'''
        #include <stdio.h>
        #include <stdint.h>

        int main()
        {
          // i32 vs i64
          int32_t small = -1;
          int64_t large = -1;
          printf("*%d*\n", small == large);
          small++;
          printf("*%d*\n", small == large);
          uint32_t usmall = -1;
          uint64_t ularge = -1;
          printf("*%d*\n", usmall == ularge);
          return 0;
        }
      '''

      self.do_run(src, '*1*\n*0*\n*0*\n')

  def test_i64_b(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

      src = r'''
        #include <stdio.h>
        #include <sys/time.h>

        typedef long long int64;

        #define PRMJ_USEC_PER_SEC       1000000L

        int main(int argc, char * argv[]) {
            int64 sec = 1329409675 + argc;
            int64 usec = 2329509675;
            int64 mul = int64(sec) * PRMJ_USEC_PER_SEC;
            int64 add = mul + int64(usec);
            int add_low = add;
            int add_high = add >> 32;
            printf("*%lld,%lld,%u,%u*\n", mul, add, add_low, add_high);
            int64 x = sec + (usec << 25);
            x >>= argc*3;
            printf("*%llu*\n", x);
            return 0;
        }
      '''

      self.do_run(src, '*1329409676000000,1329412005509675,3663280683,309527*\n*9770671914067409*\n')

  def test_i64_cmp(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

      src = r'''
        #include <stdio.h>

        typedef long long int64;

        bool compare(int64 val) {
          return val == -12;
        }

        bool compare2(int64 val) {
          return val < -12;
        }

        int main(int argc, char * argv[]) {
            printf("*%d,%d,%d,%d,%d,%d*\n", argc, compare(argc-1-12), compare(1000+argc), compare2(argc-1-10), compare2(argc-1-14), compare2(argc+1000));
            return 0;
        }
      '''

      self.do_run(src, '*1,1,0,0,1,0*\n')

  def test_i64_cmp2(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

      src = r'''
        #include <inttypes.h>
        #include <stdio.h>

        typedef int32_t INT32;
        typedef int64_t INT64;
        typedef uint8_t UINT8;

        void interface_clock_changed()
        {
          UINT8 m_divshift;
          INT32 m_divisor;

          //INT64 attos = m_attoseconds_per_cycle;
          INT64 attos = 279365114840;
          m_divshift = 0;
          while (attos >= (1UL << 31))
          {
            m_divshift++;
            printf("m_divshift is %i, on %Ld >?= %lu\n", m_divshift, attos, 1UL << 31);
            attos >>= 1;
          }
          m_divisor = attos;

          printf("m_divisor is %i\n",m_divisor);
        }

        int main() {
          interface_clock_changed();
          return 0;
        }
      '''
      self.do_run(src, '''m_divshift is 1, on 279365114840 >?= 2147483648
m_divshift is 2, on 139682557420 >?= 2147483648
m_divshift is 3, on 69841278710 >?= 2147483648
m_divshift is 4, on 34920639355 >?= 2147483648
m_divshift is 5, on 17460319677 >?= 2147483648
m_divshift is 6, on 8730159838 >?= 2147483648
m_divshift is 7, on 4365079919 >?= 2147483648
m_divshift is 8, on 2182539959 >?= 2147483648
m_divisor is 1091269979
''')

  def test_i64_double(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')


      src = r'''
        #include <stdio.h>

        typedef long long int64;
        #define JSDOUBLE_HI32_SIGNBIT   0x80000000

        bool JSDOUBLE_IS_NEGZERO(double d)
        {
          union {
            struct {
              unsigned int lo, hi;
            } s;
            double d;
          } x;
          if (d != 0)
            return false;
          x.d = d;
          return (x.s.hi & JSDOUBLE_HI32_SIGNBIT) != 0;
        }

        bool JSINT64_IS_NEGZERO(int64 l)
        {
          union {
            int64 i;
            double d;
          } x;
          if (l != 0)
            return false;
          x.i = l;
          return x.d == -0;
        }

        int main(int argc, char * argv[]) {
          printf("*%d,%d,%d,%d*\n", JSDOUBLE_IS_NEGZERO(0), JSDOUBLE_IS_NEGZERO(-0), JSDOUBLE_IS_NEGZERO(-1), JSDOUBLE_IS_NEGZERO(+1));
          printf("*%d,%d,%d,%d*\n", JSINT64_IS_NEGZERO(0), JSINT64_IS_NEGZERO(-0), JSINT64_IS_NEGZERO(-1), JSINT64_IS_NEGZERO(+1));
          return 0;
        }
      '''
      self.do_run(src, '*0,0,0,0*\n*1,1,0,0*\n') # same as gcc

  def test_i64_umul(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

      src = r'''
        #include <inttypes.h>
        #include <stdio.h>

        typedef uint32_t UINT32;
        typedef uint64_t UINT64;

        int main() {
        volatile UINT32 testu32a = 2375724032U;
        UINT32 bigu32 = 0xffffffffU;
        volatile UINT64 testu64a = 14746250828952703000U;

        while ((UINT64)testu32a * (UINT64)bigu32 < testu64a) {
          printf("testu64a is %llu\n", testu64a);
          testu64a /= 2;
        }

        return 0;
        }
      '''
      self.do_run(src, 'testu64a is 14746250828952703000\n')

  def test_i64_precise(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

      src = r'''
        #include <inttypes.h>
        #include <stdio.h>

        int main() {
          uint64_t x = 0, y = 0;
          for (int i = 0; i < 64; i++) {
            x += 1ULL << i;
            y += x;
            x /= 3;
            y *= 5;
            printf("unsigned %d: %llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu\n", i, x, y, x+y, x-y, x*y, y ? x/y : 0, x ? y/x : 0, y ? x%y : 0, x ? y%x : 0);
          }
          int64_t x2 = 0, y2 = 0;
          for (int i = 0; i < 64; i++) {
            x2 += 1LL << i;
            y2 += x2;
            x2 /= 3 * (i % 7 ? -1 : 1);
            y2 *= 5 * (i % 2 ? -1 : 1);
            printf("signed %d: %lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld\n", i, x2, y2, x2+y2, x2-y2, x2*y2, y2 ? x2/y2 : 0, x2 ? y2/x2 : 0, y2 ? x2%y2 : 0, x2 ? y2%x2 : 0);
          }
          return 0;
        }
      '''
      self.do_run(src, open(path_from_root('tests', 'i64_precise.txt')).read())

      # Verify that even if we ask for precision, if it is not needed it is not included
      Settings.PRECISE_I64_MATH = 1
      src = '''
        #include <inttypes.h>
        #include <stdio.h>

        int main(int argc, char **argv) {
          uint64_t x = 2125299906845564, y = 1225891506842664;
          if (argc == 12) {
            x = x >> 1;
            y = y >> 1;
          }
          x = x & 12ULL;
          y = y | 12ULL;
          x = x ^ y;
          x <<= 2;
          y >>= 3;
          printf("*%llu, %llu*\\n", x, y);
        }
      '''
      self.do_run(src, '*4903566027370624, 153236438355333*')
      code = open(os.path.join(self.get_dir(), 'src.cpp.o.js')).read()
      assert 'goog.math.Long' not in code, 'i64 precise math should not have been included if not actually used'

      # But if we force it to be included, it is. First, a case where we don't need it
      Settings.PRECISE_I64_MATH = 2
      self.do_run(open(path_from_root('tests', 'hello_world.c')).read(), 'hello')
      code = open(os.path.join(self.get_dir(), 'src.cpp.o.js')).read()
      assert 'goog.math.Long' in code, 'i64 precise math should be included if forced'

      # and now one where we do
      self.do_run(r'''
        #include <stdio.h>

        int main( int argc, char ** argv )
        {
            unsigned long a = 0x60DD1695U;
            unsigned long b = 0xCA8C4E7BU;
            unsigned long long c = (unsigned long long)a * b;
            printf( "c = %016llx\n", c );

            return 0;
        }
      ''', 'c = 4ca38a6bd2973f97')

  def test_i64_llabs(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
    Settings.PRECISE_I64_MATH = 2
    self.do_run(r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main(int argc, char ** argv) {
        printf("%lld,%lld\n", llabs(-576460752303423489), llabs(576460752303423489));
        return 0;
      }
    ''', '576460752303423489,576460752303423489')

  def test_i64_zextneg(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

    src = r'''
      #include <stdint.h>
      #include <stdio.h>

      int main(int argc, char *argv[])
      {
          uint8_t byte = 0x80;
          uint16_t two = byte;
          uint32_t four = byte;
          uint64_t eight = byte;

          printf("value: %d,%d,%d,%lld.\n", byte, two, four, eight);

          return 0;
      }
    '''
    self.do_run(src, 'value: 128,128,128,128.')

  def test_i64_7z(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

    src = r'''
      #include <stdint.h>
      #include <stdio.h>
      uint64_t a, b;
      int main(int argc, char *argv[])
      {
          a = argc;
          b = argv[1][0];
          printf("%d,%d\n", a, b);
          if (a > a + b || a > a + b + 1) {
              printf("one %lld, %lld", a, b);
              return 0;
          }
          printf("zero %lld, %lld", a, b);
          return 0;
      }
    '''
    self.do_run(src, 'zero 2, 104', ['hallo'])

  def test_i64_i16(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

    src = r'''
      #include <stdint.h>
      #include <stdio.h>
      int main(int argc, char ** argv){
          int y=-133;
          int64_t x= ((int64_t)((short)(y)))*(100 + argc);
          if(x>0)
              printf(">0\n");
          else
              printf("<=0\n");
      }
    '''
    self.do_run(src, '<=0')

  def test_i64_qdouble(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

    src = r'''
      #include <stdio.h>
      typedef long long qint64; /* 64 bit signed */
      typedef double qreal;


      int main(int argc, char **argv)
      {
        qreal c = 111;
        qint64 d = -111 + (argc - 1);
        c += d;
        if (c < -1 || c > 1)
        {
                printf("Failed!\n");
        }
        else
        {
                printf("Succeeded!\n");
        }
      };
    '''
    self.do_run(src, 'Succeeded!')

  def test_i64_varargs(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')

    src = r'''
      #include <stdio.h>
      #include <stdint.h>
      #include <stdarg.h>

      int64_t ccv_cache_generate_signature(char *msg, int len, int64_t sig_start, ...) {
        if (sig_start < 10123)
          printf("%s\n", msg+len);
        va_list v;
        va_start(v, sig_start);
        if (sig_start > 1413)
          printf("%d\n", va_arg(v, int));
        else
          printf("nada\n");
        va_end(v);
        return len*sig_start*(msg[0]+1);
      }

      int main(int argc, char **argv)
      {
        for (int i = 0; i < argc; i++) {
          int64_t x;
          if (i % 123123 == 0)
            x = ccv_cache_generate_signature(argv[i], i+2, (int64_t)argc*argc, 54.111);
          else
            x = ccv_cache_generate_signature(argv[i], i+2, (int64_t)argc*argc, 13);
          printf("%lld\n", x);
        }
      };
    '''
    self.do_run(src, '''in/this.program
nada
1536
a
nada
5760
fl
nada
6592
sdfasdfasdf
nada
7840
''', 'waka fleefl asdfasdfasdfasdf'.split(' '))

  def test_i32_mul_precise(self):
    if self.emcc_args == None: return self.skip('needs ta2')

    src = r'''
      #include <stdio.h>

      int main(int argc, char **argv) {
        unsigned long d1 = 0x847c9b5d;
        unsigned long q =  0x549530e1;
        if (argc > 1000) { q += argc; d1 -= argc; } // confuse optimizer
        printf("%lu\n", d1*q);
        return 0;
      }
    '''
    self.do_run(src, '3217489085')

  def test_i32_mul_semiprecise(self):
    if Settings.ASM_JS: return self.skip('asm is always fully precise')

    Settings.PRECISE_I32_MUL = 0 # we want semiprecise here

    src = r'''
      #include <stdio.h>

      typedef unsigned int uint;

      // from cube2, zlib licensed

      #define N (624)
      #define M (397)
      #define K (0x9908B0DFU)

      static uint state[N];
      static int next = N;

      void seedMT(uint seed)
      {
          state[0] = seed;
          for(uint i = 1; i < N; i++) // if we do not do this precisely, at least we should coerce to int immediately, not wait
              state[i] = seed = 1812433253U * (seed ^ (seed >> 30)) + i;
          next = 0;
      }

      int main() {
        seedMT(5497);
        for (int i = 0; i < 10; i++) printf("%d: %u\n", i, state[i]);
        return 0;
      }
    '''
    self.do_run(src, '''0: 5497
1: 2916432318
2: 2502517762
3: 3151524867
4: 2323729668
5: 2053478917
6: 2409490438
7: 848473607
8: 691103752
9: 3915535113
''')

  def test_i16_emcc_intrinsic(self):
    Settings.CORRECT_SIGNS = 1 # Relevant to this test

    src = r'''
      #include <stdio.h>

      int test(unsigned short a, unsigned short b) {
          unsigned short result = a;
          result += b;
          if (result < b) printf("C!");
          return result;
      }

      int main(void) {
          printf(",%d,", test(0, 0));
          printf(",%d,", test(1, 1));
          printf(",%d,", test(65535, 1));
          printf(",%d,", test(1, 65535));
          printf(",%d,", test(32768, 32767));
          printf(",%d,", test(32768, 32768));
          return 0;
      }
    '''
    self.do_run(src, ',0,,2,C!,0,C!,0,,65535,C!,0,')

  def test_double_i64_conversion(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2')

    src = r'''
      #include <cassert>
      #include <inttypes.h>
      #include <stdio.h>

      __attribute((noinline)) bool eq(double d, int64_t i) {
        int64_t i2 = (int64_t)d;
        if (i != i2) {
          printf("%.20g converted to int64 returns %lld, not %lld as expected!\n", d, i2, i);
        }
        return i == i2;
      }

      int main() {
        assert(eq(0.0, 0));
        assert(eq(-0.0, 0));
        assert(eq(0.1, 0));
        assert(eq(-0.1, 0));
        assert(eq(0.6, 0));
        assert(eq(-0.6, 0));
        assert(eq(1.0, 1));
        assert(eq(-1.0, -1));
        assert(eq(1.1, 1));
        assert(eq(-1.1, -1));
        assert(eq(1.6, 1));
        assert(eq(-1.6, -1));
        assert(eq(4294967295.0, 4294967295LL));
        assert(eq(4294967295.5, 4294967295LL));
        assert(eq(4294967296.0, 4294967296LL));
        assert(eq(4294967296.5, 4294967296LL));
        assert(eq(14294967295.0, 14294967295LL));
        assert(eq(14294967295.5, 14294967295LL));
        assert(eq(14294967296.0, 14294967296LL));
        assert(eq(14294967296.5, 14294967296LL));
        assert(eq(-4294967295.0, -4294967295LL));
        assert(eq(-4294967295.5, -4294967295LL));
        assert(eq(-4294967296.0, -4294967296LL));
        assert(eq(-4294967296.5, -4294967296LL));
        assert(eq(-14294967295.0, -14294967295LL));
        assert(eq(-14294967295.5, -14294967295LL));
        assert(eq(-14294967296.0, -14294967296LL));
        assert(eq(-14294967296.5, -14294967296LL));

        assert(eq(4294967295.3, 4294967295LL));
        assert(eq(4294967296.3, 4294967296LL));
        assert(eq(14294967295.3, 14294967295LL));
        assert(eq(14294967296.3, 14294967296LL));
        assert(eq(-4294967295.3, -4294967295LL));
        assert(eq(-4294967296.3, -4294967296LL));
        assert(eq(-14294967295.3, -14294967295LL));
        assert(eq(-14294967296.3, -14294967296LL));

        assert(eq(4294967295.8, 4294967295LL));
        assert(eq(4294967296.8, 4294967296LL));
        assert(eq(14294967295.8, 14294967295LL));
        assert(eq(14294967296.8, 14294967296LL));
        assert(eq(-4294967295.8, -4294967295LL));
        assert(eq(-4294967296.8, -4294967296LL));
        assert(eq(-14294967295.8, -14294967295LL));
        assert(eq(-14294967296.8, -14294967296LL));

        // The following number is the largest double such that all integers smaller than this can exactly be represented in a double.
        assert(eq(9007199254740992.0, 9007199254740992LL /* == 2^53 */));
        assert(eq(-9007199254740992.0, -9007199254740992LL /* == -2^53 */));

        printf("OK!\n");
        return 0;
      }
    '''
    self.do_run(src, 'OK!\n');

  def test_negative_zero(self):
    src = r'''
      #include <stdio.h>
      #include <math.h>

      int main() {
        #define TEST(x, y) \
          printf("%.2f, %.2f ==> %.2f\n", x, y, copysign(x, y));
        TEST( 5.0f,  5.0f);
        TEST( 5.0f, -5.0f);
        TEST(-5.0f,  5.0f);
        TEST(-5.0f, -5.0f);
        TEST( 5.0f,  4.0f);
        TEST( 5.0f, -4.0f);
        TEST(-5.0f,  4.0f);
        TEST(-5.0f, -4.0f);
        TEST( 0.0f,  5.0f);
        TEST( 0.0f, -5.0f);
        TEST(-0.0f,  5.0f);
        TEST(-0.0f, -5.0f);
        TEST( 5.0f,  0.0f);
        TEST( 5.0f, -0.0f);
        TEST(-5.0f,  0.0f);
        TEST(-5.0f, -0.0f);
        TEST( 0.0f,  0.0f);
        TEST( 0.0f, -0.0f);
        TEST(-0.0f,  0.0f);
        TEST(-0.0f, -0.0f);
        return 0;
      }
    '''
    self.do_run(src, '''5.00, 5.00 ==> 5.00
5.00, -5.00 ==> -5.00
-5.00, 5.00 ==> 5.00
-5.00, -5.00 ==> -5.00
5.00, 4.00 ==> 5.00
5.00, -4.00 ==> -5.00
-5.00, 4.00 ==> 5.00
-5.00, -4.00 ==> -5.00
0.00, 5.00 ==> 0.00
0.00, -5.00 ==> -0.00
-0.00, 5.00 ==> 0.00
-0.00, -5.00 ==> -0.00
5.00, 0.00 ==> 5.00
5.00, -0.00 ==> -5.00
-5.00, 0.00 ==> 5.00
-5.00, -0.00 ==> -5.00
0.00, 0.00 ==> 0.00
0.00, -0.00 ==> -0.00
-0.00, 0.00 ==> 0.00
-0.00, -0.00 ==> -0.00
''')

  def test_llvm_intrinsics(self):
    if self.emcc_args == None: return self.skip('needs ta2')

    Settings.PRECISE_I64_MATH = 2 # for bswap64

    src = r'''
      #include <stdio.h>
      #include <sys/types.h>

      extern "C" {
        extern unsigned short llvm_bswap_i16(unsigned short x);
        extern unsigned int llvm_bswap_i32(unsigned int x);
        extern int32_t llvm_ctlz_i32(int32_t x);
        extern int64_t llvm_ctlz_i64(int64_t x);
        extern int32_t llvm_cttz_i32(int32_t x);
        extern int64_t llvm_cttz_i64(int64_t x);
        extern int32_t llvm_ctpop_i32(int32_t x);
        extern int64_t llvm_ctpop_i64(int64_t x);
        extern int llvm_expect_i32(int x, int y);
      }

      int main(void) {
          unsigned short x = 0xc8ef;
          printf("%x,%x\n", x&0xff, x >> 8);
          x = llvm_bswap_i16(x);
          printf("%x,%x\n", x&0xff, x >> 8);

          unsigned int y = 0xc5de158a;
          printf("%x,%x,%x,%x\n", y&0xff, (y>>8)&0xff, (y>>16)&0xff, (y>>24)&0xff);
          y = llvm_bswap_i32(y);
          printf("%x,%x,%x,%x\n", y&0xff, (y>>8)&0xff, (y>>16)&0xff, (y>>24)&0xff);

          printf("%d,%d\n", (int)llvm_ctlz_i64(((int64_t)1) << 40), llvm_ctlz_i32(1<<10));
          printf("%d,%d\n", (int)llvm_cttz_i64(((int64_t)1) << 40), llvm_cttz_i32(1<<10));
          printf("%d,%d\n", (int)llvm_ctpop_i64((0x3101ULL << 32) | 1), llvm_ctpop_i32(0x3101));
          printf("%d\n", (int)llvm_ctpop_i32(-594093059));

          printf("%d\n", llvm_expect_i32(x % 27, 3));

          int64_t a = 1;
          a = __builtin_bswap64(a);
          printf("%lld\n", a);

          return 0;
      }
    '''
    self.do_run(src, '''ef,c8
c8,ef
8a,15,de,c5
c5,de,15,8a
23,21
40,10
5,4
22
13
72057594037927936
''')

  def test_bswap64(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2')

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      #include <iostream>
      #include <string>
      #include <sstream>

      typedef unsigned long long quint64;

      using namespace std;

      inline quint64 qbswap(quint64 source)
      {
          return 0
              | ((source & quint64(0x00000000000000ffLL)) << 56)
              | ((source & quint64(0x000000000000ff00LL)) << 40)
              | ((source & quint64(0x0000000000ff0000LL)) << 24)
              | ((source & quint64(0x00000000ff000000LL)) << 8)
              | ((source & quint64(0x000000ff00000000LL)) >> 8)
              | ((source & quint64(0x0000ff0000000000LL)) >> 24)
              | ((source & quint64(0x00ff000000000000LL)) >> 40)
              | ((source & quint64(0xff00000000000000LL)) >> 56);
      }

      int main()
      {
        quint64 v = strtoull("4433ffeeddccbb00", NULL, 16);
        printf("%lld\n", v);

        const string string64bitInt = "4433ffeeddccbb00";
        stringstream s(string64bitInt);
        quint64 int64bitInt = 0;
        printf("1\n");
        s >> hex >> int64bitInt;
        printf("2\n");

        stringstream out;
        out << hex << qbswap(int64bitInt);

        cout << out.str() << endl;
        cout << hex << int64bitInt << endl;
        cout << string64bitInt << endl;

        if (out.str() != "bbccddeeff3344")
        {
          cout << "Failed!" << endl;
        }
        else
        {
          cout << "Succeeded!" << endl;
        }

        return 0;
      }
      '''
    self.do_run(src, '''4914553019779824384
1
2
bbccddeeff3344
4433ffeeddccbb00
4433ffeeddccbb00
Succeeded!
''')

  def test_sha1(self):
    if self.emcc_args == None: return self.skip('needs ta2')

    self.do_run(open(path_from_root('tests', 'sha1.c')).read(), 'SHA1=15dd99a1991e0b3826fede3deffc1feba42278e6')

  def test_cube2md5(self):
    if self.emcc_args == None: return self.skip('needs emcc')
    self.emcc_args += ['--embed-file', 'cube2md5.txt']
    shutil.copyfile(path_from_root('tests', 'cube2md5.txt'), os.path.join(self.get_dir(), 'cube2md5.txt'))
    self.do_run(open(path_from_root('tests', 'cube2md5.cpp')).read(), open(path_from_root('tests', 'cube2md5.ok')).read())

  def test_cube2hash(self):
    try:
      old_chunk_size = os.environ.get('EMSCRIPT_MAX_CHUNK_SIZE') or ''
      os.environ['EMSCRIPT_MAX_CHUNK_SIZE'] = '1' # test splitting out each function to a chunk in emscripten.py (21 functions here)

      # A good test of i64 math
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2 C-style memory aliasing')
      self.do_run('', 'Usage: hashstring <seed>',
                  libraries=self.get_library('cube2hash', ['cube2hash.bc'], configure=None),
                  includes=[path_from_root('tests', 'cube2hash')])

      for text, output in [('fleefl', '892BDB6FD3F62E863D63DA55851700FDE3ACF30204798CE9'),
                           ('fleefl2', 'AA2CC5F96FC9D540CA24FDAF1F71E2942753DB83E8A81B61'),
                           ('64bitisslow', '64D8470573635EC354FEE7B7F87C566FCAF1EFB491041670')]:
        self.do_run('', 'hash value: ' + output, [text], no_build=True)
    finally:
      os.environ['EMSCRIPT_MAX_CHUNK_SIZE'] = old_chunk_size

    assert 'asm1' in test_modes
    if self.run_name == 'asm1':
      assert Settings.RELOOP
      generated = open('src.cpp.o.js').read()
      main = generated[generated.find('function _main'):]
      main = main[:main.find('\n}')]
      num_vars = 0
      for v in re.findall('var [^;]+;', main):
        num_vars += v.count(',') + 1
      assert num_vars == 10, 'no variable elimination should have been run, but seeing %d' % num_vars

  def test_unaligned(self):
      if Settings.QUANTUM_SIZE == 1: return self.skip('No meaning to unaligned addresses in q1')

      src = r'''
        #include<stdio.h>

        struct S {
          double x;
          int y;
        };

        int main() {
          // the 64-bit value here will not be 8-byte aligned
          S s0[3] = { {0x12a751f430142, 22}, {0x17a5c85bad144, 98}, {1, 1}};
          char buffer[10*sizeof(S)];
          int b = int(buffer);
          S *s = (S*)(b + 4-b%8);
          s[0] = s0[0];
          s[1] = s0[1];
          s[2] = s0[2];

          printf("*%d : %d : %d\n", sizeof(S), ((unsigned int)&s[0]) % 8 != ((unsigned int)&s[1]) % 8,
                                               ((unsigned int)&s[1]) - ((unsigned int)&s[0]));
          s[0].x++;
          s[0].y++;
          s[1].x++;
          s[1].y++;
          printf("%.1f,%d,%.1f,%d\n", s[0].x, s[0].y, s[1].x, s[1].y);
          return 0;
        }
        '''

      # TODO: A version of this with int64s as well

      if self.is_le32():
        return self.skip('LLVM marks the reads of s as fully aligned, making this test invalid')
      else:
        self.do_run(src, '*12 : 1 : 12\n328157500735811.0,23,416012775903557.0,99\n')

      return # TODO: continue to the next part here

      # Test for undefined behavior in C. This is not legitimate code, but does exist

      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('No meaning to unaligned addresses without t2')

      src = r'''
        #include <stdio.h>

        int main()
        {
          int x[10];
          char *p = (char*)&x[0];
          p++;
          short *q = (short*)p;
          *q = 300;
          printf("*%d:%d*\n", *q, ((int)q)%2);
          int *r = (int*)p;
          *r = 515559;
          printf("*%d*\n", *r);
          long long *t = (long long*)p;
          *t = 42949672960;
          printf("*%Ld*\n", *t);
          return 0;
        }
      '''

      try:
        self.do_run(src, '*300:1*\n*515559*\n*42949672960*\n')
      except Exception, e:
        assert 'must be aligned' in str(e), e # expected to fail without emulation

  def test_align64(self):
    src = r'''
      #include <stdio.h>

      // inspired by poppler

      enum Type {
        A = 10,
        B = 20
      };

      struct Object {
        Type type;
        union {
          int intg;
          double real;
          char *name;
        };
      };

      struct Principal {
        double x;
        Object a;
        double y;
      };

      int main(int argc, char **argv)
      {
        int base = argc-1;
        Object *o = NULL;
        printf("%d,%d\n", sizeof(Object), sizeof(Principal));
        printf("%d,%d,%d,%d\n", (int)&o[base].type, (int)&o[base].intg, (int)&o[base].real, (int)&o[base].name);
        printf("%d,%d,%d,%d\n", (int)&o[base+1].type, (int)&o[base+1].intg, (int)&o[base+1].real, (int)&o[base+1].name);
        Principal p, q;
        p.x = p.y = q.x = q.y = 0;
        p.a.type = A;
        p.a.real = 123.456;
        *(&q.a) = p.a;
        printf("%.2f,%d,%.2f,%.2f : %.2f,%d,%.2f,%.2f\n", p.x, p.a.type, p.a.real, p.y, q.x, q.a.type, q.a.real, q.y);
        return 0;
      }
    '''

    if self.is_le32():
      self.do_run(src, '''16,32
0,8,8,8
16,24,24,24
0.00,10,123.46,0.00 : 0.00,10,123.46,0.00
''')
    else:
      self.do_run(src, '''12,28
0,4,4,4
12,16,16,16
0.00,10,123.46,0.00 : 0.00,10,123.46,0.00
''')

  def test_unsigned(self):
      Settings.CORRECT_SIGNS = 1 # We test for exactly this sort of thing here
      Settings.CHECK_SIGNS = 0
      src = '''
        #include <stdio.h>
        const signed char cvals[2] = { -1, -2 }; // compiler can store this is a string, so -1 becomes \FF, and needs re-signing
        int main()
        {
          {
            unsigned char x = 200;
            printf("*%d*\\n", x);
            unsigned char y = -22;
            printf("*%d*\\n", y);
          }

          int varey = 100;
          unsigned int MAXEY = -1, MAXEY2 = -77;
          printf("*%u,%d,%u*\\n", MAXEY, varey >= MAXEY, MAXEY2); // 100 >= -1? not in unsigned!

          int y = cvals[0];
          printf("*%d,%d,%d,%d*\\n", cvals[0], cvals[0] < 0, y, y < 0);
          y = cvals[1];
          printf("*%d,%d,%d,%d*\\n", cvals[1], cvals[1] < 0, y, y < 0);

          // zext issue - see mathop in jsifier
          unsigned char x8 = -10;
          unsigned long hold = 0;
          hold += x8;
          int y32 = hold+50;
          printf("*%u,%u*\\n", hold, y32);

          // Comparisons
          x8 = 0;
          for (int i = 0; i < 254; i++) x8++; // make it an actual 254 in JS - not a -2
          printf("*%d,%d*\\n", x8+1 == 0xff, x8+1 != 0xff); // 0xff may be '-1' in the bitcode

          return 0;
        }
      '''
      self.do_run(src, '*4294967295,0,4294967219*\n*-1,1,-1,1*\n*-2,1,-2,1*\n*246,296*\n*1,0*')

      # Now let's see some code that should just work in USE_TYPED_ARRAYS == 2, but requires
      # corrections otherwise
      if Settings.USE_TYPED_ARRAYS == 2:
        Settings.CORRECT_SIGNS = 0
        Settings.CHECK_SIGNS = 1 if not Settings.ASM_JS else 0
      else:
        Settings.CORRECT_SIGNS = 1
        Settings.CHECK_SIGNS = 0

      src = '''
        #include <stdio.h>
        int main()
        {
          {
            unsigned char x;
            unsigned char *y = &x;
            *y = -1;
            printf("*%d*\\n", x);
          }
          {
            unsigned short x;
            unsigned short *y = &x;
            *y = -1;
            printf("*%d*\\n", x);
          }
          /*{ // This case is not checked. The hint for unsignedness is just the %u in printf, and we do not analyze that
            unsigned int x;
            unsigned int *y = &x;
            *y = -1;
            printf("*%u*\\n", x);
          }*/
          {
            char x;
            char *y = &x;
            *y = 255;
            printf("*%d*\\n", x);
          }
          {
            char x;
            char *y = &x;
            *y = 65535;
            printf("*%d*\\n", x);
          }
          {
            char x;
            char *y = &x;
            *y = 0xffffffff;
            printf("*%d*\\n", x);
          }
          return 0;
        }
      '''
      self.do_run(src, '*255*\n*65535*\n*-1*\n*-1*\n*-1*')

  def test_bitfields(self):
      if self.emcc_args is None: Settings.SAFE_HEAP = 0 # bitfields do loads on invalid areas, by design
      src = '''
        #include <stdio.h>
        struct bitty {
          unsigned x : 1;
          unsigned y : 1;
          unsigned z : 1;
        };
        int main()
        {
          bitty b;
          printf("*");
          for (int i = 0; i <= 1; i++)
            for (int j = 0; j <= 1; j++)
              for (int k = 0; k <= 1; k++) {
                b.x = i;
                b.y = j;
                b.z = k;
                printf("%d,%d,%d,", b.x, b.y, b.z);
              }
          printf("*\\n");
          return 0;
        }
      '''
      self.do_run(src, '*0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,1,1,1,0,1,1,1,*')

  def test_floatvars(self):
      src = '''
        #include <stdio.h>

        // headers test, see issue #1013
        #include<cfloat>
        #include<cmath>

        int main(int argc, char **argv)
        {
          float x = 1.234, y = 3.5, q = 0.00000001;
          y *= 3;
          int z = x < y;
          printf("*%d,%d,%.1f,%d,%.4f,%.2f*\\n", z, int(y), y, (int)x, x, q);

          printf("%.2f, %.2f, %.2f, %.2f\\n", fmin(0.5, 3.3), fmin(NAN, 3.3), fmax(0.5, 3.3), fmax(NAN, 3.3));

          printf("small: %.10f\\n", argc * 0.000001);

          /*
          // Rounding behavior
          float fs[6] = { -2.75, -2.50, -2.25, 2.25, 2.50, 2.75 };
          double ds[6] = { -2.75, -2.50, -2.25, 2.25, 2.50, 2.75 };
          for (int i = 0; i < 6; i++)
            printf("*int(%.2f)=%d,%d*\\n", fs[i], int(fs[i]), int(ds[i]));
          */

          return 0;
        }
      '''
      self.do_run(src, '*1,10,10.5,1,1.2340,0.00*\n0.50, 3.30, 3.30, 3.30\nsmall: 0.0000010000\n')

  def test_zerodiv(self):
    self.do_run(r'''
      #include <stdio.h>
      int main(int argc, const char* argv[])
      {
        float f1 = 1.0f;
        float f2 = 0.0f;
        float f_zero = 0.0f;

        float f3 = 0.0f / f2;
        float f4 = f2 / 0.0f;
        float f5 = f2 / f2;
        float f6 = f2 / f_zero;

        printf("f3: %f\n", f3);
        printf("f4: %f\n", f4);
        printf("f5: %f\n", f5);
        printf("f6: %f\n", f6);

        return 0;
      }
    ''', '''f3: nan
f4: nan
f5: nan
f6: nan
''')

  def test_zero_multiplication(self):
    src = '''
      #include <stdio.h>
      int main(int argc, char * argv[]) {
        int one = argc;

        printf("%d ", 0 * one);
        printf("%d ", 0 * -one);
        printf("%d ", -one * 0);
        printf("%g ", 0.0 * one);
        printf("%g ", 0.0 * -one);
        printf("%g", -one * 0.0);
        return 0;
      }
    '''
    self.do_run(src, '0 0 0 0 -0 -0')

  def test_isnan(self):
    src = r'''
      #include <stdio.h>

      int IsNaN(double x){
        int rc;   /* The value return */
        volatile double y = x;
        volatile double z = y;
        rc = (y!=z);
        return rc;
      }

      int main() {
        double tests[] = { 1.0, 3.333, 1.0/0.0, 0.0/0.0, -1.0/0.0, -0, 0, -123123123, 12.0E200 };
        for (int i = 0; i < sizeof(tests)/sizeof(double); i++)
          printf("%d - %f - %d\n", i, tests[i], IsNaN(tests[i]));
      }
      '''
    self.do_run(src, '''0 - 1.000000 - 0
1 - 3.333000 - 0
2 - inf - 0
3 - nan - 1
4 - -inf - 0
5 - 0.000000 - 0
6 - 0.000000 - 0
7 - -123123123.000000 - 0
8 - 1.2e+201 - 0
''')

  def test_globaldoubles(self):
      src = r'''
        #include <stdlib.h>
        #include <stdio.h>

        double      testVu,    testVv,    testWu,    testWv;

        void Test(double _testVu, double _testVv, double _testWu, double _testWv)
        {
            testVu = _testVu;
            testVv = _testVv;
            testWu = _testWu;
            testWv = _testWv;
            printf("BUG?\n");
            printf("Display: Vu=%f  Vv=%f  Wu=%f  Wv=%f\n", testVu, testVv, testWu, testWv);
        }

        int main(void)
        {
            double v1 = 465.1;
            double v2 = 465.2;
            double v3 = 160.3;
            double v4 = 111.4;
            Test(v1, v2, v3, v4);
            return 0;
        }
      '''
      self.do_run(src, 'BUG?\nDisplay: Vu=465.100000  Vv=465.200000  Wu=160.300000  Wv=111.400000')

  def test_math(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2')
      src = '''
        #include <stdio.h>
        #include <stdlib.h>
        #include <cmath>
        int main()
        {
          printf("*%.2f,%.2f,%d", M_PI, -M_PI, (1/0.0) > 1e300); // could end up as infinity, or just a very very big number
          printf(",%d", isfinite(NAN) != 0);
          printf(",%d", isfinite(INFINITY) != 0);
          printf(",%d", isfinite(-INFINITY) != 0);
          printf(",%d", isfinite(12.3) != 0);
          printf(",%d", isinf(NAN) != 0);
          printf(",%d", isinf(INFINITY) != 0);
          printf(",%d", isinf(-INFINITY) != 0);
          printf(",%d", isinf(12.3) != 0);
          div_t div_result = div(23, 10);
          printf(",%d", div_result.quot);
          printf(",%d", div_result.rem);
          double sine = -1.0, cosine = -1.0;
          sincos(0.0, &sine, &cosine);
          printf(",%1.1lf", sine);
          printf(",%1.1lf", cosine);
          float fsine = -1.0f, fcosine = -1.0f;
          sincosf(0.0, &fsine, &fcosine);
          printf(",%1.1f", fsine);
          printf(",%1.1f", fcosine);
          printf("*\\n");
          return 0;
        }
      '''
      self.do_run(src, '*3.14,-3.14,1,0,0,0,1,0,1,1,0,2,3,0.0,1.0,0.0,1.0*')

  def test_erf(self):
      src = '''
        #include <math.h>
        #include <stdio.h>
        int main()
        {
          printf("%1.6f, %1.6f, %1.6f, %1.6f, %1.6f, %1.6f\\n",
                 erf(1.0),
                 erf(3.0),
                 erf(-1.0),
                 erfc(1.0),
                 erfc(3.0),
                 erfc(-1.5));
          return 0;
        }
      '''
      self.do_run(src, '0.842701, 0.999978, -0.842701, 0.157299, 0.000022, 1.966105')

  def test_math_hyperbolic(self):
      src = open(path_from_root('tests', 'hyperbolic', 'src.c'), 'r').read()
      expected = open(path_from_root('tests', 'hyperbolic', 'output.txt'), 'r').read()
      self.do_run(src, expected)

  def test_frexp(self):
      src = '''
        #include <stdio.h>
        #include <math.h>
        #include <assert.h>

        static const double tol=1e-16;

        void test_value(double value)
        {
          int exponent;
          double x=frexp(value, &exponent);
          double expected=x*pow(2.0, exponent);

          printf("%f=%f*2^%d\\n", value, x, exponent);

          assert(fabs(expected-value)<tol);
          assert(x==0 || (fabs(x)>=5e-1 && fabs(x)<1)); // x has a magnitude in the interval [1/2, 1)
        }

        int main()
        {
          test_value(0);
          test_value(100.1);
          test_value(-100.1);
          test_value(.5);
          test_value(-.5);
          test_value(1-1e-16);
          test_value(-(1-1e-16));

          return 0;
        }
      '''
      self.do_run(src, '''0.000000=0.000000*2^0
100.100000=0.782031*2^7
-100.100000=-0.782031*2^7
0.500000=0.500000*2^0
-0.500000=-0.500000*2^0
1.000000=1.000000*2^0
-1.000000=-1.000000*2^0''')

  def test_rounding(self):
      src = '''
        #include <stdio.h>
        #include <math.h>

        int main()
        {
          printf("%.1f ", round(1.4));
          printf("%.1f ", round(1.6));
          printf("%.1f ", round(-1.4));
          printf("%.1f ", round(-1.6));

          printf("%.1f ", round(1.5));
          printf("%.1f ", round(2.5));
          printf("%.1f ", round(-1.5));
          printf("%.1f ", round(-2.5));

          printf("%ld ", lrint(1.4));
          printf("%ld ", lrint(1.6));
          printf("%ld ", lrint(-1.4));
          printf("%ld ", lrint(-1.6));

          printf("%ld ", lrint(1.5));
          printf("%ld ", lrint(2.5));
          printf("%ld ", lrint(-1.5));
          printf("%ld ", lrint(-2.5));

          return 0;
        }
        '''
      self.do_run(src, "1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 "
                       "1 2 -1 -2 2 2 -2 -2")

  # This example borrowed from MSDN documentation
  def test_fcvt(self):
      if self.emcc_args is None: return self.skip('requires emcc')

      src = '''
        #include <stdlib.h>
        #include <stdio.h>

        int main() {
           int  decimal, sign;
           char *buffer;
           double source = 3.1415926535;

           buffer = fcvt(source, 7, &decimal, &sign);
           printf("source: %2.10f   buffer: '%s'   decimal: %d   sign: %d\\n",
                   source, buffer, decimal, sign);
        }
        '''
      self.do_run(src, "source: 3.1415926535   buffer: '31415927'   decimal: 1   sign: 0");

  def test_llrint(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2')
    src = r'''
      #include <stdio.h>
      #include <math.h>
      int main() {
        printf("%lld\n%lld\n%lld\n%lld\n", llrint(0.1), llrint(0.6), llrint(1.25), llrint(1099511627776.667));
        return 0;
      }
    '''
    self.do_run(src, '0\n1\n1\n1099511627777\n')

  def test_getgep(self):
      # Generated code includes getelementptr (getelementptr, 0, 1), i.e., GEP as the first param to GEP
      src = '''
        #include <stdio.h>
        struct {
          int y[10];
          int z[10];
        } commonblock;

        int main()
        {
          for (int i = 0; i < 10; ++i) {
            commonblock.y[i] = 1;
            commonblock.z[i] = 2;
          }
          printf("*%d %d*\\n", commonblock.y[0], commonblock.z[0]);
          return 0;
        }
      '''
      self.do_run(src, '*1 2*')

  def test_multiply_defined_symbols(self):
    a1 = "int f() { return 1; }"
    a1_name = os.path.join(self.get_dir(), 'a1.c')
    open(a1_name, 'w').write(a1)
    a2 = "void x() {}"
    a2_name = os.path.join(self.get_dir(), 'a2.c')
    open(a2_name, 'w').write(a2)
    b1 = "int f() { return 2; }"
    b1_name = os.path.join(self.get_dir(), 'b1.c')
    open(b1_name, 'w').write(b1)
    b2 = "void y() {}"
    b2_name = os.path.join(self.get_dir(), 'b2.c')
    open(b2_name, 'w').write(b2)
    main = r'''
      #include <stdio.h>
      int f();
      int main() {
        printf("result: %d\n", f());
        return 0;
      }
    '''
    main_name = os.path.join(self.get_dir(), 'main.c')
    open(main_name, 'w').write(main)

    Building.emcc(a1_name)
    Building.emcc(a2_name)
    Building.emcc(b1_name)
    Building.emcc(b2_name)
    Building.emcc(main_name)

    liba_name = os.path.join(self.get_dir(), 'liba.a')
    Building.emar('cr', liba_name, [a1_name + '.o', a2_name + '.o'])
    libb_name = os.path.join(self.get_dir(), 'libb.a')
    Building.emar('cr', libb_name, [b1_name + '.o', b2_name + '.o'])

    all_name = os.path.join(self.get_dir(), 'all.bc')
    Building.link([main_name + '.o', liba_name, libb_name], all_name)

    self.do_ll_run(all_name, 'result: 1')

  def test_if(self):
      src = '''
        #include <stdio.h>
        int main()
        {
          int x = 5;
          if (x > 3) {
            printf("*yes*\\n");
          }
          return 0;
        }
      '''
      self.do_run(src, '*yes*')

  def test_if_else(self):
      src = '''
        #include <stdio.h>
        int main()
        {
          int x = 5;
          if (x > 10) {
            printf("*yes*\\n");
          } else {
            printf("*no*\\n");
          }
          return 0;
        }
      '''
      self.do_run(src, '*no*')

  def test_loop(self):
      src = '''
        #include <stdio.h>
        int main()
        {
          int x = 5;
          for (int i = 0; i < 6; i++) {
            x += x*i;
            if (x > 1000) {
              if (x % 7 == 0) printf("cheez\\n");
              x /= 2;
              break;
            }
          }
          printf("*%d*\\n", x);
          return 0;
        }
      '''

      self.do_run(src, '*1800*')

      generated = open('src.cpp.o.js', 'r').read()

  def test_stack(self):
      Settings.INLINING_LIMIT = 50

      src = '''
        #include <stdio.h>
        int test(int i) {
          int x = 10;
          if (i > 0) {
            return test(i-1);
          }
          return int(&x); // both for the number, and forces x to not be nativized
        }
        int main(int argc, char **argv)
        {
          // We should get the same value for the first and last - stack has unwound
          int x1 = test(argc - 2);
          int x2 = test(100);
          int x3 = test((argc - 2) / 4);
          printf("*%d,%d*\\n", x3-x1, x2 != x1);
          return 0;
        }
      '''
      self.do_run(src, '*0,1*')

  def test_strings(self):
      src = '''
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>

        int main(int argc, char **argv)
        {
          int x = 5, y = 9, magic = 7; // fool compiler with magic
          memmove(&x, &y, magic-7); // 0 should not crash us

          int xx, yy, zz;
          char s[32];
          int cc = sscanf("abc_10.b1_xyz9_543_defg", "abc_%d.%2x_xyz9_%3d_%3s", &xx, &yy, &zz, s);
          printf("%d:%d,%d,%d,%s\\n", cc, xx, yy, zz, s);

          printf("%d\\n", argc);
          puts(argv[1]);
          puts(argv[2]);
          printf("%d\\n", atoi(argv[3])+2);
          const char *foolingthecompiler = "\\rabcd";
          printf("%d\\n", strlen(foolingthecompiler)); // Tests parsing /0D in llvm - should not be a 0 (end string) then a D!
          printf("%s\\n", NULL); // Should print '(null)', not the string at address 0, which is a real address for us!
          printf("/* a comment */\\n"); // Should not break the generated code!
          printf("// another\\n"); // Should not break the generated code!

          char* strdup_val = strdup("test");
          printf("%s\\n", strdup_val);
          free(strdup_val);

          {
            char *one = "one 1 ONE !";
            char *two = "two 2 TWO ?";
            char three[1024];
            memset(three, '.', 1024);
            three[50] = 0;
            strncpy(three + argc, one + (argc/2), argc+1);
            strncpy(three + argc*3, two + (argc/3), argc+2);
            printf("waka %s\\n", three);
          }

          {
            char *one = "string number one top notch";
            char *two = "fa la sa ho fi FI FO FUM WHEN WHERE WHY HOW WHO";
            char three[1000];
            strcpy(three, &one[argc*2]);
            char *four = strcat(three, &two[argc*3]);
            printf("cat |%s|\\n", three);
            printf("returned |%s|\\n", four);
          }

          return 0;
        }
      '''
      for named in (0, 1):
        print named
        Settings.NAMED_GLOBALS = named
        self.do_run(src, '''4:10,177,543,def\n4\nwowie\ntoo\n76\n5\n(null)\n/* a comment */\n// another\ntest\nwaka ....e 1 O...wo 2 T................................
cat |umber one top notchfi FI FO FUM WHEN WHERE WHY HOW WHO|
returned |umber one top notchfi FI FO FUM WHEN WHERE WHY HOW WHO|''', ['wowie', 'too', '74'])
        if self.emcc_args == []:
          gen = open(self.in_dir('src.cpp.o.js')).read()
          assert ('var __str1;' in gen) == named

  def test_strcmp_uni(self):
    src = '''
      #include <stdio.h>
      #include <string.h>
      int main()
      {
        #define TEST(func) \
        { \
          char *word = "WORD"; \
          char wordEntry[2] = { -61,-126 }; /* "Â"; */ \
          int cmp = func(word, wordEntry, 2); \
          printf("Compare value " #func " is %d\\n", cmp); \
        }
        TEST(strncmp);
        TEST(strncasecmp);
        TEST(memcmp);
      }
    '''
    self.do_run(src, 'Compare value strncmp is -1\nCompare value strncasecmp is -1\nCompare value memcmp is -1\n')

  def test_strndup(self):
      src = '''
        //---------------
        //- http://pubs.opengroup.org/onlinepubs/9699919799/functions/strndup.html
        //---------------

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>

        int main(int argc, char **argv) {
          const char* source = "strndup - duplicate a specific number of bytes from a string";

          char* strdup_val = strndup(source, 0);
          printf("1:%s\\n", strdup_val);
          free(strdup_val);

          strdup_val = strndup(source, 7);
          printf("2:%s\\n", strdup_val);
          free(strdup_val);

          strdup_val = strndup(source, 1000);
          printf("3:%s\\n", strdup_val);
          free(strdup_val);

          strdup_val = strndup(source, 60);
          printf("4:%s\\n", strdup_val);
          free(strdup_val);

          strdup_val = strndup(source, 19);
          printf("5:%s\\n", strdup_val);
          free(strdup_val);

          strdup_val = strndup(source, -1);
          printf("6:%s\\n", strdup_val);
          free(strdup_val);

          return 0;
        }
      '''
      self.do_run(src, '1:\n2:strndup\n3:strndup - duplicate a specific number of bytes from a string\n4:strndup - duplicate a specific number of bytes from a string\n5:strndup - duplicate\n6:\n')

  def test_errar(self):
      src = r'''
        #include <stdio.h>
        #include <errno.h>
        #include <string.h>

        int main() {
          char* err;
          char buffer[200];

          err = strerror(EDOM);
          strerror_r(EWOULDBLOCK, buffer, 200);
          printf("<%s>\n", err);
          printf("<%s>\n", buffer);

          printf("<%d>\n", strerror_r(EWOULDBLOCK, buffer, 0));
          errno = 123;
          printf("<%d>\n", errno);

          return 0;
        }
        '''
      expected = '''
        <Math arg out of domain of func>
        <No more processes>
        <34>
        <123>
        '''
      self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected))

  def test_mainenv(self):
      src = '''
        #include <stdio.h>
        int main(int argc, char **argv, char **envp)
        {
          printf("*%p*\\n", envp);
          return 0;
        }
      '''
      self.do_run(src, '*(nil)*')

  def test_funcs(self):
      src = '''
        #include <stdio.h>
        int funcy(int x)
        {
          return x*9;
        }
        int main()
        {
          printf("*%d,%d*\\n", funcy(8), funcy(10));
          return 0;
        }
      '''
      self.do_run(src, '*72,90*')

  def test_structs(self):
      src = '''
        #include <stdio.h>
        struct S
        {
          int x, y;
        };
        int main()
        {
          S a, b;
          a.x = 5; a.y = 6;
          b.x = 101; b.y = 7009;
          S *c, *d;
          c = &a;
          c->x *= 2;
          c = &b;
          c->y -= 1;
          d = c;
          d->y += 10;
          printf("*%d,%d,%d,%d,%d,%d,%d,%d*\\n", a.x, a.y, b.x, b.y, c->x, c->y, d->x, d->y);
          return 0;
        }
      '''
      self.do_run(src, '*10,6,101,7018,101,7018,101,7018*')

  gen_struct_src = '''
        #include <stdio.h>
        #include <stdlib.h>
        #include "emscripten.h"

        struct S
        {
          int x, y;
        };
        int main()
        {
          S* a = {{gen_struct}};
          a->x = 51; a->y = 62;
          printf("*%d,%d*\\n", a->x, a->y);
          {{del_struct}}(a);
          return 0;
        }
  '''

  def test_mallocstruct(self):
      self.do_run(self.gen_struct_src.replace('{{gen_struct}}', '(S*)malloc(sizeof(S))').replace('{{del_struct}}', 'free'), '*51,62*')

  def test_newstruct(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      self.do_run(self.gen_struct_src.replace('{{gen_struct}}', 'new S').replace('{{del_struct}}', 'delete'), '*51,62*')

  def test_addr_of_stacked(self):
      src = '''
        #include <stdio.h>
        void alter(int *y)
        {
          *y += 5;
        }
        int main()
        {
          int x = 2;
          alter(&x);
          printf("*%d*\\n", x);
          return 0;
        }
      '''
      self.do_run(src, '*7*')

  def test_globals(self):
      src = '''
        #include <stdio.h>

        char cache[256], *next = cache;

        int main()
        {
          cache[10] = 25;
          next[20] = 51;
          printf("*%d,%d*\\n", next[10], cache[20]);
          return 0;
        }
      '''
      self.do_run(src, '*25,51*')

  def test_linked_list(self):
      src = '''
        #include <stdio.h>
        struct worker_args {
          int value;
          struct worker_args *next;
        };
        int main()
        {
          worker_args a;
          worker_args b;
          a.value = 60;
          a.next = &b;
          b.value = 900;
          b.next = NULL;
          worker_args* c = &a;
          int total = 0;
          while (c) {
            total += c->value;
            c = c->next;
          }

          // Chunk of em
          worker_args chunk[10];
          for (int i = 0; i < 9; i++) {
            chunk[i].value = i*10;
            chunk[i].next = &chunk[i+1];
          }
          chunk[9].value = 90;
          chunk[9].next = &chunk[0];

          c = chunk;
          do {
            total += c->value;
            c = c->next;
          } while (c != chunk);

          printf("*%d,%d*\\n", total, b.next);
          // NULL *is* 0, in C/C++. No JS null! (null == 0 is false, etc.)

          return 0;
        }
      '''
      self.do_run(src, '*1410,0*')

  def test_sup(self):
      src = '''
        #include <stdio.h>

        struct S4   { int x;          }; // size: 4
        struct S4_2 { short x, y;     }; // size: 4, but for alignment purposes, 2
        struct S6   { short x, y, z;  }; // size: 6
        struct S6w  { char x[6];      }; // size: 6 also
        struct S6z  { int x; short y; }; // size: 8, since we align to a multiple of the biggest - 4

        struct C___  { S6 a, b, c; int later; };
        struct Carr  { S6 a[3]; int later; }; // essentially the same, but differently defined
        struct C__w  { S6 a; S6w b; S6 c; int later; }; // same size, different struct
        struct Cp1_  { int pre; short a; S6 b, c; int later; }; // fillers for a
        struct Cp2_  { int a; short pre; S6 b, c; int later; }; // fillers for a (get addr of the other filler)
        struct Cint  { S6 a; int  b; S6 c; int later; }; // An int (different size) for b
        struct C4__  { S6 a; S4   b; S6 c; int later; }; // Same size as int from before, but a struct
        struct C4_2  { S6 a; S4_2 b; S6 c; int later; }; // Same size as int from before, but a struct with max element size 2
        struct C__z  { S6 a; S6z  b; S6 c; int later; }; // different size, 8 instead of 6

        int main()
        {
          #define TEST(struc) \\
          { \\
            struc *s = 0; \\
            printf("*%s: %d,%d,%d,%d<%d*\\n", #struc, (int)&(s->a), (int)&(s->b), (int)&(s->c), (int)&(s->later), sizeof(struc)); \\
          }
          #define TEST_ARR(struc) \\
          { \\
            struc *s = 0; \\
            printf("*%s: %d,%d,%d,%d<%d*\\n", #struc, (int)&(s->a[0]), (int)&(s->a[1]), (int)&(s->a[2]), (int)&(s->later), sizeof(struc)); \\
          }
          printf("sizeofs:%d,%d\\n", sizeof(S6), sizeof(S6z));
          TEST(C___);
          TEST_ARR(Carr);
          TEST(C__w);
          TEST(Cp1_);
          TEST(Cp2_);
          TEST(Cint);
          TEST(C4__);
          TEST(C4_2);
          TEST(C__z);
          return 0;
        }
      '''
      if Settings.QUANTUM_SIZE == 1:
        self.do_run(src, 'sizeofs:6,8\n*C___: 0,3,6,9<24*\n*Carr: 0,3,6,9<24*\n*C__w: 0,3,9,12<24*\n*Cp1_: 1,2,5,8<24*\n*Cp2_: 0,2,5,8<24*\n*Cint: 0,3,4,7<24*\n*C4__: 0,3,4,7<24*\n*C4_2: 0,3,5,8<20*\n*C__z: 0,3,5,8<28*')
      else:
        self.do_run(src, 'sizeofs:6,8\n*C___: 0,6,12,20<24*\n*Carr: 0,6,12,20<24*\n*C__w: 0,6,12,20<24*\n*Cp1_: 4,6,12,20<24*\n*Cp2_: 0,6,12,20<24*\n*Cint: 0,8,12,20<24*\n*C4__: 0,8,12,20<24*\n*C4_2: 0,6,10,16<20*\n*C__z: 0,8,16,24<28*')

  def test_assert(self):
      src = '''
        #include <stdio.h>
        #include <assert.h>
        int main() {
          assert(1 == true); // pass
          assert(1 == false); // fail
          return 0;
        }
      '''
      self.do_run(src, 'Assertion failed: 1 == false')

  def test_libcextra(self):
      if self.emcc_args is None: return self.skip('needs emcc for libcextra')
      src = r'''
        #include <stdio.h>
        #include <wchar.h>

        int main()
        {
            const wchar_t* wstr = L"Hello";

            printf("wcslen: %d\n", wcslen(wstr));

            return 0;
        }
      '''
      self.do_run(src, 'wcslen: 5')

  def test_longjmp(self):
      src = r'''
        #include <stdio.h>
        #include <setjmp.h>

        static jmp_buf buf;

        void second(void) {
            printf("second\n");
            longjmp(buf,-1);
        }

        void first(void) {
            printf("first\n");                         // prints
            longjmp(buf,1);                            // jumps back to where setjmp was called - making setjmp now return 1
        }

        int main() {
            volatile int x = 0;
            int jmpval = setjmp(buf);
            if (!jmpval) {
                x++;                                   // should be properly restored once longjmp jumps back
                first();                               // when executed, setjmp returns 1
                printf("skipped\n");                   // does not print
            } else if (jmpval == 1) {                  // when first() jumps back, setjmp returns 1
                printf("result: %d %d\n", x, jmpval);  // prints
                x++;
                second();                              // when executed, setjmp returns -1
            } else if (jmpval == -1) {                 // when second() jumps back, setjmp returns -1
                printf("result: %d %d\n", x, jmpval);  // prints
            }

            return 0;
        }
      '''
      self.do_run(src, 'first\nresult: 1 1\nsecond\nresult: 2 -1')

  def test_longjmp2(self):
    src = r'''
      #include <setjmp.h>
      #include <stdio.h>

      typedef struct {
        jmp_buf* jmp;
      } jmp_state;

      void stack_manipulate_func(jmp_state* s, int level) {
        jmp_buf buf;

        printf("Entering stack_manipulate_func, level: %d\n", level);

        if (level == 0) {
          s->jmp = &buf;
          if (setjmp(*(s->jmp)) == 0) {
            printf("Setjmp normal execution path, level: %d\n", level);
            stack_manipulate_func(s, level + 1);
          } else {
            printf("Setjmp error execution path, level: %d\n", level);
          }
        } else {
          printf("Perform longjmp at level %d\n", level);
          longjmp(*(s->jmp), 1);
        }

        printf("Exiting stack_manipulate_func, level: %d\n", level);
      }

      int main(int argc, char *argv[]) {
        jmp_state s;
        s.jmp = NULL;
        stack_manipulate_func(&s, 0);

        return 0;
      }
      '''
    self.do_run(src, '''Entering stack_manipulate_func, level: 0
Setjmp normal execution path, level: 0
Entering stack_manipulate_func, level: 1
Perform longjmp at level 1
Setjmp error execution path, level: 0
Exiting stack_manipulate_func, level: 0
''')

  def test_longjmp3(self):
    src = r'''
      #include <setjmp.h>
      #include <stdio.h>

      typedef struct {
        jmp_buf* jmp;
      } jmp_state;

      void setjmp_func(jmp_state* s, int level) {
        jmp_buf* prev_jmp = s->jmp;
        jmp_buf c_jmp;

        if (level == 2) {
          printf("level is 2, perform longjmp!\n");
          longjmp(*(s->jmp), 1);
        }

        if (setjmp(c_jmp) == 0) {
          printf("setjmp normal execution path, level: %d\n", level);
          s->jmp = &c_jmp;
          setjmp_func(s, level + 1);
        } else {
          printf("setjmp exception execution path, level: %d\n", level);
          if (prev_jmp) {
            printf("prev_jmp is not empty, continue with longjmp!\n");
            s->jmp = prev_jmp;
            longjmp(*(s->jmp), 1);
          }
        }

        printf("Exiting setjmp function, level: %d\n", level);
      }

      int main(int argc, char *argv[]) {
        jmp_state s;
        s.jmp = NULL;

        setjmp_func(&s, 0);

        return 0;
      }
      '''
    self.do_run(src, '''setjmp normal execution path, level: 0
setjmp normal execution path, level: 1
level is 2, perform longjmp!
setjmp exception execution path, level: 1
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 0
Exiting setjmp function, level: 0
''')

  def test_longjmp4(self):
    src = r'''
      #include <setjmp.h>
      #include <stdio.h>

      typedef struct {
        jmp_buf* jmp;
      } jmp_state;

      void second_func(jmp_state* s);

      void first_func(jmp_state* s) {
        jmp_buf* prev_jmp = s->jmp;
        jmp_buf c_jmp;
        volatile int once = 0;

        if (setjmp(c_jmp) == 0) {
          printf("Normal execution path of first function!\n");

          s->jmp = &c_jmp;
          second_func(s);
        } else {
          printf("Exception execution path of first function! %d\n", once);

          if (!once) {
            printf("Calling longjmp the second time!\n");
            once = 1;
            longjmp(*(s->jmp), 1);
          }
        }
      }

      void second_func(jmp_state* s) {
        longjmp(*(s->jmp), 1);
      }

      int main(int argc, char *argv[]) {
        jmp_state s;
        s.jmp = NULL;

        first_func(&s);

        return 0;
      }
      '''
    self.do_run(src, '''Normal execution path of first function!
Exception execution path of first function! 0
Calling longjmp the second time!
Exception execution path of first function! 1
''')

  def test_longjmp_funcptr(self):
      src = r'''
        #include <stdio.h>
        #include <setjmp.h>

        static jmp_buf buf;

        void (*fp)() = NULL;

        void second(void) {
            printf("second\n");         // prints
            longjmp(buf,1);             // jumps back to where setjmp was called - making setjmp now return 1
        }

        void first(void) {
            fp();
            printf("first\n");          // does not print
        }

        int main(int argc, char **argv) {
            fp = argc == 200 ? NULL : second;

            volatile int x = 0;
            if ( ! setjmp(buf) ) {
                x++;
                first();                // when executed, setjmp returns 0
            } else {                    // when longjmp jumps back, setjmp returns 1
                printf("main: %d\n", x);       // prints
            }

            return 0;
        }
      '''
      self.do_run(src, 'second\nmain: 1\n')

  def test_longjmp_repeat(self):
      Settings.MAX_SETJMPS = 1

      src = r'''
        #include <stdio.h>
        #include <setjmp.h>

        static jmp_buf buf;

        int main() {
          volatile int x = 0;
          printf("setjmp:%d\n", setjmp(buf));
          x++;
          printf("x:%d\n", x);
          if (x < 4) longjmp(buf, x*2);
          return 0;
        }
      '''
      self.do_run(src, '''setjmp:0
x:1
setjmp:2
x:2
setjmp:4
x:3
setjmp:6
x:4
''')

  def test_longjmp_stacked(self):
      src = r'''
        #include <stdio.h>
        #include <setjmp.h>
        #include <stdlib.h>
        #include <string.h>

        int bottom, top;

        int run(int y) {
          // confuse stack
          char *s = (char*)alloca(100);
          memset(s, 1, 100);
          s[y] = y;
          s[y/2] = y*2;
          volatile int x = s[y];
          top = (int)alloca(4);
          if (x <= 2) return x;
          jmp_buf buf;
          printf("setjmp of %d\n", x);
          if (setjmp(buf) == 0) {
            printf("going\n");
            x += run(x/2);
            longjmp(buf, 1);
          }
          printf("back\n");
          return x/2;
        }

        int main(int argc, char **argv) {
          int sum = 0;
          for (int i = 0; i < argc*2; i++) {
            bottom = (int)alloca(4);
            sum += run(10);
            // scorch the earth
            if (bottom < top) {
              memset((void*)bottom, 1, top - bottom);
            } else {
              memset((void*)top, 1, bottom - top);
            }
          }
          printf("%d\n", sum);
          return sum;
        }
      '''
      self.do_run(src, '''setjmp of 10
going
setjmp of 5
going
back
back
setjmp of 10
going
setjmp of 5
going
back
back
12
''')

  def test_longjmp_exc(self):
    src = r'''
      #include <stdlib.h>
      #include <stdio.h>
      #include <setjmp.h>
      #include <emscripten.h>

      jmp_buf abortframe;

      void dostuff(int a) {
        printf("pre\n");
        if (a != 42) emscripten_run_script("waka_waka()"); // this should fail, and never reach "never"
        printf("never\n");

        if (a == 100) {
          longjmp (abortframe, -1);
        }

        if (setjmp(abortframe)) {
          printf("got 100");
        }
      }

      int main(int argc, char **argv) {
        dostuff(argc);
        exit(1);
        return 1;
      }
      '''
    self.do_run(src, 'waka_waka');

  def test_setjmp_many(self):
    src = r'''
      #include <stdio.h>
      #include <setjmp.h>

      int main(int argc) {
        jmp_buf buf;
        for (int i = 0; i < NUM; i++) printf("%d\n", setjmp(buf));
        if (argc-- == 1131) longjmp(buf, 11);
        return 0;
      }
    '''
    for num in [Settings.MAX_SETJMPS, Settings.MAX_SETJMPS+1]:
      print num
      self.do_run(src.replace('NUM', str(num)), '0\n' * num if num <= Settings.MAX_SETJMPS or not Settings.ASM_JS else 'build with a higher value for MAX_SETJMPS')

  def test_exceptions(self):
      if Settings.QUANTUM_SIZE == 1: return self.skip("we don't support libcxx in q1")
      if self.emcc_args is None: return self.skip('need emcc to add in libcxx properly')

      Settings.EXCEPTION_DEBUG = 1

      Settings.DISABLE_EXCEPTION_CATCHING = 0
      if '-O2' in self.emcc_args:
        self.emcc_args += ['--closure', '1'] # Use closure here for some additional coverage

      src = '''
        #include <stdio.h>
        void thrower() {
          printf("infunc...");
          throw(99);
          printf("FAIL");
        }
        int main() {
          try {
            printf("*throw...");
            throw(1);
            printf("FAIL");
          } catch(...) {
            printf("caught!");
          }
          try {
            thrower();
          } catch(...) {
            printf("done!*\\n");
          }
          return 0;
        }
      '''
      self.do_run(src, '*throw...caught!infunc...done!*')

      Settings.DISABLE_EXCEPTION_CATCHING = 1
      self.do_run(src, 'Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0')

      src = '''
      #include <iostream>

      class MyException
      {
      public:
          MyException(){ std::cout << "Construct..."; }
          MyException( const MyException & ) { std::cout << "Copy..."; }
          ~MyException(){ std::cout << "Destruct..."; }
      };

      int function()
      {
          std::cout << "Throw...";
          throw MyException();
      }

      int function2()
      {
          return function();
      }

      int main()
      {
          try
          {
              function2();
          }
          catch (MyException & e)
          {
              std::cout << "Catched...";
          }

          try
          {
              function2();
          }
          catch (MyException e)
          {
              std::cout << "Catched...";
          }

          return 0;
      }
      '''

      Settings.DISABLE_EXCEPTION_CATCHING = 0
      if '-O2' in self.emcc_args:
        self.emcc_args.pop() ; self.emcc_args.pop() # disable closure to work around a closure bug
      self.do_run(src, 'Throw...Construct...Catched...Destruct...Throw...Construct...Copy...Catched...Destruct...Destruct...')

  def test_exception_2(self):
    if self.emcc_args is None: return self.skip('need emcc to add in libcxx properly')
    Settings.DISABLE_EXCEPTION_CATCHING = 0
    src = r'''
      #include <stdexcept>
      #include <stdio.h>

      typedef void (*FuncPtr)();

      void ThrowException()
      {
        throw std::runtime_error("catch me!");
      }

      FuncPtr ptr = ThrowException;

      int main()
      {
        try
        {
	        ptr();
        }
        catch(...)
        {
	        printf("Exception caught successfully!\n");
        }
        return 0;
      }
    '''
    self.do_run(src, 'Exception caught successfully!')

  def test_white_list_exception(self):
    Settings.DISABLE_EXCEPTION_CATCHING = 2
    Settings.EXCEPTION_CATCHING_WHITELIST = ["__Z12somefunctionv"]
    Settings.INLINING_LIMIT = 50 # otherwise it is inlined and not identified

    src = '''
        #include <stdio.h>

        void thrower() {
          printf("infunc...");
          throw(99);
          printf("FAIL");
        }

        void somefunction() {
          try {
            thrower();
          } catch(...) {
            printf("done!*\\n");
          }
        }

        int main() {
          somefunction();
          return 0;
        }
      '''
    self.do_run(src, 'infunc...done!*')

    Settings.DISABLE_EXCEPTION_CATCHING = 0
    Settings.EXCEPTION_CATCHING_WHITELIST = []

  def test_uncaught_exception(self):
      if self.emcc_args is None: return self.skip('no libcxx inclusion without emcc')

      Settings.DISABLE_EXCEPTION_CATCHING = 0

      src = r'''
        #include <stdio.h>
        #include <exception>
        struct X {
          ~X() {
            printf("exception? %s\n", std::uncaught_exception() ? "yes" : "no");
          }
        };
        int main() {
          printf("exception? %s\n", std::uncaught_exception() ? "yes" : "no");
          try {
            X x;
            throw 1;
          } catch(...) {
            printf("exception? %s\n", std::uncaught_exception() ? "yes" : "no");
          }
          printf("exception? %s\n", std::uncaught_exception() ? "yes" : "no");
          return 0;
        }
      '''
      self.do_run(src, 'exception? no\nexception? yes\nexception? no\nexception? no\n')

      src = r'''
        #include <fstream>
        #include <iostream>
        int main() {
          std::ofstream os("test");
          os << std::unitbuf << "foo"; // trigger a call to std::uncaught_exception from
                                       // std::basic_ostream::sentry::~sentry
          std::cout << "success";
        }
      '''
      self.do_run(src, 'success')

  def test_typed_exceptions(self):
      Settings.DISABLE_EXCEPTION_CATCHING = 0
      Settings.SAFE_HEAP = 0  # Throwing null will cause an ignorable null pointer access.
      src = open(path_from_root('tests', 'exceptions', 'typed.cpp'), 'r').read()
      expected = open(path_from_root('tests', 'exceptions', 'output.txt'), 'r').read()
      self.do_run(src, expected)

  def test_multiexception(self):
    Settings.DISABLE_EXCEPTION_CATCHING = 0
    src = r'''
#include <stdio.h>

static int current_exception_id = 0;

typedef struct {
int jmp;
} jmp_state;

void setjmp_func(jmp_state* s, int level) {
int prev_jmp = s->jmp;
int c_jmp;

if (level == 2) {
  printf("level is 2, perform longjmp!\n");
  throw 1;
}

c_jmp = current_exception_id++;
try {
  printf("setjmp normal execution path, level: %d, prev_jmp: %d\n", level, prev_jmp);
  s->jmp = c_jmp;
  setjmp_func(s, level + 1);
} catch (int catched_eid) {
  printf("caught %d\n", catched_eid);
  if (catched_eid == c_jmp) {
    printf("setjmp exception execution path, level: %d, prev_jmp: %d\n", level, prev_jmp);
    if (prev_jmp != -1) {
      printf("prev_jmp is not empty, continue with longjmp!\n");
      s->jmp = prev_jmp;
      throw s->jmp;
    }
  } else {
    throw;
  }
}

printf("Exiting setjmp function, level: %d, prev_jmp: %d\n", level, prev_jmp);
}

int main(int argc, char *argv[]) {
jmp_state s;
s.jmp = -1;

setjmp_func(&s, 0);

return 0;
}
'''
    self.do_run(src, '''setjmp normal execution path, level: 0, prev_jmp: -1
setjmp normal execution path, level: 1, prev_jmp: 0
level is 2, perform longjmp!
caught 1
setjmp exception execution path, level: 1, prev_jmp: 0
prev_jmp is not empty, continue with longjmp!
caught 0
setjmp exception execution path, level: 0, prev_jmp: -1
Exiting setjmp function, level: 0, prev_jmp: -1
''')

  def test_std_exception(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    Settings.DISABLE_EXCEPTION_CATCHING = 0
    self.emcc_args += ['-s', 'SAFE_HEAP=0']

    src = r'''
      #include <stdio.h>
      #include <exception>

      int main()
      {
          std::exception e;
          try {
            throw e;
          } catch(std::exception e) {
            printf("caught std::exception\n");
          }
          return 0;
      }
    '''
    self.do_run(src, 'caught std::exception')

  def test_async_exit(self):
    open('main.c', 'w').write(r'''
      #include <stdio.h>
      #include <stdlib.h>
      #include "emscripten.h"

      void main_loop() {
        exit(EXIT_SUCCESS);
      }

      int main() {
        emscripten_set_main_loop(main_loop, 60, 0);
        return 0;
      }
    ''')

    Popen([PYTHON, EMCC, 'main.c']).communicate()
    self.assertNotContained('Reached an unreachable!', run_js(self.in_dir('a.out.js'), stderr=STDOUT))

  def test_exit_stack(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if Settings.ASM_JS: return self.skip('uses report_stack without exporting')

    Settings.INLINING_LIMIT = 50

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      extern "C" {
        extern void report_stack(int x);
      }

      char moar() {
        char temp[125];
        for (int i = 0; i < 125; i++) temp[i] = i*i;
        for (int i = 1; i < 125; i++) temp[i] += temp[i-1]/2;
        if (temp[100] != 99) exit(1);
        return temp[120];
      }

      int main(int argc, char *argv[]) {
        report_stack((int)alloca(4));
        printf("*%d*\n", moar());
        return 0;
      }
    '''

    open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
      var initialStack = -1;
      var _report_stack = function(x) {
        Module.print('reported');
        initialStack = x;
      }
      var Module = {
        postRun: function() {
          Module.print('Exit Status: ' + EXITSTATUS);
          Module.print('postRun');
          assert(initialStack == STACKTOP, [initialStack, STACKTOP]);
          Module.print('ok.');
        }
      };
    ''')

    self.emcc_args += ['--pre-js', 'pre.js']
    self.do_run(src, '''reported\nexit(1) called\nExit Status: 1\npostRun\nok.\n''')

  def test_class(self):
      src = '''
        #include <stdio.h>
        struct Random {
           enum { IM = 139968, IA = 3877, IC = 29573 };
           Random() : last(42) {}
           float get( float max = 1.0f ) {
              last = ( last * IA + IC ) % IM;
              return max * last / IM;
           }
        protected:
           unsigned int last;
        } rng1;
        int main()
        {
          Random rng2;
          int count = 0;
          for (int i = 0; i < 100; i++) {
            float x1 = rng1.get();
            float x2 = rng2.get();
            printf("%f, %f\\n", x1, x2);
            if (x1 != x2) count += 1;
          }
          printf("*%d*\\n", count);
          return 0;
        }
      '''
      self.do_run(src, '*0*')

  def test_inherit(self):
      src = '''
        #include <stdio.h>
        struct Parent {
          int x1, x2;
        };
        struct Child : Parent {
          int y;
        };
        int main()
        {
          Parent a;
          a.x1 = 50;
          a.x2 = 87;
          Child b;
          b.x1 = 78;
          b.x2 = 550;
          b.y = 101;
          Child* c = (Child*)&a;
          c->x1 ++;
          c = &b;
          c->y --;
          printf("*%d,%d,%d,%d,%d,%d,%d*\\n", a.x1, a.x2, b.x1, b.x2, b.y, c->x1, c->x2);
          return 0;
        }
      '''
      self.do_run(src, '*51,87,78,550,100,78,550*')

  def test_isdigit_l(self):
      if self.emcc_args is None: return self.skip('no libcxx inclusion without emcc')

      src = '''
        #include <iostream>
        int main() {
          using namespace std;
          use_facet<num_put<char> >(cout.getloc()).put(cout, cout, '0', 3.14159265);
        }
      '''
      self.do_run(src, '3.14159')

  def test_iswdigit(self):
      if self.emcc_args is None: return self.skip('no libcxx inclusion without emcc')

      src = '''
        #include <stdio.h>
        #include <cctype>
        #include <cwctype>

        int main() {
          using namespace std;
          printf("%d ", isdigit('0'));
          printf("%d ", iswdigit(L'0'));
          return 0;
        }
      '''
      self.do_run(src, '1 1')

  def test_polymorph(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      src = '''
        #include <stdio.h>
        struct Pure {
          virtual int implme() = 0;
        };
        struct Parent : Pure {
          virtual int getit() { return 11; };
          int implme() { return 32; }
        };
        struct Child : Parent {
          int getit() { return 74; }
          int implme() { return 1012; }
        };

        struct Other {
          int one() { return 11; }
          int two() { return 22; }
        };

        int main()
        {
          Parent *x = new Parent();
          Parent *y = new Child();
          printf("*%d,%d,%d,%d*\\n", x->getit(), y->getit(), x->implme(), y->implme());

          Other *o = new Other;
          int (Other::*Ls)() = &Other::one;
          printf("*%d*\\n", (o->*(Ls))());
          Ls = &Other::two;
          printf("*%d*\\n", (o->*(Ls))());

          return 0;
        }
      '''
      self.do_run(src, '*11,74,32,1012*\n*11*\n*22*')

  def test_segfault(self):
    if self.emcc_args is None: return self.skip('SAFE_HEAP without ta2 means we check types too, which hide segfaults')
    if Settings.ASM_JS: return self.skip('asm does not support safe heap')

    Settings.SAFE_HEAP = 1

    for addr in ['0', 'new D2()']:
      print addr
      src = r'''
        #include <stdio.h>

        struct Classey {
          virtual void doIt() = 0;
        };

        struct D1 : Classey {
          virtual void doIt() { printf("fleefl\n"); }
        };

        struct D2 : Classey {
          virtual void doIt() { printf("marfoosh\n"); }
        };

        int main(int argc, char **argv)
        {
          Classey *p = argc == 100 ? new D1() : (Classey*)%s;

          p->doIt();

          return 0;
        }
      ''' % addr
      self.do_run(src, 'segmentation fault' if addr.isdigit() else 'marfoosh')

  def test_safe_dyncalls(self):
    if Settings.ASM_JS: return self.skip('asm does not support missing function stack traces')
    if Settings.SAFE_HEAP: return self.skip('safe heap warning will appear instead')
    if self.emcc_args is None: return self.skip('need libc')

    Settings.SAFE_DYNCALLS = 1

    for cond, body, work in [(True, True, False), (True, False, False), (False, True, True), (False, False, False)]:
      print cond, body, work
      src = r'''
        #include <stdio.h>

        struct Classey {
          virtual void doIt() = 0;
        };

        struct D1 : Classey {
          virtual void doIt() BODY;
        };

        int main(int argc, char **argv)
        {
          Classey *p = argc COND 100 ? new D1() : NULL;
          printf("%p\n", p);
          p->doIt();

          return 0;
        }
      '''.replace('COND', '==' if cond else '!=').replace('BODY', r'{ printf("all good\n"); }' if body else '')
      self.do_run(src, 'dyncall error: vi' if not work else 'all good')

  def test_dynamic_cast(self):
      if self.emcc_args is None: return self.skip('need libcxxabi')

      src = r'''
        #include <stdio.h>

        struct Support {
          virtual void f() {
            printf("f()\n");
          }
        };

        struct Derived : Support {
        };

        int main() {
          Support * p = new Derived;
          dynamic_cast<Derived*>(p)->f();
        }
      '''
      self.do_run(src, 'f()\n')

  def test_dynamic_cast_b(self):
      if self.emcc_args is None: return self.skip('need libcxxabi')

      src = '''
        #include <stdio.h>

        class CBase { virtual void dummy() {} };
        class CDerived : public CBase { int a; };
        class CDerivedest : public CDerived { float b; };

        int main ()
        {
          CBase *pa = new CBase;
          CBase *pb = new CDerived;
          CBase *pc = new CDerivedest;

          printf("a1: %d\\n", dynamic_cast<CDerivedest*>(pa) != NULL);
          printf("a2: %d\\n", dynamic_cast<CDerived*>(pa) != NULL);
          printf("a3: %d\\n", dynamic_cast<CBase*>(pa) != NULL);

          printf("b1: %d\\n", dynamic_cast<CDerivedest*>(pb) != NULL);
          printf("b2: %d\\n", dynamic_cast<CDerived*>(pb) != NULL);
          printf("b3: %d\\n", dynamic_cast<CBase*>(pb) != NULL);

          printf("c1: %d\\n", dynamic_cast<CDerivedest*>(pc) != NULL);
          printf("c2: %d\\n", dynamic_cast<CDerived*>(pc) != NULL);
          printf("c3: %d\\n", dynamic_cast<CBase*>(pc) != NULL);

          return 0;
        }
      '''
      self.do_run(src, 'a1: 0\na2: 0\na3: 1\nb1: 0\nb2: 1\nb3: 1\nc1: 1\nc2: 1\nc3: 1\n')

  def test_dynamic_cast_2(self):
    if self.emcc_args is None: return self.skip('need libcxxabi')

    src = r'''
      #include <stdio.h>
      #include <typeinfo>

      class Class {};

      int main() {
          const Class* dp = dynamic_cast<const Class*>(&typeid(Class));
          // should return dp == NULL,
          printf("pointer: %p\n", dp);
      }
      '''
    self.do_run(src, "pointer: (nil)")

  def test_funcptr(self):
      src = '''
        #include <stdio.h>
        int calc1() { return 26; }
        int calc2() { return 90; }
        typedef int (*fp_t)();

        fp_t globally1 = calc1;
        fp_t globally2 = calc2;

        int nothing(const char *str) { return 0; }

        int main()
        {
          fp_t fp = calc1;
          void *vp = (void*)fp;
          fp_t fpb = (fp_t)vp;
          fp_t fp2 = calc2;
          void *vp2 = (void*)fp2;
          fp_t fpb2 = (fp_t)vp2;
          printf("*%d,%d,%d,%d,%d,%d*\\n", fp(), fpb(), fp2(), fpb2(), globally1(), globally2());

          fp_t t = calc1;
          printf("*%d,%d", t == calc1, t == calc2);
          t = calc2;
          printf(",%d,%d*\\n", t == calc1, t == calc2);

          int (*other)(const char *str);
          other = nothing;
          other("*hello!*");
          other = puts;
          other("*goodbye!*");

          return 0;
        }
      '''
      self.do_run(src, '*26,26,90,90,26,90*\n*1,0,0,1*\n*goodbye!*')

  def test_mathfuncptr(self):
      src = '''
        #include <math.h>
        #include <stdio.h>

        int
        main(int argc, char **argv) {
         float (*fn)(float) =  argc != 12 ? &sqrtf : &fabsf;
         float (*fn2)(float) = argc != 13 ? &fabsf : &sqrtf;
         float (*fn3)(float) = argc != 14 ? &erff  : &fabsf;
         printf("fn2(-5) = %d, fn(10) = %.2f, erf(10) = %.2f\\n", (int)fn2(-5), fn(10), fn3(10));
         return 0;
        }
        '''
      self.do_run(src, 'fn2(-5) = 5, fn(10) = 3.16, erf(10) = 1.00')

  def test_funcptrfunc(self):
    src = r'''
      #include <stdio.h>

      typedef void (*funcptr)(int, int);
      typedef funcptr (*funcptrfunc)(int);

      funcptr __attribute__ ((noinline)) getIt(int x) {
        return (funcptr)x;
      }

      int main(int argc, char **argv)
      {
        funcptrfunc fpf = argc < 100 ? getIt : NULL;
        printf("*%p*\n", fpf(argc));
        return 0;
      }
    '''
    self.do_run(src, '*0x1*')

  def test_funcptr_namecollide(self):
    src = r'''
      #include <stdio.h>

      void do_call(void (*puts)(const char *), const char *str);

      void do_print(const char *str) {
        if (!str) do_call(NULL, "delusion");
        if ((int)str == -1) do_print(str+10);
        puts("====");
        puts(str);
        puts("====");
      }

      void do_call(void (*puts)(const char *), const char *str) {
        if (!str) do_print("confusion");
        if ((int)str == -1) do_call(NULL, str-10);
        (*puts)(str);
      }

      int main(int argc, char **argv)
      {
        for (int i = 0; i < argc; i++) {
          do_call(i != 10 ? do_print : NULL, i != 15 ? "waka waka" : NULL);
        }
        return 0;
      }
    '''
    self.do_run(src, 'waka', force_c=True)

  def test_emptyclass(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      src = '''
      #include <stdio.h>

      struct Randomized {
        Randomized(int x) {
          printf("*zzcheezzz*\\n");
        }
      };

      int main( int argc, const char *argv[] ) {
        new Randomized(55);

        return 0;
      }
      '''
      self.do_run(src, '*zzcheezzz*')

  def test_alloca(self):
    src = '''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        char *pc;
        pc = (char *)alloca(5);
        printf("z:%d*%d*\\n", pc > 0, (int)pc);
        return 0;
      }
    '''
    self.do_run(src, 'z:1*', force_c=True)

  def test_rename(self):
    src = open(path_from_root('tests', 'stdio', 'test_rename.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_alloca_stack(self):
    if self.emcc_args is None: return # too slow in other modes

    # We should not blow up the stack with numerous allocas
    src = '''
      #include <stdio.h>
      #include <stdlib.h>

      func(int i) {
        char *pc = (char *)alloca(100);
        *pc = i;
        (*pc)++;
        return (*pc) % 10;
      }
      int main() {
        int total = 0;
        for (int i = 0; i < 1024*1024; i++)
          total += func(i);
        printf("ok:%d*\\n", total);
        return 0;
      }
    '''
    self.do_run(src, 'ok:-32768*', force_c=True)

  def test_stack_byval(self):
    if self.emcc_args is None: return # too slow in other modes

    # We should also not blow up the stack with byval arguments
    src = r'''
      #include<stdio.h>
      struct vec {
        int x, y, z;
        vec(int x_, int y_, int z_) : x(x_), y(y_), z(z_) {}
        static vec add(vec a, vec b) {
          return vec(a.x+b.x, a.y+b.y, a.z+b.z);
        }
      };
      int main() {
        int total = 0;
        for (int i = 0; i < 1000; i++) {
          for (int j = 0; j < 1000; j++) {
            vec c(i+i%10, j*2, i%255);
            vec d(j*2, j%255, i%120);
            vec f = vec::add(c, d);
            total += (f.x + f.y + f.z) % 100;
            total %= 10240;
          }
        }
        printf("sum:%d*\n", total);
        return 0;
      }
    '''
    self.do_run(src, 'sum:9780*')

  def test_stack_varargs(self):
    if self.emcc_args is None: return # too slow in other modes

    Settings.INLINING_LIMIT = 50

    # We should not blow up the stack with numerous varargs
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      void func(int i) {
        printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
                 i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i);
      }
      int main() {
        for (int i = 0; i < 1024; i++)
          func(i);
        printf("ok!\n");
        return 0;
      }
    '''
    Settings.TOTAL_STACK = 1024
    self.do_run(src, 'ok!')

  def test_stack_varargs2(self):
    if self.emcc_args is None: return # too slow in other modes
    Settings.TOTAL_STACK = 1024
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      void func(int i) {
      }
      int main() {
        for (int i = 0; i < 1024; i++) {
          printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
                   i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i);
        }
        printf("ok!\n");
        return 0;
      }
    '''
    self.do_run(src, 'ok!')

    print 'with return'

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        for (int i = 0; i < 1024; i++) {
          int j = printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
                   i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i);
          printf(" (%d)\n", j);
        }
        printf("ok!\n");
        return 0;
      }
    '''
    self.do_run(src, 'ok!')

    print 'with definitely no return'

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>
      #include <stdarg.h>

      void vary(const char *s, ...)
      {
        va_list v;
        va_start(v, s);
        char d[20];
        vsnprintf(d, 20, s, v);
        puts(d);

        // Try it with copying
        va_list tempva;
        va_copy(tempva, v);
        vsnprintf(d, 20, s, tempva);
        puts(d);

        va_end(v);
      }

      int main() {
        for (int i = 0; i < 1024; i++) {
          int j = printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
                   i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i);
          printf(" (%d)\n", j);
          vary("*cheez: %d+%d*", 99, 24);
          vary("*albeit*");
        }
        printf("ok!\n");
        return 0;
      }
    '''
    self.do_run(src, 'ok!')

  def test_stack_void(self):
    Settings.INLINING_LIMIT = 50

    src = r'''
      #include <stdio.h>

      static char s[100]="aaaaa";
      static int func(void) {
        if(s[0]!='a') return 0;
        printf("iso open %s\n", s, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001, 1.001);
        return 0;
      }
      int main(){
        int i;
        for(i=0;i<5000;i++)
          func();
        printf(".ok.\n");
      }
    '''
    self.do_run(src, '.ok.\n')

  def test_life(self):
    if self.emcc_args is None: return self.skip('need c99')
    self.emcc_args += ['-std=c99']
    src = open(path_from_root('tests', 'life.c'), 'r').read()
    self.do_run(src, '''--------------------------------
[]                                    []                  [][][]
                    []  []    []    [][]  []            []  []  
[]                [][]  [][]              [][][]      []        
                  []    []      []      []  [][]    []        []
                  []  [][]    []        []    []  []    [][][][]
                    [][]      [][]  []    [][][]  []        []  
                                []  [][]  [][]    [][]  [][][]  
                                    [][]          [][][]  []  []
                                    [][]              [][]    []
                                                          [][][]
                                                            []  
                                                                
                                                                
                                                                
                                                                
                                        [][][]                  
                                      []      [][]      [][]    
                                      [][]      []  [][]  [][]  
                                                    [][]  [][]  
                                                      []        
                  [][]                                          
                  [][]                                        []
[]                                                      [][]  []
                                                  [][][]      []
                                                []      [][]    
[]                                                    []      []
                                                          []    
[]                                                        []  []
                                              [][][]            
                                                                
                                  []                            
                              [][][]                          []
--------------------------------
''', ['2'], force_c=True)

  def test_array2(self):
      src = '''
        #include <stdio.h>

        static const double grid[4][2] = {
         {-3/3.,-1/3.},{+1/3.,-3/3.},
         {-1/3.,+3/3.},{+3/3.,+1/3.}
        };

        int main() {
          for (int i = 0; i < 4; i++)
            printf("%d:%.2f,%.2f ", i, grid[i][0], grid[i][1]);
          printf("\\n");
          return 0;
        }
        '''
      self.do_run(src, '0:-1.00,-0.33 1:0.33,-1.00 2:-0.33,1.00 3:1.00,0.33')

  def test_array2b(self):
      src = '''
        #include <stdio.h>

        static const struct {
          unsigned char left;
          unsigned char right;
        } prioritah[] = {
           {6, 6}, {6, 6}, {7, 95}, {7, 7}
        };

        int main() {
          printf("*%d,%d\\n", prioritah[1].left, prioritah[1].right);
          printf("%d,%d*\\n", prioritah[2].left, prioritah[2].right);
          return 0;
        }
        '''
      self.do_run(src, '*6,6\n7,95*')


  def test_constglobalstructs(self):
      src = '''
        #include <stdio.h>
        struct IUB {
           int c;
           double p;
           unsigned int pi;
        };

        IUB iub[] = {
           { 'a', 0.27, 5 },
           { 'c', 0.15, 4 },
           { 'g', 0.12, 3 },
           { 't', 0.27, 2 },
        };

        const unsigned char faceedgesidx[6][4] =
        {
            { 4, 5, 8, 10 },
            { 6, 7, 9, 11 },
            { 0, 2, 8, 9  },
            { 1, 3, 10,11 },
            { 0, 1, 4, 6 },
            { 2, 3, 5, 7 },
        };

        int main( int argc, const char *argv[] ) {
           printf("*%d,%d,%d,%d*\\n", iub[0].c, int(iub[1].p*100), iub[2].pi, faceedgesidx[3][2]);
           return 0;
        }
        '''
      self.do_run(src, '*97,15,3,10*')

  def test_conststructs(self):
      src = '''
        #include <stdio.h>
        struct IUB {
           int c;
           double p;
           unsigned int pi;
        };

        int main( int argc, const char *argv[] ) {
           int before = 70;
           IUB iub[] = {
              { 'a', 0.3029549426680, 5 },
              { 'c', 0.15, 4 },
              { 'g', 0.12, 3 },
              { 't', 0.27, 2 },
           };
           int after = 90;
           printf("*%d,%d,%d,%d,%d,%d*\\n", before, iub[0].c, int(iub[1].p*100), iub[2].pi, int(iub[0].p*10000), after);
           return 0;
        }
        '''
      self.do_run(src, '*70,97,15,3,3029,90*')

  def test_bigarray(self):
    if self.emcc_args is None: return self.skip('need ta2 to compress type data on zeroinitializers')

    # avoid "array initializer too large" errors
    src = r'''
      #include <stdio.h>
      #include <assert.h>

      #define SIZE (1024*100)
      struct Struct {
        char x;
        int y;
      };
      Struct buffy[SIZE];

      int main() {
        for (int i = 0; i < SIZE; i++) { assert(buffy[i].x == 0 && buffy[i].y == 0); } // we were zeroinitialized
        for (int i = 0; i < SIZE; i++) { buffy[i].x = i*i; buffy[i].y = i*i*i; } // we can save data
        printf("*%d*\n", buffy[SIZE/3].x);
        return 0;
      }
      '''
    self.do_run(src, '*57*')

  def test_mod_globalstruct(self):
      src = '''
        #include <stdio.h>

        struct malloc_params {
          size_t magic, page_size;
        };

        malloc_params mparams;

        #define SIZE_T_ONE ((size_t)1)
        #define page_align(S) (((S) + (mparams.page_size - SIZE_T_ONE)) & ~(mparams.page_size - SIZE_T_ONE))

        int main()
        {
          mparams.page_size = 4096;
          printf("*%d,%d,%d,%d*\\n", mparams.page_size, page_align(1000), page_align(6000), page_align(66474));
          return 0;
        }
      '''
      self.do_run(src, '*4096,4096,8192,69632*')

  def test_pystruct(self):
      src = '''
        #include <stdio.h>

        // Based on CPython code
        union PyGC_Head {
            struct {
                union PyGC_Head *gc_next;
                union PyGC_Head *gc_prev;
                size_t gc_refs;
            } gc;
            long double dummy;  /* force worst-case alignment */
        } ;

        struct gc_generation {
            PyGC_Head head;
            int threshold; /* collection threshold */
            int count; /* count of allocations or collections of younger
                          generations */
        };

        #define NUM_GENERATIONS 3
        #define GEN_HEAD(n) (&generations[n].head)

        /* linked lists of container objects */
        static struct gc_generation generations[NUM_GENERATIONS] = {
            /* PyGC_Head,                               threshold,      count */
            {{{GEN_HEAD(0), GEN_HEAD(0), 0}},           700,            0},
            {{{GEN_HEAD(1), GEN_HEAD(1), 0}},           10,             0},
            {{{GEN_HEAD(2), GEN_HEAD(2), 0}},           10,             0},
        };

        int main()
        {
          gc_generation *n = NULL;
          printf("*%d,%d,%d,%d,%d,%d,%d,%d*\\n",
            (int)(&n[0]),
            (int)(&n[0].head),
            (int)(&n[0].head.gc.gc_next),
            (int)(&n[0].head.gc.gc_prev),
            (int)(&n[0].head.gc.gc_refs),
            (int)(&n[0].threshold), (int)(&n[0].count), (int)(&n[1])
          );
          printf("*%d,%d,%d*\\n",
            (int)(&generations[0]) ==
            (int)(&generations[0].head.gc.gc_next),
            (int)(&generations[0]) ==
            (int)(&generations[0].head.gc.gc_prev),
            (int)(&generations[0]) ==
            (int)(&generations[1])
          );
          int x1 = (int)(&generations[0]);
          int x2 = (int)(&generations[1]);
          printf("*%d*\\n", x1 == x2);
          for (int i = 0; i < NUM_GENERATIONS; i++) {
            PyGC_Head *list = GEN_HEAD(i);
            printf("%d:%d,%d\\n", i, (int)list == (int)(list->gc.gc_prev), (int)list ==(int)(list->gc.gc_next));
          }
          printf("*%d,%d,%d*\\n", sizeof(PyGC_Head), sizeof(gc_generation), int(GEN_HEAD(2)) - int(GEN_HEAD(1)));
        }
      '''
      if Settings.QUANTUM_SIZE == 1:
        # Compressed memory. Note that sizeof() does give the fat sizes, however!
        self.do_run(src, '*0,0,0,1,2,3,4,5*\n*1,0,0*\n*0*\n0:1,1\n1:1,1\n2:1,1\n*12,20,5*')
      else:
        if self.is_le32():
          self.do_run(src, '*0,0,0,4,8,16,20,24*\n*1,0,0*\n*0*\n0:1,1\n1:1,1\n2:1,1\n*16,24,24*')
        else:
          self.do_run(src, '*0,0,0,4,8,12,16,20*\n*1,0,0*\n*0*\n0:1,1\n1:1,1\n2:1,1\n*12,20,20*')

  def test_ptrtoint(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      src = '''
        #include <stdio.h>

        int main( int argc, const char *argv[] ) {
          char *a = new char[10];
          char *a0 = a+0;
          char *a5 = a+5;
          int *b = new int[10];
          int *b0 = b+0;
          int *b5 = b+5;
          int c = (int)b5-(int)b0; // Emscripten should warn!
          int d = (int)b5-(int)b0; // Emscripten should warn!
          printf("*%d*\\n", (int)a5-(int)a0);
          return 0;
        }
        '''
      runner = self
      def check_warnings(output):
          runner.assertEquals(filter(lambda line: 'Warning' in line, output.split('\n')).__len__(), 4)
      self.do_run(src, '*5*', output_processor=check_warnings)

  def test_sizeof(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      # Has invalid writes between printouts
      Settings.SAFE_HEAP = 0

      src = '''
        #include <stdio.h>
        #include <string.h>
        #include "emscripten.h"

        struct A { int x, y; };

        int main( int argc, const char *argv[] ) {
          int *a = new int[10];
          int *b = new int[1];
          int *c = new int[10];
          for (int i = 0; i < 10; i++)
            a[i] = 2;
          *b = 5;
          for (int i = 0; i < 10; i++)
            c[i] = 8;
          printf("*%d,%d,%d,%d,%d*\\n", a[0], a[9], *b, c[0], c[9]);
          // Should overwrite a, but not touch b!
          memcpy(a, c, 10*sizeof(int));
          printf("*%d,%d,%d,%d,%d*\\n", a[0], a[9], *b, c[0], c[9]);

          // Part 2
          A as[3] = { { 5, 12 }, { 6, 990 }, { 7, 2 } };
          memcpy(&as[0], &as[2], sizeof(A));

          printf("*%d,%d,%d,%d,%d,%d*\\n", as[0].x, as[0].y, as[1].x, as[1].y, as[2].x, as[2].y);
          return 0;
        }
        '''
      self.do_run(src, '*2,2,5,8,8***8,8,5,8,8***7,2,6,990,7,2*', [], lambda x, err: x.replace('\n', '*'))

  def test_float_h(self):
    process = Popen([PYTHON, EMCC, path_from_root('tests', 'float+.c')], stdout=PIPE, stderr=PIPE)
    process.communicate()
    assert process.returncode is 0, 'float.h should agree with our system'

  def test_emscripten_api(self):
      #if Settings.MICRO_OPTS or Settings.RELOOP or Building.LLVM_OPTS: return self.skip('FIXME')

      src = r'''
        #include <stdio.h>
        #include "emscripten.h"

        extern "C" {
          void save_me_aimee() { printf("mann\n"); }
        }

        int main() {
          // EMSCRIPTEN_COMMENT("hello from the source");
          emscripten_run_script("Module.print('hello world' + '!')");
          printf("*%d*\n", emscripten_run_script_int("5*20"));
          printf("*%s*\n", emscripten_run_script_string("'five'+'six'"));
          emscripten_run_script("Module['_save_me_aimee']()");
          return 0;
        }
        '''

      check = '''
def process(filename):
  src = open(filename, 'r').read()
  # TODO: restore this (see comment in emscripten.h) assert '// hello from the source' in src
'''
      Settings.EXPORTED_FUNCTIONS = ['_main', '_save_me_aimee']
      self.do_run(src, 'hello world!\n*100*\n*fivesix*\nmann\n', post_build=check)

      # test EXPORT_ALL
      Settings.EXPORTED_FUNCTIONS = []
      Settings.EXPORT_ALL = 1
      self.do_run(src, 'hello world!\n*100*\n*fivesix*\nmann\n', post_build=check)

  def test_emscripten_get_now(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2')

      if self.run_name == 'o2':
        self.emcc_args += ['--closure', '1'] # Use closure here for some additional coverage
      self.do_run(open(path_from_root('tests', 'emscripten_get_now.cpp')).read(), 'Timer resolution is good.')

  def test_inlinejs(self):
      if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code
      if not self.is_le32(): return self.skip('le32 needed for inline js')
      src = r'''
        #include <stdio.h>

        double get() {
          double ret = 0;
          __asm __volatile__("Math.abs(-12/3.3)":"=r"(ret)); // write to a variable
          return ret;
        }

        int main() {
          asm("Module.print('Inline JS is very cool')");
          printf("%.2f\n", get());

          // Test that passing multiple input and output variables works.
          int src1 = 1, src2 = 2, src3 = 3;
          int dst1 = 0, dst2 = 0, dst3 = 0;
          // TODO asm("Module.print(%3); Module.print(%4); Module.print(%5); %0 = %3; %1 = %4; %2 = %5;" : "=r"(dst1),"=r"(dst2),"=r"(dst3): "r"(src1),"r"(src2),"r"(src3));
          // TODO printf("%d\n%d\n%d\n", dst1, dst2, dst3);

          return 0;
        }
        '''

      self.do_run(src, 'Inline JS is very cool\n3.64\n') # TODO 1\n2\n3\n1\n2\n3\n')

  def test_inlinejs2(self):
      if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code
      if not self.is_le32(): return self.skip('le32 needed for inline js')
      src = r'''
        #include <stdio.h>

        int mix(int x, int y) {
          int ret;
          asm("Math.pow(2, %0+%1+1)" : "=r"(ret) : "r"(x), "r"(y)); // read and write
          return ret;
        }

        void mult() {
          asm("var $_$1 = Math.abs(-100); $_$1 *= 2; Module.print($_$1)"); // multiline
          asm __volatile__("Module.print('done')");
        }

        int main(int argc, char **argv) {
          printf("%d\n", mix(argc, argc/2));
          mult();
          return 0;
        }
        '''

      self.do_run(src, '4\n200\ndone\n')

  def test_inlinejs3(self):
    src = r'''
      #include <stdio.h>
      #include <emscripten.h>

      int main() {
        EM_ASM(Module.print('hello dere1'));
        EM_ASM(
          Module.print('hello dere2');
        );
        for (int i = 0; i < 3; i++) {
          EM_ASM(
            Module.print('hello dere3');
            Module.print('hello dere' + 4);
          );
        }
        return 0;
      }
      '''

    self.do_run(src, 'hello dere1\nhello dere2\nhello dere3\nhello dere4\nhello dere3\nhello dere4\nhello dere3\nhello dere4\n')

  def test_memorygrowth(self):
    if Settings.USE_TYPED_ARRAYS == 0: return self.skip('memory growth is only supported with typed arrays')
    if Settings.ASM_JS: return self.skip('asm does not support memory growth yet')

    # With typed arrays in particular, it is dangerous to use more memory than TOTAL_MEMORY,
    # since we then need to enlarge the heap(s).
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <assert.h>
      #include "emscripten.h"

      int main(int argc, char **argv)
      {
        char *buf1 = (char*)malloc(100);
        char *data1 = "hello";
        memcpy(buf1, data1, strlen(data1)+1);

        float *buf2 = (float*)malloc(100);
        float pie = 4.955;
        memcpy(buf2, &pie, sizeof(float));

        printf("*pre: %s,%.3f*\n", buf1, buf2[0]);

        int totalMemory = emscripten_run_script_int("TOTAL_MEMORY");
        char *buf3 = (char*)malloc(totalMemory+1);
        buf3[argc] = (int)buf2;
        if (argc % 7 == 6) printf("%d\n", memcpy(buf3, buf1, argc));
        char *buf4 = (char*)malloc(100);
        float *buf5 = (float*)malloc(100);
        //printf("totalMemory: %d bufs: %d,%d,%d,%d,%d\n", totalMemory, buf1, buf2, buf3, buf4, buf5);
        assert((int)buf4 > (int)totalMemory && (int)buf5 > (int)totalMemory);

        printf("*%s,%.3f*\n", buf1, buf2[0]); // the old heap data should still be there

        memcpy(buf4, buf1, strlen(data1)+1);
        memcpy(buf5, buf2, sizeof(float));
        printf("*%s,%.3f*\n", buf4, buf5[0]); // and the new heap space should work too

        return 0;
      }
    '''

    # Fail without memory growth
    self.do_run(src, 'Cannot enlarge memory arrays.')
    fail = open('src.cpp.o.js').read()

    # Win with it
    Settings.ALLOW_MEMORY_GROWTH = 1
    self.do_run(src, '*pre: hello,4.955*\n*hello,4.955*\n*hello,4.955*')
    win = open('src.cpp.o.js').read()

    if self.emcc_args and '-O2' in self.emcc_args:
      # Make sure ALLOW_MEMORY_GROWTH generates different code (should be less optimized)
      code_start = 'var TOTAL_MEMORY = '
      fail = fail[fail.find(code_start):]
      win = win[win.find(code_start):]
      assert len(fail) < len(win), 'failing code - without memory growth on - is more optimized, and smaller'

  def test_ssr(self): # struct self-ref
      src = '''
        #include <stdio.h>

        // see related things in openjpeg
        typedef struct opj_mqc_state {
          unsigned int qeval;
          int mps;
          struct opj_mqc_state *nmps;
          struct opj_mqc_state *nlps;
        } opj_mqc_state_t;

        static opj_mqc_state_t mqc_states[2] = {
          {0x5600, 0, &mqc_states[2], &mqc_states[3]},
          {0x5602, 1, &mqc_states[3], &mqc_states[2]},
        };

        int main() {
          printf("*%d*\\n", (int)(mqc_states+1)-(int)mqc_states);
          for (int i = 0; i < 2; i++)
            printf("%d:%d,%d,%d,%d\\n", i, mqc_states[i].qeval, mqc_states[i].mps,
                   (int)mqc_states[i].nmps-(int)mqc_states, (int)mqc_states[i].nlps-(int)mqc_states);
          return 0;
        }
        '''
      if Settings.QUANTUM_SIZE == 1:
        self.do_run(src, '''*4*\n0:22016,0,8,12\n1:22018,1,12,8\n''')
      else:
        self.do_run(src, '''*16*\n0:22016,0,32,48\n1:22018,1,48,32\n''')

  def test_tinyfuncstr(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      src = '''
        #include <stdio.h>

        struct Class {
          static char *name1() { return "nameA"; }
                 char *name2() { return "nameB"; }
        };

        int main() {
          printf("*%s,%s*\\n", Class::name1(), (new Class())->name2());
          return 0;
        }
        '''
      self.do_run(src, '*nameA,nameB*')

  def test_llvmswitch(self):
      Settings.CORRECT_SIGNS = 1

      src = '''
        #include <stdio.h>
        #include <string.h>

        int switcher(int p)
        {
          switch(p) {
            case 'a':
            case 'b':
            case 'c':
                return p-1;
            case -15:
                return p+1;
          }
          return p;
        }

        int main( int argc, const char *argv[] ) {
          unsigned int x = 0xfffffff1;
          x >>= (argc-1); // force it to be unsigned for purpose of checking our switch comparison in signed/unsigned
          printf("*%d,%d,%d,%d,%d,%d*\\n", switcher('a'), switcher('b'), switcher('c'), switcher(x), switcher(-15), switcher('e'));
          return 0;
        }
        '''
      self.do_run(src, '*96,97,98,-14,-14,101*')

  # By default, when user has not specified a -std flag, Emscripten should always build .cpp files using the C++03 standard,
  # i.e. as if "-std=c++03" had been passed on the command line. On Linux with Clang 3.2 this is the case, but on Windows
  # with Clang 3.2 -std=c++11 has been chosen as default, because of
  # < jrose> clb: it's deliberate, with the idea that for people who don't care about the standard, they should be using the "best" thing we can offer on that platform
  def test_cxx03_do_run(self):
      src = '''
        #include <stdio.h>

        #if __cplusplus != 199711L
        #error By default, if no -std is specified, emscripten should be compiling with -std=c++03!
        #endif

        int main( int argc, const char *argv[] ) {
          printf("Hello world!\\n");
          return 0;
        }
        '''
      self.do_run(src, 'Hello world!')

  def test_bigswitch(self):
    if self.run_name != 'default': return self.skip('TODO: issue #781')

    src = open(path_from_root('tests', 'bigswitch.cpp')).read()
    self.do_run(src, '''34962: GL_ARRAY_BUFFER (0x8892)
26214: what?
35040: GL_STREAM_DRAW (0x88E0)
''', args=['34962', '26214', '35040'])

  def test_indirectbr(self):
      Building.COMPILER_TEST_OPTS = filter(lambda x: x != '-g', Building.COMPILER_TEST_OPTS)

      src = '''
        #include <stdio.h>
        int main(void) {
          const void *addrs[2] = { &&FOO, &&BAR };

          // confuse the optimizer so it doesn't hardcode the jump and avoid generating an |indirectbr| instruction
          int which = 0;
          for (int x = 0; x < 1000; x++) which = (which + x*x) % 7;
          which = (which % 2) + 1;

          goto *addrs[which];

        FOO:
          printf("bad\\n");
          return 0;
        BAR:
          printf("good\\n");
          const void *addr = &&FOO;
          goto *addr;
        }
        '''
      self.do_run(src, 'good\nbad')

  def test_indirectbr_many(self):
      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('blockaddr > 255 requires ta2')

      blocks = range(1500)
      init = ', '.join(['&&B%d' % b for b in blocks])
      defs = '\n'.join(['B%d: printf("%d\\n"); return 0;' % (b,b) for b in blocks])
      src = '''
        #include <stdio.h>
        int main(int argc, char **argv) {
          printf("\\n");
          const void *addrs[] = { %s };
          goto *addrs[argc*argc + 1000];

%s
          return 0;
        }
        ''' % (init, defs)
      self.do_run(src, '\n1001\n')

  def test_pack(self):
      src = '''
        #include <stdio.h>
        #include <string.h>

        #pragma pack(push,1)
        typedef struct header
        {
            unsigned char  id;
            unsigned short colour;
            unsigned char  desc;
        } header;
        #pragma pack(pop)

        typedef struct fatheader
        {
            unsigned char  id;
            unsigned short colour;
            unsigned char  desc;
        } fatheader;

        int main( int argc, const char *argv[] ) {
          header h, *ph = 0;
          fatheader fh, *pfh = 0;
          printf("*%d,%d,%d*\\n", sizeof(header), (int)((int)&h.desc - (int)&h.id), (int)(&ph[1])-(int)(&ph[0]));
          printf("*%d,%d,%d*\\n", sizeof(fatheader), (int)((int)&fh.desc - (int)&fh.id), (int)(&pfh[1])-(int)(&pfh[0]));
          return 0;
        }
        '''
      if Settings.QUANTUM_SIZE == 1:
        self.do_run(src, '*4,2,3*\n*6,2,3*')
      else:
        self.do_run(src, '*4,3,4*\n*6,4,6*')

  def test_varargs(self):
      if Settings.QUANTUM_SIZE == 1: return self.skip('FIXME: Add support for this')
      if not self.is_le32(): return self.skip('we do not support all varargs stuff without le32')

      src = '''
        #include <stdio.h>
        #include <stdarg.h>

        void vary(const char *s, ...)
        {
          va_list v;
          va_start(v, s);
          char d[20];
          vsnprintf(d, 20, s, v);
          puts(d);

          // Try it with copying
          va_list tempva;
          va_copy(tempva, v);
          vsnprintf(d, 20, s, tempva);
          puts(d);

          va_end(v);
        }

        void vary2(char color, const char *s, ...)
        {
          va_list v;
          va_start(v, s);
          char d[21];
          d[0] = color;
          vsnprintf(d+1, 20, s, v);
          puts(d);
          va_end(v);
        }
        
        void varargs_listoffsets_list_evaluate(int count, va_list ap, int vaIteration)
        {
          while(count > 0)
          {
              const char* string = va_arg(ap, const char*);
              printf("%s", string);
              count--;
          }
          printf("\\n");
        }
          
        void varags_listoffsets_list_copy(int count, va_list ap, int iteration)
        {
          va_list ap_copy;
          va_copy(ap_copy, ap);
          varargs_listoffsets_list_evaluate(count, ap_copy, iteration);
          va_end(ap_copy);
        }
          
        void varargs_listoffsets_args(int type, int count, ...)
        {
          va_list ap;
          va_start(ap, count);
          
          // evaluate a copied list
          varags_listoffsets_list_copy(count, ap, 1);
          varags_listoffsets_list_copy(count, ap, 2);
          varags_listoffsets_list_copy(count, ap, 3);
          varags_listoffsets_list_copy(count, ap, 4);
          
          varargs_listoffsets_list_evaluate(count, ap, 1);
          
          // NOTE: we expect this test to fail, so we will check the stdout for <BAD+0><BAD+1>.....
          varargs_listoffsets_list_evaluate(count, ap, 2);
          
          // NOTE: this test has to work again, as we restart the list
          va_end(ap);
          va_start(ap, count);
          varargs_listoffsets_list_evaluate(count, ap, 3);
          va_end(ap);
        }
        
        void varargs_listoffsets_main()
        {
          varargs_listoffsets_args(0, 5, "abc", "def", "ghi", "jkl", "mno", "<BAD+0>", "<BAD+1>", "<BAD+2>", "<BAD+3>", "<BAD+4>", "<BAD+5>", "<BAD+6>", "<BAD+7>", "<BAD+8>", "<BAD+9>", "<BAD+10>", "<BAD+11>", "<BAD+12>", "<BAD+13>", "<BAD+14>", "<BAD+15>", "<BAD+16>");
        }

        #define GETMAX(pref, type) \
          type getMax##pref(int num, ...) \
          { \
            va_list vv; \
            va_start(vv, num); \
            type maxx = va_arg(vv, type); \
            for (int i = 1; i < num; i++) \
            { \
              type curr = va_arg(vv, type); \
              maxx = curr > maxx ? curr : maxx; \
            } \
            va_end(vv); \
            return maxx; \
          }
        GETMAX(i, int);
        GETMAX(D, double);

        int main(int argc, char **argv) {
          vary("*cheez: %d+%d*", 0, 24); // Also tests that '0' is not special as an array ender
          vary("*albeit*"); // Should not fail with no var args in vararg function
          vary2('Q', "%d*", 85);

          int maxxi = getMaxi(6,        2, 5, 21, 4, -10, 19);
          printf("maxxi:%d*\\n", maxxi);
          double maxxD = getMaxD(6,        (double)2.1, (double)5.1, (double)22.1, (double)4.1, (double)-10.1, (double)19.1, (double)2);
          printf("maxxD:%.2f*\\n", (float)maxxD);

          // And, as a function pointer
          void (*vfp)(const char *s, ...) = argc == 1211 ? NULL : vary;
          vfp("*vfp:%d,%d*", 22, 199);

          // ensure lists work properly when copied, reinited etc.
          varargs_listoffsets_main();

          return 0;
        }
        '''
      self.do_run(src, '*cheez: 0+24*\n*cheez: 0+24*\n*albeit*\n*albeit*\nQ85*\nmaxxi:21*\nmaxxD:22.10*\n*vfp:22,199*\n*vfp:22,199*\n'+
      'abcdefghijklmno\nabcdefghijklmno\nabcdefghijklmno\nabcdefghijklmno\nabcdefghijklmno\n<BAD+0><BAD+1><BAD+2><BAD+3><BAD+4>\nabcdefghijklmno\n')

  def test_varargs_byval(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('FIXME: Add support for this')
    if self.is_le32(): return self.skip('clang cannot compile this code with that target yet')

    src = r'''
      #include <stdio.h>
      #include <stdarg.h>

      typedef struct type_a {
        union {
          double f;
          void *p;
          int i;
          short sym;
        } value;
      } type_a;

      enum mrb_vtype {
        MRB_TT_FALSE = 0,   /*   0 */
        MRB_TT_CLASS = 9    /*   9 */
      };

      typedef struct type_b {
        enum mrb_vtype tt:8;
      } type_b;

      void print_type_a(int argc, ...);
      void print_type_b(int argc, ...);

      int main(int argc, char *argv[])
      {
        type_a a;
        type_b b;
        a.value.p = (void*) 0x12345678;
        b.tt = MRB_TT_CLASS;

        printf("The original address of a is: %p\n", a.value.p);
        printf("The original type of b is: %d\n", b.tt);

        print_type_a(1, a);
        print_type_b(1, b);

        return 0;
      }

      void print_type_a(int argc, ...) {
        va_list ap;
        type_a a;

        va_start(ap, argc);
        a = va_arg(ap, type_a);
        va_end(ap);

        printf("The current address of a is: %p\n", a.value.p);
      }

      void print_type_b(int argc, ...) {
        va_list ap;
        type_b b;

        va_start(ap, argc);
        b = va_arg(ap, type_b);
        va_end(ap);

        printf("The current type of b is: %d\n", b.tt);
      }
      '''
    self.do_run(src, '''The original address of a is: 0x12345678
The original type of b is: 9
The current address of a is: 0x12345678
The current type of b is: 9
''')

  def test_functionpointer_libfunc_varargs(self):
    src = r'''
      #include <stdio.h>
      #include <fcntl.h>
      typedef int (*fp_t)(int, int, ...);
      int main(int argc, char **argv) {
        fp_t fp = &fcntl;
        if (argc == 1337) fp = (fp_t)&main;
        (*fp)(0, 10);
        (*fp)(0, 10, 5);
        printf("waka\n");
        return 0;
      }
    '''
    self.do_run(src, '''waka''')

  def test_structbyval(self):
      Settings.INLINING_LIMIT = 50

      # part 1: make sure that normally, passing structs by value works

      src = r'''
        #include <stdio.h>

        struct point
        {
          int x, y;
        };

        void dump(struct point p) {
          p.x++; // should not modify
          p.y++; // anything in the caller!
          printf("dump: %d,%d\n", p.x, p.y);
        }

        void dumpmod(struct point *p) {
          p->x++; // should not modify
          p->y++; // anything in the caller!
          printf("dump: %d,%d\n", p->x, p->y);
        }

        int main( int argc, const char *argv[] ) {
          point p = { 54, 2 };
          printf("pre:  %d,%d\n", p.x, p.y);
          dump(p);
          void (*dp)(point p) = dump; // And, as a function pointer
          dp(p);
          printf("post: %d,%d\n", p.x, p.y);
          dumpmod(&p);
          dumpmod(&p);
          printf("last: %d,%d\n", p.x, p.y);
          return 0;
        }
      '''
      self.do_run(src, 'pre:  54,2\ndump: 55,3\ndump: 55,3\npost: 54,2\ndump: 55,3\ndump: 56,4\nlast: 56,4')

      # Check for lack of warning in the generated code (they should appear in part 2)
      generated = open(os.path.join(self.get_dir(), 'src.cpp.o.js')).read()
      assert 'Casting a function pointer type to another with a different number of arguments.' not in generated, 'Unexpected warning'

      # part 2: make sure we warn about mixing c and c++ calling conventions here

      if not (self.emcc_args is None or self.emcc_args == []): return # Optimized code is missing the warning comments

      header = r'''
        struct point
        {
          int x, y;
        };

      '''
      open(os.path.join(self.get_dir(), 'header.h'), 'w').write(header)

      supp = r'''
        #include <stdio.h>
        #include "header.h"

        void dump(struct point p) {
          p.x++; // should not modify
          p.y++; // anything in the caller!
          printf("dump: %d,%d\n", p.x, p.y);
        }
      '''
      supp_name = os.path.join(self.get_dir(), 'supp.c')
      open(supp_name, 'w').write(supp)

      main = r'''
        #include <stdio.h>
        #include "header.h"

        #ifdef __cplusplus
        extern "C" {
        #endif
          void dump(struct point p);
        #ifdef __cplusplus
        }
        #endif

        int main( int argc, const char *argv[] ) {
          struct point p = { 54, 2 };
          printf("pre:  %d,%d\n", p.x, p.y);
          dump(p);
          void (*dp)(struct point p) = dump; // And, as a function pointer
          dp(p);
          printf("post: %d,%d\n", p.x, p.y);
          return 0;
        }
      '''
      main_name = os.path.join(self.get_dir(), 'main.cpp')
      open(main_name, 'w').write(main)

      Building.emcc(supp_name)
      Building.emcc(main_name)
      all_name = os.path.join(self.get_dir(), 'all.bc')
      Building.link([supp_name + '.o', main_name + '.o'], all_name)

      # This will fail! See explanation near the warning we check for, in the compiler source code
      output = Popen([PYTHON, EMCC, all_name], stderr=PIPE).communicate()

      # Check for warning in the generated code
      generated = open(os.path.join(self.get_dir(), 'src.cpp.o.js')).read()
      if 'i386-pc-linux-gnu' in COMPILER_OPTS:
        assert 'Casting a function pointer type to a potentially incompatible one' in output[1], 'Missing expected warning'
      else:
        print >> sys.stderr, 'skipping C/C++ conventions warning check, since not i386-pc-linux-gnu'

  def test_stdlibs(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      if Settings.USE_TYPED_ARRAYS == 2:
          # Typed arrays = 2 + safe heap prints a warning that messes up our output.
          Settings.SAFE_HEAP = 0
      src = '''
        #include <stdio.h>
        #include <stdlib.h>
        #include <sys/time.h>

        void clean()
        {
          printf("*cleaned*\\n");
        }

        int comparer(const void *a, const void *b) {
          int aa = *((int*)a);
          int bb = *((int*)b);
          return aa - bb;
        }

        int main() {
          // timeofday
          timeval t;
          gettimeofday(&t, NULL);
          printf("*%d,%d\\n", int(t.tv_sec), int(t.tv_usec)); // should not crash

          // atexit
          atexit(clean);

          // qsort
          int values[6] = { 3, 2, 5, 1, 5, 6 };
          qsort(values, 5, sizeof(int), comparer);
          printf("*%d,%d,%d,%d,%d,%d*\\n", values[0], values[1], values[2], values[3], values[4], values[5]);

          printf("*stdin==0:%d*\\n", stdin == 0); // check that external values are at least not NULL
          printf("*%%*\\n");
          printf("*%.1ld*\\n", 5);

          printf("*%.1f*\\n", strtod("66", NULL)); // checks dependency system, as our strtod needs _isspace etc.

          printf("*%ld*\\n", strtol("10", NULL, 0));
          printf("*%ld*\\n", strtol("0", NULL, 0));
          printf("*%ld*\\n", strtol("-10", NULL, 0));
          printf("*%ld*\\n", strtol("12", NULL, 16));

          printf("*%lu*\\n", strtoul("10", NULL, 0));
          printf("*%lu*\\n", strtoul("0", NULL, 0));
          printf("*%lu*\\n", strtoul("-10", NULL, 0));

          printf("*malloc(0)!=0:%d*\\n", malloc(0) != 0); // We should not fail horribly

          return 0;
        }
        '''

      self.do_run(src, '*1,2,3,5,5,6*\n*stdin==0:0*\n*%*\n*5*\n*66.0*\n*10*\n*0*\n*-10*\n*18*\n*10*\n*0*\n*4294967286*\n*malloc(0)!=0:1*\n*cleaned*')

      src = r'''
        #include <stdio.h>
        #include <stdbool.h>

        int main() {
          bool x = true;
          bool y = false;
          printf("*%d*\n", x != y);
          return 0;
        }
      '''

      self.do_run(src, '*1*', force_c=True)

  def test_strtoll_hex(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    # tests strtoll for hex strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "0x4 -0x3A +0xDEADBEEF";
        char *end_char;

        // undefined base
        long long int l1 = strtoll(STRING, &end_char, 0);
        long long int l2 = strtoll(end_char, &end_char, 0);
        long long int l3 = strtoll(end_char, NULL, 0);

        // defined base
        long long int l4 = strtoll(STRING, &end_char, 16);
        long long int l5 = strtoll(end_char, &end_char, 16);
        long long int l6 = strtoll(end_char, NULL, 16);

        printf("%d%d%d%d%d%d\n", l1==0x4, l2==-0x3a, l3==0xdeadbeef, l4==0x4, l5==-0x3a, l6==0xdeadbeef);
        return 0;
      }
    '''
    self.do_run(src, '111111')

  def test_strtoll_dec(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    # tests strtoll for decimal strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "4 -38 +4711";
        char *end_char;

        // undefined base
        long long int l1 = strtoll(STRING, &end_char, 0);
        long long int l2 = strtoll(end_char, &end_char, 0);
        long long int l3 = strtoll(end_char, NULL, 0);

        // defined base
        long long int l4 = strtoll(STRING, &end_char, 10);
        long long int l5 = strtoll(end_char, &end_char, 10);
        long long int l6 = strtoll(end_char, NULL, 10);

        printf("%d%d%d%d%d%d\n", l1==4, l2==-38, l3==4711, l4==4, l5==-38, l6==4711);
        return 0;
      }
    '''
    self.do_run(src, '111111')

  def test_strtoll_bin(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    # tests strtoll for binary strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "1 -101 +1011";
        char *end_char;

        // defined base
        long long int l4 = strtoll(STRING, &end_char, 2);
        long long int l5 = strtoll(end_char, &end_char, 2);
        long long int l6 = strtoll(end_char, NULL, 2);

        printf("%d%d%d\n", l4==1, l5==-5, l6==11);
        return 0;
      }
    '''
    self.do_run(src, '111')

  def test_strtoll_oct(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    # tests strtoll for decimal strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "0 -035 +04711";
        char *end_char;

        // undefined base
        long long int l1 = strtoll(STRING, &end_char, 0);
        long long int l2 = strtoll(end_char, &end_char, 0);
        long long int l3 = strtoll(end_char, NULL, 0);

        // defined base
        long long int l4 = strtoll(STRING, &end_char, 8);
        long long int l5 = strtoll(end_char, &end_char, 8);
        long long int l6 = strtoll(end_char, NULL, 8);

        printf("%d%d%d%d%d%d\n", l1==0, l2==-29, l3==2505, l4==0, l5==-29, l6==2505);
        return 0;
      }
    '''
    self.do_run(src, '111111')
  
  def test_strtol_hex(self):
    # tests strtoll for hex strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "0x4 -0x3A +0xDEAD";
        char *end_char;

        // undefined base
        long l1 = strtol(STRING, &end_char, 0);
        long l2 = strtol(end_char, &end_char, 0);
        long l3 = strtol(end_char, NULL, 0);

        // defined base
        long l4 = strtol(STRING, &end_char, 16);
        long l5 = strtol(end_char, &end_char, 16);
        long l6 = strtol(end_char, NULL, 16);

        printf("%d%d%d%d%d%d\n", l1==0x4, l2==-0x3a, l3==0xdead, l4==0x4, l5==-0x3a, l6==0xdead);
        return 0;
      }
    '''
    self.do_run(src, '111111')

  def test_strtol_dec(self):
    # tests strtoll for decimal strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "4 -38 +4711";
        char *end_char;

        // undefined base
        long l1 = strtol(STRING, &end_char, 0);
        long l2 = strtol(end_char, &end_char, 0);
        long l3 = strtol(end_char, NULL, 0);

        // defined base
        long l4 = strtol(STRING, &end_char, 10);
        long l5 = strtol(end_char, &end_char, 10);
        long l6 = strtol(end_char, NULL, 10);

        printf("%d%d%d%d%d%d\n", l1==4, l2==-38, l3==4711, l4==4, l5==-38, l6==4711);
        return 0;
      }
    '''
    self.do_run(src, '111111')

  def test_strtol_bin(self):
    # tests strtoll for binary strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "1 -101 +1011";
        char *end_char;

        // defined base
        long l4 = strtol(STRING, &end_char, 2);
        long l5 = strtol(end_char, &end_char, 2);
        long l6 = strtol(end_char, NULL, 2);

        printf("%d%d%d\n", l4==1, l5==-5, l6==11);
        return 0;
      }
    '''
    self.do_run(src, '111')

  def test_strtol_oct(self):
    # tests strtoll for decimal strings (0x...) 
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        const char *STRING = "0 -035 +04711";
        char *end_char;

        // undefined base
        long l1 = strtol(STRING, &end_char, 0);
        long l2 = strtol(end_char, &end_char, 0);
        long l3 = strtol(end_char, NULL, 0);

        // defined base
        long l4 = strtol(STRING, &end_char, 8);
        long l5 = strtol(end_char, &end_char, 8);
        long l6 = strtol(end_char, NULL, 8);

        printf("%d%d%d%d%d%d\n", l1==0, l2==-29, l3==2505, l4==0, l5==-29, l6==2505);
        return 0;
      }
    '''
    self.do_run(src, '111111')

  def test_atexit(self):
    # Confirms they are called in reverse order
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      static void cleanA() {
        printf("A");
      }
      static void cleanB() {
        printf("B");
      }

      int main() {
        atexit(cleanA);
        atexit(cleanB);
        return 0;
      }
      '''
    self.do_run(src, 'BA')

  def test_pthread_specific(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    src = open(path_from_root('tests', 'pthread', 'specific.c'), 'r').read()
    expected = open(path_from_root('tests', 'pthread', 'specific.c.txt'), 'r').read()
    self.do_run(src, expected, force_c=True)

  def test_tcgetattr(self):
    src = open(path_from_root('tests', 'termios', 'test_tcgetattr.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_time(self):
    # XXX Not sure what the right output is here. Looks like the test started failing with daylight savings changes. Modified it to pass again.
    src = open(path_from_root('tests', 'time', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'time', 'output.txt'), 'r').read()
    expected2 = open(path_from_root('tests', 'time', 'output2.txt'), 'r').read()
    self.do_run(src, [expected, expected2],
                 extra_emscripten_args=['-H', 'libc/time.h'])
                 #extra_emscripten_args=['-H', 'libc/fcntl.h,libc/sys/unistd.h,poll.h,libc/math.h,libc/langinfo.h,libc/time.h'])

  def test_timeb(self):
    # Confirms they are called in reverse order
    src = r'''
      #include <stdio.h>
      #include <assert.h>
      #include <sys/timeb.h>

      int main() {
        timeb tb;
        tb.timezone = 1;
        printf("*%d\n", ftime(&tb));
        assert(tb.time > 10000);
        assert(tb.timezone == 0);
        assert(tb.dstflag == 0);
        return 0;
      }
      '''
    self.do_run(src, '*0\n')

  def test_time_c(self):
    src = r'''
      #include <time.h>
      #include <stdio.h>

      int main() {
        time_t t = time(0);
        printf("time: %s\n", ctime(&t));
      }
    '''
    self.do_run(src, 'time: ') # compilation check, mainly

  def test_gmtime(self):
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <assert.h>

      int main(void)
      {
          time_t t=time(NULL);
          struct tm *ptm=gmtime(&t);
          struct tm tmCurrent=*ptm;
          int hour=tmCurrent.tm_hour;

          t-=hour*3600; // back to midnight
          int yday = -1;
          for(hour=0;hour<24;hour++)
          {
              ptm=gmtime(&t);
              // tm_yday must be constant all day...
              printf("yday: %d, hour: %d\n", ptm->tm_yday, hour);
              if (yday == -1) yday = ptm->tm_yday;
              else assert(yday == ptm->tm_yday);
              t+=3600; // add one hour
          }
          printf("ok!\n");
          return(0);
      }
    '''
    self.do_run(src, '''ok!''')

  def test_strptime_tm(self):
    src=r'''
      #include <time.h>
      #include <stdio.h>
      #include <string.h>

      int main() {
        struct tm tm;
        char *ptr = strptime("17410105012000", "%H%M%S%d%m%Y", &tm);

        printf("%s: %s, %d/%d/%d %d:%d:%d", 
          (ptr != NULL && *ptr=='\0') ? "OK" : "ERR", 
          tm.tm_wday == 0 ? "Sun" : (tm.tm_wday == 1 ? "Mon" : (tm.tm_wday == 2 ? "Tue" : (tm.tm_wday == 3 ? "Wed" : (tm.tm_wday == 4 ? "Thu" : (tm.tm_wday == 5 ? "Fri" : (tm.tm_wday == 6 ? "Sat" : "ERR")))))),
          tm.tm_mon+1,
          tm.tm_mday,
          tm.tm_year+1900,
          tm.tm_hour,
          tm.tm_min,
          tm.tm_sec            
        );
      }
    ''' 
    self.do_run(src, 'OK: Wed, 1/5/2000 17:41:1')

  def test_strptime_days(self):
    src = r'''
      #include <time.h>
      #include <stdio.h>
      #include <string.h>

      static const struct {
        const char *input;
        const char *format;
      } day_tests[] = {
        { "2000-01-01", "%Y-%m-%d"},
        { "03/03/00", "%D"},
        { "9/9/99", "%x"},
        { "19990502123412", "%Y%m%d%H%M%S"},
        { "2001 20 Mon", "%Y %U %a"},
        { "2006 4 Fri", "%Y %U %a"},
        { "2001 21 Mon", "%Y %W %a"},
        { "2013 29 Wed", "%Y %W %a"},
        { "2000-01-01 08:12:21 AM", "%Y-%m-%d %I:%M:%S %p"},
        { "2000-01-01 08:12:21 PM", "%Y-%m-%d %I:%M:%S %p"},
        { "2001 17 Tue", "%Y %U %a"},
        { "2001 8 Thursday", "%Y %W %a"},
      };

      int main() {  
        struct tm tm;

        for (int i = 0; i < sizeof (day_tests) / sizeof (day_tests[0]); ++i) {
          memset (&tm, '\0', sizeof (tm));
          char *ptr = strptime(day_tests[i].input, day_tests[i].format, &tm);

          printf("%s: %d/%d/%d (%dth DoW, %dth DoY)\n", (ptr != NULL && *ptr=='\0') ? "OK" : "ERR", tm.tm_mon+1, tm.tm_mday, 1900+tm.tm_year, tm.tm_wday, tm.tm_yday);
        }
      }
    '''
    self.do_run(src, 'OK: 1/1/2000 (6th DoW, 0th DoY)\n'\
                     'OK: 3/3/2000 (5th DoW, 62th DoY)\n'\
                     'OK: 9/9/1999 (4th DoW, 251th DoY)\n'\
                     'OK: 5/2/1999 (0th DoW, 121th DoY)\n'\
                     'OK: 5/21/2001 (1th DoW, 140th DoY)\n'\
                     'OK: 1/27/2006 (5th DoW, 26th DoY)\n'\
                     'OK: 5/21/2001 (1th DoW, 140th DoY)\n'\
                     'OK: 7/24/2013 (3th DoW, 204th DoY)\n'\
                     'OK: 1/1/2000 (6th DoW, 0th DoY)\n'\
                     'OK: 1/1/2000 (6th DoW, 0th DoY)\n'\
                     'OK: 5/1/2001 (2th DoW, 120th DoY)\n'\
                     'OK: 2/22/2001 (4th DoW, 52th DoY)\n'\
    )

  def test_strptime_reentrant(self):
    src=r'''
      #include <time.h>
      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>

      int main () {
        int result = 0;
        struct tm tm;

        memset (&tm, 0xaa, sizeof (tm));

        /* Test we don't crash on uninitialized struct tm.
           Some fields might contain bogus values until everything
           needed is initialized, but we shouldn't crash.  */
        if (strptime ("2007", "%Y", &tm) == NULL
            || strptime ("12", "%d", &tm) == NULL
            || strptime ("Feb", "%b", &tm) == NULL
            || strptime ("13", "%M", &tm) == NULL
            || strptime ("21", "%S", &tm) == NULL
            || strptime ("16", "%H", &tm) == NULL) {
          printf("ERR: returned NULL");
          exit(EXIT_FAILURE);
        }

        if (tm.tm_sec != 21 || tm.tm_min != 13 || tm.tm_hour != 16
            || tm.tm_mday != 12 || tm.tm_mon != 1 || tm.tm_year != 107
            || tm.tm_wday != 1 || tm.tm_yday != 42) {
          printf("ERR: unexpected tm content (1) - %d/%d/%d %d:%d:%d", tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
          exit(EXIT_FAILURE);
        }

        if (strptime ("8", "%d", &tm) == NULL) {
          printf("ERR: strptime failed");
          exit(EXIT_FAILURE);
        }

        if (tm.tm_sec != 21 || tm.tm_min != 13 || tm.tm_hour != 16
            || tm.tm_mday != 8 || tm.tm_mon != 1 || tm.tm_year != 107
            || tm.tm_wday != 4 || tm.tm_yday != 38) {
          printf("ERR: unexpected tm content (2) - %d/%d/%d %d:%d:%d", tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
          exit(EXIT_FAILURE);
        }

        printf("OK");
      }
    '''
    self.do_run(src, 'OK')

  def test_strftime(self):
    src=r'''
      #include <time.h>
      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>

      void test(int result, const char* comment, const char* parsed = "") {
        printf("%d",result);
        if (!result) {
          printf("\nERROR: %s (\"%s\")\n", comment, parsed);
        }
      }

      int cmp(const char *s1, const char *s2) {
        for ( ; *s1 == *s2 ; s1++,s2++ ) {
          if ( *s1 == '\0' )
            break;
        } 

        return (*s1 - *s2);
      }

      int main() {
          struct tm tm;
          char s[1000];
          size_t size;
          
          tm.tm_sec = 4;
          tm.tm_min = 23;
          tm.tm_hour = 20;
          tm.tm_mday = 21;
          tm.tm_mon = 1;
          tm.tm_year = 74;
          tm.tm_wday = 4;
          tm.tm_yday = 51;
          tm.tm_isdst = 0;
          
          size = strftime(s, 1000, "", &tm);
          test((size==0) && (*s=='\0'), "strftime test #1", s);

          size = strftime(s, 1000, "%a", &tm);
          test((size==3) && !cmp(s, "Thu"), "strftime test #2", s);

          size = strftime(s, 1000, "%A", &tm);
          test((size==8) && !cmp(s, "Thursday"), "strftime test #3", s);

          size = strftime(s, 1000, "%b", &tm);
          test((size==3) && !cmp(s, "Feb"), "strftime test #4", s);

          size = strftime(s, 1000, "%B", &tm);
          test((size==8) && !cmp(s, "February"),
                             "strftime test #5", s);

          size = strftime(s, 1000, "%d", &tm);
          test((size==2) && !cmp(s, "21"),
                             "strftime test #6", s);

          size = strftime(s, 1000, "%H", &tm);
          test((size==2) && !cmp(s, "20"),
                             "strftime test #7", s);

          size = strftime(s, 1000, "%I", &tm);
          test((size==2) && !cmp(s, "08"),
                             "strftime test #8", s);

          size = strftime(s, 1000, "%j", &tm);
          test((size==3) && !cmp(s, "052"),
                             "strftime test #9", s);

          size = strftime(s, 1000, "%m", &tm);
          test((size==2) && !cmp(s, "02"),
                             "strftime test #10", s);

          size = strftime(s, 1000, "%M", &tm);
          test((size==2) && !cmp(s, "23"),
                             "strftime test #11", s);

          size = strftime(s, 1000, "%p", &tm);
          test((size==2) && !cmp(s, "PM"),
                             "strftime test #12", s);

          size = strftime(s, 1000, "%S", &tm);
          test((size==2) && !cmp(s, "04"),
                             "strftime test #13", s);

          size = strftime(s, 1000, "%U", &tm);
          test((size==2) && !cmp(s, "07"),
                             "strftime test #14", s);

          size = strftime(s, 1000, "%w", &tm);
          test((size==1) && !cmp(s, "4"),
                             "strftime test #15", s);

          size = strftime(s, 1000, "%W", &tm);
          test((size==2) && !cmp(s, "07"),
                             "strftime test #16", s);

          size = strftime(s, 1000, "%y", &tm);
          test((size==2) && !cmp(s, "74"),
                             "strftime test #17", s);

          size = strftime(s, 1000, "%Y", &tm);
          test((size==4) && !cmp(s, "1974"),
                             "strftime test #18", s);

          size = strftime(s, 1000, "%%", &tm);
          test((size==1) && !cmp(s, "%"),
                             "strftime test #19", s);

          size = strftime(s, 5, "%Y", &tm);
          test((size==4) && !cmp(s, "1974"),
                             "strftime test #20", s);

          size = strftime(s, 4, "%Y", &tm);
          test((size==0), "strftime test #21", s);

          tm.tm_mon = 0;
          tm.tm_mday = 1;
          size = strftime(s, 10, "%U", &tm);
          test((size==2) && !cmp(s, "00"), "strftime test #22", s);

          size = strftime(s, 10, "%W", &tm);
          test((size==2) && !cmp(s, "00"), "strftime test #23", s);

          // 1/1/1973 was a Sunday and is in CW 1
          tm.tm_year = 73;
          size = strftime(s, 10, "%W", &tm);
          test((size==2) && !cmp(s, "01"), "strftime test #24", s);

          // 1/1/1978 was a Monday and is in CW 1
          tm.tm_year = 78;
          size = strftime(s, 10, "%U", &tm);
          test((size==2) && !cmp(s, "01"), "strftime test #25", s);

          // 2/1/1999
          tm.tm_year = 99;
          tm.tm_yday = 1;
          size = strftime(s, 10, "%G (%V)", &tm);
          test((size==9) && !cmp(s, "1998 (53)"), "strftime test #26", s);

          size = strftime(s, 10, "%g", &tm);
          test((size==2) && !cmp(s, "98"), "strftime test #27", s);

          // 30/12/1997
          tm.tm_year = 97;
          tm.tm_yday = 363;
          size = strftime(s, 10, "%G (%V)", &tm);
          test((size==9) && !cmp(s, "1998 (01)"), "strftime test #28", s);

          size = strftime(s, 10, "%g", &tm);
          test((size==2) && !cmp(s, "98"), "strftime test #29", s);
      } 
    '''
    self.do_run(src, '11111111111111111111111111111')

  def test_intentional_fault(self):
    # Some programs intentionally segfault themselves, we should compile that into a throw
    src = r'''
      int main () {
        *(volatile char *)0 = 0;
        return *(volatile char *)0;
      }
      '''
    self.do_run(src, 'fault on write to 0' if not Settings.ASM_JS else 'abort()')

  def test_trickystring(self):
    src = r'''
      #include <stdio.h>

      typedef struct
      {
        int (*f)(void *);
        void *d;
        char s[16];
      } LMEXFunctionStruct;

      int f(void *user)
      {
        return 0;
      }

      static LMEXFunctionStruct const a[] =
      {
        {f, (void *)(int)'a', "aa"}
      };

      int main()
      {
        printf("ok\n");
        return a[0].f(a[0].d);
      }
    '''
    self.do_run(src, 'ok\n')

  def test_statics(self):
      # static initializers save i16 but load i8 for some reason (or i64 and load i8)
      if Settings.SAFE_HEAP:
        Settings.SAFE_HEAP = 3
        Settings.SAFE_HEAP_LINES = ['src.cpp:19', 'src.cpp:26', 'src.cpp:28']

      src = '''
        #include <stdio.h>
        #include <string.h>

        #define CONSTRLEN 32

        char * (*func)(char *, const char *) = NULL;

        void conoutfv(const char *fmt)
        {
            static char buf[CONSTRLEN];
            func(buf, fmt); // call by function pointer to make sure we test strcpy here
            puts(buf);
        }

        struct XYZ {
          float x, y, z;
          XYZ(float a, float b, float c) : x(a), y(b), z(c) { }
          static const XYZ& getIdentity()
          {
            static XYZ iT(1,2,3);
            return iT;
          }
        };
        struct S {
          static const XYZ& getIdentity()
          {
            static const XYZ iT(XYZ::getIdentity());
            return iT;
          }
        };

        int main() {
          func = &strcpy;
          conoutfv("*staticccz*");
          printf("*%.2f,%.2f,%.2f*\\n", S::getIdentity().x, S::getIdentity().y, S::getIdentity().z);
          return 0;
        }
        '''
      self.do_run(src, '*staticccz*\n*1.00,2.00,3.00*')

  def test_copyop(self):
      if self.emcc_args is None: return self.skip('requires emcc')

      # clang generated code is vulnerable to this, as it uses
      # memcpy for assignments, with hardcoded numbers of bytes
      # (llvm-gcc copies items one by one). See QUANTUM_SIZE in
      # settings.js.
      src = '''
        #include <stdio.h>
        #include <math.h>
        #include <string.h>

        struct vec {
          double x,y,z;
          vec() : x(0), y(0), z(0) { };
          vec(const double a, const double b, const double c) : x(a), y(b), z(c) { };
        };

        struct basis {
          vec a, b, c;
          basis(const vec& v) {
            a=v; // should not touch b!
            printf("*%.2f,%.2f,%.2f*\\n", b.x, b.y, b.z);
          }
        };

        int main() {
          basis B(vec(1,0,0));

          // Part 2: similar problem with memset and memmove
          int x = 1, y = 77, z = 2;
          memset((void*)&x, 0, sizeof(int));
          memset((void*)&z, 0, sizeof(int));
          printf("*%d,%d,%d*\\n", x, y, z);
          memcpy((void*)&x, (void*)&z, sizeof(int));
          memcpy((void*)&z, (void*)&x, sizeof(int));
          printf("*%d,%d,%d*\\n", x, y, z);
          memmove((void*)&x, (void*)&z, sizeof(int));
          memmove((void*)&z, (void*)&x, sizeof(int));
          printf("*%d,%d,%d*\\n", x, y, z);
          return 0;
        }
        '''
      self.do_run(src, '*0.00,0.00,0.00*\n*0,77,0*\n*0,77,0*\n*0,77,0*')

  def test_memcpy_memcmp(self):
      src = '''
        #include <stdio.h>
        #include <string.h>
        #include <assert.h>

        #define MAXX 48
        void reset(unsigned char *buffer) {
          for (int i = 0; i < MAXX; i++) buffer[i] = i+1;
        }
        void dump(unsigned char *buffer) {
          for (int i = 0; i < MAXX-1; i++) printf("%2d,", buffer[i]);
          printf("%d\\n", buffer[MAXX-1]);
        }
        int main() {
          unsigned char buffer[MAXX];
          for (int i = MAXX/4; i < MAXX-MAXX/4; i++) {
            for (int j = MAXX/4; j < MAXX-MAXX/4; j++) {
              for (int k = 1; k < MAXX/4; k++) {
                if (i == j) continue;
                if (i < j && i+k > j) continue;
                if (j < i && j+k > i) continue;
                printf("[%d,%d,%d] ", i, j, k);
                reset(buffer);
                memcpy(buffer+i, buffer+j, k);
                dump(buffer);
                assert(memcmp(buffer+i, buffer+j, k) == 0);
                buffer[i + k/2]++;
                if (buffer[i + k/2] != 0) {
                  assert(memcmp(buffer+i, buffer+j, k) > 0);
                } else {
                  assert(memcmp(buffer+i, buffer+j, k) < 0);
                }
                buffer[i + k/2]--;
                buffer[j + k/2]++;
                if (buffer[j + k/2] != 0) {
                  assert(memcmp(buffer+i, buffer+j, k) < 0);
                } else {
                  assert(memcmp(buffer+i, buffer+j, k) > 0);
                }
              }
            }
          }
          return 0;
        }
        '''
      def check(result, err):
        return hashlib.sha1(result).hexdigest()
      self.do_run(src, '6c9cdfe937383b79e52ca7a2cce83a21d9f5422c',
                  output_nicerizer = check)

  def test_memcpy2(self):
    src = r'''
      #include <stdio.h>
      #include <string.h>
      #include <assert.h>
      int main() {
        char buffer[256];
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 35; k++) {
              for (int t = 0; t < 256; t++) buffer[t] = t;
              char *dest = buffer + i + 128;
              char *src = buffer+j;
              //printf("%d, %d, %d\n", i, j, k);
              assert(memcpy(dest, src, k) == dest);
              assert(memcmp(dest, src, k) == 0);
            }
          }
        }
        printf("ok.\n");
        return 1;
      }
    '''
    self.do_run(src, 'ok.');

  def test_getopt(self):
      if self.emcc_args is None: return self.skip('needs emcc for libc')

      src = '''
        #pragma clang diagnostic ignored "-Winvalid-pp-token"
        #include <unistd.h>
        #include <stdlib.h>
        #include <stdio.h>

        int
        main(int argc, char *argv[])
        {
           int flags, opt;
           int nsecs, tfnd;

           nsecs = 0;
           tfnd = 0;
           flags = 0;
           while ((opt = getopt(argc, argv, "nt:")) != -1) {
               switch (opt) {
               case 'n':
                   flags = 1;
                   break;
               case 't':
                   nsecs = atoi(optarg);
                   tfnd = 1;
                   break;
               default: /* '?' */
                   fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\\n",
                           argv[0]);
                   exit(EXIT_FAILURE);
               }
           }

           printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);

           if (optind >= argc) {
               fprintf(stderr, "Expected argument after options\\n");
               exit(EXIT_FAILURE);
           }

           printf("name argument = %s\\n", argv[optind]);

           /* Other code omitted */

           exit(EXIT_SUCCESS);
        }
      '''
      self.do_run(src, 'flags=1; tfnd=1; optind=4\nname argument = foobar', args=['-t', '12', '-n', 'foobar'])

  def test_getopt_long(self):
      if self.emcc_args is None: return self.skip('needs emcc for libc')

      src = '''
        #pragma clang diagnostic ignored "-Winvalid-pp-token"
        #pragma clang diagnostic ignored "-Wdeprecated-writable-strings"
        #include <stdio.h>     /* for printf */
        #include <stdlib.h>    /* for exit */
        #include <getopt.h>

        int
        main(int argc, char **argv)
        {
           int c;
           int digit_optind = 0;

           while (1) {
               int this_option_optind = optind ? optind : 1;
               int option_index = 0;
               static struct option long_options[] = {
                   {"add",     required_argument, 0,  0 },
                   {"append",  no_argument,       0,  0 },
                   {"delete",  required_argument, 0,  0 },
                   {"verbose", no_argument,       0,  0 },
                   {"create",  required_argument, 0, 'c'},
                   {"file",    required_argument, 0,  0 },
                   {0,         0,                 0,  0 }
               };

               c = getopt_long(argc, argv, "abc:d:012",
                        long_options, &option_index);
               if (c == -1)
                   break;

               switch (c) {
               case 0:
                   printf("option %s", long_options[option_index].name);
                   if (optarg)
                       printf(" with arg %s", optarg);
                   printf("\\n");
                   break;

               case '0':
               case '1':
               case '2':
                   if (digit_optind != 0 && digit_optind != this_option_optind)
                     printf("digits occur in two different argv-elements.\\n");
                   digit_optind = this_option_optind;
                   printf("option %c\\n", c);
                   break;

               case 'a':
                   printf("option a\\n");
                   break;

               case 'b':
                   printf("option b\\n");
                   break;

               case 'c':
                   printf("option c with value '%s'\\n", optarg);
                   break;

               case 'd':
                   printf("option d with value '%s'\\n", optarg);
                   break;

               case '?':
                   break;

               default:
                   printf("?? getopt returned character code 0%o ??\\n", c);
               }
           }

           if (optind < argc) {
               printf("non-option ARGV-elements: ");
               while (optind < argc)
                   printf("%s ", argv[optind++]);
               printf("\\n");
           }

           exit(EXIT_SUCCESS);
        }
      '''
      self.do_run(src, 'option file with arg foobar\noption b', args=['--file', 'foobar', '-b'])

  def test_memmove(self):
    src = '''
      #include <stdio.h>
      #include <string.h>
      int main() {
        char str[] = "memmove can be very useful....!";
        memmove (str+20, str+15, 11);
        puts(str);
        return 0;
      }
    '''
    self.do_run(src, 'memmove can be very very useful')

  def test_memmove2(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('need ta2')

    src = r'''
      #include <stdio.h>
      #include <string.h>
      #include <assert.h>
      int main() {
        int sum = 0;
        char buffer[256];
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 35; k++) {
              for (int t = 0; t < 256; t++) buffer[t] = t;
              char *dest = buffer + i;
              char *src = buffer + j;
              if (dest == src) continue;
              //printf("%d, %d, %d\n", i, j, k);
              assert(memmove(dest, src, k) == dest);
              for (int t = 0; t < 256; t++) sum += buffer[t];
            }
          }
        }
        printf("final: %d.\n", sum);
        return 1;
      }
    '''
    self.do_run(src, 'final: -403200.');

  def test_memmove3(self):
    src = '''
      #include <stdio.h>
      #include <string.h>
      int main() {
        char str[] = "memmove can be vvery useful....!";
        memmove(str+15, str+16, 17);
        puts(str);
        return 0;
      }
    '''
    self.do_run(src, 'memmove can be very useful....!')

  def test_flexarray_struct(self):
    src = r'''
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct
{
  uint16_t length;
  struct 
  {
    int32_t int32;
  } value[];
} Tuple;

int main() {
  Tuple T[10];
  Tuple *t = &T[0];

  t->length = 4;
  t->value->int32 = 100;

  printf("(%d, %d)\n", t->length, t->value->int32);
  return 0;
}
'''
    self.do_run(src, '(4, 100)')

  def test_bsearch(self):
    if Settings.QUANTUM_SIZE == 1: return self.skip('Test cannot work with q1')

    src = '''
        #include <stdlib.h>
        #include <stdio.h>

        int cmp(const void* key, const void* member) {
          return *(int *)key - *(int *)member;
        }

        void printResult(int* needle, int* haystack, unsigned int len) {
          void *result = bsearch(needle, haystack, len, sizeof(unsigned int), cmp);

          if (result == NULL) {
            printf("null\\n");
          } else {
            printf("%d\\n", *(unsigned int *)result);
          }
        }

        int main() {
          int a[] = { -2, -1, 0, 6, 7, 9 };
          int b[] = { 0, 1 };

          /* Find all keys that exist. */
          for(int i = 0; i < 6; i++) {
            int val = a[i];

            printResult(&val, a, 6);
          }

          /* Keys that are covered by the range of the array but aren't in
           * the array cannot be found.
           */
          int v1 = 3;
          int v2 = 8;
          printResult(&v1, a, 6);
          printResult(&v2, a, 6);

          /* Keys outside the range of the array cannot be found. */
          int v3 = -1;
          int v4 = 2;

          printResult(&v3, b, 2);
          printResult(&v4, b, 2);

          return 0;
        }
        '''

    self.do_run(src, '-2\n-1\n0\n6\n7\n9\nnull\nnull\nnull\nnull')

  def test_nestedstructs(self):
      src = '''
        #include <stdio.h>
        #include "emscripten.h"

        struct base {
          int x;
          float y;
          union {
            int a;
            float b;
          };
          char c;
        };

        struct hashtableentry {
          int key;
          base data;
        };

        struct hashset {
          typedef hashtableentry entry;
          struct chain { entry elem; chain *next; };
        //  struct chainchunk { chain chains[100]; chainchunk *next; };
        };

        struct hashtable : hashset {
          hashtable() {
            base *b = NULL;
            entry *e = NULL;
            chain *c = NULL;
            printf("*%d,%d,%d,%d,%d,%d|%d,%d,%d,%d,%d,%d,%d,%d|%d,%d,%d,%d,%d,%d,%d,%d,%d,%d*\\n",
              sizeof(base),
              int(&(b->x)), int(&(b->y)), int(&(b->a)), int(&(b->b)), int(&(b->c)),
              sizeof(hashtableentry),
              int(&(e->key)), int(&(e->data)), int(&(e->data.x)), int(&(e->data.y)), int(&(e->data.a)), int(&(e->data.b)), int(&(e->data.c)),
              sizeof(hashset::chain),
              int(&(c->elem)), int(&(c->next)), int(&(c->elem.key)), int(&(c->elem.data)), int(&(c->elem.data.x)), int(&(c->elem.data.y)), int(&(c->elem.data.a)), int(&(c->elem.data.b)), int(&(c->elem.data.c))
            );
          }
        };

        struct B { char buffer[62]; int last; char laster; char laster2; };

        struct Bits {
          unsigned short A : 1;
          unsigned short B : 1;
          unsigned short C : 1;
          unsigned short D : 1;
          unsigned short x1 : 1;
          unsigned short x2 : 1;
          unsigned short x3 : 1;
          unsigned short x4 : 1;
        };

        int main() {
          hashtable t;

          // Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next
          // one is aligned properly. Also handle char; char; etc. properly.
          B *b = NULL;
          printf("*%d,%d,%d,%d,%d,%d,%d,%d,%d*\\n", int(b), int(&(b->buffer)), int(&(b->buffer[0])), int(&(b->buffer[1])), int(&(b->buffer[2])),
                                                    int(&(b->last)), int(&(b->laster)), int(&(b->laster2)), sizeof(B));

          // Part 3 - bitfields, and small structures
          Bits *b2 = NULL;
          printf("*%d*\\n", sizeof(Bits));

          return 0;
        }
        '''
      if Settings.QUANTUM_SIZE == 1:
        # Compressed memory. Note that sizeof() does give the fat sizes, however!
        self.do_run(src, '*16,0,1,2,2,3|20,0,1,1,2,3,3,4|24,0,5,0,1,1,2,3,3,4*\n*0,0,0,1,2,62,63,64,72*\n*2*')
      else:
        # Bloated memory; same layout as C/C++
        self.do_run(src, '*16,0,4,8,8,12|20,0,4,4,8,12,12,16|24,0,20,0,4,4,8,12,12,16*\n*0,0,0,1,2,64,68,69,72*\n*2*')

  def test_runtimelink(self):
    return self.skip('BUILD_AS_SHARED_LIB=2 is deprecated')
    if Building.LLVM_OPTS: return self.skip('LLVM opts will optimize printf into puts in the parent, and the child will still look for puts')
    if Settings.ASM_JS: return self.skip('asm does not support runtime linking')

    main, supp = self.setup_runtimelink_test()

    self.banned_js_engines = [NODE_JS] # node's global scope behaves differently than everything else, needs investigation FIXME
    Settings.LINKABLE = 1
    Settings.BUILD_AS_SHARED_LIB = 2
    Settings.NAMED_GLOBALS = 1

    self.build(supp, self.get_dir(), self.in_dir('supp.cpp'))
    shutil.move(self.in_dir('supp.cpp.o.js'), self.in_dir('liblib.so'))
    Settings.BUILD_AS_SHARED_LIB = 0

    Settings.RUNTIME_LINKED_LIBS = ['liblib.so'];
    self.do_run(main, 'supp: 54,2\nmain: 56\nsupp see: 543\nmain see: 76\nok.')

  def can_dlfcn(self):
    if self.emcc_args and '--memory-init-file' in self.emcc_args:
      for i in range(len(self.emcc_args)):
        if self.emcc_args[i] == '--memory-init-file':
          self.emcc_args = self.emcc_args[:i] + self.emcc_args[i+2:]
          break

    if Settings.ASM_JS:
      Settings.DLOPEN_SUPPORT = 1
    else:
      Settings.NAMED_GLOBALS = 1

    if not self.is_le32():
      self.skip('need le32 for dlfcn support')
      return False
    else:
      return True

  def prep_dlfcn_lib(self):
    if Settings.ASM_JS:
      Settings.MAIN_MODULE = 0
      Settings.SIDE_MODULE = 1
    else:
      Settings.BUILD_AS_SHARED_LIB = 1
      Settings.INCLUDE_FULL_LIBRARY = 0

  def prep_dlfcn_main(self):
    if Settings.ASM_JS:
      Settings.MAIN_MODULE = 1
      Settings.SIDE_MODULE = 0
    else:
      Settings.BUILD_AS_SHARED_LIB = 0
      Settings.INCLUDE_FULL_LIBRARY = 1

  dlfcn_post_build = '''
def process(filename):
  src = open(filename, 'r').read().replace(
    '// {{PRE_RUN_ADDITIONS}}',
    "FS.createLazyFile('/', 'liblib.so', 'liblib.so', true, false);"
  )
  open(filename, 'w').write(src)
'''

  def test_dlfcn_basic(self):
    if not self.can_dlfcn(): return

    self.prep_dlfcn_lib()
    lib_src = '''
      #include <cstdio>

      class Foo {
      public:
        Foo() {
          printf("Constructing lib object.\\n");
        }
      };

      Foo global;
      '''
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = '''
      #include <cstdio>
      #include <dlfcn.h>

      class Bar {
      public:
        Bar() {
          printf("Constructing main object.\\n");
        }
      };

      Bar global;

      int main() {
        dlopen("liblib.so", RTLD_NOW);
        return 0;
      }
      '''
    self.do_run(src, 'Constructing main object.\nConstructing lib object.\n',
                post_build=self.dlfcn_post_build)

  def test_dlfcn_qsort(self):
    if not self.can_dlfcn(): return

    if Settings.USE_TYPED_ARRAYS == 2:
      Settings.CORRECT_SIGNS = 1 # Needed for unsafe optimizations

    self.prep_dlfcn_lib()
    Settings.EXPORTED_FUNCTIONS = ['_get_cmp']
    lib_src = '''
      int lib_cmp(const void* left, const void* right) {
        const int* a = (const int*) left;
        const int* b = (const int*) right;
        if(*a > *b) return 1;
        else if(*a == *b) return  0;
        else return -1;
      }

      typedef int (*CMP_TYPE)(const void*, const void*);

      extern "C" CMP_TYPE get_cmp() {
        return lib_cmp;
      }
      '''
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc']
    src = '''
      #include <stdio.h>
      #include <stdlib.h>
      #include <dlfcn.h>

      typedef int (*CMP_TYPE)(const void*, const void*);

      int main_cmp(const void* left, const void* right) {
        const int* a = (const int*) left;
        const int* b = (const int*) right;
        if(*a < *b) return 1;
        else if(*a == *b) return  0;
        else return -1;
      }

      int main() {
        void* lib_handle;
        CMP_TYPE (*getter_ptr)();
        CMP_TYPE lib_cmp_ptr;
        int arr[5] = {4, 2, 5, 1, 3};

        qsort((void*)arr, 5, sizeof(int), main_cmp);
        printf("Sort with main comparison: ");
        for (int i = 0; i < 5; i++) {
          printf("%d ", arr[i]);
        }
        printf("\\n");

        lib_handle = dlopen("liblib.so", RTLD_NOW);
        if (lib_handle == NULL) {
          printf("Could not load lib.\\n");
          return 1;
        }
        getter_ptr = (CMP_TYPE (*)()) dlsym(lib_handle, "get_cmp");
        if (getter_ptr == NULL) {
          printf("Could not find func.\\n");
          return 1;
        }
        lib_cmp_ptr = getter_ptr();
        qsort((void*)arr, 5, sizeof(int), lib_cmp_ptr);
        printf("Sort with lib comparison: ");
        for (int i = 0; i < 5; i++) {
          printf("%d ", arr[i]);
        }
        printf("\\n");

        return 0;
      }
      '''
    self.do_run(src, 'Sort with main comparison: 5 4 3 2 1 *Sort with lib comparison: 1 2 3 4 5 *',
                output_nicerizer=lambda x, err: x.replace('\n', '*'),
                post_build=self.dlfcn_post_build)

    if Settings.ASM_JS and os.path.exists(SPIDERMONKEY_ENGINE[0]):
      out = run_js('liblib.so', engine=SPIDERMONKEY_ENGINE, full_output=True, stderr=STDOUT)
      if 'asm' in out:
        self.validate_asmjs(out)

  def test_dlfcn_data_and_fptr(self):
    if Settings.ASM_JS: return self.skip('this is not a valid case - libraries should not be able to access their parents globals willy nilly')
    if not self.can_dlfcn(): return

    if Building.LLVM_OPTS: return self.skip('LLVM opts will optimize out parent_func')

    self.prep_dlfcn_lib()
    lib_src = '''
      #include <stdio.h>

      int global = 42;

      extern void parent_func(); // a function that is defined in the parent

      void lib_fptr() {
        printf("Second calling lib_fptr from main.\\n");
        parent_func();
        // call it also through a pointer, to check indexizing
        void (*p_f)();
        p_f = parent_func;
        p_f();
      }

      extern "C" void (*func(int x, void(*fptr)()))() {
        printf("In func: %d\\n", x);
        fptr();
        return lib_fptr;
      }
      '''
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    Settings.EXPORTED_FUNCTIONS = ['_func']
    Settings.EXPORTED_GLOBALS = ['_global']
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    Settings.LINKABLE = 1
    src = '''
      #include <stdio.h>
      #include <dlfcn.h>
      #include <emscripten.h>

      typedef void (*FUNCTYPE(int, void(*)()))();

      FUNCTYPE func;

      void EMSCRIPTEN_KEEPALIVE parent_func() {
        printf("parent_func called from child\\n");
      }

      void main_fptr() {
        printf("First calling main_fptr from lib.\\n");
      }

      int main() {
        void* lib_handle;
        FUNCTYPE* func_fptr;

        // Test basic lib loading.
        lib_handle = dlopen("liblib.so", RTLD_NOW);
        if (lib_handle == NULL) {
          printf("Could not load lib.\\n");
          return 1;
        }

        // Test looked up function.
        func_fptr = (FUNCTYPE*) dlsym(lib_handle, "func");
        // Load twice to test cache.
        func_fptr = (FUNCTYPE*) dlsym(lib_handle, "func");
        if (func_fptr == NULL) {
          printf("Could not find func.\\n");
          return 1;
        }

        // Test passing function pointers across module bounds.
        void (*fptr)() = func_fptr(13, main_fptr);
        fptr();

        // Test global data.
        int* global = (int*) dlsym(lib_handle, "global");
        if (global == NULL) {
          printf("Could not find global.\\n");
          return 1;
        }

        printf("Var: %d\\n", *global);

        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main']
    Settings.EXPORTED_GLOBALS = []
    self.do_run(src, 'In func: 13*First calling main_fptr from lib.*Second calling lib_fptr from main.*parent_func called from child*parent_func called from child*Var: 42*',
                 output_nicerizer=lambda x, err: x.replace('\n', '*'),
                 post_build=self.dlfcn_post_build)

  def test_dlfcn_alias(self):
    if Settings.ASM_JS: return self.skip('this is not a valid case - libraries should not be able to access their parents globals willy nilly')

    Settings.LINKABLE = 1
    Settings.NAMED_GLOBALS = 1

    if Building.LLVM_OPTS == 2: return self.skip('LLVM LTO will optimize away stuff we expect from the shared library')

    lib_src = r'''
      #include <stdio.h>
      extern int parent_global;
      extern "C" void func() {
        printf("Parent global: %d.\n", parent_global);
      }
      '''
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    Settings.BUILD_AS_SHARED_LIB = 1
    Settings.EXPORTED_FUNCTIONS = ['_func']
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    src = r'''
      #include <dlfcn.h>

      int parent_global = 123;

      int main() {
        void* lib_handle;
        void (*fptr)();

        lib_handle = dlopen("liblib.so", RTLD_NOW);
        fptr = (void (*)())dlsym(lib_handle, "func");
        fptr();
        parent_global = 456;
        fptr();

        return 0;
      }
      '''
    Settings.BUILD_AS_SHARED_LIB = 0
    Settings.INCLUDE_FULL_LIBRARY = 1
    Settings.EXPORTED_FUNCTIONS = ['_main']
    self.do_run(src, 'Parent global: 123.*Parent global: 456.*',
                output_nicerizer=lambda x, err: x.replace('\n', '*'),
                post_build=self.dlfcn_post_build,
                extra_emscripten_args=['-H', 'libc/fcntl.h,libc/sys/unistd.h,poll.h,libc/math.h,libc/time.h,libc/langinfo.h'])
    Settings.INCLUDE_FULL_LIBRARY = 0

  def test_dlfcn_varargs(self):
    if Settings.ASM_JS: return self.skip('this is not a valid case - libraries should not be able to access their parents globals willy nilly')

    if not self.can_dlfcn(): return

    Settings.LINKABLE = 1

    if Building.LLVM_OPTS == 2: return self.skip('LLVM LTO will optimize things that prevent shared objects from working')
    if Settings.QUANTUM_SIZE == 1: return self.skip('FIXME: Add support for this')

    self.prep_dlfcn_lib()
    lib_src = r'''
      void print_ints(int n, ...);
      extern "C" void func() {
        print_ints(2, 13, 42);
      }
      '''
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    Settings.EXPORTED_FUNCTIONS = ['_func']
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = r'''
      #include <stdarg.h>
      #include <stdio.h>
      #include <dlfcn.h>
      #include <assert.h>

      void print_ints(int n, ...) {
        va_list args;
        va_start(args, n);
        for (int i = 0; i < n; i++) {
          printf("%d\n", va_arg(args, int));
        }
        va_end(args);
      }

      int main() {
        void* lib_handle;
        void (*fptr)();

        print_ints(2, 100, 200);

        lib_handle = dlopen("liblib.so", RTLD_NOW);
        assert(lib_handle);
        fptr = (void (*)())dlsym(lib_handle, "func");
        fptr();

        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main']
    self.do_run(src, '100\n200\n13\n42\n',
                post_build=self.dlfcn_post_build)

  def test_dlfcn_self(self):
    if Settings.USE_TYPED_ARRAYS == 1: return self.skip('Does not work with USE_TYPED_ARRAYS=1')
    Settings.DLOPEN_SUPPORT = 1

    src = r'''
#include <stdio.h>
#include <dlfcn.h>
#include <emscripten.h>

int EMSCRIPTEN_KEEPALIVE global = 123;

extern "C" EMSCRIPTEN_KEEPALIVE void foo(int x) {
printf("%d\n", x);
}

extern "C" EMSCRIPTEN_KEEPALIVE void repeatable() {
void* self = dlopen(NULL, RTLD_LAZY);
int* global_ptr = (int*)dlsym(self, "global");
void (*foo_ptr)(int) = (void (*)(int))dlsym(self, "foo");
foo_ptr(*global_ptr);
dlclose(self);
}

int main() {
repeatable();
repeatable();
return 0;
}'''
    def post(filename):
      with open(filename) as f:
        for line in f:
          if 'var SYMBOL_TABLE' in line:
            table = line
            break
        else:
          raise Exception('Could not find symbol table!')
      table = table[table.find('{'):table.rfind('}')+1]
      # ensure there aren't too many globals; we don't want unnamed_addr
      assert table.count(',') <= 4
    self.do_run(src, '123\n123', post_build=(None, post))


  def test_dlfcn_unique_sig(self):
    if not self.can_dlfcn(): return

    self.prep_dlfcn_lib()
    lib_src = '''
      #include <stdio.h>

      int myfunc(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l, int m) {
        return 13;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_myfunc']
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.c')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = '''
      #include <assert.h>
      #include <stdio.h>
      #include <dlfcn.h>

      typedef int (*FUNCTYPE)(int, int, int, int, int, int, int, int, int, int, int, int, int);

      int main() {
        void *lib_handle;
        FUNCTYPE func_ptr;

        lib_handle = dlopen("liblib.so", RTLD_NOW);
        assert(lib_handle != NULL);

        func_ptr = (FUNCTYPE)dlsym(lib_handle, "myfunc");
        assert(func_ptr != NULL);
        assert(func_ptr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == 13);

        puts("success");

        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc']
    self.do_run(src, 'success', force_c=True, post_build=self.dlfcn_post_build)

  def test_dlfcn_stacks(self):
    if not self.can_dlfcn(): return

    self.prep_dlfcn_lib()
    lib_src = '''
      #include <assert.h>
      #include <stdio.h>
      #include <string.h>

      int myfunc(const char *input) {
        char bigstack[1024] = { 0 };

        // make sure we didn't just trample the stack!
        assert(!strcmp(input, "foobar"));

        snprintf(bigstack, sizeof(bigstack), input);
        return strlen(bigstack);
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_myfunc']
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.c')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = '''
      #include <assert.h>
      #include <stdio.h>
      #include <dlfcn.h>

      typedef int (*FUNCTYPE)(const char *);

      int main() {
        void *lib_handle;
        FUNCTYPE func_ptr;
        char str[128];

        snprintf(str, sizeof(str), "foobar");

        lib_handle = dlopen("liblib.so", RTLD_NOW);
        assert(lib_handle != NULL);

        func_ptr = (FUNCTYPE)dlsym(lib_handle, "myfunc");
        assert(func_ptr != NULL);
        assert(func_ptr(str) == 6);

        puts("success");

        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc']
    self.do_run(src, 'success', force_c=True, post_build=self.dlfcn_post_build)

  def test_dlfcn_funcs(self):
    if not self.can_dlfcn(): return

    self.prep_dlfcn_lib()
    lib_src = r'''
      #include <assert.h>
      #include <stdio.h>
      #include <string.h>

      typedef void (*voidfunc)();
      typedef void (*intfunc)(int);

      void callvoid(voidfunc f) { f(); }
      void callint(voidfunc f, int x) { f(x); }

      void void_0() { printf("void 0\n"); }
      void void_1() { printf("void 1\n"); }
      voidfunc getvoid(int i) {
        switch(i) {
          case 0: return void_0;
          case 1: return void_1;
          default: return NULL;
        }
      }

      void int_0(int x) { printf("int 0 %d\n", x); }
      void int_1(int x) { printf("int 1 %d\n", x); }
      intfunc getint(int i) {
        switch(i) {
          case 0: return int_0;
          case 1: return int_1;
          default: return NULL;
        }
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_callvoid', '_callint', '_getvoid', '_getint']
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.c')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = r'''
      #include <assert.h>
      #include <stdio.h>
      #include <dlfcn.h>

      typedef void (*voidfunc)();
      typedef void (*intfunc)(int);

      typedef void (*voidcaller)(voidfunc);
      typedef void (*intcaller)(intfunc, int);

      typedef voidfunc (*voidgetter)(int);
      typedef intfunc (*intgetter)(int);

      void void_main() { printf("main.\n"); }
      void int_main(int x) { printf("main %d\n", x); }

      int main() {
        printf("go\n");
        void *lib_handle;
        lib_handle = dlopen("liblib.so", RTLD_NOW);
        assert(lib_handle != NULL);

        voidcaller callvoid = (voidcaller)dlsym(lib_handle, "callvoid");
        assert(callvoid != NULL);
        callvoid(void_main);

        intcaller callint = (intcaller)dlsym(lib_handle, "callint");
        assert(callint != NULL);
        callint(int_main, 201);

        voidgetter getvoid = (voidgetter)dlsym(lib_handle, "getvoid");
        assert(getvoid != NULL);
        callvoid(getvoid(0));
        callvoid(getvoid(1));

        intgetter getint = (intgetter)dlsym(lib_handle, "getint");
        assert(getint != NULL);
        callint(getint(0), 54);
        callint(getint(1), 9000);

        assert(getint(1000) == NULL);

        puts("ok");
        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc']
    self.do_run(src, '''go
main.
main 201
void 0
void 1
int 0 54
int 1 9000
ok
''', force_c=True, post_build=self.dlfcn_post_build)

  def test_dlfcn_mallocs(self):
    if not Settings.ASM_JS: return self.skip('needs asm')

    if not self.can_dlfcn(): return

    Settings.TOTAL_MEMORY = 64*1024*1024 # will be exhausted without functional malloc/free

    self.prep_dlfcn_lib()
    lib_src = r'''
      #include <assert.h>
      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>

      void *mallocproxy(int n) { return malloc(n); }
      void freeproxy(void *p) { free(p); }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_mallocproxy', '_freeproxy']
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.c')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = open(path_from_root('tests', 'dlmalloc_proxy.c')).read()
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc', '_free']
    self.do_run(src, '''*294,153*''', force_c=True, post_build=self.dlfcn_post_build)

  def test_dlfcn_longjmp(self):
    if not self.can_dlfcn(): return

    self.prep_dlfcn_lib()
    lib_src = r'''
      #include <setjmp.h>

      void jumpy(jmp_buf buf) {
        static int i = 0;
        i++;
        if (i == 10) longjmp(buf, i);
        printf("pre %d\n", i);
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_jumpy']
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.c')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = r'''
      #include <assert.h>
      #include <stdio.h>
      #include <dlfcn.h>
      #include <setjmp.h>

      typedef void (*jumpfunc)(jmp_buf);

      int main() {
        printf("go!\n");

        void *lib_handle;
        lib_handle = dlopen("liblib.so", RTLD_NOW);
        assert(lib_handle != NULL);

        jumpfunc jumpy = (jumpfunc)dlsym(lib_handle, "jumpy");
        assert(jumpy);

        jmp_buf buf;
        int jmpval = setjmp(buf);
        if (jmpval == 0) {
          while (1) jumpy(buf);
        } else {
          printf("out!\n");
        }

        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc', '_free']
    self.do_run(src, '''go!
pre 1
pre 2
pre 3
pre 4
pre 5
pre 6
pre 7
pre 8
pre 9
out!
''', post_build=self.dlfcn_post_build, force_c=True)

  def zzztest_dlfcn_exceptions(self): # TODO: make this work. need to forward tempRet0 across modules
    if not self.can_dlfcn(): return

    Settings.DISABLE_EXCEPTION_CATCHING = 0

    self.prep_dlfcn_lib()
    lib_src = r'''
      extern "C" {
      int ok() {
        return 65;
      }
      int fail() {
        throw 123;
      }
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_ok', '_fail']
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    self.build(lib_src, dirname, filename)
    shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so'))

    self.prep_dlfcn_main()
    src = r'''
      #include <assert.h>
      #include <stdio.h>
      #include <dlfcn.h>

      typedef int (*intfunc)();

      int main() {
        printf("go!\n");

        void *lib_handle;
        lib_handle = dlopen("liblib.so", RTLD_NOW);
        assert(lib_handle != NULL);

        intfunc okk = (intfunc)dlsym(lib_handle, "ok");
        intfunc faill = (intfunc)dlsym(lib_handle, "fail");
        assert(okk && faill);

        try {
          printf("ok: %d\n", okk());
        } catch(...) {
          printf("wha\n");
        }

        try {
          printf("fail: %d\n", faill());
        } catch(int x) {
          printf("int %d\n", x);
        }

        try {
          printf("fail: %d\n", faill());
        } catch(double x) {
          printf("caught %f\n", x);
        }

        return 0;
      }
      '''
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc', '_free']
    self.do_run(src, '''go!
ok: 65
int 123
ok
''', post_build=self.dlfcn_post_build)

  def test_rand(self):
    return self.skip('rand() is now random') # FIXME

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        printf("%d\n", rand());
        printf("%d\n", rand());

        srand(123);
        printf("%d\n", rand());
        printf("%d\n", rand());
        srand(123);
        printf("%d\n", rand());
        printf("%d\n", rand());

        unsigned state = 0;
        int r;
        r = rand_r(&state);
        printf("%d, %u\n", r, state);
        r = rand_r(&state);
        printf("%d, %u\n", r, state);
        state = 0;
        r = rand_r(&state);
        printf("%d, %u\n", r, state);

        return 0;
      }
      '''
    expected = '''
      1250496027
      1116302336
      440917656
      1476150784
      440917656
      1476150784
      12345, 12345
      1406932606, 3554416254
      12345, 12345
      '''
    self.do_run(src, re.sub(r'(^|\n)\s+', r'\1', expected))

  def test_strtod(self):
    if self.emcc_args is None: return self.skip('needs emcc for libc')

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        char* endptr;

        printf("\n");
        printf("%g\n", strtod("0", &endptr));
        printf("%g\n", strtod("0.", &endptr));
        printf("%g\n", strtod("0.0", &endptr));
        printf("%g\n", strtod("-0.0", &endptr));
        printf("%g\n", strtod("1", &endptr));
        printf("%g\n", strtod("1.", &endptr));
        printf("%g\n", strtod("1.0", &endptr));
        printf("%g\n", strtod("z1.0", &endptr));
        printf("%g\n", strtod("0.5", &endptr));
        printf("%g\n", strtod(".5", &endptr));
        printf("%g\n", strtod(".a5", &endptr));
        printf("%g\n", strtod("123", &endptr));
        printf("%g\n", strtod("123.456", &endptr));
        printf("%g\n", strtod("-123.456", &endptr));
        printf("%g\n", strtod("1234567891234567890", &endptr));
        printf("%g\n", strtod("1234567891234567890e+50", &endptr));
        printf("%g\n", strtod("84e+220", &endptr));
        printf("%g\n", strtod("123e-50", &endptr));
        printf("%g\n", strtod("123e-250", &endptr));
        printf("%g\n", strtod("123e-450", &endptr));

        char str[] = "  12.34e56end";
        printf("%g\n", strtod(str, &endptr));
        printf("%d\n", endptr - str);
        printf("%g\n", strtod("84e+420", &endptr));

        printf("%.12f\n", strtod("1.2345678900000000e+08", NULL));

        return 0;
      }
      '''
    expected = '''
      0
      0
      0
      -0
      1
      1
      1
      0
      0.5
      0.5
      0
      123
      123.456
      -123.456
      1.23457e+18
      1.23457e+68
      8.4e+221
      1.23e-48
      1.23e-248
      0
      1.234e+57
      10
      inf
      123456789.000000000000
      '''

    self.do_run(src, re.sub(r'\n\s+', '\n', expected))
    self.do_run(src.replace('strtod', 'strtold'), re.sub(r'\n\s+', '\n', expected)) # XXX add real support for long double

  def test_strtok(self):
    src = r'''
      #include<stdio.h>
      #include<string.h>

      int main() {
        char test[80], blah[80];
        char *sep = "\\/:;=-";
        char *word, *phrase, *brkt, *brkb;

        strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");

        for (word = strtok_r(test, sep, &brkt); word; word = strtok_r(NULL, sep, &brkt)) {
          strcpy(blah, "blah:blat:blab:blag");
          for (phrase = strtok_r(blah, sep, &brkb); phrase; phrase = strtok_r(NULL, sep, &brkb)) {
            printf("at %s:%s\n", word, phrase);
          }
        }
        return 0;
      }
    '''

    expected = '''at This:blah
at This:blat
at This:blab
at This:blag
at is.a:blah
at is.a:blat
at is.a:blab
at is.a:blag
at test:blah
at test:blat
at test:blab
at test:blag
at of:blah
at of:blat
at of:blab
at of:blag
at the:blah
at the:blat
at the:blab
at the:blag
at string:blah
at string:blat
at string:blab
at string:blag
at tokenizer:blah
at tokenizer:blat
at tokenizer:blab
at tokenizer:blag
at function.:blah
at function.:blat
at function.:blab
at function.:blag
'''
    self.do_run(src, expected)

  def test_parseInt(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('i64 mode 1 requires ta2')
    if Settings.QUANTUM_SIZE == 1: return self.skip('Q1 and I64_1 do not mix well yet')
    src = open(path_from_root('tests', 'parseInt', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'parseInt', 'output.txt'), 'r').read()
    self.do_run(src, expected)

  def test_transtrcase(self):
    src = '''
      #include <stdio.h>
      #include <string.h>
      int main()  {
        char szToupr[] = "hello, ";
        char szTolwr[] = "EMSCRIPTEN";
        strupr(szToupr);
        strlwr(szTolwr);
        printf(szToupr);
        printf(szTolwr);
        return 0;
      }
      '''
    self.do_run(src, 'HELLO, emscripten')

  def test_printf(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('i64 mode 1 requires ta2')
    self.banned_js_engines = [NODE_JS, V8_ENGINE] # SpiderMonkey and V8 do different things to float64 typed arrays, un-NaNing, etc.
    src = open(path_from_root('tests', 'printf', 'test.c'), 'r').read()
    expected = [open(path_from_root('tests', 'printf', 'output.txt'), 'r').read(),
                open(path_from_root('tests', 'printf', 'output_i64_1.txt'), 'r').read()]
    self.do_run(src, expected)

  def test_printf_2(self):
    src = r'''
      #include <stdio.h>

      int main() {
        char c = '1';
        short s = 2;
        int i = 3;
        long long l = 4;
        float f = 5.5;
        double d = 6.6;

        printf("%c,%hd,%d,%lld,%.1f,%.1llf\n", c, s, i, l, f, d);
        printf("%#x,%#x\n", 1, 0);

        return 0;
      }
      '''
    self.do_run(src, '1,2,3,4,5.5,6.6\n0x1,0\n')

  def test_vprintf(self):
    src = r'''
      #include <stdio.h>
      #include <stdarg.h>

      void print(char* format, ...) {
        va_list args;
        va_start (args, format);
        vprintf (format, args);
        va_end (args);
      }

      int main () {
         print("Call with %d variable argument.\n", 1);
         print("Call with %d variable %s.\n", 2, "arguments");

         return 0;
      }
      '''
    expected = '''
      Call with 1 variable argument.
      Call with 2 variable arguments.
      '''
    self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected))

  def test_vsnprintf(self):
    if self.emcc_args is None: return self.skip('needs i64 math')

    src = r'''
      #include <stdio.h>
      #include <stdarg.h>
      #include <stdint.h>

      void printy(const char *f, ...)
      {
        char buffer[256];
        va_list args;
        va_start(args, f);
        vsnprintf(buffer, 256, f, args);
        puts(buffer);
        va_end(args);
      }

      int main(int argc, char **argv) {
        int64_t x = argc - 1;
        int64_t y = argc - 1 + 0x400000;
        if (x % 3 == 2) y *= 2;

        printy("0x%llx_0x%llx", x, y);
        printy("0x%llx_0x%llx", x, x);
        printy("0x%llx_0x%llx", y, x);
        printy("0x%llx_0x%llx", y, y);

        {
          uint64_t A = 0x800000;
          uint64_t B = 0x800000000000ULL;
          printy("0x%llx_0x%llx", A, B);
        }
        {
          uint64_t A = 0x800;
          uint64_t B = 0x12340000000000ULL;
          printy("0x%llx_0x%llx", A, B);
        }
        {
          uint64_t A = 0x000009182746756;
          uint64_t B = 0x192837465631ACBDULL;
          printy("0x%llx_0x%llx", A, B);
        }

        return 0;
      }
    '''
    self.do_run(src, '''0x0_0x400000
0x0_0x0
0x400000_0x0
0x400000_0x400000
0x800000_0x800000000000
0x800_0x12340000000000
0x9182746756_0x192837465631acbd
''')

  def test_printf_more(self):
    src = r'''
      #include <stdio.h>
      int main()  {
        int size = snprintf(NULL, 0, "%s %d %.2f\n", "me and myself", 25, 1.345);
        char buf[size];
        snprintf(buf, size, "%s %d %.2f\n", "me and myself", 25, 1.345);
        printf("%d : %s\n", size, buf);
        char *buff = NULL;
        asprintf(&buff, "%d waka %d\n", 21, 95);
        puts(buff);
        return 0;
      }
      '''
    self.do_run(src, '22 : me and myself 25 1.34\n21 waka 95\n')

  def test_perrar(self):
    src = r'''
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <stdio.h>

      int main( int argc, char** argv ){
        int retval = open( "NonExistingFile", O_RDONLY );
        if( retval == -1 )
        perror( "Cannot open NonExistingFile" );
        return 0;
      }
      '''
    self.do_run(src, 'Cannot open NonExistingFile: No such file or directory\n')

  def test_atoX(self):
    if self.emcc_args is None: return self.skip('requires ta2')

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main () {
        printf("%d*", atoi(""));
        printf("%d*", atoi("a"));
        printf("%d*", atoi(" b"));
        printf("%d*", atoi(" c "));
        printf("%d*", atoi("6"));
        printf("%d*", atoi(" 5"));
        printf("%d*", atoi("4 "));
        printf("%d*", atoi("3 6"));
        printf("%d*", atoi(" 3 7"));
        printf("%d*", atoi("9 d"));
        printf("%d\n", atoi(" 8 e"));
        printf("%d*", atol(""));
        printf("%d*", atol("a"));
        printf("%d*", atol(" b"));
        printf("%d*", atol(" c "));
        printf("%d*", atol("6"));
        printf("%d*", atol(" 5"));
        printf("%d*", atol("4 "));
        printf("%d*", atol("3 6"));
        printf("%d*", atol(" 3 7"));
        printf("%d*", atol("9 d"));
        printf("%d\n", atol(" 8 e"));
        printf("%lld*", atoll("6294967296"));
        printf("%lld*", atoll(""));
        printf("%lld*", atoll("a"));
        printf("%lld*", atoll(" b"));
        printf("%lld*", atoll(" c "));
        printf("%lld*", atoll("6"));
        printf("%lld*", atoll(" 5"));
        printf("%lld*", atoll("4 "));
        printf("%lld*", atoll("3 6"));
        printf("%lld*", atoll(" 3 7"));
        printf("%lld*", atoll("9 d"));
        printf("%lld\n", atoll(" 8 e"));
        return 0;
      }
      '''
    self.do_run(src, '0*0*0*0*6*5*4*3*3*9*8\n0*0*0*0*6*5*4*3*3*9*8\n6294967296*0*0*0*0*6*5*4*3*3*9*8\n')

  def test_strstr(self):
    src = r'''
      #include <stdio.h>
      #include <string.h>

      int main()
      {
        printf("%d\n", !!strstr("\\n", "\\n"));
        printf("%d\n", !!strstr("cheezy", "ez"));
        printf("%d\n", !!strstr("cheeezy", "ez"));
        printf("%d\n", !!strstr("cheeeeeeeeeezy", "ez"));
        printf("%d\n", !!strstr("cheeeeeeeeee1zy", "ez"));
        printf("%d\n", !!strstr("che1ezy", "ez"));
        printf("%d\n", !!strstr("che1ezy", "che"));
        printf("%d\n", !!strstr("ce1ezy", "che"));
        printf("%d\n", !!strstr("ce1ezy", "ezy"));
        printf("%d\n", !!strstr("ce1ezyt", "ezy"));
        printf("%d\n", !!strstr("ce1ez1y", "ezy"));
        printf("%d\n", !!strstr("cheezy", "a"));
        printf("%d\n", !!strstr("cheezy", "b"));
        printf("%d\n", !!strstr("cheezy", "c"));
        printf("%d\n", !!strstr("cheezy", "d"));
        printf("%d\n", !!strstr("cheezy", "g"));
        printf("%d\n", !!strstr("cheezy", "h"));
        printf("%d\n", !!strstr("cheezy", "i"));
        printf("%d\n", !!strstr("cheezy", "e"));
        printf("%d\n", !!strstr("cheezy", "x"));
        printf("%d\n", !!strstr("cheezy", "y"));
        printf("%d\n", !!strstr("cheezy", "z"));
        printf("%d\n", !!strstr("cheezy", "_"));

        const char *str = "a big string";
        printf("%d\n", strstr(str, "big") - str);
        return 0;
      }
    '''
    self.do_run(src, '''1
1
1
1
0
1
1
0
1
1
0
0
0
1
0
0
1
0
1
0
1
1
0
2
''')

  def test_sscanf(self):
    if self.emcc_args is None: return self.skip('needs emcc for libc')

    src = r'''
      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>

      int main () {
        #define CHECK(str) \
        { \
          char name[1000]; \
          memset(name, 0, 1000); \
          int prio = 99; \
          sscanf(str, "%s %d", name, &prio); \
          printf("%s : %d\n", name, prio); \
        }
        CHECK("en-us 2");
        CHECK("en-r");
        CHECK("en 3");

        printf("%f, %f\n", atof("1.234567"), atof("cheez"));

        char float_formats[] = "fegE";
        char format[] = "%_";
        for(int i = 0; i < 4; ++i) {
          format[1] = float_formats[i];

          float n = -1;
          sscanf(" 2.8208", format, &n);
          printf("%.4f\n", n);

          float a = -1;
          sscanf("-3.03", format, &a);
          printf("%.4f\n", a);
        }

        char buffy[100];
        sscanf("cheez some thing moar 123\nyet more\n", "cheez %s", buffy);
        printf("|%s|\n", buffy);
        sscanf("cheez something\nmoar 123\nyet more\n", "cheez %s", buffy);
        printf("|%s|\n", buffy);
        sscanf("cheez somethingmoar\tyet more\n", "cheez %s", buffy);
        printf("|%s|\n", buffy);

        int numverts = -1;
        printf("%d\n", sscanf("	numverts 1499\n", " numverts %d", &numverts)); // white space is the same, even if tab vs space
        printf("%d\n", numverts);

        int index;
        float u, v;
        short start, count;
        printf("%d\n", sscanf("	vert 87 ( 0.481565 0.059481 ) 0 1\n", " vert %d ( %f %f ) %hu %hu", &index, &u, &v, &start, &count));
        printf("%d,%.6f,%.6f,%hu,%hu\n", index, u, v, start, count);

        int neg, neg2, neg3 = 0;
        printf("%d\n", sscanf("-123 -765 -34-6", "%d %u %d", &neg, &neg2, &neg3));
        printf("%d,%u,%d\n", neg, neg2, neg3);

        {
          int a = 0;
          sscanf("1", "%i", &a);
          printf("%i\n", a);
        }

        char buf1[100], buf2[100], buf3[100], buf4[100];

        int numItems = sscanf("level=4:ref=3", "%255[^:=]=%255[^:]:%255[^=]=%255c", buf1, buf2, buf3, buf4);
        printf("%d, %s, %s, %s, %s\n", numItems, buf1, buf2, buf3, buf4);

        numItems = sscanf("def|456", "%[a-z]|%[0-9]", buf1, buf2);
        printf("%d, %s, %s\n", numItems, buf1, buf2);

        numItems = sscanf("3-4,-ab", "%[-0-9],%[ab-z-]", buf1, buf2);
        printf("%d, %s, %s\n", numItems, buf1, buf2);

        numItems = sscanf("Hello,World", "%[A-Za-z],%[^0-9]", buf1, buf2);
        printf("%d, %s, %s\n", numItems, buf1, buf2);

        numItems = sscanf("Hello4711", "%[^0-9],%[^0-9]", buf1, buf2);
        printf("%d, %s\n", numItems, buf1);

        numItems = sscanf("JavaScript", "%4[A-Za-z]", buf1);
        printf("%d, %s\n", numItems, buf1);
    
        numItems = sscanf("[]", "%1[[]%1[]]", buf1, buf2);
        printf("%d, %s, %s\n", numItems, buf1, buf2);

        return 0;
      }
      '''
    self.do_run(src, 'en-us : 2\nen-r : 99\nen : 3\n1.234567, 0.000000\n2.8208\n-3.0300\n2.8208\n-3.0300\n2.8208\n-3.0300\n2.8208\n-3.0300\n|some|\n|something|\n|somethingmoar|\n' +
                     '1\n1499\n' +
                     '5\n87,0.481565,0.059481,0,1\n' +
                     '3\n-123,4294966531,-34\n' +
                     '1\n' +
                     '4, level, 4, ref, 3\n' +
                     '2, def, 456\n' +
                     '2, 3-4, -ab\n' +
                     '2, Hello, World\n' +
                     '1, Hello\n' +
                     '1, Java\n' +
                     '2, [, ]')

  def test_sscanf_2(self):
    # doubles
    if Settings.USE_TYPED_ARRAYS == 2:
      for ftype in ['float', 'double']:
        src = r'''
          #include <stdio.h>

          int main(){
              char strval1[] = "1.2345678901";
              char strval2[] = "1.23456789e5";
              char strval3[] = "1.23456789E5";
              char strval4[] = "1.2345678e-5";
              char strval5[] = "1.2345678E-5";
              double dblval = 1.2345678901;
              double tstval;

              sscanf(strval1, "%lf", &tstval);
              if(dblval != tstval) printf("FAIL: Values are not equal: %lf %lf\n", dblval, tstval);
              else printf("Pass: %lf %lf\n", tstval, dblval);

              sscanf(strval2, "%lf", &tstval);
              dblval = 123456.789;
              if(dblval != tstval) printf("FAIL: Values are not equal: %lf %lf\n", dblval, tstval);
              else printf("Pass: %lf %lf\n", tstval, dblval);

              sscanf(strval3, "%lf", &tstval);
              dblval = 123456.789;
              if(dblval != tstval) printf("FAIL: Values are not equal: %lf %lf\n", dblval, tstval);
              else printf("Pass: %lf %lf\n", tstval, dblval);

              sscanf(strval4, "%lf", &tstval);
              dblval = 0.000012345678;
              if(dblval != tstval) printf("FAIL: Values are not equal: %lf %lf\n", dblval, tstval);
              else printf("Pass: %lf %lf\n", tstval, dblval);

              sscanf(strval5, "%lf", &tstval);
              dblval = 0.000012345678;
              if(dblval != tstval) printf("FAIL: Values are not equal: %lf %lf\n", dblval, tstval);
              else printf("Pass: %lf %lf\n", tstval, dblval);

              return 0;
          }
        '''
        if ftype == 'float':
          self.do_run(src.replace('%lf', '%f').replace('double', 'float'), '''Pass: 1.234568 1.234568
Pass: 123456.789063 123456.789063
Pass: 123456.789063 123456.789063
Pass: 0.000012 0.000012
Pass: 0.000012 0.000012''')
        else:
          self.do_run(src, '''Pass: 1.234568 1.234568
Pass: 123456.789000 123456.789000
Pass: 123456.789000 123456.789000
Pass: 0.000012 0.000012
Pass: 0.000012 0.000012''')

  def test_sscanf_n(self):
    src = r'''
      #include<stdio.h>
      int main() {
        char *line = "version 1.0";
        int i, l, lineno;
        char word[80];
        if (sscanf(line, "%s%n", word, &l) != 1) {
            printf("Header format error, line %d\n", lineno);
        }
        printf("[DEBUG] word 1: %s, l: %d\n", word, l);

        int x = sscanf("one %n two", "%s %n", word, &l);
        printf("%d,%s,%d\n", x, word, l);
        {
          int a, b, c, count;
          count = sscanf("12345 6789", "%d %n%d", &a, &b, &c);
          printf("%i %i %i %i\n", count, a, b, c);
        }
        return 0;
      }
    '''
    self.do_run(src, '''[DEBUG] word 1: version, l: 7\n1,one,4\n2 12345 6 6789\n''')

  def test_sscanf_whitespace(self):
    src = r'''
      #include<stdio.h>

      int main() {
        short int x;
        short int y;

        const char* buffer[] = {
          "173,16",
          "    16,173",
          "183,   173",
          "  17,   287",
          " 98,  123,   "
        };

        for (int i=0; i<5; ++i) {
          sscanf(buffer[i], "%hd,%hd", &x, &y);
          printf("%d:%d,%d ", i, x, y);
        }

        return 0;
      }
    '''
    self.do_run(src, '''0:173,16 1:16,173 2:183,173 3:17,287 4:98,123''')

  def test_sscanf_other_whitespace(self):
    Settings.SAFE_HEAP = 0 # use i16s in printf

    src = r'''
      #include<stdio.h>

      int main() {
        short int x;
        short int y;

        const char* buffer[] = {
          "\t2\t3\t", /* TAB - horizontal tab */
          "\t\t5\t\t7\t\t",
          "\n11\n13\n",  /* LF - line feed */
          "\n\n17\n\n19\n\n",
          "\v23\v29\v",  /* VT - vertical tab */
          "\v\v31\v\v37\v\v",
          "\f41\f43\f",  /* FF - form feed */
          "\f\f47\f\f53\f\f",
          "\r59\r61\r",  /* CR - carrage return */
          "\r\r67\r\r71\r\r"
        };

        for (int i=0; i<10; ++i) {
          x = 0; y = 0;
          sscanf(buffer[i], " %d %d ", &x, &y);
          printf("%d, %d, ",  x, y);
        }

        return 0;
      }
    '''
    self.do_run(src, '''2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, ''')

  def test_sscanf_3(self):
    # i64
    if not Settings.USE_TYPED_ARRAYS == 2: return self.skip('64-bit sscanf only supported in ta2')
    src = r'''
      #include <stdint.h>
      #include <stdio.h>

      int main(){

          int64_t s, m, l;
          printf("%d\n", sscanf("123 1073741823 1125899906842620", "%lld %lld %lld", &s, &m, &l));
          printf("%lld,%lld,%lld\n", s, m, l);

          int64_t negS, negM, negL;
          printf("%d\n", sscanf("-123 -1073741823 -1125899906842620", "%lld %lld %lld", &negS, &negM, &negL));
          printf("%lld,%lld,%lld\n", negS, negM, negL);

          return 0;
      }
    '''

    self.do_run(src, '3\n123,1073741823,1125899906842620\n' +
                   '3\n-123,-1073741823,-1125899906842620\n')

  def test_sscanf_4(self):
    src = r'''
      #include <stdio.h>

      int main()
      {
        char pYear[16], pMonth[16], pDay[16], pDate[64];
        printf("%d\n", sscanf("Nov 19 2012", "%s%s%s", pMonth, pDay, pYear));
        printf("day %s, month %s, year %s \n", pDay, pMonth, pYear);
        return(0);
      }
    '''
    self.do_run(src, '3\nday 19, month Nov, year 2012');

  def test_sscanf_5(self):
    src = r'''
      #include "stdio.h"

      static const char *colors[] = {
        "  c black",
        ". c #001100",
        "X c #111100"
      };

      int main(){
        unsigned char code;
        char color[32];
        int rcode;
        for(int i = 0; i < 3; i++) {
          rcode = sscanf(colors[i], "%c c %s", &code, color);
          printf("%i, %c, %s\n", rcode, code, color);
        }
      }
    '''
    self.do_run(src, '2,  , black\n2, ., #001100\n2, X, #111100');

  def test_sscanf_6(self):
    src = r'''
      #include <stdio.h>
      #include <string.h>
      int main()
      {
        char *date = "18.07.2013w";
        char c[10];
        memset(c, 0, 10);
        int y, m, d, i;
        i = sscanf(date, "%d.%d.%4d%c", &d, &m, &y, c);
        printf("date: %s; day %2d, month %2d, year %4d, extra: %c, %d\n", date, d, m, y, c[0], i);
        i = sscanf(date, "%d.%d.%3c", &d, &m, c);
        printf("date: %s; day %2d, month %2d, year %4d, extra: %s, %d\n", date, d, m, y, c, i);
      }
    '''
    self.do_run(src, '''date: 18.07.2013w; day 18, month  7, year 2013, extra: w, 4
date: 18.07.2013w; day 18, month  7, year 2013, extra: 201, 3
''');

  def test_sscanf_skip(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip("need ta2 for full i64")

    src = r'''
      #include <stdint.h>
      #include <stdio.h>

      int main(){
          int val1;
          printf("%d\n", sscanf("10 20 30 40", "%*lld %*d %d", &val1));
          printf("%d\n", val1);

          int64_t large, val2;
          printf("%d\n", sscanf("1000000 -1125899906842620 -123 -1073741823", "%lld %*lld %ld %*d", &large, &val2));
          printf("%lld,%d\n", large, val2);

          return 0;
      }
    '''
    self.do_run(src, '1\n30\n2\n1000000,-123\n')

  def test_sscanf_caps(self):
    src = r'''
      #include "stdio.h"

      int main(){
        unsigned int a;
        float e, f, g;
        sscanf("a 1.1 1.1 1.1", "%X %E %F %G", &a, &e, &f, &g);
        printf("%d %.1F %.1F %.1F\n", a, e, f, g);
      }
    '''
    self.do_run(src, '10 1.1 1.1 1.1');

  def test_sscanf_hex(self):
    src = r'''
      #include "stdio.h"

      int main(){
        unsigned int a, b;
        sscanf("0x12AB 12AB", "%x %x", &a, &b);
        printf("%d %d\n", a, b);
      }
    '''
    self.do_run(src, '4779 4779')

  def test_sscanf_float(self):
    src = r'''
      #include "stdio.h"

      int main(){
        float f1, f2, f3, f4, f5, f6, f7, f8, f9;
        sscanf("0.512 0.250x5.129_-9.98 1.12*+54.32E3 +54.32E3^87.5E-3 87.5E-3$", "%f %fx%f_%f %f*%f %f^%f %f$", &f1, &f2, &f3, &f4, &f5, &f6, &f7, &f8, &f9);
        printf("\n%f, %f, %f, %f, %f, %f, %f, %f, %f\n", f1, f2, f3, f4, f5, f6, f7, f8, f9);
      }
    '''
    self.do_run(src, '\n0.512000, 0.250000, 5.129000, -9.980000, 1.120000, 54320.000000, 54320.000000, 0.087500, 0.087500\n')

  def test_langinfo(self):
    src = open(path_from_root('tests', 'langinfo', 'test.c'), 'r').read()
    expected = open(path_from_root('tests', 'langinfo', 'output.txt'), 'r').read()
    self.do_run(src, expected, extra_emscripten_args=['-H', 'libc/langinfo.h'])

  def test_files(self):
    self.banned_js_engines = [SPIDERMONKEY_ENGINE] # closure can generate variables called 'gc', which pick up js shell stuff
    if self.emcc_args is not None and '-O2' in self.emcc_args:
      self.emcc_args += ['--closure', '1'] # Use closure here, to test we don't break FS stuff
      self.emcc_args = filter(lambda x: x != '-g', self.emcc_args) # ensure we test --closure 1 --memory-init-file 1 (-g would disable closure)
      self.emcc_args += ["-s", "CHECK_HEAP_ALIGN=0"] # disable heap align check here, it mixes poorly with closure

    Settings.CORRECT_SIGNS = 1 # Just so our output is what we expect. Can flip them both.
    post = '''
def process(filename):
  src = \'\'\'
    var Module = {
      'noFSInit': true,
      'preRun': function() {
        FS.createLazyFile('/', 'test.file', 'test.file', true, false);
        // Test FS_* exporting
        Module['FS_createDataFile']('/', 'somefile.binary', [100, 200, 50, 25, 10, 77, 123], true, false);  // 200 becomes -56, since signed chars are used in memory
        var test_files_input = 'hi there!';
        var test_files_input_index = 0;
        FS.init(function() {
          return test_files_input.charCodeAt(test_files_input_index++) || null;
        });
      }
    };
  \'\'\' + open(filename, 'r').read()
  open(filename, 'w').write(src)
'''
    other = open(os.path.join(self.get_dir(), 'test.file'), 'w')
    other.write('some data');
    other.close()

    src = open(path_from_root('tests', 'files.cpp'), 'r').read()

    mem_file = 'src.cpp.o.js.mem'
    try_delete(mem_file)
    self.do_run(src, ('size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\ntexte\n', 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\ntexte\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\n'),
                 post_build=post, extra_emscripten_args=['-H', 'libc/fcntl.h'])
    if self.emcc_args and '--memory-init-file' in self.emcc_args:
      assert os.path.exists(mem_file)

  def test_files_m(self):
    # Test for Module.stdin etc.

    Settings.CORRECT_SIGNS = 1

    post = '''
def process(filename):
  src = \'\'\'
    var data = [10, 20, 40, 30];
    var Module = {
      stdin: function() { return data.pop() || null },
      stdout: function(x) { Module.print('got: ' + x) }
    };
  \'\'\' + open(filename, 'r').read()
  open(filename, 'w').write(src)
'''
    src = r'''
      #include <stdio.h>
      #include <unistd.h>

      int main () {
        char c;
        fprintf(stderr, "isatty? %d,%d,%d\n", isatty(fileno(stdin)), isatty(fileno(stdout)), isatty(fileno(stderr)));
        while ((c = fgetc(stdin)) != EOF) {
          putc(c+5, stdout);
        }
        return 0;
      }
      '''
    self.do_run(src, ('got: 35\ngot: 45\ngot: 25\ngot: 15\nisatty? 0,0,1\n', 'isatty? 0,0,1\ngot: 35\ngot: 45\ngot: 25\ngot: 15\n'), post_build=post)

  def test_fwrite_0(self):
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main ()
      {
          FILE *fh;

          fh = fopen("a.txt", "wb");
          if (!fh) exit(1);
          fclose(fh);

          fh = fopen("a.txt", "rb");
          if (!fh) exit(1);

          char data[] = "foobar";
          size_t written = fwrite(data, 1, sizeof(data), fh);

          printf("written=%zu\n", written);
      }
      '''
    self.do_run(src, 'written=0')

  def test_fgetc_ungetc(self):
    src = open(path_from_root('tests', 'stdio', 'test_fgetc_ungetc.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_fgetc_unsigned(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    src = r'''
      #include <stdio.h>
      int main() {
        FILE *file = fopen("file_with_byte_234.txt", "rb");
        int c = fgetc(file);
        printf("*%d\n", c);
      }
    '''
    open('file_with_byte_234.txt', 'wb').write('\xea')
    self.emcc_args += ['--embed-file', 'file_with_byte_234.txt']
    self.do_run(src, '*234\n')

  def test_fgets_eol(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    src = r'''
      #include <stdio.h>
      char buf[32];
      int main()
      {
        char *r = "SUCCESS";
        FILE *f = fopen("eol.txt", "r");
        while (fgets(buf, 32, f) != NULL) {
          if (buf[0] == '\0') {
            r = "FAIL";
            break;
          }
        }
        printf("%s\n", r);
        fclose(f);
        return 0;
      }
    '''
    open('eol.txt', 'wb').write('\n')
    self.emcc_args += ['--embed-file', 'eol.txt']
    self.do_run(src, 'SUCCESS\n')

  def test_fscanf(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    open(os.path.join(self.get_dir(), 'three_numbers.txt'), 'w').write('''-1 0.1 -.1''')
    src = r'''
      #include <stdio.h>
      #include <assert.h>
      #include <float.h>
      int main()
      {
          float x = FLT_MAX, y = FLT_MAX, z = FLT_MAX;

          FILE* fp = fopen("three_numbers.txt", "r");
          if (fp) {
              int match = fscanf(fp, " %f %f %f ", &x, &y, &z);
              printf("match = %d\n", match);
              printf("x = %0.1f, y = %0.1f, z = %0.1f\n", x, y, z);
          } else {
              printf("failed to open three_numbers.txt\n");
          }
          return 0;
      }
    '''
    self.emcc_args += ['--embed-file', 'three_numbers.txt']
    self.do_run(src, 'match = 3\nx = -1.0, y = 0.1, z = -0.1\n')

  def test_readdir(self):
    src = open(path_from_root('tests', 'dirent', 'test_readdir.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_stat(self):
    src = open(path_from_root('tests', 'stat', 'test_stat.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_stat_chmod(self):
    src = open(path_from_root('tests', 'stat', 'test_chmod.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_stat_mknod(self):
    src = open(path_from_root('tests', 'stat', 'test_mknod.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_fcntl(self):
    add_pre_run = '''
def process(filename):
  src = open(filename, 'r').read().replace(
    '// {{PRE_RUN_ADDITIONS}}',
    "FS.createDataFile('/', 'test', 'abcdef', true, true);"
  )
  open(filename, 'w').write(src)
'''
    src = open(path_from_root('tests', 'fcntl', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'fcntl', 'output.txt'), 'r').read()
    self.do_run(src, expected, post_build=add_pre_run, extra_emscripten_args=['-H', 'libc/fcntl.h'])

  def test_fcntl_open(self):
    src = open(path_from_root('tests', 'fcntl-open', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'fcntl-open', 'output.txt'), 'r').read()
    self.do_run(src, expected, force_c=True, extra_emscripten_args=['-H', 'libc/fcntl.h'])

  def test_fcntl_misc(self):
    add_pre_run = '''
def process(filename):
  src = open(filename, 'r').read().replace(
    '// {{PRE_RUN_ADDITIONS}}',
    "FS.createDataFile('/', 'test', 'abcdef', true, true);"
  )
  open(filename, 'w').write(src)
'''
    src = open(path_from_root('tests', 'fcntl-misc', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'fcntl-misc', 'output.txt'), 'r').read()
    self.do_run(src, expected, post_build=add_pre_run, extra_emscripten_args=['-H', 'libc/fcntl.h'])

  def test_poll(self):
    add_pre_run = '''
def process(filename):
  src = open(filename, 'r').read().replace(
    '// {{PRE_RUN_ADDITIONS}}',
    \'\'\'
      var dummy_device = FS.makedev(64, 0);
      FS.registerDevice(dummy_device, {});

      FS.createDataFile('/', 'file', 'abcdef', true, true);
      FS.mkdev('/device', dummy_device);
    \'\'\'
  )
  open(filename, 'w').write(src)
'''
    src = r'''
      #include <stdio.h>
      #include <errno.h>
      #include <fcntl.h>
      #include <poll.h>

      int main() {
        struct pollfd multi[5];
        multi[0].fd = open("/file", O_RDONLY, 0777);
        multi[1].fd = open("/device", O_RDONLY, 0777);
        multi[2].fd = 123;
        multi[3].fd = open("/file", O_RDONLY, 0777);
        multi[4].fd = open("/file", O_RDONLY, 0777);
        multi[0].events = POLLIN | POLLOUT | POLLNVAL | POLLERR;
        multi[1].events = POLLIN | POLLOUT | POLLNVAL | POLLERR;
        multi[2].events = POLLIN | POLLOUT | POLLNVAL | POLLERR;
        multi[3].events = 0x00;
        multi[4].events = POLLOUT | POLLNVAL | POLLERR;

        printf("ret: %d\n", poll(multi, 5, 123));
        printf("errno: %d\n", errno);
        printf("multi[0].revents: %d\n", multi[0].revents == (POLLIN | POLLOUT));
        printf("multi[1].revents: %d\n", multi[1].revents == (POLLIN | POLLOUT));
        printf("multi[2].revents: %d\n", multi[2].revents == POLLNVAL);
        printf("multi[3].revents: %d\n", multi[3].revents == 0);
        printf("multi[4].revents: %d\n", multi[4].revents == POLLOUT);

        return 0;
      }
      '''
    expected = r'''
      ret: 4
      errno: 0
      multi[0].revents: 1
      multi[1].revents: 1
      multi[2].revents: 1
      multi[3].revents: 1
      multi[4].revents: 1
      '''
    self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected), post_build=add_pre_run, extra_emscripten_args=['-H', 'libc/fcntl.h,poll.h'])

  def test_statvfs(self):
    src = r'''
      #include <stdio.h>
      #include <errno.h>
      #include <sys/statvfs.h>

      int main() {
        struct statvfs s;

        printf("result: %d\n", statvfs("/test", &s));
        printf("errno: %d\n", errno);

        printf("f_bsize: %lu\n", s.f_bsize);
        printf("f_frsize: %lu\n", s.f_frsize);
        printf("f_blocks: %lu\n", s.f_blocks);
        printf("f_bfree: %lu\n", s.f_bfree);
        printf("f_bavail: %lu\n", s.f_bavail);
        printf("f_files: %d\n", s.f_files > 5);
        printf("f_ffree: %lu\n", s.f_ffree);
        printf("f_favail: %lu\n", s.f_favail);
        printf("f_fsid: %lu\n", s.f_fsid);
        printf("f_flag: %lu\n", s.f_flag);
        printf("f_namemax: %lu\n", s.f_namemax);

        return 0;
      }
      '''
    expected = r'''
      result: 0
      errno: 0
      f_bsize: 4096
      f_frsize: 4096
      f_blocks: 1000000
      f_bfree: 500000
      f_bavail: 500000
      f_files: 1
      f_ffree: 1000000
      f_favail: 1000000
      f_fsid: 42
      f_flag: 2
      f_namemax: 255
      '''
    self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected))

  def test_libgen(self):
    src = r'''
      #include <stdio.h>
      #include <libgen.h>

      int main() {
        char p1[16] = "/usr/lib", p1x[16] = "/usr/lib";
        printf("%s -> ", p1);
        printf("%s : %s\n", dirname(p1x), basename(p1));

        char p2[16] = "/usr", p2x[16] = "/usr";
        printf("%s -> ", p2);
        printf("%s : %s\n", dirname(p2x), basename(p2));

        char p3[16] = "/usr/", p3x[16] = "/usr/";
        printf("%s -> ", p3);
        printf("%s : %s\n", dirname(p3x), basename(p3));

        char p4[16] = "/usr/lib///", p4x[16] = "/usr/lib///";
        printf("%s -> ", p4);
        printf("%s : %s\n", dirname(p4x), basename(p4));

        char p5[16] = "/", p5x[16] = "/";
        printf("%s -> ", p5);
        printf("%s : %s\n", dirname(p5x), basename(p5));

        char p6[16] = "///", p6x[16] = "///";
        printf("%s -> ", p6);
        printf("%s : %s\n", dirname(p6x), basename(p6));

        char p7[16] = "/usr/../lib/..", p7x[16] = "/usr/../lib/..";
        printf("%s -> ", p7);
        printf("%s : %s\n", dirname(p7x), basename(p7));

        char p8[16] = "", p8x[16] = "";
        printf("(empty) -> %s : %s\n", dirname(p8x), basename(p8));

        printf("(null) -> %s : %s\n", dirname(0), basename(0));

        return 0;
      }
      '''
    expected = '''
      /usr/lib -> /usr : lib
      /usr -> / : usr
      /usr/ -> / : usr
      /usr/lib/// -> /usr : lib
      / -> / : /
      /// -> / : /
      /usr/../lib/.. -> /usr/../lib : ..
      (empty) -> . : .
      (null) -> . : .
    '''
    self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected))

  def test_utime(self):
    src = open(path_from_root('tests', 'utime', 'test_utime.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_utf(self):
    self.banned_js_engines = [SPIDERMONKEY_ENGINE] # only node handles utf well
    Settings.EXPORTED_FUNCTIONS = ['_main', '_malloc']

    src = r'''
      #include <stdio.h>
      #include <emscripten.h>

      int main() {
        char *c = "μ†ℱ ╋ℯ╳╋ 😇";
        printf("%d %d %d %d %s\n", c[0]&0xff, c[1]&0xff, c[2]&0xff, c[3]&0xff, c);
        emscripten_run_script(
          "cheez = _malloc(100);"
          "Module.writeStringToMemory(\"μ†ℱ ╋ℯ╳╋ 😇\", cheez);"
          "Module.print([Pointer_stringify(cheez), Module.getValue(cheez, 'i8')&0xff, Module.getValue(cheez+1, 'i8')&0xff, Module.getValue(cheez+2, 'i8')&0xff, Module.getValue(cheez+3, 'i8')&0xff, ]);");
      }
    '''
    self.do_run(src, '206 188 226 128 μ†ℱ ╋ℯ╳╋ 😇\nμ†ℱ ╋ℯ╳╋ 😇,206,188,226,128\n');

  def test_utf32(self):
    if self.emcc_args is None: return self.skip('need libc for wcslen()')
    if not self.is_le32(): return self.skip('this test uses inline js, which requires le32')
    self.do_run(open(path_from_root('tests', 'utf32.cpp')).read(), 'OK.')
    self.do_run(open(path_from_root('tests', 'utf32.cpp')).read(), 'OK.', args=['-fshort-wchar'])

  def test_direct_string_constant_usage(self):
    if self.emcc_args is None: return self.skip('requires libcxx')

    src = '''
      #include <iostream>
      template<int i>
      void printText( const char (&text)[ i ] )
      {
         std::cout << text;
      }
      int main()
      {
        printText( "some string constant" );
        return 0;
      }
    '''
    self.do_run(src, "some string constant")

  def test_std_cout_new(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    src = '''
      #include <iostream>

      struct NodeInfo { //structure that we want to transmit to our shaders
          float x;
          float y;
          float s;
          float c;
      };
      const int nbNodes = 100;
      NodeInfo * data = new NodeInfo[nbNodes]; //our data that will be transmitted using float texture.

      template<int i>
      void printText( const char (&text)[ i ] )
      {
         std::cout << text << std::endl;
      }

      int main()
      {
        printText( "some string constant" );
        return 0;
      }
    '''

    self.do_run(src, "some string constant")

  def test_istream(self):
    if self.emcc_args is None: return self.skip('requires libcxx')

    src = '''
      #include <string>
      #include <sstream>
      #include <iostream>

      int main()
      {
          std::string mystring("1 2 3");
          std::istringstream is(mystring);
          int one, two, three;

          is >> one >> two >> three;

          printf( "%i %i %i", one, two, three );
      }
    '''
    for linkable in [0]:#, 1]:
      print linkable
      Settings.LINKABLE = linkable # regression check for issue #273
      self.do_run(src, "1 2 3")

  def test_fs_base(self):
    Settings.INCLUDE_FULL_LIBRARY = 1
    try:
      addJS = '''
def process(filename):
  import tools.shared as shared
  src = open(filename, 'r').read().replace('FS.init();', '').replace( # Disable normal initialization, replace with ours
    '// {{PRE_RUN_ADDITIONS}}',
    open(shared.path_from_root('tests', 'filesystem', 'src.js'), 'r').read())
  open(filename, 'w').write(src)
'''
      src = 'int main() {return 0;}\n'
      expected = open(path_from_root('tests', 'filesystem', 'output.txt'), 'r').read()
      self.do_run(src, expected, post_build=addJS, extra_emscripten_args=['-H', 'libc/fcntl.h,libc/sys/unistd.h,poll.h,libc/math.h,libc/langinfo.h,libc/time.h'])
    finally:
      Settings.INCLUDE_FULL_LIBRARY = 0

  def test_fs_nodefs_rw(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    src = open(path_from_root('tests', 'fs', 'test_nodefs_rw.c'), 'r').read()
    self.do_run(src, 'success', force_c=True, js_engines=[NODE_JS])

  def test_unistd_access(self):
    self.clear()
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    for fs in ['MEMFS', 'NODEFS']:
      src = open(path_from_root('tests', 'unistd', 'access.c'), 'r').read()
      expected = open(path_from_root('tests', 'unistd', 'access.out'), 'r').read()
      Building.COMPILER_TEST_OPTS += ['-D' + fs]
      self.do_run(src, expected, js_engines=[NODE_JS])

  def test_unistd_curdir(self):
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    src = open(path_from_root('tests', 'unistd', 'curdir.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'curdir.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_close(self):
    src = open(path_from_root('tests', 'unistd', 'close.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'close.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_confstr(self):
    src = open(path_from_root('tests', 'unistd', 'confstr.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'confstr.out'), 'r').read()
    self.do_run(src, expected, extra_emscripten_args=['-H', 'libc/unistd.h'])

  def test_unistd_ttyname(self):
    src = open(path_from_root('tests', 'unistd', 'ttyname.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_unistd_dup(self):
    src = open(path_from_root('tests', 'unistd', 'dup.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'dup.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_pathconf(self):
    src = open(path_from_root('tests', 'unistd', 'pathconf.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'pathconf.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_truncate(self):
    self.clear()
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    for fs in ['MEMFS', 'NODEFS']:
      src = open(path_from_root('tests', 'unistd', 'truncate.c'), 'r').read()
      expected = open(path_from_root('tests', 'unistd', 'truncate.out'), 'r').read()
      Building.COMPILER_TEST_OPTS += ['-D' + fs]
      self.do_run(src, expected, js_engines=[NODE_JS])

  def test_unistd_swab(self):
    src = open(path_from_root('tests', 'unistd', 'swab.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'swab.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_isatty(self):
    src = open(path_from_root('tests', 'unistd', 'isatty.c'), 'r').read()
    self.do_run(src, 'success', force_c=True)

  def test_unistd_sysconf(self):
    src = open(path_from_root('tests', 'unistd', 'sysconf.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'sysconf.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_login(self):
    src = open(path_from_root('tests', 'unistd', 'login.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'login.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_unlink(self):
    self.clear()
    if self.emcc_args is None: return self.skip('requires emcc')
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    for fs in ['MEMFS', 'NODEFS']:
      src = open(path_from_root('tests', 'unistd', 'unlink.c'), 'r').read()
      Building.COMPILER_TEST_OPTS += ['-D' + fs]
      self.do_run(src, 'success', force_c=True, js_engines=[NODE_JS])

  def test_unistd_links(self):
    self.clear()
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    for fs in ['MEMFS', 'NODEFS']:
      if WINDOWS and fs == 'NODEFS':
        print >> sys.stderr, 'Skipping NODEFS part of this test for test_unistd_links on Windows, since it would require administrative privileges.'
        # Also, other detected discrepancies if you do end up running this test on NODEFS:
        # test expects /, but Windows gives \ as path slashes.
        # Calling readlink() on a non-link gives error 22 EINVAL on Unix, but simply error 0 OK on Windows.
        continue
      src = open(path_from_root('tests', 'unistd', 'links.c'), 'r').read()
      expected = open(path_from_root('tests', 'unistd', 'links.out'), 'r').read()
      Building.COMPILER_TEST_OPTS += ['-D' + fs]
      self.do_run(src, expected, js_engines=[NODE_JS])

  def test_unistd_sleep(self):
    src = open(path_from_root('tests', 'unistd', 'sleep.c'), 'r').read()
    expected = open(path_from_root('tests', 'unistd', 'sleep.out'), 'r').read()
    self.do_run(src, expected)

  def test_unistd_io(self):
    self.clear()
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    if self.run_name == 'o2': return self.skip('non-asm optimized builds can fail with inline js')
    if self.emcc_args is None: return self.skip('requires emcc')
    for fs in ['MEMFS', 'NODEFS']:
      src = open(path_from_root('tests', 'unistd', 'io.c'), 'r').read()
      expected = open(path_from_root('tests', 'unistd', 'io.out'), 'r').read()
      Building.COMPILER_TEST_OPTS += ['-D' + fs]
      self.do_run(src, expected, js_engines=[NODE_JS])

  def test_unistd_misc(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if not self.is_le32(): return self.skip('le32 needed for inline js')
    for fs in ['MEMFS', 'NODEFS']:
      src = open(path_from_root('tests', 'unistd', 'misc.c'), 'r').read()
      expected = open(path_from_root('tests', 'unistd', 'misc.out'), 'r').read()
      Building.COMPILER_TEST_OPTS += ['-D' + fs]
      self.do_run(src, expected, js_engines=[NODE_JS])

  def test_uname(self):
    src = r'''
      #include <stdio.h>
      #include <sys/utsname.h>

      int main() {
        struct utsname u;
        printf("ret: %d\n", uname(&u));
        printf("sysname: %s\n", u.sysname);
        printf("nodename: %s\n", u.nodename);
        printf("release: %s\n", u.release);
        printf("version: %s\n", u.version);
        printf("machine: %s\n", u.machine);
        printf("invalid: %d\n", uname(0));
        return 0;
      }
      '''
    expected = '''
      ret: 0
      sysname: Emscripten
      nodename: emscripten
      release: 1.0
      version: #1
      machine: x86-JS
    '''
    self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected))

  def test_env(self):
    src = open(path_from_root('tests', 'env', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'env', 'output.txt'), 'r').read()
    self.do_run(src, expected)

  def test_systypes(self):
    src = open(path_from_root('tests', 'systypes', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'systypes', 'output.txt'), 'r').read()
    self.do_run(src, expected)

  def test_getloadavg(self):
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main() {
        double load[5] = {42.13, 42.13, 42.13, 42.13, 42.13};
        printf("ret: %d\n", getloadavg(load, 5));
        printf("load[0]: %.3lf\n", load[0]);
        printf("load[1]: %.3lf\n", load[1]);
        printf("load[2]: %.3lf\n", load[2]);
        printf("load[3]: %.3lf\n", load[3]);
        printf("load[4]: %.3lf\n", load[4]);
        return 0;
      }
      '''
    expected = '''
      ret: 3
      load[0]: 0.100
      load[1]: 0.100
      load[2]: 0.100
      load[3]: 42.130
      load[4]: 42.130
    '''
    self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected))

  def test_799(self):
    src = open(path_from_root('tests', '799.cpp'), 'r').read()
    self.do_run(src, '''Set PORT family: 0, port: 3979
Get PORT family: 0
PORT: 3979
''')

  def test_ctype(self):
    # The bit fiddling done by the macros using __ctype_b_loc requires this.
    Settings.CORRECT_SIGNS = 1
    src = open(path_from_root('tests', 'ctype', 'src.c'), 'r').read()
    expected = open(path_from_root('tests', 'ctype', 'output.txt'), 'r').read()
    self.do_run(src, expected)

  def test_strcasecmp(self):
    src = r'''
      #include <stdio.h>
      #include <strings.h>
      int sign(int x) {
        if (x < 0) return -1;
        if (x > 0) return 1;
        return 0;
      }
      int main() {
        printf("*\n");

        printf("%d\n", sign(strcasecmp("hello", "hello")));
        printf("%d\n", sign(strcasecmp("hello1", "hello")));
        printf("%d\n", sign(strcasecmp("hello", "hello1")));
        printf("%d\n", sign(strcasecmp("hello1", "hello1")));
        printf("%d\n", sign(strcasecmp("iello", "hello")));
        printf("%d\n", sign(strcasecmp("hello", "iello")));
        printf("%d\n", sign(strcasecmp("A", "hello")));
        printf("%d\n", sign(strcasecmp("Z", "hello")));
        printf("%d\n", sign(strcasecmp("a", "hello")));
        printf("%d\n", sign(strcasecmp("z", "hello")));
        printf("%d\n", sign(strcasecmp("hello", "a")));
        printf("%d\n", sign(strcasecmp("hello", "z")));

        printf("%d\n", sign(strcasecmp("Hello", "hello")));
        printf("%d\n", sign(strcasecmp("Hello1", "hello")));
        printf("%d\n", sign(strcasecmp("Hello", "hello1")));
        printf("%d\n", sign(strcasecmp("Hello1", "hello1")));
        printf("%d\n", sign(strcasecmp("Iello", "hello")));
        printf("%d\n", sign(strcasecmp("Hello", "iello")));
        printf("%d\n", sign(strcasecmp("A", "hello")));
        printf("%d\n", sign(strcasecmp("Z", "hello")));
        printf("%d\n", sign(strcasecmp("a", "hello")));
        printf("%d\n", sign(strcasecmp("z", "hello")));
        printf("%d\n", sign(strcasecmp("Hello", "a")));
        printf("%d\n", sign(strcasecmp("Hello", "z")));

        printf("%d\n", sign(strcasecmp("hello", "Hello")));
        printf("%d\n", sign(strcasecmp("hello1", "Hello")));
        printf("%d\n", sign(strcasecmp("hello", "Hello1")));
        printf("%d\n", sign(strcasecmp("hello1", "Hello1")));
        printf("%d\n", sign(strcasecmp("iello", "Hello")));
        printf("%d\n", sign(strcasecmp("hello", "Iello")));
        printf("%d\n", sign(strcasecmp("A", "Hello")));
        printf("%d\n", sign(strcasecmp("Z", "Hello")));
        printf("%d\n", sign(strcasecmp("a", "Hello")));
        printf("%d\n", sign(strcasecmp("z", "Hello")));
        printf("%d\n", sign(strcasecmp("hello", "a")));
        printf("%d\n", sign(strcasecmp("hello", "z")));

        printf("%d\n", sign(strcasecmp("Hello", "Hello")));
        printf("%d\n", sign(strcasecmp("Hello1", "Hello")));
        printf("%d\n", sign(strcasecmp("Hello", "Hello1")));
        printf("%d\n", sign(strcasecmp("Hello1", "Hello1")));
        printf("%d\n", sign(strcasecmp("Iello", "Hello")));
        printf("%d\n", sign(strcasecmp("Hello", "Iello")));
        printf("%d\n", sign(strcasecmp("A", "Hello")));
        printf("%d\n", sign(strcasecmp("Z", "Hello")));
        printf("%d\n", sign(strcasecmp("a", "Hello")));
        printf("%d\n", sign(strcasecmp("z", "Hello")));
        printf("%d\n", sign(strcasecmp("Hello", "a")));
        printf("%d\n", sign(strcasecmp("Hello", "z")));

        printf("%d\n", sign(strncasecmp("hello", "hello", 3)));
        printf("%d\n", sign(strncasecmp("hello1", "hello", 3)));
        printf("%d\n", sign(strncasecmp("hello", "hello1", 3)));
        printf("%d\n", sign(strncasecmp("hello1", "hello1", 3)));
        printf("%d\n", sign(strncasecmp("iello", "hello", 3)));
        printf("%d\n", sign(strncasecmp("hello", "iello", 3)));
        printf("%d\n", sign(strncasecmp("A", "hello", 3)));
        printf("%d\n", sign(strncasecmp("Z", "hello", 3)));
        printf("%d\n", sign(strncasecmp("a", "hello", 3)));
        printf("%d\n", sign(strncasecmp("z", "hello", 3)));
        printf("%d\n", sign(strncasecmp("hello", "a", 3)));
        printf("%d\n", sign(strncasecmp("hello", "z", 3)));

        printf("*\n");

        return 0;
      }
    '''
    self.do_run(src, '''*\n0\n1\n-1\n0\n1\n-1\n-1\n1\n-1\n1\n1\n-1\n0\n1\n-1\n0\n1\n-1\n-1\n1\n-1\n1\n1\n-1\n0\n1\n-1\n0\n1\n-1\n-1\n1\n-1\n1\n1\n-1\n0\n1\n-1\n0\n1\n-1\n-1\n1\n-1\n1\n1\n-1\n0\n0\n0\n0\n1\n-1\n-1\n1\n-1\n1\n1\n-1\n*\n''')

  def test_atomic(self):
    src = '''
      #include <stdio.h>
      int main() {
        int x = 10;
        int y = __sync_add_and_fetch(&x, 5);
        printf("*%d,%d*\\n", x, y);
        x = 10;
        y = __sync_fetch_and_add(&x, 5);
        printf("*%d,%d*\\n", x, y);
        x = 10;
        y = __sync_lock_test_and_set(&x, 6);
        printf("*%d,%d*\\n", x, y);
        x = 10;
        y = __sync_bool_compare_and_swap(&x, 9, 7);
        printf("*%d,%d*\\n", x, y);
        y = __sync_bool_compare_and_swap(&x, 10, 7);
        printf("*%d,%d*\\n", x, y);
        return 0;
      }
    '''

    self.do_run(src, '*15,15*\n*15,10*\n*6,10*\n*10,0*\n*7,1*')

  def test_phiundef(self):
    src = r'''
#include <stdlib.h>
#include <stdio.h>

static int state;

struct my_struct {
union {
  struct {
    unsigned char a;
    unsigned char b;
  } c;
  unsigned int d;
} e;
unsigned int f;
};

int main(int argc, char **argv) {
  struct my_struct r;

  state = 0;

  for (int i=0;i<argc+10;i++)
  {
      if (state % 2 == 0)
          r.e.c.a = 3;
      else
          printf("%d\n", r.e.c.a);
      state++;
  }
  return 0;
}
    '''

    self.do_run(src, '3\n3\n3\n3\n3\n')

  # libc++ tests

  def test_iostream(self):
    if Settings.QUANTUM_SIZE == 1: return self.skip("we don't support libcxx in q1")
    if self.emcc_args is None: return self.skip('needs ta2 and emcc')

    src = '''
      #include <iostream>

      int main()
      {
        std::cout << "hello world" << std::endl << 77 << "." << std::endl;
        return 0;
      }
    '''

    # FIXME: should not have so many newlines in output here
    self.do_run(src, 'hello world\n77.\n')

  def test_stdvec(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    src = '''
      #include <vector>
      #include <stdio.h>

      struct S {
          int a;
          float b;
      };

      void foo(int a, float b)
      {
        printf("%d:%.2f\\n", a, b);
      }

      int main ( int argc, char *argv[] )
      {
        std::vector<S> ar;
        S s;

        s.a = 789;
        s.b = 123.456f;
        ar.push_back(s);

        s.a = 0;
        s.b = 100.1f;
        ar.push_back(s);

        foo(ar[0].a, ar[0].b);
        foo(ar[1].a, ar[1].b);
      }
    '''

    self.do_run(src, '789:123.46\n0:100.1')

  def test_reinterpreted_ptrs(self):
    if self.emcc_args is None: return self.skip('needs emcc and libc')

    src = r'''
#include <stdio.h>

class Foo {
private:
  float bar;
public:
  int baz;

  Foo(): bar(0), baz(4711) {};

  int getBar() const;
};

int Foo::getBar() const {
  return this->bar;
};

const Foo *magic1 = reinterpret_cast<Foo*>(0xDEAD111F);
const Foo *magic2 = reinterpret_cast<Foo*>(0xDEAD888F);

static void runTest() {

  const Foo *a = new Foo();
  const Foo *b = a;

  if (a->getBar() == 0) {
      if (a->baz == 4712)
          b = magic1;
      else
          b = magic2;
  }

  printf("%s\n", (b == magic1 ? "magic1" : (b == magic2 ? "magic2" : "neither")));
};

extern "C" {
  int main(int argc, char **argv) {
      runTest();
  }
}
'''
    self.do_run(src, 'magic2')

  def test_jansson(self):
      return self.skip('currently broken')

      if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2')
      if Settings.SAFE_HEAP: return self.skip('jansson is not safe-heap safe')

      src = '''
        #include <jansson.h>
        #include <stdio.h>
        #include <string.h>

        int main()
        {
          const char* jsonString = "{\\"key\\": \\"value\\",\\"array\\": [\\"array_item1\\",\\"array_item2\\",\\"array_item3\\"],\\"dict\\":{\\"number\\": 3,\\"float\\": 2.2}}";

          json_error_t error;
          json_t *root = json_loadb(jsonString, strlen(jsonString), 0, &error);

          if(!root) {
            printf("Node `root` is `null`.");
            return 0;
          }

          if(!json_is_object(root)) {
            printf("Node `root` is no object.");
            return 0;
          }

          printf("%s\\n", json_string_value(json_object_get(root, "key")));

          json_t *array = json_object_get(root, "array");
          if(!array) {
            printf("Node `array` is `null`.");
            return 0;
          }

          if(!json_is_array(array)) {
            printf("Node `array` is no array.");
            return 0;
          }

          for(size_t i=0; i<json_array_size(array); ++i)
          {
            json_t *arrayNode = json_array_get(array, i);
            if(!root || !json_is_string(arrayNode))
              return 0;
            printf("%s\\n", json_string_value(arrayNode));
          }

          json_t *dict = json_object_get(root, "dict");
          if(!dict || !json_is_object(dict))
            return 0;

          json_t *numberNode = json_object_get(dict, "number");
          json_t *floatNode = json_object_get(dict, "float");

          if(!numberNode || !json_is_number(numberNode) ||
             !floatNode || !json_is_real(floatNode))
            return 0;

          printf("%i\\n", json_integer_value(numberNode));
          printf("%.2f\\n", json_number_value(numberNode));
          printf("%.2f\\n", json_real_value(floatNode));

          json_t *invalidNode = json_object_get(dict, "invalidNode");
          if(invalidNode)
            return 0;

          printf("%i\\n", json_number_value(invalidNode));

          json_decref(root);

          if(!json_is_object(root))
            printf("jansson!\\n");

          return 0;
        }
      '''
      self.do_run(src, 'value\narray_item1\narray_item2\narray_item3\n3\n3.00\n2.20\nJansson: Node with ID `0` not found. Context has `10` nodes.\n0\nJansson: No JSON context.\njansson!')

  ### 'Medium' tests

  def test_fannkuch(self):
      results = [ (1,0), (2,1), (3,2), (4,4), (5,7), (6,10), (7, 16), (8,22) ]
      for i, j in results:
        src = open(path_from_root('tests', 'fannkuch.cpp'), 'r').read()
        self.do_run(src, 'Pfannkuchen(%d) = %d.' % (i,j), [str(i)], no_build=i>1)

  def test_raytrace(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      if Settings.USE_TYPED_ARRAYS == 2: return self.skip('Relies on double value rounding, extremely sensitive')

      src = open(path_from_root('tests', 'raytrace.cpp'), 'r').read().replace('double', 'float')
      output = open(path_from_root('tests', 'raytrace.ppm'), 'r').read()
      self.do_run(src, output, ['3', '16'])#, build_ll_hook=self.do_autodebug)

  def test_fasta(self):
      if self.emcc_args is None: return self.skip('requires emcc')
      results = [ (1,'''GG*ctt**tgagc*'''), (20,'''GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTT*cttBtatcatatgctaKggNcataaaSatgtaaaDcDRtBggDtctttataattcBgtcg**tacgtgtagcctagtgtttgtgttgcgttatagtctatttgtggacacagtatggtcaaa**tgacgtcttttgatctgacggcgttaacaaagatactctg*'''),
(50,'''GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGA*TCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACAT*cttBtatcatatgctaKggNcataaaSatgtaaaDcDRtBggDtctttataattcBgtcg**tactDtDagcctatttSVHtHttKtgtHMaSattgWaHKHttttagacatWatgtRgaaa**NtactMcSMtYtcMgRtacttctWBacgaa**agatactctgggcaacacacatacttctctcatgttgtttcttcggacctttcataacct**ttcctggcacatggttagctgcacatcacaggattgtaagggtctagtggttcagtgagc**ggaatatcattcgtcggtggtgttaatctatctcggtgtagcttataaatgcatccgtaa**gaatattatgtttatttgtcggtacgttcatggtagtggtgtcgccgatttagacgtaaa**ggcatgtatg*''') ]
      for i, j in results:
        src = open(path_from_root('tests', 'fasta.cpp'), 'r').read()
        self.do_run(src, j, [str(i)], lambda x, err: x.replace('\n', '*'), no_build=i>1)

  def test_whets(self):
    if not Settings.ASM_JS: return self.skip('mainly a test for asm validation here')
    self.do_run(open(path_from_root('tests', 'whets.cpp')).read(), 'Single Precision C Whetstone Benchmark')

  def test_dlmalloc(self):
    if self.emcc_args is None: self.emcc_args = [] # dlmalloc auto-inclusion is only done if we use emcc

    self.banned_js_engines = [NODE_JS] # slower, and fail on 64-bit
    Settings.CORRECT_SIGNS = 2
    Settings.CORRECT_SIGNS_LINES = ['src.cpp:' + str(i+4) for i in [4816, 4191, 4246, 4199, 4205, 4235, 4227]]
    Settings.TOTAL_MEMORY = 128*1024*1024 # needed with typed arrays

    src = open(path_from_root('system', 'lib', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()
    self.do_run(src, '*1,0*', ['200', '1'])
    self.do_run(src, '*400,0*', ['400', '400'], no_build=True)

    # Linked version
    src = open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()
    self.do_run(src, '*1,0*', ['200', '1'], extra_emscripten_args=['-m'])
    self.do_run(src, '*400,0*', ['400', '400'], extra_emscripten_args=['-m'], no_build=True)

    if self.emcc_args == []: # TODO: do this in other passes too, passing their opts into emcc
      # emcc should build in dlmalloc automatically, and do all the sign correction etc. for it

      try_delete(os.path.join(self.get_dir(), 'src.cpp.o.js'))
      output = Popen([PYTHON, EMCC, path_from_root('tests', 'dlmalloc_test.c'), '-s', 'TOTAL_MEMORY=' + str(128*1024*1024),
                      '-o', os.path.join(self.get_dir(), 'src.cpp.o.js')], stdout=PIPE, stderr=self.stderr_redirect).communicate()

      self.do_run('x', '*1,0*', ['200', '1'], no_build=True)
      self.do_run('x', '*400,0*', ['400', '400'], no_build=True)

      # The same for new and all its variants
      src = open(path_from_root('tests', 'new.cpp')).read()
      for new, delete in [
        ('malloc(100)', 'free'),
        ('new char[100]', 'delete[]'),
        ('new Structy', 'delete'),
        ('new int', 'delete'),
        ('new Structy[10]', 'delete[]'),
      ]:
        self.do_run(src.replace('{{{ NEW }}}', new).replace('{{{ DELETE }}}', delete), '*1,0*')

  def test_dlmalloc_partial(self):
    if self.emcc_args is None: return self.skip('only emcc will link in dlmalloc')
    # present part of the symbols of dlmalloc, not all
    src = open(path_from_root('tests', 'new.cpp')).read().replace('{{{ NEW }}}', 'new int').replace('{{{ DELETE }}}', 'delete') + '''
void *
operator new(size_t size)
{
printf("new %d!\\n", size);
return malloc(size);
}
'''
    self.do_run(src, 'new 4!\n*1,0*')

  def test_dlmalloc_partial_2(self):
    if self.emcc_args is None or 'SAFE_HEAP' in str(self.emcc_args) or 'CHECK_HEAP_ALIGN' in str(self.emcc_args): return self.skip('only emcc will link in dlmalloc, and we do unsafe stuff')
    # present part of the symbols of dlmalloc, not all. malloc is harder to link than new which is weak.
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>
      void *malloc(size_t size)
      {
        return (void*)123;
      }
      int main() {
        void *x = malloc(10);
        printf("got %p\n", x);
        free(x);
        printf("freed the faker\n");
        return 1;
      }
'''
    self.do_run(src, 'got 0x7b\nfreed')

  def test_libcxx(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    self.do_run(open(path_from_root('tests', 'hashtest.cpp')).read(),
                 'june -> 30\nPrevious (in alphabetical order) is july\nNext (in alphabetical order) is march')

    self.do_run('''
      #include <set>
      #include <stdio.h>
      int main() {
        std::set<int> *fetchOriginatorNums = new std::set<int>();
        fetchOriginatorNums->insert(171);
        printf("hello world\\n");
        return 0;
      }
      ''', 'hello world');

  def test_typeid(self):
    self.do_run(r'''
      #include <stdio.h>
      #include <string.h>
      #include <typeinfo>
      int main() {
        printf("*\n");
        #define MAX 100
        int ptrs[MAX];
        int groups[MAX];
        memset(ptrs, 0, MAX*sizeof(int));
        memset(groups, 0, MAX*sizeof(int));
        int next_group = 1;
        #define TEST(X) { \
          int ptr = (int)&typeid(X); \
          int group = 0; \
          int i; \
          for (i = 0; i < MAX; i++) { \
            if (!groups[i]) break; \
            if (ptrs[i] == ptr) { \
              group = groups[i]; \
              break; \
            } \
          } \
          if (!group) { \
            groups[i] = group = next_group++; \
            ptrs[i] = ptr; \
          } \
          printf("%s:%d\n", #X, group); \
        }
        TEST(int);
        TEST(unsigned int);
        TEST(unsigned);
        TEST(signed int);
        TEST(long);
        TEST(unsigned long);
        TEST(signed long);
        TEST(long long);
        TEST(unsigned long long);
        TEST(signed long long);
        TEST(short);
        TEST(unsigned short);
        TEST(signed short);
        TEST(char);
        TEST(unsigned char);
        TEST(signed char);
        TEST(float);
        TEST(double);
        TEST(long double);
        TEST(void);
        TEST(void*);
        printf("*\n");
      }
      ''', '''*
int:1
unsigned int:2
unsigned:2
signed int:1
long:3
unsigned long:4
signed long:3
long long:5
unsigned long long:6
signed long long:5
short:7
unsigned short:8
signed short:7
char:9
unsigned char:10
signed char:11
float:12
double:13
long double:14
void:15
void*:16
*
''');

  def test_static_variable(self):
    if self.emcc_args is None: Settings.SAFE_HEAP = 0 # LLVM mixes i64 and i8 in the guard check
    src = '''
      #include <stdio.h>

      struct DATA
      {
          int value;

          DATA()
          {
              value = 0;
          }
      };

      DATA & GetData()
      {
          static DATA data;

          return data;
      }

      int main()
      {
          GetData().value = 10;
          printf( "value:%i", GetData().value );
      }
    '''
    self.do_run(src, 'value:10')

  def test_fakestat(self):
    src = r'''
      #include <stdio.h>
      struct stat { int x, y; };
      int main() {
        stat s;
        s.x = 10;
        s.y = 22;
        printf("*%d,%d*\n", s.x, s.y);
      }
    '''
    self.do_run(src, '*10,22*')

  def test_mmap(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    Settings.TOTAL_MEMORY = 128*1024*1024

    src = '''
      #include <stdio.h>
      #include <sys/mman.h>
      #include <assert.h>

      int main(int argc, char *argv[]) {
          for (int i = 0; i < 10; i++) {
            int* map = (int*)mmap(0, 5000, PROT_READ | PROT_WRITE,
                    MAP_SHARED | MAP_ANON, -1, 0);
            /* TODO: Should we align to 4k?
            assert(((int)map) % 4096 == 0); // aligned
            */
            assert(munmap(map, 5000) == 0);
          }

          const int NUM_BYTES = 8 * 1024 * 1024;
          const int NUM_INTS = NUM_BYTES / sizeof(int);

          int* map = (int*)mmap(0, NUM_BYTES, PROT_READ | PROT_WRITE,
                  MAP_SHARED | MAP_ANON, -1, 0);
          assert(map != MAP_FAILED);

          int i;

          for (i = 0; i < NUM_INTS; i++) {
              map[i] = i;
          }

          for (i = 0; i < NUM_INTS; i++) {
              assert(map[i] == i);
          }

          assert(munmap(map, NUM_BYTES) == 0);

          printf("hello,world");
          return 0;
      }
    '''
    self.do_run(src, 'hello,world')
    self.do_run(src, 'hello,world', force_c=True)

  def test_mmap_file(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    self.emcc_args += ['--embed-file', 'data.dat']

    open(self.in_dir('data.dat'), 'w').write('data from the file ' + ('.' * 9000))

    src = r'''
      #include <stdio.h>
      #include <sys/mman.h>

      int main() {
        printf("*\n");
        FILE *f = fopen("data.dat", "r");
        char *m;
        m = (char*)mmap(NULL, 9000, PROT_READ, MAP_PRIVATE, fileno(f), 0);
        for (int i = 0; i < 20; i++) putchar(m[i]);
        munmap(m, 9000);
        printf("\n");
        m = (char*)mmap(NULL, 9000, PROT_READ, MAP_PRIVATE, fileno(f), 5);
        for (int i = 0; i < 20; i++) putchar(m[i]);
        munmap(m, 9000);
        printf("\n*\n");
        return 0;
      }
    '''
    self.do_run(src, '*\ndata from the file .\nfrom the file ......\n*\n')

  def test_cubescript(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if self.run_name == 'o2':
      self.emcc_args += ['--closure', '1'] # Use closure here for some additional coverage

    Building.COMPILER_TEST_OPTS = filter(lambda x: x != '-g', Building.COMPILER_TEST_OPTS) # remove -g, so we have one test without it by default
    if self.emcc_args is None: Settings.SAFE_HEAP = 0 # Has some actual loads of unwritten-to places, in the C++ code...

    # Overflows happen in hash loop
    Settings.CORRECT_OVERFLOWS = 1
    Settings.CHECK_OVERFLOWS = 0

    if Settings.USE_TYPED_ARRAYS == 2:
      Settings.CORRECT_SIGNS = 1

    self.do_run(path_from_root('tests', 'cubescript'), '*\nTemp is 33\n9\n5\nhello, everyone\n*', main_file='command.cpp')

    assert 'asm2g' in test_modes
    if self.run_name == 'asm2g':
      results = {}
      original = open('src.cpp.o.js').read()
      results[Settings.ALIASING_FUNCTION_POINTERS] = len(original)
      Settings.ALIASING_FUNCTION_POINTERS = 1 - Settings.ALIASING_FUNCTION_POINTERS
      self.do_run(path_from_root('tests', 'cubescript'), '*\nTemp is 33\n9\n5\nhello, everyone\n*', main_file='command.cpp')
      final = open('src.cpp.o.js').read()
      results[Settings.ALIASING_FUNCTION_POINTERS] = len(final)
      open('original.js', 'w').write(original)
      print results
      assert results[1] < 0.99*results[0]
      assert ' & 3]()' in original, 'small function table exists'
      assert ' & 3]()' not in final, 'small function table does not exist'
      assert ' & 255]()' not in original, 'big function table does not exist'
      assert ' & 255]()' in final, 'big function table exists'

    assert 'asm1' in test_modes
    if self.run_name == 'asm1':
      generated = open('src.cpp.o.js').read()
      main = generated[generated.find('function runPostSets'):]
      main = main[:main.find('\n}')]
      assert main.count('\n') == 7, 'must not emit too many postSets: %d' % main.count('\n')

  def test_simd(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2')
    if Settings.ASM_JS: Settings.ASM_JS = 2 # does not validate
    src = r'''
#include <stdio.h>

#include <emscripten/vector.h>

static inline float32x4 __attribute__((always_inline))
_mm_set_ps(const float __Z, const float __Y, const float __X, const float __W)
{
  return (float32x4){ __W, __X, __Y, __Z };
}

static __inline__ float32x4 __attribute__((__always_inline__))
_mm_setzero_ps(void)
{
  return (float32x4){ 0.0, 0.0, 0.0, 0.0 };
}

int main(int argc, char **argv) {
  float data[8];
  for (int i = 0; i < 32; i++) data[i] = (1+i+argc)*(2+i+argc*argc);
  {
    float32x4 *a = (float32x4*)&data[0];
    float32x4 *b = (float32x4*)&data[4];
    float32x4 c, d;
    c = *a;
    d = *b;
    printf("1floats! %d, %d, %d, %d   %d, %d, %d, %d\n", (int)c[0], (int)c[1], (int)c[2], (int)c[3], (int)d[0], (int)d[1], (int)d[2], (int)d[3]);
    c = c+d;
    printf("2floats! %d, %d, %d, %d   %d, %d, %d, %d\n", (int)c[0], (int)c[1], (int)c[2], (int)c[3], (int)d[0], (int)d[1], (int)d[2], (int)d[3]);
    d = c*d;
    printf("3floats! %d, %d, %d, %d   %d, %d, %d, %d\n", (int)c[0], (int)c[1], (int)c[2], (int)c[3], (int)d[0], (int)d[1], (int)d[2], (int)d[3]);
    c = _mm_setzero_ps();
    printf("zeros %d, %d, %d, %d\n", (int)c[0], (int)c[1], (int)c[2], (int)c[3]);
  }
  {
    uint32x4 *a = (uint32x4*)&data[0];
    uint32x4 *b = (uint32x4*)&data[4];
    uint32x4 c, d, e, f;
    c = *a;
    d = *b;
    printf("4uints! %d, %d, %d, %d   %d, %d, %d, %d\n", c[0], c[1], c[2], c[3], d[0], d[1], d[2], d[3]);
    e = c+d;
    f = c-d;
    printf("5uints! %d, %d, %d, %d   %d, %d, %d, %d\n", e[0], e[1], e[2], e[3], f[0], f[1], f[2], f[3]);
  }
  {
    float32x4 c, d, e, f;
    c = _mm_set_ps(9.0, 4.0, 0, -9.0);
    d = _mm_set_ps(10.0, 14.0, -12, -2.0);
    printf("6floats! %d, %d, %d, %d   %d, %d, %d, %d\n", (int)c[0], (int)c[1], (int)c[2], (int)c[3], (int)d[0], (int)d[1], (int)d[2], (int)d[3]);
    printf("7calcs: %d\n", emscripten_float32x4_signmask(c)); // TODO: just not just compilation but output as well
  }

  return 0;
}
    '''

    self.do_run(src, '''1floats! 6, 12, 20, 30   42, 56, 72, 90
2floats! 48, 68, 92, 120   42, 56, 72, 90
3floats! 48, 68, 92, 120   2016, 3808, 6624, 10800
zeros 0, 0, 0, 0
4uints! 1086324736, 1094713344, 1101004800, 1106247680   1109917696, 1113587712, 1116733440, 1119092736
5uints! -2098724864, -2086666240, -2077229056, -2069626880   -23592960, -18874368, -15728640, -12845056
6floats! -9, 0, 4, 9   -2, -12, 14, 10
''')

  def test_simd2(self):
    if Settings.ASM_JS: Settings.ASM_JS = 2 # does not validate

    self.do_run(r'''
      #include <stdio.h>

      typedef float __m128 __attribute__ ((__vector_size__ (16)));

      static inline __m128 __attribute__((always_inline))
      _mm_set_ps(const float __Z, const float __Y, const float __X, const float __W)
      {
        return (__m128){ __W, __X, __Y, __Z };
      }

      static inline void __attribute__((always_inline))
      _mm_store_ps(float *__P, __m128 __A)
      {
        *(__m128 *)__P = __A;
      }

      static inline __m128 __attribute__((always_inline))
      _mm_add_ps(__m128 __A, __m128 __B)
      {
        return __A + __B;
      }

      using namespace std;

      int main(int argc, char ** argv) {
          float __attribute__((__aligned__(16))) ar[4];
          __m128 v1 = _mm_set_ps(9.0, 4.0, 0, -9.0);
          __m128 v2 = _mm_set_ps(7.0, 3.0, 2.5, 1.0);
          __m128 v3 = _mm_add_ps(v1, v2);
          _mm_store_ps(ar, v3);
          
          for (int i = 0; i < 4; i++) {
              printf("%f\n", ar[i]);
          }
          
          return 0;
      }
      ''', '''-8.000000
2.500000
7.000000
16.000000
''')

  def test_gcc_unmangler(self):
    Settings.NAMED_GLOBALS = 1 # test coverage for this

    Building.COMPILER_TEST_OPTS += ['-I' + path_from_root('third_party')]

    self.do_run(open(path_from_root('third_party', 'gcc_demangler.c')).read(), '*d_demangle(char const*, int, unsigned int*)*', args=['_ZL10d_demanglePKciPj'])

    #### Code snippet that is helpful to search for nonportable optimizations ####
    #global LLVM_OPT_OPTS
    #for opt in ['-aa-eval', '-adce', '-always-inline', '-argpromotion', '-basicaa', '-basiccg', '-block-placement', '-break-crit-edges', '-codegenprepare', '-constmerge', '-constprop', '-correlated-propagation', '-count-aa', '-dce', '-deadargelim', '-deadtypeelim', '-debug-aa', '-die', '-domfrontier', '-domtree', '-dse', '-extract-blocks', '-functionattrs', '-globaldce', '-globalopt', '-globalsmodref-aa', '-gvn', '-indvars', '-inline', '-insert-edge-profiling', '-insert-optimal-edge-profiling', '-instcombine', '-instcount', '-instnamer', '-internalize', '-intervals', '-ipconstprop', '-ipsccp', '-iv-users', '-jump-threading', '-lazy-value-info', '-lcssa', '-lda', '-libcall-aa', '-licm', '-lint', '-live-values', '-loop-deletion', '-loop-extract', '-loop-extract-single', '-loop-index-split', '-loop-reduce', '-loop-rotate', '-loop-unroll', '-loop-unswitch', '-loops', '-loopsimplify', '-loweratomic', '-lowerinvoke', '-lowersetjmp', '-lowerswitch', '-mem2reg', '-memcpyopt', '-memdep', '-mergefunc', '-mergereturn', '-module-debuginfo', '-no-aa', '-no-profile', '-partial-inliner', '-partialspecialization', '-pointertracking', '-postdomfrontier', '-postdomtree', '-preverify', '-prune-eh', '-reassociate', '-reg2mem', '-regions', '-scalar-evolution', '-scalarrepl', '-sccp', '-scev-aa', '-simplify-libcalls', '-simplify-libcalls-halfpowr', '-simplifycfg', '-sink', '-split-geps', '-sretpromotion', '-strip', '-strip-dead-debug-info', '-strip-dead-prototypes', '-strip-debug-declare', '-strip-nondebug', '-tailcallelim', '-tailduplicate', '-targetdata', '-tbaa']:
    #  LLVM_OPT_OPTS = [opt]
    #  try:
    #    self.do_run(path_from_root(['third_party']), '*d_demangle(char const*, int, unsigned int*)*', args=['_ZL10d_demanglePKciPj'], main_file='gcc_demangler.c')
    #    print opt, "ok"
    #  except:
    #    print opt, "FAIL"

  def test_lua(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if Settings.QUANTUM_SIZE == 1: return self.skip('TODO: make this work')

    self.do_run('',
                'hello lua world!\n17\n1\n2\n3\n4\n7',
                args=['-e', '''print("hello lua world!");print(17);for x = 1,4 do print(x) end;print(10-3)'''],
                libraries=self.get_library('lua', [os.path.join('src', 'lua'), os.path.join('src', 'liblua.a')], make=['make', 'generic'], configure=None),
                includes=[path_from_root('tests', 'lua')],
                output_nicerizer=lambda string, err: (string + err).replace('\n\n', '\n').replace('\n\n', '\n'))

  def get_freetype(self):
    Settings.DEAD_FUNCTIONS += ['_inflateEnd', '_inflate', '_inflateReset', '_inflateInit2_']

    return self.get_library('freetype',
                            os.path.join('objs', '.libs', 'libfreetype.a'))

  def test_freetype(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if Settings.QUANTUM_SIZE == 1: return self.skip('TODO: Figure out and try to fix')

    assert 'asm2g' in test_modes
    if self.run_name == 'asm2g':
      Settings.ALIASING_FUNCTION_POINTERS = 1 - Settings.ALIASING_FUNCTION_POINTERS # flip for some more coverage here

    if Settings.CORRECT_SIGNS == 0: Settings.CORRECT_SIGNS = 1 # Not sure why, but needed

    post = '''
def process(filename):
  import tools.shared as shared
  # Embed the font into the document
  src = open(filename, 'r').read().replace(
    '// {{PRE_RUN_ADDITIONS}}',
    "FS.createDataFile('/', 'font.ttf', %s, true, false);" % str(
      map(ord, open(shared.path_from_root('tests', 'freetype', 'LiberationSansBold.ttf'), 'rb').read())
    )
  )
  open(filename, 'w').write(src)
'''

    # Not needed for js, but useful for debugging
    shutil.copyfile(path_from_root('tests', 'freetype', 'LiberationSansBold.ttf'), os.path.join(self.get_dir(), 'font.ttf'))

    # Main
    for outlining in [0, 5000]:
      Settings.OUTLINING_LIMIT = outlining
      print >> sys.stderr, 'outlining:', outlining
      self.do_run(open(path_from_root('tests', 'freetype', 'main.c'), 'r').read(),
                   open(path_from_root('tests', 'freetype', 'ref.txt'), 'r').read(),
                   ['font.ttf', 'test!', '150', '120', '25'],
                   libraries=self.get_freetype(),
                   includes=[path_from_root('tests', 'freetype', 'include')],
                   post_build=post)

    # github issue 324
    print '[issue 324]'
    self.do_run(open(path_from_root('tests', 'freetype', 'main_2.c'), 'r').read(),
                 open(path_from_root('tests', 'freetype', 'ref_2.txt'), 'r').read(),
                 ['font.ttf', 'w', '32', '32', '25'],
                 libraries=self.get_freetype(),
                 includes=[path_from_root('tests', 'freetype', 'include')],
                 post_build=post)

    print '[issue 324 case 2]'
    self.do_run(open(path_from_root('tests', 'freetype', 'main_3.c'), 'r').read(),
                 open(path_from_root('tests', 'freetype', 'ref_3.txt'), 'r').read(),
                 ['font.ttf', 'W', '32', '32', '0'],
                 libraries=self.get_freetype(),
                 includes=[path_from_root('tests', 'freetype', 'include')],
                 post_build=post)

    print '[issue 324 case 3]'
    self.do_run('',
                 open(path_from_root('tests', 'freetype', 'ref_4.txt'), 'r').read(),
                 ['font.ttf', 'ea', '40', '32', '0'],
                 no_build=True)

  def test_sqlite(self):
    # gcc -O3 -I/home/alon/Dev/emscripten/tests/sqlite -ldl src.c
    if self.emcc_args is None: return self.skip('Very slow without ta2, and we would also need to include dlmalloc manually without emcc')
    if Settings.QUANTUM_SIZE == 1: return self.skip('TODO FIXME')
    self.banned_js_engines = [NODE_JS] # OOM in older node

    Settings.CORRECT_SIGNS = 1
    Settings.CORRECT_OVERFLOWS = 0
    Settings.CORRECT_ROUNDINGS = 0
    if self.emcc_args is None: Settings.SAFE_HEAP = 0 # uses time.h to set random bytes, other stuff
    Settings.DISABLE_EXCEPTION_CATCHING = 1
    Settings.FAST_MEMORY = 4*1024*1024
    Settings.EXPORTED_FUNCTIONS += ['_sqlite3_open', '_sqlite3_close', '_sqlite3_exec', '_sqlite3_free', '_callback'];
    if Settings.ASM_JS == 1 and '-g' in self.emcc_args:
      print "disabling inlining" # without registerize (which -g disables), we generate huge amounts of code
      Settings.INLINING_LIMIT = 50

    self.do_run(r'''
                      #define SQLITE_DISABLE_LFS
                      #define LONGDOUBLE_TYPE double
                      #define SQLITE_INT64_TYPE long long int
                      #define SQLITE_THREADSAFE 0
                 ''' + open(path_from_root('tests', 'sqlite', 'sqlite3.c'), 'r').read() +
                       open(path_from_root('tests', 'sqlite', 'benchmark.c'), 'r').read(),
                 open(path_from_root('tests', 'sqlite', 'benchmark.txt'), 'r').read(),
                 includes=[path_from_root('tests', 'sqlite')],
                 force_c=True)

  def test_zlib(self):
    if not Settings.USE_TYPED_ARRAYS == 2: return self.skip('works in general, but cached build will be optimized and fail, so disable this')

    if Settings.ASM_JS:
      self.banned_js_engines = [NODE_JS] # TODO investigate

    if self.emcc_args is not None and '-O2' in self.emcc_args and 'ASM_JS=0' not in self.emcc_args: # without asm, closure minifies Math.imul badly
      self.emcc_args += ['--closure', '1'] # Use closure here for some additional coverage

    Settings.CORRECT_SIGNS = 1

    self.do_run(open(path_from_root('tests', 'zlib', 'example.c'), 'r').read(),
                 open(path_from_root('tests', 'zlib', 'ref.txt'), 'r').read(),
                 libraries=self.get_library('zlib', os.path.join('libz.a'), make_args=['libz.a']),
                 includes=[path_from_root('tests', 'zlib')],
                 force_c=True)

  def test_the_bullet(self): # Called thus so it runs late in the alphabetical cycle... it is long
    if self.emcc_args is None: return self.skip('requires emcc')
    if Building.LLVM_OPTS and self.emcc_args is None: Settings.SAFE_HEAP = 0 # Optimizations make it so we do not have debug info on the line we need to ignore

    Settings.DEAD_FUNCTIONS = ['__ZSt9terminatev']

    # Note: this is also a good test of per-file and per-line changes (since we have multiple files, and correct specific lines)
    if Settings.SAFE_HEAP:
      # Ignore bitfield warnings
      Settings.SAFE_HEAP = 3
      Settings.SAFE_HEAP_LINES = ['btVoronoiSimplexSolver.h:40', 'btVoronoiSimplexSolver.h:41',
                                  'btVoronoiSimplexSolver.h:42', 'btVoronoiSimplexSolver.h:43']

    configure_commands = [['sh', './configure'], ['cmake', '.']]
    configure_args = [['--disable-demos','--disable-dependency-tracking'], ['-DBUILD_DEMOS=OFF', '-DBUILD_EXTRAS=OFF']]
    for c in range(0,2):
      configure = configure_commands[c]
      # Windows cannot run configure sh scripts.
      if WINDOWS and configure[0] == 'sh':
        continue

      # Depending on whether 'configure' or 'cmake' is used to build, Bullet places output files in different directory structures.
      if configure[0] == 'sh':
        generated_libs = [os.path.join('src', '.libs', 'libBulletDynamics.a'),
                          os.path.join('src', '.libs', 'libBulletCollision.a'),
                          os.path.join('src', '.libs', 'libLinearMath.a')]
      else:
        generated_libs = [os.path.join('src', 'BulletDynamics', 'libBulletDynamics.a'),
                          os.path.join('src', 'BulletCollision', 'libBulletCollision.a'),
                          os.path.join('src', 'LinearMath', 'libLinearMath.a')]

      def test():
        self.do_run(open(path_from_root('tests', 'bullet', 'Demos', 'HelloWorld', 'HelloWorld.cpp'), 'r').read(),
                     [open(path_from_root('tests', 'bullet', 'output.txt'), 'r').read(), # different roundings
                      open(path_from_root('tests', 'bullet', 'output2.txt'), 'r').read(),
                      open(path_from_root('tests', 'bullet', 'output3.txt'), 'r').read()],
                     libraries=self.get_library('bullet', generated_libs, configure=configure, configure_args=configure_args[c], cache_name_extra=configure[0]),
                     includes=[path_from_root('tests', 'bullet', 'src')])
      test()

      assert 'asm2g' in test_modes
      if self.run_name == 'asm2g' and configure[0] == 'sh':
        # Test forced alignment
        print >> sys.stderr, 'testing FORCE_ALIGNED_MEMORY'
        old = open('src.cpp.o.js').read()
        Settings.FORCE_ALIGNED_MEMORY = 1
        test()
        new = open('src.cpp.o.js').read()
        print len(old), len(new), old.count('tempBigInt'), new.count('tempBigInt')
        assert len(old) > len(new)
        assert old.count('tempBigInt') > new.count('tempBigInt')

  def test_poppler(self):
    if self.emcc_args is None: return self.skip('very slow, we only do this in emcc runs')

    Settings.CORRECT_OVERFLOWS = 1
    Settings.CORRECT_SIGNS = 1

    Building.COMPILER_TEST_OPTS += [
      '-I' + path_from_root('tests', 'freetype', 'include'),
      '-I' + path_from_root('tests', 'poppler', 'include'),
    ]

    Settings.INVOKE_RUN = 0 # We append code that does run() ourselves

    # See post(), below
    input_file = open(os.path.join(self.get_dir(), 'paper.pdf.js'), 'w')
    input_file.write(str(map(ord, open(path_from_root('tests', 'poppler', 'paper.pdf'), 'rb').read())))
    input_file.close()

    post = '''
def process(filename):
  # To avoid loading this large file to memory and altering it, we simply append to the end
  src = open(filename, 'a')
  src.write(
    \'\'\'
      FS.createDataFile('/', 'paper.pdf', eval(Module.read('paper.pdf.js')), true, false);
      Module.callMain(Module.arguments);
      Module.print("Data: " + JSON.stringify(FS.root.contents['filename-1.ppm'].contents.map(function(x) { return unSign(x, 8) })));
    \'\'\'
  )
  src.close()
'''

    #fontconfig = self.get_library('fontconfig', [os.path.join('src', '.libs', 'libfontconfig.a')]) # Used in file, but not needed, mostly

    freetype = self.get_freetype()

    poppler = self.get_library('poppler',
                               [os.path.join('utils', 'pdftoppm.o'),
                                os.path.join('utils', 'parseargs.o'),
                                os.path.join('poppler', '.libs', 'libpoppler.a')],
                               env_init={ 'FONTCONFIG_CFLAGS': ' ', 'FONTCONFIG_LIBS': ' ' },
                               configure_args=['--disable-libjpeg', '--disable-libpng', '--disable-poppler-qt', '--disable-poppler-qt4', '--disable-cms', '--disable-cairo-output', '--disable-abiword-output', '--enable-shared=no'])

    # Combine libraries

    combined = os.path.join(self.get_dir(), 'poppler-combined.bc')
    Building.link(poppler + freetype, combined)

    self.do_ll_run(combined,
                   map(ord, open(path_from_root('tests', 'poppler', 'ref.ppm'), 'r').read()).__str__().replace(' ', ''),
                   args='-scale-to 512 paper.pdf filename'.split(' '),
                   post_build=post)
                   #, build_ll_hook=self.do_autodebug)

  def test_openjpeg(self):
    if self.emcc_args is None: return self.skip('needs libc for getopt')

    Building.COMPILER_TEST_OPTS = filter(lambda x: x != '-g', Building.COMPILER_TEST_OPTS) # remove -g, so we have one test without it by default

    if Settings.USE_TYPED_ARRAYS == 2:
      Settings.CORRECT_SIGNS = 1
    else:
      Settings.CORRECT_SIGNS = 2
      Settings.CORRECT_SIGNS_LINES = ["mqc.c:566", "mqc.c:317"]

    post = '''
def process(filename):
  import tools.shared as shared
  original_j2k = shared.path_from_root('tests', 'openjpeg', 'syntensity_lobby_s.j2k')
  src = open(filename, 'r').read().replace(
    '// {{PRE_RUN_ADDITIONS}}',
    "FS.createDataFile('/', 'image.j2k', %s, true, false);" % shared.line_splitter(str(
      map(ord, open(original_j2k, 'rb').read())
    ))
  ).replace(
    '// {{POST_RUN_ADDITIONS}}',
    "Module.print('Data: ' + JSON.stringify(FS.analyzePath('image.raw').object.contents));"
  )
  open(filename, 'w').write(src)
'''

    shutil.copy(path_from_root('tests', 'openjpeg', 'opj_config.h'), self.get_dir())

    lib = self.get_library('openjpeg',
                           [os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/index.c.o'.split('/')),
                            os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'.split('/')),
                            os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'.split('/')),
                            os.path.join('bin', 'libopenjpeg.so.1.4.0')],
                           configure=['cmake', '.'],
                           #configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'],
                           make_args=[]) # no -j 2, since parallel builds can fail

    # We use doubles in JS, so we get slightly different values than native code. So we
    # check our output by comparing the average pixel difference
    def image_compare(output, err):
      # Get the image generated by JS, from the JSON.stringify'd array
      m = re.search('\[[\d, -]*\]', output)
      try:
        js_data = eval(m.group(0))
      except AttributeError:
        print 'Failed to find proper image output in: ' + output
        raise

      js_data = map(lambda x: x if x >= 0 else 256+x, js_data) # Our output may be signed, so unsign it

      # Get the correct output
      true_data = open(path_from_root('tests', 'openjpeg', 'syntensity_lobby_s.raw'), 'rb').read()

      # Compare them
      assert(len(js_data) == len(true_data))
      num = len(js_data)
      diff_total = js_total = true_total = 0
      for i in range(num):
        js_total += js_data[i]
        true_total += ord(true_data[i])
        diff_total += abs(js_data[i] - ord(true_data[i]))
      js_mean = js_total/float(num)
      true_mean = true_total/float(num)
      diff_mean = diff_total/float(num)

      image_mean = 83.265
      #print '[image stats:', js_mean, image_mean, true_mean, diff_mean, num, ']'
      assert abs(js_mean - image_mean) < 0.01
      assert abs(true_mean - image_mean) < 0.01
      assert diff_mean < 0.01

      return output

    self.emcc_args += ['--minify', '0'] # to compare the versions

    def do_test():
      self.do_run(open(path_from_root('tests', 'openjpeg', 'codec', 'j2k_to_image.c'), 'r').read(),
                   'Successfully generated', # The real test for valid output is in image_compare
                   '-i image.j2k -o image.raw'.split(' '),
                   libraries=lib,
                   includes=[path_from_root('tests', 'openjpeg', 'libopenjpeg'),
                             path_from_root('tests', 'openjpeg', 'codec'),
                             path_from_root('tests', 'openjpeg', 'common'),
                             os.path.join(self.get_build_dir(), 'openjpeg')],
                   force_c=True,
                   post_build=post,
                   output_nicerizer=image_compare)#, build_ll_hook=self.do_autodebug)

    do_test()

    # some test coverage for EMCC_DEBUG 1 and 2
    if self.emcc_args and '-O2' in self.emcc_args and 'EMCC_DEBUG' not in os.environ and '-g' in self.emcc_args:
      shutil.copyfile('src.c.o.js', 'release.js')
      try:
        os.environ['EMCC_DEBUG'] = '1'
        print '2'
        do_test()
        shutil.copyfile('src.c.o.js', 'debug1.js')
        os.environ['EMCC_DEBUG'] = '2'
        print '3'
        do_test()
        shutil.copyfile('src.c.o.js', 'debug2.js')
      finally:
        del os.environ['EMCC_DEBUG']
      for debug in [1,2]:
        def clean(text):
          text = text.replace('\n\n', '\n').replace('\n\n', '\n').replace('\n\n', '\n').replace('\n\n', '\n').replace('\n\n', '\n').replace('{\n}', '{}')
          return '\n'.join(sorted(text.split('\n')))
        self.assertIdentical(clean(open('release.js').read()), clean(open('debug%d.js' % debug).read())) # EMCC_DEBUG=1 mode must not generate different code!
        print >> sys.stderr, 'debug check %d passed too' % debug

      try:
        os.environ['EMCC_FORCE_STDLIBS'] = '1'
        print 'EMCC_FORCE_STDLIBS'
        do_test()
      finally:
        del os.environ['EMCC_FORCE_STDLIBS']
      print >> sys.stderr, 'EMCC_FORCE_STDLIBS ok'

      try_delete(CANONICAL_TEMP_DIR)
    else:
      print >> sys.stderr, 'not doing debug check'

  def test_python(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    if Settings.QUANTUM_SIZE == 1: return self.skip('TODO: make this work')
    if not self.is_le32(): return self.skip('fails on non-le32') # FIXME

    #Settings.EXPORTED_FUNCTIONS += ['_PyRun_SimpleStringFlags'] # for the demo

    if self.is_le32():
      bitcode = path_from_root('tests', 'python', 'python.le32.bc')
    else:
      bitcode = path_from_root('tests', 'python', 'python.small.bc')

    self.do_ll_run(bitcode,
                    'hello python world!\n[0, 2, 4, 6]\n5\n22\n5.470000',
                    args=['-S', '-c' '''print "hello python world!"; print [x*2 for x in range(4)]; t=2; print 10-3-t; print (lambda x: x*2)(11); print '%f' % 5.47'''])

  def test_lifetime(self):
    if self.emcc_args is None: return self.skip('test relies on emcc opts')

    self.do_ll_run(path_from_root('tests', 'lifetime.ll'), 'hello, world!\n')
    if '-O1' in self.emcc_args or '-O2' in self.emcc_args:
      assert 'a18' not in open(os.path.join(self.get_dir(), 'src.cpp.o.js')).read(), 'lifetime stuff and their vars must be culled'

  # Test cases in separate files. Note that these files may contain invalid .ll!
  # They are only valid enough for us to read for test purposes, not for llvm-as
  # to process.
  def test_cases(self):
    if Building.LLVM_OPTS: return self.skip("Our code is not exactly 'normal' llvm assembly")

    emcc_args = self.emcc_args

    try:
      os.environ['EMCC_LEAVE_INPUTS_RAW'] = '1'
      Settings.CHECK_OVERFLOWS = 0

      for name in glob.glob(path_from_root('tests', 'cases', '*.ll')):
        shortname = name.replace('.ll', '')
        if '' not in shortname: continue
        if '_ta2' in shortname and not Settings.USE_TYPED_ARRAYS == 2:
          print self.skip('case "%s" only relevant for ta2' % shortname)
          continue
        if '_noasm' in shortname and Settings.ASM_JS:
          print self.skip('case "%s" not relevant for asm.js' % shortname)
          continue
        if '_le32' in shortname and not self.is_le32():
          print self.skip('case "%s" not relevant for non-le32 target' % shortname)
          continue
        self.emcc_args = emcc_args
        if os.path.exists(shortname + '.emcc'):
          if not self.emcc_args: continue
          self.emcc_args = self.emcc_args + json.loads(open(shortname + '.emcc').read())
        print >> sys.stderr, "Testing case '%s'..." % shortname
        output_file = path_from_root('tests', 'cases', shortname + '.txt')
        if Settings.QUANTUM_SIZE == 1:
          q1_output_file = path_from_root('tests', 'cases', shortname + '_q1.txt')
          if os.path.exists(q1_output_file):
            output_file = q1_output_file
        if os.path.exists(output_file):
          output = open(output_file, 'r').read()
        else:
          output = 'hello, world!'
        if output.rstrip() != 'skip':
          self.do_ll_run(path_from_root('tests', 'cases', name), output)
        # Optional source checking, a python script that gets a global generated with the source
        src_checker = path_from_root('tests', 'cases', shortname + '.py')
        if os.path.exists(src_checker):
          generated = open('src.cpp.o.js').read()
          exec(open(src_checker).read())

    finally:
      del os.environ['EMCC_LEAVE_INPUTS_RAW']
      self.emcc_args = emcc_args

  def test_fuzz(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2')

    Building.COMPILER_TEST_OPTS += ['-I' + path_from_root('tests', 'fuzz')]

    def run_all(x):
      print x
      for name in glob.glob(path_from_root('tests', 'fuzz', '*.c')):
        print name
        self.do_run(open(path_from_root('tests', 'fuzz', name)).read(),
                    open(path_from_root('tests', 'fuzz', name + '.txt')).read(), force_c=True)

    run_all('normal')

    self.emcc_args += ['--llvm-lto', '1']

    run_all('lto')

  # Autodebug the code
  def do_autodebug(self, filename):
    output = Popen([PYTHON, AUTODEBUGGER, filename+'.o.ll', filename+'.o.ll.ll'], stdout=PIPE, stderr=self.stderr_redirect).communicate()[0]
    assert 'Success.' in output, output
    self.prep_ll_run(filename, filename+'.o.ll.ll', force_recompile=True) # rebuild .bc # TODO: use code in do_autodebug_post for this

  # Autodebug the code, after LLVM opts. Will only work once!
  def do_autodebug_post(self, filename):
    if not hasattr(self, 'post'):
      print 'Asking for post re-call'
      self.post = True
      return True
    print 'Autodebugging during post time'
    delattr(self, 'post')
    output = Popen([PYTHON, AUTODEBUGGER, filename+'.o.ll', filename+'.o.ll.ll'], stdout=PIPE, stderr=self.stderr_redirect).communicate()[0]
    assert 'Success.' in output, output
    shutil.copyfile(filename + '.o.ll.ll', filename + '.o.ll')
    Building.llvm_as(filename)
    Building.llvm_dis(filename)

  def test_autodebug(self):
    if Building.LLVM_OPTS: return self.skip('LLVM opts mess us up')
    Building.COMPILER_TEST_OPTS += ['--llvm-opts', '0']

    # Run a test that should work, generating some code
    self.test_structs()

    filename = os.path.join(self.get_dir(), 'src.cpp')
    self.do_autodebug(filename)

    # Compare to each other, and to expected output
    self.do_ll_run(path_from_root('tests', filename+'.o.ll.ll'), '''AD:-1,1''')
    assert open('stdout').read().startswith('AD:-1'), 'We must note when we enter functions'

    # Test using build_ll_hook
    src = '''
        #include <stdio.h>

        char cache[256], *next = cache;

        int main()
        {
          cache[10] = 25;
          next[20] = 51;
          int x = cache[10];
          double y = 11.52;
          printf("*%d,%d,%.2f*\\n", x, cache[20], y);
          return 0;
        }
      '''
    self.do_run(src, '''AD:-1,1''', build_ll_hook=self.do_autodebug)

  def test_corruption(self):
    if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm')
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test')

    Settings.CORRUPTION_CHECK = 1

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      int main(int argc, char **argv) {
        int size = 1024*argc;
        char *buffer = (char*)malloc(size);
      #if CORRUPT
        memset(buffer, argc, size+15);
      #else
        memset(buffer, argc, size);
      #endif
        for (int x = 0; x < size; x += argc*3) buffer[x] = x/3;
        int ret = 0;
        for (int x = 0; x < size; x++) ret += buffer[x];
        free(buffer);
        printf("All ok, %d\n", ret);
      }
    '''

    for corrupt in [1]:
      self.do_run(src.replace('CORRUPT', str(corrupt)), 'Heap corruption detected!' if corrupt else 'All ok, 4209')

  def test_corruption_2(self):
    if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm')
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test')

    Settings.SAFE_HEAP = 1
    Settings.CORRUPTION_CHECK = 1

    # test for free(0), malloc(0), etc.
    src = r'''
      #include <iostream>
      #include <fstream>
      #include <stdlib.h>
      #include <stdio.h>

      void bye() {
        printf("all ok\n");
      }

      int main() {
        atexit(bye);

        std::string testPath = "/Script/WA-KA.txt";
        std::fstream str(testPath.c_str(), std::ios::in | std::ios::binary);

        if (str.is_open())
        {
          std::cout << "open!" << std::endl;
        } else {
          std::cout << "missing!" << std::endl;
        }

        return 1;
      }
      '''
    self.do_run(src, 'missing!\nall ok\n')

  def test_corruption_3(self):
    if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm')
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test')

    Settings.CORRUPTION_CHECK = 1

    # realloc
    src = r'''
      #include <stdlib.h>
      #include <stdio.h>
      #include <assert.h>

      void bye() {
        printf("all ok\n");
      }

      int main(int argc, char **argv) {
        atexit(bye);

        char *buffer = (char*)malloc(100);
        for (int i = 0; i < 100; i++) buffer[i] = (i*i)%256;
        buffer = (char*)realloc(buffer, argc + 50);
        for (int i = 0; i < argc + 50; i++) {
          //printf("%d : %d : %d : %d\n", i, (int)(buffer + i), buffer[i], (char)((i*i)%256));
          assert(buffer[i] == (char)((i*i)%256));
        }
        return 1;
      }
      '''
    self.do_run(src, 'all ok\n')

  ### Integration tests

  def test_ccall(self):
    if self.emcc_args is not None and '-O2' in self.emcc_args:
      self.emcc_args += ['--closure', '1'] # Use closure here, to test we export things right

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      extern "C" {
        int get_int() { return 5; }
        float get_float() { return 3.14; }
        char * get_string() { return "hello world"; }
        void print_int(int x) { printf("%d\n", x); }
        void print_float(float x) { printf("%.2f\n", x); }
        void print_string(char *x) { printf("%s\n", x); }
        int multi(int x, float y, int z, char *str) { if (x) puts(str); return (x+y)*z; }
        int * pointer(int *in) { printf("%d\n", *in); static int ret = 21; return &ret; }
      }

      int main(int argc, char **argv) {
        return 0;
      }
    '''

    post = '''
def process(filename):
  src = \'\'\'
    var Module = { 'noInitialRun': true };
    \'\'\' + open(filename, 'r').read() + \'\'\'
    Module.addOnExit(function () {
      Module.print('*');
      var ret;
      ret = Module['ccall']('get_int', 'number'); Module.print([typeof ret, ret]);
      ret = ccall('get_float', 'number'); Module.print([typeof ret, ret.toFixed(2)]);
      ret = ccall('get_string', 'string'); Module.print([typeof ret, ret]);
      ret = ccall('print_int', null, ['number'], [12]); Module.print(typeof ret);
      ret = ccall('print_float', null, ['number'], [14.56]); Module.print(typeof ret);
      ret = ccall('print_string', null, ['string'], ["cheez"]); Module.print(typeof ret);
      ret = ccall('print_string', null, ['array'], [[97, 114, 114, 45, 97, 121, 0]]); Module.print(typeof ret);
      ret = ccall('multi', 'number', ['number', 'number', 'number', 'string'], [2, 1.4, 3, 'more']); Module.print([typeof ret, ret]);
      var p = ccall('malloc', 'pointer', ['number'], [4]);
      setValue(p, 650, 'i32');
      ret = ccall('pointer', 'pointer', ['pointer'], [p]); Module.print([typeof ret, getValue(ret, 'i32')]);
      Module.print('*');
      // part 2: cwrap
      var multi = Module['cwrap']('multi', 'number', ['number', 'number', 'number', 'string']);
      Module.print(multi(2, 1.4, 3, 'atr'));
      Module.print(multi(8, 5.4, 4, 'bret'));
      Module.print('*');
      // part 3: avoid stack explosion
      for (var i = 0; i < TOTAL_STACK/60; i++) {
        ccall('multi', 'number', ['number', 'number', 'number', 'string'], [0, 0, 0, '123456789012345678901234567890123456789012345678901234567890']);
      }
      Module.print('stack is ok.');
    });
    Module.callMain();
  \'\'\'
  open(filename, 'w').write(src)
'''

    Settings.EXPORTED_FUNCTIONS += ['_get_int', '_get_float', '_get_string', '_print_int', '_print_float', '_print_string', '_multi', '_pointer', '_malloc']

    self.do_run(src, '*\nnumber,5\nnumber,3.14\nstring,hello world\n12\nundefined\n14.56\nundefined\ncheez\nundefined\narr-ay\nundefined\nmore\nnumber,10\n650\nnumber,21\n*\natr\n10\nbret\n53\n*\nstack is ok.\n', post_build=post)

  def test_pgo(self):
    if Settings.ASM_JS: return self.skip('PGO does not work in asm mode')

    def run_all(name, src):
      print name
      def test(expected, args=[], no_build=False):
        self.do_run(src, expected, args=args, no_build=no_build)
        return open(self.in_dir('src.cpp.o.js')).read()

      # Sanity check that it works and the dead function is emitted
      js = test('*9*')
      assert 'function _unused(' in js

      # Run with PGO, see that unused is true to its name
      Settings.PGO = 1
      test("*9*\n-s DEAD_FUNCTIONS='[\"_unused\"]'")
      Settings.PGO = 0

      # Kill off the dead function, still works and it is not emitted
      Settings.DEAD_FUNCTIONS = ['_unused']
      js = test('*9*')
      assert 'function _unused($' not in js # no compiled code
      assert 'function _unused(' in js # lib-generated stub
      Settings.DEAD_FUNCTIONS = []

      # Run the same code with argc that uses the dead function, see abort
      test(('missing function: unused'), args=['a', 'b'], no_build=True)

    # Normal stuff
    run_all('normal', r'''
      #include <stdio.h>
      extern "C" {
      int used(int x) {
        if (x == 0) return -1;
        return used(x/3) + used(x/17) + x%5;
      }
      int unused(int x) {
        if (x == 0) return -1;
        return unused(x/4) + unused(x/23) + x%7;
      }
      }
      int main(int argc, char **argv) {
        printf("*%d*\n", argc == 3 ? unused(argv[0][0] + 1024) : used(argc + 1555));
        return 0;
      }
    ''')

    # Call by function pointer
    run_all('function pointers', r'''
      #include <stdio.h>
      extern "C" {
      int used(int x) {
        if (x == 0) return -1;
        return used(x/3) + used(x/17) + x%5;
      }
      int unused(int x) {
        if (x == 0) return -1;
        return unused(x/4) + unused(x/23) + x%7;
      }
      }
      typedef int (*ii)(int);
      int main(int argc, char **argv) {
        ii pointers[256];
        for (int i = 0; i < 256; i++) {
          pointers[i] = (i == 3) ? unused : used;
        }
        printf("*%d*\n", pointers[argc](argc + 1555));
        return 0;
      }
    ''')

  def test_asm_pgo(self):
    if not Settings.ASM_JS: return self.skip('this is a test for PGO for asm (NB: not *in* asm)')

    src = open(path_from_root('tests', 'hello_libcxx.cpp')).read()
    output = 'hello, world!'

    self.do_run(src, output)
    shutil.move(self.in_dir('src.cpp.o.js'), self.in_dir('normal.js'))

    Settings.ASM_JS = 0
    Settings.PGO = 1
    self.do_run(src, output)
    Settings.ASM_JS = 1
    Settings.PGO = 0

    shutil.move(self.in_dir('src.cpp.o.js'), self.in_dir('pgo.js'))
    pgo_output = run_js(self.in_dir('pgo.js')).split('\n')[1]
    open('pgo_data.rsp', 'w').write(pgo_output)

    # with response file

    self.emcc_args += ['@pgo_data.rsp']
    self.do_run(src, output)
    self.emcc_args.pop()
    shutil.move(self.in_dir('src.cpp.o.js'), self.in_dir('pgoed.js'))

    before = len(open('normal.js').read())
    after = len(open('pgoed.js').read())
    assert after < 0.90 * before, [before, after] # expect a size reduction

    # with response in settings element itself

    open('dead_funcs', 'w').write(pgo_output[pgo_output.find('['):-1])
    self.emcc_args += ['-s', 'DEAD_FUNCTIONS=@' + self.in_dir('dead_funcs')]
    self.do_run(src, output)
    self.emcc_args.pop()
    self.emcc_args.pop()
    shutil.move(self.in_dir('src.cpp.o.js'), self.in_dir('pgoed2.js'))
    assert open('pgoed.js').read() == open('pgoed2.js').read()

    # with relative response in settings element itself

    open('dead_funcs', 'w').write(pgo_output[pgo_output.find('['):-1])
    self.emcc_args += ['-s', 'DEAD_FUNCTIONS=@dead_funcs']
    self.do_run(src, output)
    self.emcc_args.pop()
    self.emcc_args.pop()
    shutil.move(self.in_dir('src.cpp.o.js'), self.in_dir('pgoed2.js'))
    assert open('pgoed.js').read() == open('pgoed2.js').read()

  def test_exported_response(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      extern "C" {
        int other_function() { return 5; }
      }

      int main() {
        printf("waka!\n");
        return 0;
      }
    '''
    open('exps', 'w').write('["_main","_other_function"]')

    self.emcc_args += ['-s', 'EXPORTED_FUNCTIONS=@exps']
    self.do_run(src, '''waka!''')
    assert 'other_function' in open('src.cpp.o.js').read()

  def test_add_function(self):
    if self.emcc_args is None: return self.skip('requires emcc')

    Settings.INVOKE_RUN = 0
    Settings.RESERVED_FUNCTION_POINTERS = 1

    src = r'''
      #include <stdio.h>
      #include <stdlib.h>

      int main(int argc, char **argv) {
        int fp = atoi(argv[1]);
        printf("fp: %d\n", fp);
        void (*f)(int) = reinterpret_cast<void (*)(int)>(fp);
        f(7);
        return 0;
      }
    '''

    open(os.path.join(self.get_dir(), 'post.js'), 'w').write('''
      var newFuncPtr = Runtime.addFunction(function(num) {
        Module.print('Hello ' + num + ' from JS!');
      });
      Module.callMain([newFuncPtr.toString()]);
    ''')

    self.emcc_args += ['--post-js', 'post.js']
    self.do_run(src, '''Hello 7 from JS!''')

    if Settings.ASM_JS:
      Settings.RESERVED_FUNCTION_POINTERS = 0
      self.do_run(src, '''Finished up all reserved function pointers. Use a higher value for RESERVED_FUNCTION_POINTERS.''')
      generated = open('src.cpp.o.js').read()
      assert 'jsCall' not in generated
      Settings.RESERVED_FUNCTION_POINTERS = 1

      Settings.ALIASING_FUNCTION_POINTERS = 1 - Settings.ALIASING_FUNCTION_POINTERS # flip the test
      self.do_run(src, '''Hello 7 from JS!''')

  def test_demangle_stacks(self):
    if Settings.ASM_JS: return self.skip('spidermonkey has stack trace issues')

    src = r'''
      #include<stdio.h>
      #include<stdlib.h>

      namespace NameSpace {
        class Class {
        public:
          int Aborter(double x, char y, int *z) {
            int addr = x+y+(int)z;
            void *p = (void*)addr;
            for (int i = 0; i < 100; i++) free(p); // will abort, should show proper stack trace
          }
        };
      }

      int main(int argc, char **argv) {
        NameSpace::Class c;
        c.Aborter(1.234, 'a', NULL);
        return 0;
      }
    '''
    self.do_run(src, 'NameSpace::Class::Aborter(double, char, int*)');

  def test_embind(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    Building.COMPILER_TEST_OPTS += ['--bind']

    src = r'''
      #include<stdio.h>
      #include<emscripten/val.h>

      using namespace emscripten;

      int main() {
        val Math = val::global("Math");

        // two ways to call Math.abs
        printf("abs(-10): %d\n", Math.call<int>("abs", -10));
        printf("abs(-11): %d\n", Math["abs"](-11).as<int>());

        return 0;
      }
    '''
    self.do_run(src, 'abs(-10): 10\nabs(-11): 11');

  def test_embind_2(self):
    if self.emcc_args is None: return self.skip('requires emcc')
    Building.COMPILER_TEST_OPTS += ['--bind', '--post-js', 'post.js']
    open('post.js', 'w').write('''
      Module.print('lerp ' + Module.lerp(1, 2, 0.66) + '.');
    ''')
    src = r'''
      #include <stdio.h>
      #include <SDL/SDL.h>
      #include <emscripten/bind.h>
      using namespace emscripten;
      float lerp(float a, float b, float t) {
          return (1 - t) * a + t * b;
      }
      EMSCRIPTEN_BINDINGS(my_module) {
          function("lerp", &lerp);
      }
    '''
    self.do_run(src, 'lerp 1.66');

  def test_scriptaclass(self):
      if self.emcc_args is None: return self.skip('requires emcc')

      Settings.EXPORT_BINDINGS = 1

      header_filename = os.path.join(self.get_dir(), 'header.h')
      header = '''
        struct ScriptMe {
          int value;
          ScriptMe(int val);
          int getVal(); // XXX Sadly, inlining these will result in LLVM not
                        // producing any code for them (when just building
                        // as a library)
          void mulVal(int mul);
        };
      '''
      h = open(header_filename, 'w')
      h.write(header)
      h.close()

      src = '''
        #include "header.h"

        ScriptMe::ScriptMe(int val) : value(val) { }
        int ScriptMe::getVal() { return value; }
        void ScriptMe::mulVal(int mul) { value *= mul; }
      '''

      # Way 1: use demangler and namespacer

      script_src = '''
        var sme = Module._.ScriptMe.__new__(83);          // malloc(sizeof(ScriptMe)), ScriptMe::ScriptMe(sme, 83) / new ScriptMe(83) (at addr sme)
        Module._.ScriptMe.mulVal(sme, 2);                 // ScriptMe::mulVal(sme, 2)       sme.mulVal(2)
        Module.print('*' + Module._.ScriptMe.getVal(sme) + '*');
        _free(sme);
        Module.print('*ok*');
      '''
      post = '''
def process(filename):
  Popen([PYTHON, DEMANGLER, filename], stdout=open(filename + '.tmp', 'w')).communicate()
  Popen([PYTHON, NAMESPACER, filename, filename + '.tmp'], stdout=open(filename + '.tmp2', 'w')).communicate()
  src = open(filename, 'r').read().replace(
    '// {{MODULE_ADDITIONS}',
    'Module["_"] = ' + open(filename + '.tmp2', 'r').read().replace('var ModuleNames = ', '').rstrip() + ';\n\n' + script_src + '\n\n' +
      '// {{MODULE_ADDITIONS}'
  )
  open(filename, 'w').write(src)
'''
      # XXX disable due to possible v8 bug -- self.do_run(src, '*166*\n*ok*', post_build=post)

      if self.emcc_args is not None and '-O2' in self.emcc_args and 'ASM_JS=0' not in self.emcc_args: # without asm, closure minifies Math.imul badly
        self.emcc_args += ['--closure', '1'] # Use closure here, to test we export things right

      # Way 2: use CppHeaderParser

      Settings.RUNTIME_TYPE_INFO = 1

      header = '''
        #include <stdio.h>

        class Parent {
        protected:
          int value;
        public:
          Parent(int val);
          Parent(Parent *p, Parent *q); // overload constructor
          int getVal() { return value; }; // inline should work just fine here, unlike Way 1 before
          void mulVal(int mul);
        };

        class Child1 : public Parent {
        public:
          Child1() : Parent(7) { printf("Child1:%d\\n", value); };
          Child1(int val) : Parent(val*2) { value -= 1; printf("Child1:%d\\n", value); };
          int getValSqr() { return value*value; }
          int getValSqr(int more) { return value*value*more; }
          int getValTimes(int times=1) { return value*times; }
        };

        class Child2 : public Parent {
        public:
          Child2() : Parent(9) { printf("Child2:%d\\n", value); };
          int getValCube() { return value*value*value; }
          static void printStatic() { printf("*static*\\n"); }

          virtual void virtualFunc() { printf("*virtualf*\\n"); }
          virtual void virtualFunc2() { printf("*virtualf2*\\n"); }
          static void runVirtualFunc(Child2 *self) { self->virtualFunc(); };
        private:
          void doSomethingSecret() { printf("security breached!\\n"); }; // we should not be able to do this
        };
      '''
      open(header_filename, 'w').write(header)

      basename = os.path.join(self.get_dir(), 'bindingtest')
      output = Popen([PYTHON, BINDINGS_GENERATOR, basename, header_filename], stdout=PIPE, stderr=self.stderr_redirect).communicate()[0]
      #print output
      assert 'Traceback' not in output, 'Failure in binding generation: ' + output

      src = '''
        #include "header.h"

        Parent::Parent(int val) : value(val) { printf("Parent:%d\\n", val); }
        Parent::Parent(Parent *p, Parent *q) : value(p->value + q->value) { printf("Parent:%d\\n", value); }
        void Parent::mulVal(int mul) { value *= mul; }

        #include "bindingtest.cpp"
      '''

      post2 = '''
def process(filename):
  src = open(filename, 'a')
  src.write(open('bindingtest.js').read() + '\\n\\n')
  src.close()
'''

      def post3(filename):
        script_src_2 = '''
          var sme = new Module.Parent(42);
          sme.mulVal(2);
          Module.print('*')
          Module.print(sme.getVal());

          Module.print('c1');

          var c1 = new Module.Child1();
          Module.print(c1.getVal());
          c1.mulVal(2);
          Module.print(c1.getVal());
          Module.print(c1.getValSqr());
          Module.print(c1.getValSqr(3));
          Module.print(c1.getValTimes()); // default argument should be 1
          Module.print(c1.getValTimes(2));

          Module.print('c1 v2');

          c1 = new Module.Child1(8); // now with a parameter, we should handle the overloading automatically and properly and use constructor #2
          Module.print(c1.getVal());
          c1.mulVal(2);
          Module.print(c1.getVal());
          Module.print(c1.getValSqr());
          Module.print(c1.getValSqr(3));

          Module.print('c2')

          var c2 = new Module.Child2();
          Module.print(c2.getVal());
          c2.mulVal(2);
          Module.print(c2.getVal());
          Module.print(c2.getValCube());
          var succeeded;
          try {
            succeeded = 0;
            Module.print(c2.doSomethingSecret()); // should fail since private
            succeeded = 1;
          } catch(e) {}
          Module.print(succeeded);
          try {
            succeeded = 0;
            Module.print(c2.getValSqr()); // function from the other class
            succeeded = 1;
          } catch(e) {}
          Module.print(succeeded);
          try {
            succeeded = 0;
            c2.getValCube(); // sanity
            succeeded = 1;
          } catch(e) {}
          Module.print(succeeded);

          Module.Child2.prototype.printStatic(); // static calls go through the prototype

          // virtual function
          c2.virtualFunc();
          Module.Child2.prototype.runVirtualFunc(c2);
          c2.virtualFunc2();

          // extend the class from JS
          var c3 = new Module.Child2;
          Module.customizeVTable(c3, [{
            original: Module.Child2.prototype.virtualFunc,
            replacement: function() {
              Module.print('*js virtualf replacement*');
            }
          }, {
            original: Module.Child2.prototype.virtualFunc2,
            replacement: function() {
              Module.print('*js virtualf2 replacement*');
            }
          }]);
          c3.virtualFunc();
          Module.Child2.prototype.runVirtualFunc(c3);
          c3.virtualFunc2();

          c2.virtualFunc(); // original should remain the same
          Module.Child2.prototype.runVirtualFunc(c2);
          c2.virtualFunc2();
          Module.print('*ok*');
        '''
        code = open(filename).read()
        src = open(filename, 'w')
        src.write('var Module = {};\n') # name Module
        src.write(code)
        src.write(script_src_2 + '\n')
        src.close()

      Settings.RESERVED_FUNCTION_POINTERS = 20

      self.do_run(src, '''*
84
c1
Parent:7
Child1:7
7
14
196
588
14
28
c1 v2
Parent:16
Child1:15
15
30
900
2700
c2
Parent:9
Child2:9
9
18
5832
0
0
1
*static*
*virtualf*
*virtualf*
*virtualf2*''' + ('''
Parent:9
Child2:9
*js virtualf replacement*
*js virtualf replacement*
*js virtualf2 replacement*
*virtualf*
*virtualf*
*virtualf2*''') + '''
*ok*
''', post_build=(post2, post3))

  def test_scriptaclass_2(self):
      if self.emcc_args is None: return self.skip('requires emcc')

      Settings.EXPORT_BINDINGS = 1

      header_filename = os.path.join(self.get_dir(), 'header.h')
      header = '''
        #include <stdio.h>
        #include <string.h>

        class StringUser {
          char *s;
          int i;
        public:
          StringUser(char *string, int integer) : s(strdup(string)), i(integer) {}
          void Print(int anotherInteger, char *anotherString) {
            printf("|%s|%d|%s|%d|\\n", s, i, anotherString, anotherInteger);
          }
          void CallOther(StringUser *fr) { fr->Print(i, s); }
        };
      '''
      open(header_filename, 'w').write(header)

      basename = os.path.join(self.get_dir(), 'bindingtest')
      output = Popen([PYTHON, BINDINGS_GENERATOR, basename, header_filename], stdout=PIPE, stderr=self.stderr_redirect).communicate()[0]
      #print output
      assert 'Traceback' not in output, 'Failure in binding generation: ' + output

      src = '''
        #include "header.h"

        #include "bindingtest.cpp"
      '''

      post = '''
def process(filename):
  src = open(filename, 'a')
  src.write(open('bindingtest.js').read() + '\\n\\n')
  src.write(\'\'\'
          var user = new Module.StringUser("hello", 43);
          user.Print(41, "world");
            \'\'\')
  src.close()
'''
      self.do_run(src, '|hello|43|world|41|', post_build=post)

  def test_typeinfo(self):
    if self.emcc_args is not None and self.emcc_args != []: return self.skip('full LLVM opts optimize out all the code that uses the type')

    Settings.RUNTIME_TYPE_INFO = 1
    if Settings.QUANTUM_SIZE != 4: return self.skip('We assume normal sizes in the output here')

    src = '''
      #include<stdio.h>
      struct UserStruct {
        int x;
        char y;
        short z;
      };
      struct Encloser {
        short x;
        UserStruct us;
        int y;
      };
      int main() {
        Encloser e;
        e.us.y = 5;
        printf("*ok:%d*\\n", e.us.y);
        return 0;
      }
    '''

    post = '''
def process(filename):
  src = open(filename, 'r').read().replace(
    '// {{POST_RUN_ADDITIONS}}',
    \'\'\'
      if (Runtime.typeInfo) {
        Module.print('|' + Runtime.typeInfo.UserStruct.fields + '|' + Runtime.typeInfo.UserStruct.flatIndexes + '|');
        var t = Runtime.generateStructInfo(['x', { us: ['x', 'y', 'z'] }, 'y'], 'Encloser')
        Module.print('|' + [t.x, t.us.x, t.us.y, t.us.z, t.y] + '|');
        Module.print('|' + JSON.stringify(Runtime.generateStructInfo(['x', 'y', 'z'], 'UserStruct')) + '|');
      } else {
        Module.print('No type info.');
      }
    \'\'\'
  )
  open(filename, 'w').write(src)
'''

    self.do_run(src,
                 '*ok:5*\n|i32,i8,i16|0,4,6|\n|0,4,8,10,12|\n|{"__size__":8,"x":0,"y":4,"z":6}|',
                 post_build=post)

    # Make sure that without the setting, we don't spam the .js with the type info
    Settings.RUNTIME_TYPE_INFO = 0
    self.do_run(src, 'No type info.', post_build=post)

  ### Tests for tools

  def test_safe_heap(self):
    if not Settings.SAFE_HEAP: return self.skip('We need SAFE_HEAP to test SAFE_HEAP')
    if Settings.USE_TYPED_ARRAYS == 2: return self.skip('It is ok to violate the load-store assumption with TA2')
    if Building.LLVM_OPTS: return self.skip('LLVM can optimize away the intermediate |x|')

    src = '''
      #include<stdio.h>
      #include<stdlib.h>
      int main() { int *x = (int*)malloc(sizeof(int));
        *x = 20;
        float *y = (float*)x;
        printf("%f\\n", *y);
        printf("*ok*\\n");
        return 0;
      }
    '''

    try:
      self.do_run(src, '*nothingatall*')
    except Exception, e:
      # This test *should* fail, by throwing this exception
      assert 'Assertion failed: Load-store consistency assumption failure!' in str(e), str(e)

    # And we should not fail if we disable checking on that line

    Settings.SAFE_HEAP = 3
    Settings.SAFE_HEAP_LINES = ["src.cpp:7"]

    self.do_run(src, '*ok*')

    # But if we disable the wrong lines, we still fail

    Settings.SAFE_HEAP_LINES = ["src.cpp:99"]

    try:
      self.do_run(src, '*nothingatall*')
    except Exception, e:
      # This test *should* fail, by throwing this exception
      assert 'Assertion failed: Load-store consistency assumption failure!' in str(e), str(e)

    # And reverse the checks with = 2

    Settings.SAFE_HEAP = 2
    Settings.SAFE_HEAP_LINES = ["src.cpp:99"]

    self.do_run(src, '*ok*')

    Settings.SAFE_HEAP = 1

    # Linking multiple files should work too

    module = '''
      #include<stdio.h>
      #include<stdlib.h>
      void callFunc() { int *x = (int*)malloc(sizeof(int));
        *x = 20;
        float *y = (float*)x;
        printf("%f\\n", *y);
      }
    '''
    module_name = os.path.join(self.get_dir(), 'module.cpp')
    open(module_name, 'w').write(module)

    main = '''
      #include<stdio.h>
      #include<stdlib.h>
      extern void callFunc();
      int main() { callFunc();
        int *x = (int*)malloc(sizeof(int));
        *x = 20;
        float *y = (float*)x;
        printf("%f\\n", *y);
        printf("*ok*\\n");
        return 0;
      }
    '''
    main_name = os.path.join(self.get_dir(), 'main.cpp')
    open(main_name, 'w').write(main)

    Building.emcc(module_name, ['-g'])
    Building.emcc(main_name, ['-g'])
    all_name = os.path.join(self.get_dir(), 'all.bc')
    Building.link([module_name + '.o', main_name + '.o'], all_name)

    try:
      self.do_ll_run(all_name, '*nothingatall*')
    except Exception, e:
      # This test *should* fail, by throwing this exception
      assert 'Assertion failed: Load-store consistency assumption failure!' in str(e), str(e)

    # And we should not fail if we disable checking on those lines

    Settings.SAFE_HEAP = 3
    Settings.SAFE_HEAP_LINES = ["module.cpp:7", "main.cpp:9"]

    self.do_ll_run(all_name, '*ok*')

    # But we will fail if we do not disable exactly what we need to - any mistake leads to error

    for lines in [["module.cpp:22", "main.cpp:9"], ["module.cpp:7", "main.cpp:29"], ["module.cpp:127", "main.cpp:449"], ["module.cpp:7"], ["main.cpp:9"]]:
      Settings.SAFE_HEAP_LINES = lines
      try:
        self.do_ll_run(all_name, '*nothingatall*')
      except Exception, e:
        # This test *should* fail, by throwing this exception
        assert 'Assertion failed: Load-store consistency assumption failure!' in str(e), str(e)

  def test_debug(self):
    if '-g' not in Building.COMPILER_TEST_OPTS: Building.COMPILER_TEST_OPTS.append('-g')
    if self.emcc_args is not None:
      if '-O1' in self.emcc_args or '-O2' in self.emcc_args: return self.skip('optimizations remove LLVM debug info')

    src = '''
      #include <stdio.h>
      #include <assert.h>

      void checker(int x) {
        x += 20;
        assert(x < 15); // this is line 7!
      }

      int main() {
        checker(10);
        return 0;
      }
    '''
    try:
      self.do_run(src, '*nothingatall*')
    except Exception, e:
      # This test *should* fail
      assert 'Assertion failed: x < 15' in str(e), str(e)

    lines = open('src.cpp.o.js', 'r').readlines()
    lines = filter(lambda line: '___assert_fail(' in line or '___assert_func(' in line, lines)
    found_line_num = any(('//@line 7 "' in line) for line in lines)
    found_filename = any(('src.cpp"\n' in line) for line in lines)
    assert found_line_num, 'Must have debug info with the line number'
    assert found_filename, 'Must have debug info with the filename'

  def test_source_map(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip("doesn't pass without typed arrays")
    if NODE_JS not in JS_ENGINES: return self.skip('sourcemapper requires Node to run')
    if '-g' not in Building.COMPILER_TEST_OPTS: Building.COMPILER_TEST_OPTS.append('-g')

    src = '''
      #include <stdio.h>
      #include <assert.h>

      __attribute__((noinline)) int foo() {
        printf("hi"); // line 6
        return 1; // line 7
      }

      int main() {
        printf("%d", foo()); // line 11
        return 0; // line 12
      }
    '''

    dirname = self.get_dir()
    src_filename = os.path.join(dirname, 'src.cpp')
    out_filename = os.path.join(dirname, 'a.out.js')
    no_maps_filename = os.path.join(dirname, 'no-maps.out.js')

    with open(src_filename, 'w') as f: f.write(src)
    assert '-g4' not in Building.COMPILER_TEST_OPTS
    Building.emcc(src_filename, Settings.serialize() + self.emcc_args +
        Building.COMPILER_TEST_OPTS, out_filename)
    # the file name may find its way into the generated code, so make sure we
    # can do an apples-to-apples comparison by compiling with the same file name
    shutil.move(out_filename, no_maps_filename)
    with open(no_maps_filename) as f: no_maps_file = f.read()
    no_maps_file = re.sub(' *//@.*$', '', no_maps_file, flags=re.MULTILINE)
    Building.COMPILER_TEST_OPTS.append('-g4')

    def build_and_check():
      import json
      Building.emcc(src_filename, Settings.serialize() + self.emcc_args +
          Building.COMPILER_TEST_OPTS, out_filename, stderr=PIPE)
      with open(out_filename) as f: out_file = f.read()
      # after removing the @line and @sourceMappingURL comments, the build
      # result should be identical to the non-source-mapped debug version.
      # this is worth checking because the parser AST swaps strings for token
      # objects when generating source maps, so we want to make sure the
      # optimizer can deal with both types.
      out_file = re.sub(' *//@.*$', '', out_file, flags=re.MULTILINE)
      def clean(code):
        code = code.replace('{\n}', '{}')
        return '\n'.join(sorted(code.split('\n')))
      self.assertIdentical(clean(no_maps_file), clean(out_file))
      map_filename = out_filename + '.map'
      data = json.load(open(map_filename, 'r'))
      self.assertPathsIdentical(out_filename, data['file'])
      self.assertPathsIdentical(src_filename, data['sources'][0])
      self.assertTextDataIdentical(src, data['sourcesContent'][0])
      mappings = json.loads(jsrun.run_js(
        path_from_root('tools', 'source-maps', 'sourcemap2json.js'),
        tools.shared.NODE_JS, [map_filename]))
      seen_lines = set()
      for m in mappings:
        self.assertPathsIdentical(src_filename, m['source'])
        seen_lines.add(m['originalLine'])
      # ensure that all the 'meaningful' lines in the original code get mapped
      assert seen_lines.issuperset([6, 7, 11, 12])

    # EMCC_DEBUG=2 causes lots of intermediate files to be written, and so
    # serves as a stress test for source maps because it needs to correlate
    # line numbers across all those files.
    old_emcc_debug = os.environ.get('EMCC_DEBUG', None)
    os.environ.pop('EMCC_DEBUG', None)
    try:
      build_and_check()
      os.environ['EMCC_DEBUG'] = '2'
      build_and_check()
    finally:
      if old_emcc_debug is not None:
        os.environ['EMCC_DEBUG'] = old_emcc_debug
      else:
        os.environ.pop('EMCC_DEBUG', None)

  def test_exception_source_map(self):
    if Settings.USE_TYPED_ARRAYS != 2: return self.skip("doesn't pass without typed arrays")
    if '-g4' not in Building.COMPILER_TEST_OPTS: Building.COMPILER_TEST_OPTS.append('-g4')
    if NODE_JS not in JS_ENGINES: return self.skip('sourcemapper requires Node to run')

    src = '''
      #include <stdio.h>

      __attribute__((noinline)) void foo(int i) {
          if (i < 10) throw i; // line 5
      }

      int main() {
        int i;
        scanf("%d", &i);
        foo(i);
        return 0;
      }
    '''

    def post(filename):
      import json
      map_filename = filename + '.map'
      mappings = json.loads(jsrun.run_js(
        path_from_root('tools', 'source-maps', 'sourcemap2json.js'),
        tools.shared.NODE_JS, [map_filename]))
      with open(filename) as f: lines = f.readlines()
      for m in mappings:
        if m['originalLine'] == 5 and '__cxa_throw' in lines[m['generatedLine']]:
          return
      assert False, 'Must label throw statements with line numbers'

    dirname = self.get_dir()
    self.build(src, dirname, os.path.join(dirname, 'src.cpp'), post_build=(None, post))

  def test_linespecific(self):
    if Settings.ASM_JS: return self.skip('asm always has corrections on')

    if '-g' not in Building.COMPILER_TEST_OPTS: Building.COMPILER_TEST_OPTS.append('-g')
    if self.emcc_args:
      self.emcc_args += ['--llvm-opts', '0'] # llvm full opts make the expected failures here not happen
      Building.COMPILER_TEST_OPTS += ['--llvm-opts', '0']

    Settings.CHECK_SIGNS = 0
    Settings.CHECK_OVERFLOWS = 0

    # Signs

    src = '''
      #include <stdio.h>
      #include <assert.h>

      int main()
      {
        int varey = 100;
        unsigned int MAXEY = -1;
        printf("*%d*\\n", varey >= MAXEY); // 100 >= -1? not in unsigned!
      }
    '''

    Settings.CORRECT_SIGNS = 0
    self.do_run(src, '*1*') # This is a fail - we expect 0

    Settings.CORRECT_SIGNS = 1
    self.do_run(src, '*0*') # Now it will work properly

    # And now let's fix just that one line
    Settings.CORRECT_SIGNS = 2
    Settings.CORRECT_SIGNS_LINES = ["src.cpp:9"]
    self.do_run(src, '*0*')

    # Fixing the wrong line should not work
    Settings.CORRECT_SIGNS = 2
    Settings.CORRECT_SIGNS_LINES = ["src.cpp:3"]
    self.do_run(src, '*1*')

    # And reverse the checks with = 2
    Settings.CORRECT_SIGNS = 3
    Settings.CORRECT_SIGNS_LINES = ["src.cpp:3"]
    self.do_run(src, '*0*')
    Settings.CORRECT_SIGNS = 3
    Settings.CORRECT_SIGNS_LINES = ["src.cpp:9"]
    self.do_run(src, '*1*')

    Settings.CORRECT_SIGNS = 0

    # Overflows

    src = '''
      #include<stdio.h>
      int main() {
        int t = 77;
        for (int i = 0; i < 30; i++) {
          t = t + t + t + t + t + 1;
        }
        printf("*%d,%d*\\n", t, t & 127);
        return 0;
      }
    '''

    correct = '*186854335,63*'
    Settings.CORRECT_OVERFLOWS = 0
    try:
      self.do_run(src, correct)
      raise Exception('UNEXPECTED-PASS')
    except Exception, e:
      assert 'UNEXPECTED' not in str(e), str(e)
      assert 'Expected to find' in str(e), str(e)

    Settings.CORRECT_OVERFLOWS = 1
    self.do_run(src, correct) # Now it will work properly

    # And now let's fix just that one line
    Settings.CORRECT_OVERFLOWS = 2
    Settings.CORRECT_OVERFLOWS_LINES = ["src.cpp:6"]
    self.do_run(src, correct)

    # Fixing the wrong line should not work
    Settings.CORRECT_OVERFLOWS = 2
    Settings.CORRECT_OVERFLOWS_LINES = ["src.cpp:3"]
    try:
      self.do_run(src, correct)
      raise Exception('UNEXPECTED-PASS')
    except Exception, e:
      assert 'UNEXPECTED' not in str(e), str(e)
      assert 'Expected to find' in str(e), str(e)

    # And reverse the checks with = 2
    Settings.CORRECT_OVERFLOWS = 3
    Settings.CORRECT_OVERFLOWS_LINES = ["src.cpp:3"]
    self.do_run(src, correct)
    Settings.CORRECT_OVERFLOWS = 3
    Settings.CORRECT_OVERFLOWS_LINES = ["src.cpp:6"]
    try:
      self.do_run(src, correct)
      raise Exception('UNEXPECTED-PASS')
    except Exception, e:
      assert 'UNEXPECTED' not in str(e), str(e)
      assert 'Expected to find' in str(e), str(e)

    Settings.CORRECT_OVERFLOWS = 0

    # Roundings

    src = '''
      #include <stdio.h>
      #include <assert.h>

      int main()
      {
        TYPE x = -5;
        printf("*%d*", x/2);
        x = 5;
        printf("*%d*", x/2);

        float y = -5.33;
        x = y;
        printf("*%d*", x);
        y = 5.33;
        x = y;
        printf("*%d*", x);

        printf("\\n");
      }
    '''

    if Settings.USE_TYPED_ARRAYS != 2: # the errors here are very specific to non-i64 mode 1
      Settings.CORRECT_ROUNDINGS = 0
      self.do_run(src.replace('TYPE', 'long long'), '*-3**2**-6**5*') # JS floor operations, always to the negative. This is an undetected error here!
      self.do_run(src.replace('TYPE', 'int'), '*-2**2**-5**5*') # We get these right, since they are 32-bit and we can shortcut using the |0 trick
      self.do_run(src.replace('TYPE', 'unsigned int'), '*-2**2**-6**5*')

    Settings.CORRECT_ROUNDINGS = 1
    Settings.CORRECT_SIGNS = 1 # To be correct here, we need sign corrections as well
    self.do_run(src.replace('TYPE', 'long long'), '*-2**2**-5**5*') # Correct
    self.do_run(src.replace('TYPE', 'int'), '*-2**2**-5**5*') # Correct
    self.do_run(src.replace('TYPE', 'unsigned int'), '*2147483645**2**-5**5*') # Correct
    Settings.CORRECT_SIGNS = 0

    if Settings.USE_TYPED_ARRAYS != 2: # the errors here are very specific to non-i64 mode 1
      Settings.CORRECT_ROUNDINGS = 2
      Settings.CORRECT_ROUNDINGS_LINES = ["src.cpp:13"] # Fix just the last mistake
      self.do_run(src.replace('TYPE', 'long long'), '*-3**2**-5**5*')
      self.do_run(src.replace('TYPE', 'int'), '*-2**2**-5**5*') # Here we are lucky and also get the first one right
      self.do_run(src.replace('TYPE', 'unsigned int'), '*-2**2**-5**5*')

    # And reverse the check with = 2
    if Settings.USE_TYPED_ARRAYS != 2: # the errors here are very specific to non-i64 mode 1
      Settings.CORRECT_ROUNDINGS = 3
      Settings.CORRECT_ROUNDINGS_LINES = ["src.cpp:999"]
      self.do_run(src.replace('TYPE', 'long long'), '*-2**2**-5**5*')
      self.do_run(src.replace('TYPE', 'int'), '*-2**2**-5**5*')
      Settings.CORRECT_SIGNS = 1 # To be correct here, we need sign corrections as well
      self.do_run(src.replace('TYPE', 'unsigned int'), '*2147483645**2**-5**5*')
      Settings.CORRECT_SIGNS = 0

  def test_exit_status(self):
    if self.emcc_args is None: return self.skip('need emcc')
    src = r'''
      #include <stdio.h>
      #include <stdlib.h>
      static void cleanup() {
        printf("cleanup\n");
      }

      int main() {
        atexit(cleanup); // this atexit should still be called
        printf("hello, world!\n");
        exit(118); // Unusual exit status to make sure it's working!
      }
    '''
    open('post.js', 'w').write('''
      Module.addOnExit(function () {
        Module.print('I see exit status: ' + EXITSTATUS);
      });
      Module.callMain();
    ''')
    self.emcc_args += ['-s', 'INVOKE_RUN=0', '--post-js', 'post.js']
    self.do_run(src, 'hello, world!\nexit(118) called\ncleanup\nI see exit status: 118')

  def test_gc(self):
    if self.emcc_args == None: return self.skip('needs ta2')
    if Settings.ASM_JS: return self.skip('asm cannot support generic function table')

    Settings.GC_SUPPORT = 1

    src = r'''
      #include <stdio.h>
      #include <gc.h>
      #include <assert.h>

      void *global;

      void finalizer(void *ptr, void *arg) {
        printf("finalizing %d (global == %d)\n", (int)arg, ptr == global);
      }

      void finalizer2(void *ptr, void *arg) {
        printf("finalizing2 %d (global == %d)\n", (int)arg, ptr == global);
      }

      int main() {
        GC_INIT();

        void *local, *local2, *local3, *local4, *local5, *local6;

        // Hold on to global, drop locals

        global = GC_MALLOC(1024); // rooted since in a static allocation
        GC_REGISTER_FINALIZER_NO_ORDER(global, finalizer, 0, 0, 0);
        printf("alloc %p\n", global);

        local = GC_MALLOC(1024); // not rooted since stack is not scanned
        GC_REGISTER_FINALIZER_NO_ORDER(local, finalizer, (void*)1, 0, 0);
        printf("alloc %p\n", local);

        assert((char*)local - (char*)global >= 1024 || (char*)global - (char*)local >= 1024);

        local2 = GC_MALLOC(1024); // no finalizer
        printf("alloc %p\n", local2);

        local3 = GC_MALLOC(1024); // with finalizable2
        GC_REGISTER_FINALIZER_NO_ORDER(local3, finalizer2, (void*)2, 0, 0);
        printf("alloc %p\n", local);

        local4 = GC_MALLOC(1024); // yet another
        GC_REGISTER_FINALIZER_NO_ORDER(local4, finalizer2, (void*)3, 0, 0);
        printf("alloc %p\n", local);

        printf("basic test\n");

        GC_FORCE_COLLECT();

        printf("*\n");

        GC_FREE(global); // force free will actually work

        // scanning inside objects

        global = GC_MALLOC(12);
        GC_REGISTER_FINALIZER_NO_ORDER(global, finalizer, 0, 0, 0);
        local = GC_MALLOC(12);
        GC_REGISTER_FINALIZER_NO_ORDER(local, finalizer, (void*)1, 0, 0);
        local2 = GC_MALLOC_ATOMIC(12);
        GC_REGISTER_FINALIZER_NO_ORDER(local2, finalizer, (void*)2, 0, 0);
        local3 = GC_MALLOC(12);
        GC_REGISTER_FINALIZER_NO_ORDER(local3, finalizer, (void*)3, 0, 0);
        local4 = GC_MALLOC(12);
        GC_REGISTER_FINALIZER_NO_ORDER(local4, finalizer, (void*)4, 0, 0);
        local5 = GC_MALLOC_UNCOLLECTABLE(12);
        // This should never trigger since local5 is uncollectable
        GC_REGISTER_FINALIZER_NO_ORDER(local5, finalizer, (void*)5, 0, 0);

        printf("heap size = %d\n", GC_get_heap_size());

        local4 = GC_REALLOC(local4, 24);

        printf("heap size = %d\n", GC_get_heap_size());

        local6 = GC_MALLOC(12);
        GC_REGISTER_FINALIZER_NO_ORDER(local6, finalizer, (void*)6, 0, 0);
        // This should be the same as a free
        GC_REALLOC(local6, 0);

        void **globalData = (void**)global;
        globalData[0] = local;
        globalData[1] = local2;

        void **localData = (void**)local;
        localData[0] = local3;

        void **local2Data = (void**)local2;
        local2Data[0] = local4; // actually ignored, because local2 is atomic, so 4 is freeable

        printf("object scan test test\n");

        GC_FORCE_COLLECT();

        printf("*\n");

        GC_FREE(global); // force free will actually work

        printf("*\n");

        GC_FORCE_COLLECT();

        printf(".\n");

        global = 0;

        return 0;
      }
    '''
    self.do_run(src, '''basic test
finalizing 1 (global == 0)
finalizing2 2 (global == 0)
finalizing2 3 (global == 0)
*
finalizing 0 (global == 1)
heap size = 72
heap size = 84
finalizing 6 (global == 0)
object scan test test
finalizing 4 (global == 0)
*
finalizing 0 (global == 1)
*
finalizing 1 (global == 0)
finalizing 2 (global == 0)
finalizing 3 (global == 0)
.
''')

# Generate tests for everything
def make_run(fullname, name=-1, compiler=-1, embetter=0, quantum_size=0,
    typed_arrays=0, emcc_args=None, env=None):

  if env is None: env = {}

  TT = type(fullname, (T,), dict(run_name = fullname, env = env))

  def tearDown(self):
    super(TT, self).tearDown()

    for k, v in self.env.iteritems():
      del os.environ[k]

    # clear global changes to Building
    Building.COMPILER_TEST_OPTS = []
    Building.COMPILER = CLANG
    Building.LLVM_OPTS = 0

  TT.tearDown = tearDown

  def setUp(self):
    super(TT, self).setUp()
    for k, v in self.env.iteritems():
      assert k not in os.environ, k + ' should not be in environment'
      os.environ[k] = v

    global checked_sanity
    if not checked_sanity:
      print '(checking sanity from test runner)' # do this after we set env stuff
      check_sanity(force=True)
      checked_sanity = True

    Building.COMPILER_TEST_OPTS = ['-g']
    os.chdir(self.get_dir()) # Ensure the directory exists and go there
    Building.COMPILER = compiler

    self.emcc_args = None if emcc_args is None else emcc_args[:]
    if self.emcc_args is not None:
      Settings.load(self.emcc_args)
      Building.LLVM_OPTS = 0
      if '-O2' in self.emcc_args:
        Building.COMPILER_TEST_OPTS = [] # remove -g in -O2 tests, for more coverage
      #Building.COMPILER_TEST_OPTS += self.emcc_args
      for arg in self.emcc_args:
        if arg.startswith('-O'):
          Building.COMPILER_TEST_OPTS.append(arg) # so bitcode is optimized too, this is for cpp to ll
        else:
          try:
            key, value = arg.split('=')
            Settings[key] = value # forward  -s K=V
          except:
            pass
      return

    # TODO: Move much of these to a init() function in shared.py, and reuse that
    Settings.USE_TYPED_ARRAYS = typed_arrays
    Settings.INVOKE_RUN = 1
    Settings.RELOOP = 0 # we only do them in the "o2" pass
    Settings.MICRO_OPTS = embetter
    Settings.QUANTUM_SIZE = quantum_size
    Settings.ASSERTIONS = 1-embetter
    Settings.SAFE_HEAP = 1-embetter
    Settings.CHECK_OVERFLOWS = 1-embetter
    Settings.CORRECT_OVERFLOWS = 1-embetter
    Settings.CORRECT_SIGNS = 0
    Settings.CORRECT_ROUNDINGS = 0
    Settings.CORRECT_OVERFLOWS_LINES = CORRECT_SIGNS_LINES = CORRECT_ROUNDINGS_LINES = SAFE_HEAP_LINES = []
    Settings.CHECK_SIGNS = 0 #1-embetter
    Settings.RUNTIME_TYPE_INFO = 0
    Settings.DISABLE_EXCEPTION_CATCHING = 0
    Settings.INCLUDE_FULL_LIBRARY = 0
    Settings.BUILD_AS_SHARED_LIB = 0
    Settings.RUNTIME_LINKED_LIBS = []
    Settings.EMULATE_UNALIGNED_ACCESSES = int(Settings.USE_TYPED_ARRAYS == 2 and Building.LLVM_OPTS == 2)
    Settings.DOUBLE_MODE = 1 if Settings.USE_TYPED_ARRAYS and Building.LLVM_OPTS == 0 else 0
    Settings.PRECISE_I64_MATH = 0
    Settings.NAMED_GLOBALS = 0 if not embetter else 1

  TT.setUp = setUp

  return TT

# Make one run with the defaults
default = make_run("default", compiler=CLANG, emcc_args=[])

# Make one run with -O1, with safe heap
o1 = make_run("o1", compiler=CLANG, emcc_args=["-O1", "-s", "ASM_JS=0", "-s", "SAFE_HEAP=1"])

# Make one run with -O2, but without closure (we enable closure in specific tests, otherwise on everything it is too slow)
o2 = make_run("o2", compiler=CLANG, emcc_args=["-O2", "-s", "ASM_JS=0", "-s", "JS_CHUNK_SIZE=1024"])

# asm.js
asm1 = make_run("asm1", compiler=CLANG, emcc_args=["-O1"])
asm2 = make_run("asm2", compiler=CLANG, emcc_args=["-O2"])
asm2g = make_run("asm2g", compiler=CLANG, emcc_args=["-O2", "-g", "-s", "ASSERTIONS=1", "--memory-init-file", "1", "-s", "CHECK_HEAP_ALIGN=1"])
asm2x86 = make_run("asm2x86", compiler=CLANG, emcc_args=["-O2", "-g", "-s", "CHECK_HEAP_ALIGN=1"], env={"EMCC_LLVM_TARGET": "i386-pc-linux-gnu"})

# Make custom runs with various options
for compiler, quantum, embetter, typed_arrays in [
  (CLANG, 4, 0, 0),
  (CLANG, 4, 1, 1),
]:
  fullname = 's_0_%d%s%s' % (
    embetter, '' if quantum == 4 else '_q' + str(quantum), '' if typed_arrays in [0, 1] else '_t' + str(typed_arrays)
  )
  locals()[fullname] = make_run(fullname, fullname, compiler, embetter, quantum, typed_arrays)

del T # T is just a shape for the specific subclasses, we don't test it itself