summaryrefslogtreecommitdiff
path: root/testbed.cc
blob: 7f5762a3ab1d831de6fe40f69f37df5154cd232c (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
/*==========================================================================
  testbed
  ---------
  Copyright: Juha Nieminen, Joel Yliluoma
  This program (testbed) is distributed under the terms of
  the GNU General Public License (GPL) version 3.
  See gpl.txt for the license text.
============================================================================*/

#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

static const char* const kVersionNumber = "2.3.0.12";

#include "fpconfig.hh"
#include "fparser.hh"

#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
#include "fparser_mpfr.hh"
#endif
#ifdef FP_SUPPORT_GMP_INT_TYPE
#include "fparser_gmpint.hh"
#endif

#include "extrasrc/fpaux.hh"

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cassert>

#define CONST 1.5

#define StringifyHlp(x) #x
#define Stringify(x) StringifyHlp(x)

#ifndef FP_DISABLE_DOUBLE_TYPE
typedef FunctionParser DefaultParser;
#elif defined(FP_SUPPORT_LONG_DOUBLE_TYPE)
typedef FunctionParser_ld DefaultParser;
#elif defined(FP_SUPPORT_FLOAT_TYPE)
typedef FunctionParser_f DefaultParser;
#elif defined(FP_SUPPORT_MPFR_FLOAT_TYPE)
typedef FunctionParser_mpfr DefaultParser;
#else
#error "FunctionParserBase<double> was disabled and no viable floating point alternative has been defined"
#endif

#undef FP_TEST_WANT_FLOAT_TYPE
#ifdef FP_SUPPORT_FLOAT_TYPE
 #define FP_TEST_WANT_FLOAT_TYPE
#endif
#undef FP_TEST_WANT_DOUBLE_TYPE
#ifndef FP_DISABLE_DOUBLE_TYPE
 #define FP_TEST_WANT_DOUBLE_TYPE
#endif
#undef FP_TEST_WANT_LONG_DOUBLE_TYPE
#ifdef FP_SUPPORT_LONG_DOUBLE_TYPE
 #define FP_TEST_WANT_LONG_DOUBLE_TYPE
#endif
#undef FP_TEST_WANT_MPFR_FLOAT_TYPE
#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
 #define FP_TEST_WANT_MPFR_FLOAT_TYPE
#endif
#undef FP_TEST_WANT_GMP_INT_TYPE
#ifdef FP_SUPPORT_GMP_INT_TYPE
 #define FP_TEST_WANT_GMP_INT_TYPE
#endif
#undef FP_TEST_WANT_LONG_INT_TYPE
#if defined(FP_SUPPORT_LONG_INT_TYPE) || defined(FP_SUPPORT_GMP_INT_TYPE)
 #define FP_TEST_WANT_LONG_INT_TYPE
#endif
#undef FP_TEST_WANT_COMPLEX_FLOAT_TYPE
#ifdef FP_SUPPORT_COMPLEX_FLOAT_TYPE
 #define FP_TEST_WANT_COMPLEX_FLOAT_TYPE
#endif
#undef FP_TEST_WANT_COMPLEX_DOUBLE_TYPE
#ifdef FP_SUPPORT_COMPLEX_DOUBLE_TYPE
 #define FP_TEST_WANT_COMPLEX_DOUBLE_TYPE
#endif
#undef FP_TEST_WANT_COMPLEX_LONG_DOUBLE_TYPE
#ifdef FP_SUPPORT_COMPLEX_LONG_DOUBLE_TYPE
 #define FP_TEST_WANT_COMPLEX_LONG_DOUBLE_TYPE
#endif

typedef DefaultParser::value_type DefaultValue_t;


namespace
{
    /* Verbosity level:
       0 = No progress output. Error reporting as in verbosity level 1.
       1 = Very brief progress and error output.
       2 = More verbose progress output, full error reporting.
       3 = Very verbose progress output, full error reporting.
    */
    int verbosityLevel = 1;


    const char* getEvalErrorName(int errorCode)
    {
        static const char* const evalErrorNames[6] =
        {
            "no error", "division by zero", "sqrt error", "log error",
            "trigonometric error", "max eval recursion level reached"
        };
        if(errorCode >= 0 && errorCode < 6)
            return evalErrorNames[errorCode];
        return "unknown";
    }

    std::vector<const char*> selectedRegressionTests;

    // Auxiliary functions
    // -------------------
    template<typename Value_t>
    inline Value_t r2d(Value_t x)
    { return x * (Value_t(180) / FUNCTIONPARSERTYPES::fp_const_pi<Value_t>()); }

    template<typename Value_t>
    inline Value_t d2r(Value_t x)
    { return x * (FUNCTIONPARSERTYPES::fp_const_pi<Value_t>() / Value_t(180)); }

    //inline double log10(double x) { return std::log(x) / std::log(10); }

    template<typename Value_t>
    Value_t userDefFuncSqr(const Value_t* p) { return p[0]*p[0]; }

    template<typename Value_t>
    Value_t userDefFuncSub(const Value_t* p) { return p[0]-p[1]; }

    template<typename Value_t>
    Value_t userDefFuncValue(const Value_t*) { return 10; }


    template<typename Value_t>
    class UserDefFuncWrapper:
        public FunctionParserBase<Value_t>::FunctionWrapper
    {
        Value_t (*mFuncPtr)(const Value_t*);
        unsigned mCounter;

     public:
        UserDefFuncWrapper(Value_t (*funcPtr)(const Value_t*)) :
            mFuncPtr(funcPtr), mCounter(0)
        {}

        virtual Value_t callFunction(const Value_t* values)
        {
            ++mCounter;
            return mFuncPtr(values);
        }

        unsigned counter() const { return mCounter; }
    };


    template<typename Value_t>
    inline Value_t testbedEpsilon() { return Value_t(1e-9); }

    template<>
    inline float testbedEpsilon<float>() { return 1e-3f; }

    template<>
    inline long double testbedEpsilon<long double>() { return 1e-10l; }

#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
    template<>
    inline MpfrFloat testbedEpsilon<MpfrFloat>()
    {
        static const MpfrFloat eps(2e-20);
        return eps;
    }
#endif

#ifdef FP_SUPPORT_COMPLEX_FLOAT_TYPE
    template<>
    inline std::complex<float> testbedEpsilon<std::complex<float> >()
    { return testbedEpsilon<float>(); }
#endif

#ifdef FP_SUPPORT_COMPLEX_LONG_DOUBLE_TYPE
    template<>
    inline std::complex<long double> testbedEpsilon<std::complex<long double> >()
    { return testbedEpsilon<long double>(); }
#endif


#ifndef _MSC_VER
    /*void setAnsiColor(unsigned color)
    {
        static int bold = 0;
        std::cout << "\33[";
        if(color > 7)
        {
            if(!bold) { std::cout << "1;"; bold=1; }
            color -= 7;
        }
        else if(bold) { std::cout << "0;"; bold=0; }
        std::cout << 30+color << "m";
    }*/

    void setAnsiBold() { std::cout << "\33[1m"; }

    void resetAnsiColor() { std::cout << "\33[0m"; }
#else
    /*void setAnsiColor(unsigned) {}*/
    void setAnsiBold() {}
    void resetAnsiColor() {}
#endif
}


//=========================================================================
// Copying testing functions
//=========================================================================
bool TestCopyingNoDeepCopy(DefaultParser p)
{
    DefaultValue_t vars[2] = { 3, 5 };

    if(std::fabs(p.Eval(vars) - 13) > testbedEpsilon<DefaultValue_t>())
    {
        if(verbosityLevel >= 2)
        {
            std::cout
                << "\n - Giving as function parameter (no deep copy) failed."
                << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p.PrintByteCode(std::cout);
#endif
        }
        return false;
    }
    return true;
}

bool TestCopyingDeepCopy(DefaultParser p)
{
    DefaultValue_t vars[2] = { 3, 5 };

    p.Parse("x*y-1", "x,y");

    if(std::fabs(p.Eval(vars) - 14) > testbedEpsilon<DefaultValue_t>())
    {
        if(verbosityLevel >= 2)
        {
            std::cout
                << "\n - Giving as function parameter (deep copy) failed."
                << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p.PrintByteCode(std::cout);
#endif
        }
        return false;
    }
    return true;
}

int TestCopying()
{
    bool retval = true;
    DefaultValue_t vars[2] = { 2, 5 };
    const DefaultValue_t epsilon = testbedEpsilon<DefaultValue_t>();

    DefaultParser p1, p3;
    p1.Parse("x*y-2", "x,y");

    DefaultParser p2(p1);
    if(std::fabs(p2.Eval(vars) - 8) > epsilon)
    {
        retval = false;
        if(verbosityLevel >= 2)
        {
            std::cout << "\n - Copy constructor with no deep copy failed."
                      << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p2.PrintByteCode(std::cout);
#endif
        }
    }

    p2.Parse("x*y-1", "x,y");
    if(std::fabs(p2.Eval(vars) - 9) > epsilon)
    {
        retval = false;
        if(verbosityLevel >= 2)
        {
            std::cout << "\n - Copy constructor with deep copy failed."
                      << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p2.PrintByteCode(std::cout);
#endif
        }
    }

    p3 = p1;
    if(std::fabs(p3.Eval(vars) - 8) > epsilon)
    {
        retval = false;
        if(verbosityLevel >= 2)
        {
            std::cout << "\n - Assignment with no deep copy failed."
                      << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p3.PrintByteCode(std::cout);
#endif
        }
    }

    p3.Parse("x*y-1", "x,y");
    if(std::fabs(p3.Eval(vars) - 9) > epsilon)
    {
        retval = false;
        if(verbosityLevel >= 2)
        {
            std::cout << "\n - Assignment with deep copy failed."
                      << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p3.PrintByteCode(std::cout);
#endif
        }
    }

    if(!TestCopyingNoDeepCopy(p1))
        retval = false;

    // Final test to check that p1 still works:
    if(std::fabs(p1.Eval(vars) - 8) > epsilon)
    {
        retval = false;
        if(verbosityLevel >= 2)
            std::cout << "\n - Failed: p1 was corrupted." << std::endl;
    }

    if(!TestCopyingDeepCopy(p1))
        retval = false;

    // Final test to check that p1 still works:
    if(std::fabs(p1.Eval(vars) - 8) > epsilon)
    {
        retval = false;
        if(verbosityLevel >= 2)
        {
            std::cout << "\n - Failed: p1 was corrupted." << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
            p1.PrintByteCode(std::cout);
#endif
        }
    }

    return retval;
}


//=========================================================================
// Test error situations
//=========================================================================
int TestErrorSituations()
{
    bool retval = true;
    DefaultParser fp, tmpfp;
    fp.AddUnit("unit", 2);
    fp.AddFunction("Value", userDefFuncValue<DefaultValue_t>, 0);
    fp.AddFunction("Sqr", userDefFuncSqr<DefaultValue_t>, 1);
    fp.AddFunctionWrapper("Sub", UserDefFuncWrapper<DefaultValue_t>
                          (userDefFuncSub<DefaultValue_t>), 2);
    tmpfp.Parse("0", "x");

    static const struct
    {
        DefaultParser::ParseErrorType expected_error;
        int                            expected_error_position;
        const char*                    function_string;
    } invalidFuncs[] =
    {
      { DefaultParser::MISSING_PARENTH,     5, "sin(x"},
      { DefaultParser::EXPECT_PARENTH_FUNC, 4, "sin x"},
      { DefaultParser::SYNTAX_ERROR,        2, "x+" },
      { DefaultParser::EXPECT_OPERATOR,     2, "x x"},
      { DefaultParser::UNKNOWN_IDENTIFIER,  4, "sin(y)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   5, "sin(x, 1)" },
      { DefaultParser::EXPECT_OPERATOR,     1, "x, x"},
      { DefaultParser::SYNTAX_ERROR,        2, "x^^2" },
      { DefaultParser::SYNTAX_ERROR,        2, "x**x" },
      { DefaultParser::SYNTAX_ERROR,        2, "x+*x" },
      { DefaultParser::SYNTAX_ERROR,        0, "unit" },
      { DefaultParser::SYNTAX_ERROR,        0, "unit x" },
      { DefaultParser::SYNTAX_ERROR,        2, "x*unit" },
      { DefaultParser::SYNTAX_ERROR,        0, "unit*unit" },
      { DefaultParser::SYNTAX_ERROR,        0, "unit unit" },
      { DefaultParser::EXPECT_OPERATOR,     1, "x(unit)"},
      { DefaultParser::SYNTAX_ERROR,        2, "x+unit" },
      { DefaultParser::SYNTAX_ERROR,        2, "x*unit" },
      { DefaultParser::EMPTY_PARENTH,       1, "()"},
      { DefaultParser::SYNTAX_ERROR,        0, "" },
      { DefaultParser::EXPECT_OPERATOR,     1, "x()"},
      { DefaultParser::EMPTY_PARENTH,       3, "x*()"},
      { DefaultParser::SYNTAX_ERROR,        4, "sin(unit)" },
      { DefaultParser::EXPECT_PARENTH_FUNC, 4, "sin unit"},
      { DefaultParser::EXPECT_OPERATOR,     2, "1..2"},
      { DefaultParser::SYNTAX_ERROR,        1, "(" },
      { DefaultParser::MISM_PARENTH,        0, ")"},
      { DefaultParser::MISSING_PARENTH,     2, "(x"},
      { DefaultParser::EXPECT_OPERATOR,     1, "x)"},
      { DefaultParser::MISM_PARENTH,        0, ")x("},
      { DefaultParser::MISSING_PARENTH,     14,"(((((((x))))))"},
      { DefaultParser::EXPECT_OPERATOR,     15,"(((((((x))))))))"},
      { DefaultParser::EXPECT_OPERATOR,     1, "2x"},
      { DefaultParser::EXPECT_OPERATOR,     3, "(2)x"},
      { DefaultParser::EXPECT_OPERATOR,     3, "(x)2"},
      { DefaultParser::EXPECT_OPERATOR,     1, "2(x)"},
      { DefaultParser::EXPECT_OPERATOR,     1, "x(2)"},
      { DefaultParser::SYNTAX_ERROR,        0, "[x]" },
      { DefaultParser::SYNTAX_ERROR,        0, "@x" },
      { DefaultParser::SYNTAX_ERROR,        0, "$x" },
      { DefaultParser::SYNTAX_ERROR,        0, "{x}" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   5, "max(x)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   8, "max(x, 1, 2)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   6, "if(x,2)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   10,"if(x, 2, 3, 4)" },
      { DefaultParser::MISSING_PARENTH,     6, "Value(x)"},
      { DefaultParser::MISSING_PARENTH,     6, "Value(1+x)"},
      { DefaultParser::MISSING_PARENTH,     6, "Value(1,x)"},
      // Note: ^should these three not return ILL_PARAMS_AMOUNT instead?
      { DefaultParser::ILL_PARAMS_AMOUNT,   4, "Sqr()"},
      { DefaultParser::ILL_PARAMS_AMOUNT,   5, "Sqr(x,1)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   5, "Sqr(1,2,x)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   4, "Sub()" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   5, "Sub(x)" },
      { DefaultParser::ILL_PARAMS_AMOUNT,   7, "Sub(x,1,2)" },
      { DefaultParser::UNKNOWN_IDENTIFIER,  2, "x+Sin(1)" },
      { DefaultParser::UNKNOWN_IDENTIFIER,  0, "sub(1,2)" },
      { DefaultParser::UNKNOWN_IDENTIFIER,  0, "sinx(1)"  },
      { DefaultParser::UNKNOWN_IDENTIFIER,  2, "1+X"      },
      { DefaultParser::UNKNOWN_IDENTIFIER,  0, "eval(x)" }
    };
    const unsigned amnt = sizeof(invalidFuncs)/sizeof(invalidFuncs[0]);
    for(unsigned i = 0; i < amnt; ++i)
    {
        int parse_result = fp.Parse(invalidFuncs[i].function_string, "x");
        if(parse_result < 0)
        {
            retval = false;
            if(verbosityLevel >= 2)
            {
                std::cout << "\n - Parsing the invalid function \""
                          << invalidFuncs[i].function_string
                          << "\" didn't fail\n";
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
                fp.PrintByteCode(std::cout);
#endif
            }
        }
        else if(fp.GetParseErrorType() != invalidFuncs[i].expected_error
             || parse_result != invalidFuncs[i].expected_error_position)
        {
            retval = false;
            if(verbosityLevel >= 2)
            {
                std::cout << "\n - Parsing the invalid function \""
                          << invalidFuncs[i].function_string
                          << "\" produced ";
                if(fp.GetParseErrorType() != invalidFuncs[i].expected_error)
                    std::cout << "wrong error code (" << fp.ErrorMsg() << ")";
                if(parse_result != invalidFuncs[i].expected_error_position)
                    std::cout << "wrong pointer (expected "
                              << invalidFuncs[i].expected_error_position
                              << ", got " << parse_result << ")";
                std::cout << "\n";
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
                fp.PrintByteCode(std::cout);
#endif
            }
        }
    }

    static const char* const invalidNames[] =
    { "s2%", "sin", "(x)", "5x", "2", "\302\240"/*nbsp*/ };
    const unsigned namesAmnt = sizeof(invalidNames)/sizeof(invalidNames[0]);

    for(unsigned i = 0; i < namesAmnt; ++i)
    {
        const char* const n = invalidNames[i];
        if(fp.AddConstant(n, 1))
        {
            retval = false;
            if(verbosityLevel >= 2)
                std::cout << "\n - Adding an invalid name (\"" << n
                          << "\") as constant didn't fail" << std::endl;
        }
        if(fp.AddFunction(n, userDefFuncSqr<DefaultValue_t>, 1))
        {
            retval = false;
            if(verbosityLevel >= 2)
                std::cout << "\n - Adding an invalid name (\"" << n
                          << "\") as funcptr didn't fail" << std::endl;
        }
        if(fp.AddFunction(n, tmpfp))
        {
            retval = false;
            if(verbosityLevel >= 2)
                std::cout << "\n - Adding an invalid name (\"" << n
                          << "\") as funcparser didn't fail" << std::endl;
        }
        if(fp.Parse("0", n) < 0)
        {
            retval = false;
            if(verbosityLevel >= 2)
                std::cout << "\n - Using an invalid name (\"" << n
                          << "\") as variable name didn't fail" << std::endl;
        }
    }

    fp.AddConstant("CONST", 1);
    fp.AddFunction("PTR", userDefFuncSqr<DefaultValue_t>, 1);
    fp.AddFunction("PARSER", tmpfp);

    if(fp.AddConstant("PTR", 1))
    {
        retval = false;
        if(verbosityLevel >= 2)
            std::cout << "\n - Adding a userdef function (\"PTR\") as "
                      << "constant didn't fail" << std::endl;
    }
    if(fp.AddFunction("CONST", userDefFuncSqr<DefaultValue_t>, 1))
    {
        retval = false;
        if(verbosityLevel >= 2)
            std::cout << "\n - Adding a userdef constant (\"CONST\") as "
                      << "funcptr didn't fail" << std::endl;
    }
    if(fp.AddFunction("CONST", tmpfp))
    {
        retval = false;
        if(verbosityLevel >= 2)
            std::cout << "\n - Adding a userdef constant (\"CONST\") as "
                      << "funcparser didn't fail" << std::endl;
    }

    return retval;
}


//=========================================================================
// Thoroughly test whitespaces
//=========================================================================
DefaultValue_t wsFunc(DefaultValue_t x)
{
    return
        x + std::sin((x*-1.5)-(.5*2.0)*(((-x)*1.5+(2-(x)*2.0)*2.0)+(3.0*2.0))+
                     (1.5*2.0))+(cos(x)*2.0);
}

bool testWsFunc(DefaultParser& fp, const std::string& function)
{
    int res = fp.Parse(function, "x");
    if(res > -1)
    {
        if(verbosityLevel >= 2)
            std::cout << "\n - Parsing function:\n\"" << function
                      << "\"\nfailed at char " << res
                      << ": " << fp.ErrorMsg() << std::endl;
        return false;
    }

    DefaultValue_t vars[1];
    for(vars[0] = -2.0; vars[0] <= 2.0; vars[0] += .1)
        if(std::fabs(fp.Eval(vars) - wsFunc(vars[0])) >
           testbedEpsilon<DefaultValue_t>())
        {
            return false;
        }
    return true;
}

int WhiteSpaceTest()
{
    DefaultParser fp;
    fp.AddConstant("const", 1.5);
    fp.AddUnit("unit", 2.0);
    std::string function(" x + sin ( ( x * - 1.5 ) - .5 unit * ( ( ( - x ) * "
                         "const + ( 2 - ( x ) unit ) unit ) + 3 unit ) + "
                         "( const ) unit ) + cos ( x ) unit ");

    if(!testWsFunc(fp, function)) return false;

    static const unsigned char WhiteSpaceTables[][4] =
    {
        { 1, 0x09, 0,0 }, // tab
        { 1, 0x0A, 0,0 }, // linefeed
        { 1, 0x0B, 0,0 }, // vertical tab
        { 1, 0x0D, 0,0 }, // carriage return
        { 1, 0x20, 0,0 }, // space
        { 2, 0xC2,0xA0, 0 }, // U+00A0 (nbsp)
        { 3, 0xE2,0x80,0x80 }, { 3, 0xE2,0x80,0x81 }, // U+2000 to...
        { 3, 0xE2,0x80,0x82 }, { 3, 0xE2,0x80,0x83 }, { 3, 0xE2,0x80,0x84 },
        { 3, 0xE2,0x80,0x85 }, { 3, 0xE2,0x80,0x86 }, { 3, 0xE2,0x80,0x87 },
        { 3, 0xE2,0x80,0x88 }, { 3, 0xE2,0x80,0x89 },
        { 3, 0xE2,0x80,0x8A }, { 3, 0xE2,0x80,0x8B }, // ... U+200B
        { 3, 0xE2,0x80,0xAF }, { 3, 0xE2,0x81,0x9F }, // U+202F and U+205F
        { 3, 0xE3,0x80,0x80 } // U+3000
    };
    const unsigned n_whitespaces =
        sizeof(WhiteSpaceTables)/sizeof(*WhiteSpaceTables);

    for(unsigned i = 0; i < function.size(); ++i)
    {
        if(function[i] == ' ')
        {
            function.erase(i, 1);
            for(std::size_t a = 0; a < n_whitespaces; ++a)
            {
                if(!testWsFunc(fp, function)) return false;
                int length = (int)WhiteSpaceTables[a][0];
                const char* sequence = (const char*)&WhiteSpaceTables[a][1];
                function.insert(i, sequence, length);
                if(!testWsFunc(fp, function)) return false;
                function.erase(i, length);
            }
        }
    }
    return true;
}


//=========================================================================
// Test integer powers
//=========================================================================
bool compareExpValues(DefaultValue_t value,
                      const std::string& funcStr,
                      DefaultValue_t v1,
                      DefaultValue_t v2,
                      bool isOptimized)
{
    const DefaultValue_t epsilon = testbedEpsilon<DefaultValue_t>();
    const DefaultValue_t diff =
        std::fabs(v1) < epsilon ?
        (std::fabs(v2) < epsilon ? std::fabs(v1 - v2) :
         std::fabs((v1 - v2) / v2)) :
        std::fabs((v1 - v2) / v1);
    if(diff > epsilon)
    {
        if(verbosityLevel >= 2)
        {
            std::cout << "\n - For \"" << funcStr << "\" with x=" << value
                      << " the library (";
            if(!isOptimized) std::cout << "not ";
            std::cout << "optimized) returned\n"
                      << std::setprecision(18) << v2
                      << " instead of " << v1 << std::endl;
        }
        return false;
    }
    return true;
}

bool runIntPowTest(DefaultParser& fp, const std::string& funcStr,
                   int exponent, bool isOptimized)
{
    const int absExponent = exponent < 0 ? -exponent : exponent;

    for(int valueOffset = 0; valueOffset <= 5; ++valueOffset)
    {
        const DefaultValue_t value =
            (exponent >= 0 && valueOffset == 0) ? 0.0 :
            1.0+(valueOffset-1)/100.0;
        DefaultValue_t v1 = exponent == 0 ? 1 : value;
        for(int i = 2; i <= absExponent; ++i)
            v1 *= value;
        if(exponent < 0) v1 = 1.0/v1;

        const DefaultValue_t v2 = fp.Eval(&value);

        if(!compareExpValues(value, funcStr, v1, v2, isOptimized))
            return false;
    }

    return true;
}

bool runFractionalPowTest(const std::string& funcStr, double exponent)
{
    DefaultParser fp;
    if(fp.Parse(funcStr, "x") != -1)
    {
        if(verbosityLevel >= 2)
            std::cout << "\n - Parsing \"" << funcStr <<"\" failed: "
                      << fp.ErrorMsg() << "\n";
        return false;
    }

    for(int i = 0; i < 3; ++i)
    {
        for(int valueOffset = 0; valueOffset <= 10; ++valueOffset)
        {
            const DefaultValue_t value =
                (exponent >= 0 && valueOffset == 0) ? 0.0 :
                1.0+(valueOffset-1)/2.0;
            const DefaultValue_t v1 = std::pow(value, exponent);
            const DefaultValue_t v2 = fp.Eval(&value);

            if(!compareExpValues(value, funcStr, v1, v2, i > 0))
                return false;
        }
        fp.Optimize();
    }

    return true;
}

int TestIntPow()
{
    DefaultParser fp;

    for(int exponent = -1300; exponent <= 1300; ++exponent)
    {
        std::ostringstream os;
        os << "x^" << exponent;
        const std::string func = os.str();
        if(fp.Parse(func, "x") != -1)
        {
            if(verbosityLevel >= 2)
                std::cout << "\n - Parsing \"" << func <<"\" failed: "
                          << fp.ErrorMsg() << "\n";
            return false;
        }

        if(!runIntPowTest(fp, func, exponent, false)) return false;
        fp.Optimize();
        if(!runIntPowTest(fp, func, exponent, true)) return false;
    }

    for(int m = -27; m <= 27; ++m)
    {
        for(int n_sqrt=0; n_sqrt<=4; ++n_sqrt)
            for(int n_cbrt=0; n_cbrt<=4; ++n_cbrt)
            {
                if(n_sqrt+n_cbrt == 0) continue;

                std::ostringstream os;
                os << "x^(" << m << "/(1";
                for(int n=0; n<n_sqrt; ++n) os << "*2";
                for(int n=0; n<n_cbrt; ++n) os << "*3";
                os << "))";
                DefaultValue_t exponent = DefaultValue_t(m);
                if(n_sqrt > 0) exponent /= std::pow(2.0, n_sqrt);
                if(n_cbrt > 0) exponent /= std::pow(3.0, n_cbrt);
                if(!runFractionalPowTest(os.str(), exponent)) return false;
            }
    }

    return true;
}


//=========================================================================
// Test UTF-8 parsing
//=========================================================================
namespace
{
    typedef unsigned char UChar;
    struct CharValueRange { const UChar first, last; };

    const CharValueRange validValueRanges[][4] =
    {
        { { 0x30, 0x39 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, // digits
        { { 0x41, 0x5A }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, // uppercase ascii
        { { 0x5F, 0x5F }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, // underscore
        { { 0x61, 0x7A }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, // lowercase ascii
        // U+0080 through U+009F
        { { 0xC2, 0xC2 }, { 0x80, 0x9F }, { 0, 0 }, { 0, 0 } },
        // U+00A1 through U+00BF
        { { 0xC2, 0xC2 }, { 0xA1, 0xBF }, { 0, 0 }, { 0, 0 } },
        // U+00C0 through U+07FF
        { { 0xC3, 0xDF }, { 0x80, 0xBF }, { 0, 0 }, { 0, 0 } },
        // U+0800 through U+1FFF (skip U+2000..U+200bB, which are whitespaces)
        { { 0xE0, 0xE0 }, { 0xA0, 0xBF }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xE1, 0xE1 }, { 0x80, 0xBF }, { 0x80, 0xBF }, { 0, 0 } },
        // U+200C through U+202E (skip U+202F, which is a whitespace)
        { { 0xE2, 0xE2 }, { 0x80, 0x80 }, { 0x8C, 0xAE }, { 0, 0 } },
        // U+2030 through U+205E (skip U+205F, which is a whitespace)
        { { 0xE2, 0xE2 }, { 0x80, 0x80 }, { 0xB0, 0xBF }, { 0, 0 } },
        { { 0xE2, 0xE2 }, { 0x81, 0x81 }, { 0x80, 0x9E }, { 0, 0 } },
        // U+2060 through U+20FF (skip U+3000, which is a whitespace)
        { { 0xE2, 0xE2 }, { 0x81, 0x81 }, { 0xA0, 0xBF }, { 0, 0 } },
        { { 0xE2, 0xE2 }, { 0x82, 0xBF }, { 0x80, 0xBF }, { 0, 0 } },
        // U+3001 through U+CFFF
        { { 0xE3, 0xE3 }, { 0x80, 0x80 }, { 0x81, 0xBF }, { 0, 0 } },
        { { 0xE3, 0xE3 }, { 0x81, 0xBF }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xE4, 0xEC }, { 0x80, 0xBF }, { 0x80, 0xBF }, { 0, 0 } },
        // U+E000 through U+FFFF
        { { 0xEE, 0xEF }, { 0x80, 0xBF }, { 0x80, 0xBF }, { 0, 0 } },
        // U+10000 through U+FFFFF
        { { 0xF0, 0xF0 }, { 0x90, 0xBF }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF1, 0xF3 }, { 0x80, 0xBF }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        // U+100000 through U+10FFFF
        { { 0xF4, 0xF4 }, { 0x80, 0x8F }, { 0x80, 0xBF }, { 0x80, 0xBF } }
    };
    const unsigned validValueRangesAmount =
        sizeof(validValueRanges)/sizeof(validValueRanges[0]);

    const CharValueRange invalidValueRanges[][4] =
    {
        // spaces:
        { { 0x09, 0x09 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x0A, 0x0A }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x0B, 0x0B }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x0D, 0x0D }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x20, 0x20 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0xC2, 0xC2 }, { 0xA0, 0xA0 }, { 0, 0 }, { 0, 0 } },
        { { 0xE2, 0xE2 }, { 0x80, 0x80 }, { 0x80, 0x8B }, { 0, 0 } },
        { { 0xE2, 0xE2 }, { 0x80, 0x80 }, { 0xAF, 0xAF }, { 0, 0 } },
        { { 0xE2, 0xE2 }, { 0x81, 0x81 }, { 0x9F, 0x9F }, { 0, 0 } },
        { { 0xE3, 0xE3 }, { 0x80, 0x80 }, { 0x80, 0x80 }, { 0, 0 } },
        // others:
        { { 0xC0, 0xC1 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0xED, 0xED }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0xF5, 0xFF }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x21, 0x2F }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x3A, 0x40 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x5B, 0x5E }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x60, 0x60 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x7B, 0x7F }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0x80, 0xFF }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
        { { 0xE0, 0xEF }, { 0x80, 0xFF }, { 0, 0 }, { 0, 0 } },
        { { 0xF0, 0xF4 }, { 0x80, 0xFF }, { 0x80, 0xFF }, { 0, 0 } },

        { { 0xC2, 0xDF }, { 0x00, 0x7F }, { 0, 0 }, { 0, 0 } },
        { { 0xC2, 0xDF }, { 0xC0, 0xFF }, { 0, 0 }, { 0, 0 } },

        { { 0xE0, 0xE0 }, { 0x00, 0x9F }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xE0, 0xE0 }, { 0xA0, 0xBF }, { 0x00, 0x7F }, { 0, 0 } },
        { { 0xE0, 0xE0 }, { 0xA0, 0xBF }, { 0xC0, 0xFF }, { 0, 0 } },

        { { 0xE1, 0xEC }, { 0x00, 0x7F }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xE1, 0xEC }, { 0xC0, 0xFF }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xE1, 0xEC }, { 0x80, 0xBF }, { 0x00, 0x7F }, { 0, 0 } },
        { { 0xE1, 0xEC }, { 0x80, 0xBF }, { 0xC0, 0xFF }, { 0, 0 } },

        { { 0xEE, 0xEF }, { 0x00, 0x7F }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xEE, 0xEF }, { 0xC0, 0xFF }, { 0x80, 0xBF }, { 0, 0 } },
        { { 0xEE, 0xEF }, { 0x80, 0xBF }, { 0x00, 0x7F }, { 0, 0 } },
        { { 0xEE, 0xEF }, { 0x80, 0xBF }, { 0xC0, 0xFF }, { 0, 0 } },

        { { 0xF0, 0xF0 }, { 0x00, 0x8F }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF0, 0xF0 }, { 0xC0, 0xFF }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF0, 0xF0 }, { 0x90, 0xBF }, { 0x00, 0x7F }, { 0x80, 0xBF } },
        { { 0xF0, 0xF0 }, { 0x90, 0xBF }, { 0xC0, 0xFF }, { 0x80, 0xBF } },
        { { 0xF0, 0xF0 }, { 0x90, 0xBF }, { 0x80, 0xBF }, { 0x00, 0x7F } },
        { { 0xF0, 0xF0 }, { 0x90, 0xBF }, { 0x80, 0xBF }, { 0xC0, 0xFF } },

        { { 0xF1, 0xF3 }, { 0x00, 0x7F }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF1, 0xF3 }, { 0xC0, 0xFF }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF1, 0xF3 }, { 0x80, 0xBF }, { 0x00, 0x7F }, { 0x80, 0xBF } },
        { { 0xF1, 0xF3 }, { 0x80, 0xBF }, { 0xC0, 0xFF }, { 0x80, 0xBF } },
        { { 0xF1, 0xF3 }, { 0x80, 0xBF }, { 0x80, 0xBF }, { 0x00, 0x7F } },
        { { 0xF1, 0xF3 }, { 0x80, 0xBF }, { 0x80, 0xBF }, { 0xC0, 0xFF } },

        { { 0xF4, 0xF4 }, { 0x00, 0x7F }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF4, 0xF4 }, { 0x90, 0xFF }, { 0x80, 0xBF }, { 0x80, 0xBF } },
        { { 0xF4, 0xF4 }, { 0x80, 0x8F }, { 0x00, 0x7F }, { 0x80, 0xBF } },
        { { 0xF4, 0xF4 }, { 0x80, 0x8F }, { 0xC0, 0xFF }, { 0x80, 0xBF } },
        { { 0xF4, 0xF4 }, { 0x80, 0x8F }, { 0x80, 0xBF }, { 0x00, 0x7F } },
        { { 0xF4, 0xF4 }, { 0x80, 0x8F }, { 0x80, 0xBF }, { 0xC0, 0xFF } }
    };
    const unsigned invalidValueRangesAmount =
        sizeof(invalidValueRanges)/sizeof(invalidValueRanges[0]);

    class CharIter
    {
        const CharValueRange (*valueRanges)[4];
        const unsigned valueRangesAmount;
        UChar charValues[4];
        unsigned rangeIndex, firstRangeIndex, skipIndex;

        void initCharValues()
        {
            for(unsigned i = 0; i < 4; ++i)
                charValues[i] = valueRanges[rangeIndex][i].first;
        }

     public:
        CharIter(bool skipDigits, bool skipLowerCaseAscii):
            valueRanges(validValueRanges),
            valueRangesAmount(validValueRangesAmount),
            rangeIndex(skipDigits ? 1 : 0),
            firstRangeIndex(skipDigits ? 1 : 0),
            skipIndex(skipLowerCaseAscii ? 3 : ~0U)
        {
            initCharValues();
        }

        CharIter():
            valueRanges(invalidValueRanges),
            valueRangesAmount(invalidValueRangesAmount),
            rangeIndex(0), firstRangeIndex(0), skipIndex(~0U)
        {
            initCharValues();
        }

        void appendChar(std::string& dest) const
        {
            for(unsigned i = 0; i < 4; ++i)
            {
                if(charValues[i] == 0) break;
                dest += char(charValues[i]);
            }
        }

        bool next()
        {
            for(unsigned i = 0; i < 4; ++i)
            {
                if(charValues[i] < valueRanges[rangeIndex][i].last)
                {
                    ++charValues[i];
                    return true;
                }
            }
            if(++rangeIndex == skipIndex) ++rangeIndex;
            if(rangeIndex < valueRangesAmount)
            {
                initCharValues();
                return true;
            }
            rangeIndex = firstRangeIndex;
            initCharValues();
            return false;
        }

        void print() const
        {
            std::printf("{");
            for(unsigned i = 0; i < 4; ++i)
            {
                if(charValues[i] == 0) break;
                if(i > 0) std::printf(",");
                std::printf("%02X", unsigned(charValues[i]));
            }
            std::printf("}");
        }
    };

    bool printUTF8TestError(const char* testType,
                            const CharIter* iters, unsigned length,
                            const std::string& identifier)
    {
        if(verbosityLevel >= 2)
        {
            std::printf("\n - %s failed with identifier ", testType);
            for(unsigned i = 0; i < length; ++i)
                iters[i].print();
            std::printf(": \"%s\"\n", identifier.c_str());
        }
        return false;
    }

    bool printUTF8TestError2(const CharIter* iters, unsigned length)
    {
        if(verbosityLevel >= 2)
        {
            std::printf("\n - Parsing didn't fail with invalid identifier ");
            for(unsigned i = 0; i < length; ++i)
                iters[(length-1)-i].print();
            std::printf("\n");
        }
        return false;
    }
}

int UTF8Test()
{
    typedef DefaultParser::value_type Value_t;

    CharIter iters[4] =
        { CharIter(true, false),
          CharIter(false, true),
          CharIter(false, false),
          CharIter(false, false) };
    std::string identifier;
    DefaultParser fp;
    const Value_t value = 0.0;

    for(unsigned length = 1; length <= 4; ++length)
    {
        if(verbosityLevel >= 1)
            std::cout << " " << length << std::flush;
        bool cont = true;
        while(cont)
        {
            identifier.clear();
            for(unsigned i = 0; i < length; ++i)
                iters[i].appendChar(identifier);

            if(fp.Parse(identifier, identifier) >= 0)
                return printUTF8TestError("Parsing", iters, length, identifier);

            if(fp.Eval(&value) != 0.0)
                return printUTF8TestError("Evaluation", iters, length,
                                          identifier);

            cont = false;
            const unsigned step = (length == 1) ? 1 : length-1;
            for(unsigned i = 0; i < length; i += step)
                if(iters[i].next())
                {
                    cont = true;
                    break;
                }
        }
    }

    CharIter invalidIters[3] =
        { CharIter(), CharIter(true, false), CharIter() };
    // test 5: inv
    // test 6: inv + normal
    // test 7: normal + inv

    for(unsigned length = 1; length <= 3; ++length)
    {
        if(verbosityLevel >= 1)
            std::cout << " " << 4+length << std::flush;
        unsigned numchars = length < 3 ? length : 2;
        unsigned firstchar = length < 3 ? 0 : 1;
        bool cont = true;
        while(cont)
        {
            identifier.clear();
            identifier += 'a';
            for(unsigned i = 0; i < numchars; ++i)
                invalidIters[firstchar+i].appendChar(identifier);
            identifier += 'a';

            if(fp.Parse(identifier, identifier) < 0)
                return printUTF8TestError2(invalidIters, length);

            cont = false;
            for(unsigned i = 0; i < numchars; ++i)
                if(invalidIters[firstchar+i].next())
                {
                    cont = true;
                    break;
                }
        }
    }

    return true;
}


//=========================================================================
// Test identifier adding and removal
//=========================================================================
bool AddIdentifier(DefaultParser& fp, const std::string& name, int type)
{
    static DefaultParser anotherParser;
    static bool anotherParserInitialized = false;
    if(!anotherParserInitialized)
    {
        anotherParser.Parse("x", "x");
        anotherParserInitialized = true;
    }

    switch(type)
    {
      case 0: return fp.AddConstant(name, 123);
      case 1: return fp.AddUnit(name, 456);
      case 2: return fp.AddFunction(name, userDefFuncSqr<DefaultValue_t>, 1);
      case 3: return fp.AddFunction(name, anotherParser);
    }
    return false;
}

int TestIdentifiers()
{
    DefaultParser fParser;
    std::vector<std::string> identifierNames(26*26, std::string("AA"));

    unsigned nameInd = 0;
    for(int i1 = 0; i1 < 26; ++i1)
    {
        for(int i2 = 0; i2 < 26; ++i2)
        {
            identifierNames.at(nameInd)[0] = char('A' + i1);
            identifierNames[nameInd][1] = char('A' + i2);

            if(!AddIdentifier(fParser, identifierNames[nameInd], (i1+26*i2)%3))
            {
                if(verbosityLevel >= 2)
                    std::cout << "\n - Failed to add identifier '"
                              << identifierNames[nameInd] << "'\n";
                return false;
            }

            ++nameInd;
        }
    }

    std::random_shuffle(identifierNames.begin(), identifierNames.end());

    for(unsigned nameInd = 0; nameInd <= identifierNames.size(); ++nameInd)
    {
        for(unsigned removedInd = 0; removedInd < nameInd; ++removedInd)
        {
            if(!AddIdentifier(fParser, identifierNames[removedInd], 3))
            {
                if(verbosityLevel >= 2)
                    std::cout
                        << "\n - Failure: Identifier '"
                        << identifierNames[removedInd]
                        << "' was still reserved even after removing it.\n";
                return false;
            }
            if(!fParser.RemoveIdentifier(identifierNames[removedInd]))
            {
                if(verbosityLevel >= 2)
                    std::cout << "\n - Failure: Removing the identifier '"
                              << identifierNames[removedInd]
                              << "' after adding it again failed.\n";
                return false;
            }
        }

        for(unsigned existingInd = nameInd;
            existingInd < identifierNames.size(); ++existingInd)
        {
            if(AddIdentifier(fParser, identifierNames[existingInd], 3))
            {
                if(verbosityLevel >= 2)
                    std::cout << "\n - Failure: Trying to add identifier '"
                              << identifierNames[existingInd]
                              << "' for a second time didn't fail.\n";
                return false;
            }
        }

        if(nameInd < identifierNames.size())
        {
            if(!fParser.RemoveIdentifier(identifierNames[nameInd]))
            {
                if(verbosityLevel >= 2)
                    std::cout << "\n - Failure: Trying to remove identifier '"
                              << identifierNames[nameInd] << "' failed.\n";
                return false;
            }
            if(fParser.RemoveIdentifier(identifierNames[nameInd]))
            {
                if(verbosityLevel >= 2)
                    std::cout << "\n - Failure: Trying to remove identifier '"
                              << identifierNames[nameInd]
                              << "' for a second time didn't fail.\n";
                return false;
            }
        }
    }

    return true;
}


//=========================================================================
// Test user-defined functions
//=========================================================================
namespace
{
    template<int VarsAmount>
    DefaultValue_t userFunction(const DefaultValue_t* p)
    {
        DefaultValue_t result = 1.0;
        for(int i = 0; i < VarsAmount; ++i)
            result += (VarsAmount+i/10.0) * p[i];
        return result;
    }

    DefaultValue_t(*userFunctions[])(const DefaultValue_t*) =
    {
        userFunction<0>, userFunction<1>, userFunction<2>, userFunction<3>,
        userFunction<4>, userFunction<5>, userFunction<6>, userFunction<7>,
        userFunction<8>, userFunction<9>, userFunction<10>, userFunction<11>
    };
    const unsigned userFunctionsAmount =
        sizeof(userFunctions) / sizeof(userFunctions[0]);

    DefaultValue_t nestedFunc1(const DefaultValue_t* p)
    {
        return p[0] + 2.0*p[1] + 3.0*p[2];
    }

    DefaultValue_t nestedFunc2(const DefaultValue_t* p)
    {
        const DefaultValue_t params[3] = { -5.0*p[0], -10.0*p[1], -p[0] };
        return p[0] + 4.0*nestedFunc1(params);
    }

    DefaultValue_t nestedFunc3(const DefaultValue_t* p)
    {
        const DefaultValue_t params1[3] = { 2.5*p[0]+2.0, p[2], p[1]/2.5 };
        const DefaultValue_t params2[2] = { p[1] / 1.5 - 1.0, p[0] - 2.5 };
        return nestedFunc1(params1) + nestedFunc2(params2);
    }
}

int testUserDefinedFunctions()
{
    const DefaultValue_t epsilon = testbedEpsilon<DefaultValue_t>();

    DefaultParser nestedParser1, nestedParser2, nestedParser3;
    nestedParser1.Parse("x + 2.0*y + 3.0*z", "x, y, z");
    nestedParser2.AddFunction("nestedFunc1", nestedParser1);
    nestedParser2.Parse("x + 4.0*nestedFunc1(-5.0*x, -10.0*y, -x)", "x,y");
    nestedParser3.AddFunction("nestedFunc1", nestedParser1);
    nestedParser3.AddFunction("nestedFunc2", nestedParser2);
    nestedParser3.Parse("nestedFunc1(2.5*x+2.0, z, y/2.5) + "
                        "nestedFunc2(y/1.5 - 1.0, x - 2.5)", "x,y,z");

    for(int iteration = 0; iteration < 2; ++iteration)
    {
        DefaultValue_t nestedFuncParams[3];
        for(int i = 0; i < 100; ++i)
        {
            nestedFuncParams[0] = -10.0 + 20.0*i/100.0;
            for(int j = 0; j < 100; ++j)
            {
                nestedFuncParams[1] = -10.0 + 20.0*j/100.0;
                for(int k = 0; k < 100; ++k)
                {
                    nestedFuncParams[2] = -10.0 + 20.0*k/100.0;

                    const DefaultValue_t v1 =
                        nestedParser3.Eval(nestedFuncParams);
                    const DefaultValue_t v2 =
                        nestedFunc3(nestedFuncParams);
                    if(std::fabs(v1-v2) > epsilon)
                    {
                        if(verbosityLevel >= 2)
                            std::cout
                                << "\n - Nested function test failed with "
                                << "parameter values ("
                                << nestedFuncParams[0] << ","
                                << nestedFuncParams[1]
                                << ").\nThe library "
                                << (iteration > 0 ? "(optimized) " : "")
                                << "returned " << v1
                                << " instead of " << v2 << "." << std::endl;
                        return false;
                    }
                }
            }
        }
        nestedParser3.Optimize();
    }

    std::string funcNames[userFunctionsAmount];
    std::string userFunctionParserFunctions[userFunctionsAmount];
    std::string userFunctionParserParameters[userFunctionsAmount];
    DefaultParser userFunctionParsers[userFunctionsAmount];
    DefaultValue_t funcParams[userFunctionsAmount];
    DefaultParser parser1, parser2;

    for(unsigned funcInd = 0; funcInd < userFunctionsAmount; ++funcInd)
    {
        std::ostringstream functionString, paramString;

        functionString << '1';
        for(unsigned paramInd = 0; paramInd < funcInd; ++paramInd)
        {
            functionString << "+" << funcInd+paramInd/10.0
                           << "*p" << paramInd;

            if(paramInd > 0) paramString << ',';
            paramString << "p" << paramInd;
        }

        userFunctionParserFunctions[funcInd] = functionString.str();
        userFunctionParserParameters[funcInd] = paramString.str();

        if(userFunctionParsers[funcInd].Parse
           (userFunctionParserFunctions[funcInd],
            userFunctionParserParameters[funcInd]) >= 0)
        {
            if(verbosityLevel >= 2)
                std::cout << "\n - Failed to parse function\n\""
                          << functionString.str() << "\"\nwith parameters: \""
                          << paramString.str() << "\":\n"
                          << userFunctionParsers[funcInd].ErrorMsg() << "\n";
            return false;
        }

        for(unsigned testInd = 0; testInd < 10; ++testInd)
        {
            for(unsigned paramInd = 0; paramInd < testInd; ++paramInd)
                funcParams[paramInd] = testInd+paramInd;
            const DefaultValue_t result = userFunctions[funcInd](funcParams);
            const DefaultValue_t parserResult =
                userFunctionParsers[funcInd].Eval(funcParams);
            if(std::fabs(result - parserResult) > epsilon)
            {
                if(verbosityLevel >= 2)
                {
                    std::cout << "\n - Function\n\"" << functionString.str()
                              << "\"\nwith parameters (";
                    for(unsigned paramInd = 0; paramInd < testInd; ++paramInd)
                    {
                        if(paramInd > 0) std::cout << ',';
                        std::cout << funcParams[paramInd];
                    }
                    std::cout << ")\nreturned " << parserResult
                              << " instead of " << result << "\n";
                }
                return false;
            }
        }
    }

    for(unsigned funcInd = 0; funcInd < userFunctionsAmount; ++funcInd)
    {
        funcNames[funcInd] = "func00";
        funcNames[funcInd][4] = char('0' + funcInd/10);
        funcNames[funcInd][5] = char('0' + funcInd%10);

        if(!parser1.AddFunction(funcNames[funcInd], userFunctions[funcInd],
                                funcInd))
        {
            if(verbosityLevel >= 2)
                std::cout << "\n - Failed to add user-defined function \""
                          << funcNames[funcInd] << "\".\n";
            return false;
        }
        if(!parser2.AddFunction(funcNames[funcInd],
                                userFunctionParsers[funcInd]))
        {
            if(verbosityLevel >= 2)
                std::cout
                    << "\n - Failed to add user-defined function parser \""
                    << funcNames[funcInd] << "\".\n";
            return false;
        }

        std::ostringstream functionString;
        for(unsigned factorInd = 0; factorInd <= funcInd; ++factorInd)
        {
            if(factorInd > 0) functionString << '+';
            functionString << factorInd+1 << "*"
                           << funcNames[factorInd] << '(';
            for(unsigned paramInd = 0; paramInd < factorInd; ++paramInd)
            {
                if(paramInd > 0) functionString << ',';
                const unsigned value = factorInd*funcInd + paramInd;
                functionString << value << "+x";
            }
            functionString << ')';
        }

        if(parser1.Parse(functionString.str(), "x") >= 0)
        {
            if(verbosityLevel >= 2)
                std::cout << "\n - parser1 failed to parse function\n\""
                          << functionString.str() << "\":\n"
                          << parser1.ErrorMsg() << "\n";
            return false;
        }
        if(parser2.Parse(functionString.str(), "x") >= 0)
        {
            if(verbosityLevel >= 2)
                std::cout << "\n - parser2 failed to parse function\n\""
                          << functionString.str() << "\":\n"
                          << parser2.ErrorMsg() << "\n";
            return false;
        }

        for(unsigned optimizeInd = 0; optimizeInd < 4; ++optimizeInd)
        {
            for(unsigned testInd = 0; testInd < 100; ++testInd)
            {
                const DefaultValue_t x = testInd/10.0;
                DefaultValue_t result = 0.0;
                for(unsigned factorInd = 0; factorInd <= funcInd; ++factorInd)
                {
                    for(unsigned paramInd = 0; paramInd < factorInd; ++paramInd)
                    {
                        const unsigned value = factorInd*funcInd + paramInd;
                        funcParams[paramInd] = value+x;
                    }
                    result +=
                        (factorInd+1) * userFunctions[factorInd](funcParams);
                }

                const DefaultValue_t parser1Result = parser1.Eval(&x);
                const DefaultValue_t parser2Result = parser2.Eval(&x);
                const bool parser1Failed =
                    std::fabs(result - parser1Result) > epsilon;
                const bool parser2Failed =
                    std::fabs(result - parser2Result) > epsilon;

                if(parser1Failed || parser2Failed)
                {
                    if(verbosityLevel >= 2)
                    {
                        std::cout << "\n - For function:\n\""
                                  << functionString.str() << "\"";
                        if(optimizeInd > 0)
                            std::cout << "\n(Optimized " << optimizeInd
                                      << (optimizeInd > 1 ?
                                          " times)" : " time)");
                        std::cout << "\nwith x=" << x
                                  << " parser";
                        if(parser1Failed)
                            std::cout << "1 returned " << parser1Result;
                        else
                            std::cout << "2 returned " << parser2Result;
                        std::cout << " instead of " << result << ".\n";

                        if(parser2Failed)
                        {
                            std::cout << "The user-defined functions are:\n";
                            for(unsigned i = 0; i <= funcInd; ++i)
                                std::cout << funcNames[i] << "=\""
                                          << userFunctionParserFunctions[i]
                                          << "\"\n";
                        }
                    }

                    return false;
                }
            }

            parser1.Optimize();
        }
    }

    return true;
}


//=========================================================================
// Multithreaded test
//=========================================================================
#if defined(FP_USE_THREAD_SAFE_EVAL) || \
    defined(FP_USE_THREAD_SAFE_EVAL_WITH_ALLOCA)
#include <boost/thread.hpp>

class TestingThread
{
    int mThreadNumber;
    DefaultParser* mFp;
    volatile static bool mOk;

    static DefaultValue_t function(const DefaultValue_t* vars)
    {
        const DefaultValue_t x = vars[0], y = vars[1];
        return sin(sqrt(x*x+y*y)) + 2*cos(2*sqrt(2*x*x+2*y*y));
    }

 public:
    TestingThread(int n, DefaultParser* fp):
        mThreadNumber(n), mFp(fp)
    {}

    static bool ok() { return mOk; }

    void operator()()
    {
        const DefaultValue_t epsilon = testbedEpsilon<DefaultValue_t>();
        DefaultValue_t vars[2];
        for(vars[0] = -10.0; vars[0] <= 10.0; vars[0] += 0.02)
        {
            for(vars[1] = -10.0; vars[1] <= 10.0; vars[1] += 0.02)
            {
                if(!mOk) return;

                const DefaultValue_t v1 = function(vars);
                const DefaultValue_t v2 = mFp->Eval(vars);
                /*
                const double scale = pow(10.0, floor(log10(fabs(v1))));
                const double sv1 = fabs(v1) < testbedEpsilon<double>() ? 0 : v1/scale;
                const double sv2 = fabs(v2) < testbedEpsilon<double>() ? 0 : v2/scale;
                const double diff = fabs(sv2-sv1);
                */
                const DefaultValue_t diff =
                    std::fabs(v1) < epsilon ?
                    (std::fabs(v2) < epsilon ?
                     std::fabs(v1 - v2) :
                     std::fabs((v1 - v2) / v2)) :
                    std::fabs((v1 - v2) / v1);

                if(std::fabs(diff) > epsilon)
                {
                    mOk = false;
                    if(verbosityLevel >= 2)
                        std::cout << "\n - Thread " << mThreadNumber
                                  << " failed ([" << vars[0] << "," << vars[1]
                                  << "] -> " << v2 << " vs. " << v1 << ")"
                                  << std::endl;
                    return;
                }
            }
        }
    }
};

volatile bool TestingThread::mOk = true;

int testMultithreadedEvaluation()
{
    DefaultParser fp;
    fp.Parse("sin(sqrt(x*x+y*y)) + 2*cos(2*sqrt(2*x*x+2*y*y))", "x,y");

    if(verbosityLevel >= 1)
        std::cout << " 1" << std::flush;
    boost::thread t1(TestingThread(1, &fp)), t2(TestingThread(2, &fp));
    t1.join();
    t2.join();
    if(!TestingThread::ok()) return false;

    if(verbosityLevel >= 1)
        std::cout << " 2" << std::flush;
    boost::thread
        t3(TestingThread(3, &fp)), t4(TestingThread(4, &fp)),
        t5(TestingThread(5, &fp)), t6(TestingThread(6, &fp));
    t3.join();
    t4.join();
    t5.join();
    t6.join();
    if(!TestingThread::ok()) return false;

    if(verbosityLevel >= 1)
        std::cout << " 3" << std::flush;
    fp.Optimize();
    boost::thread
        t7(TestingThread(7, &fp)), t8(TestingThread(8, &fp)),
        t9(TestingThread(9, &fp));
    t7.join();
    t8.join();
    t9.join();
    if(!TestingThread::ok()) return false;

    return true;
}

#else

int testMultithreadedEvaluation()
{
    return -1;
}

#endif

//=========================================================================
// Test variable deduction
//=========================================================================
template<typename Value_t>
struct TestType
{
    unsigned paramAmount;
    Value_t paramMin, paramMax, paramStep;
    bool useDegrees;

    Value_t (*funcPtr)(const Value_t*);
#ifdef FP_TEST_WANT_DOUBLE_TYPE
    double (*doubleFuncPtr)(const double*);
#endif
#ifdef FP_TEST_WANT_LONG_INT_TYPE
    long (*longFuncPtr)(const long*);
#endif

    const char* paramString;
    const char* testName;
    const char* funcString;
};


template<typename Value_t>
bool checkVarString(const char* idString,
                    FunctionParserBase<Value_t> & fp,
                    const TestType<Value_t>& testData,
                    int errorIndex,
                    int variablesAmount, const std::string& variablesString,
                    std::ostream& briefErrorMessages)
{
    const bool stringsMatch =
        (variablesString == testData.paramString);
    if(errorIndex >= 0 ||
       variablesAmount != int(testData.paramAmount) ||
       !stringsMatch)
    {
        if(verbosityLevel >= 2)
        {
            std::cout << "\n" << idString
                      << " ParseAndDeduceVariables() failed with function:\n\""
                      << testData.funcString << "\"\n";
            if(errorIndex >= 0)
                std::cout << "Error index: " << errorIndex
                          << ": " << fp.ErrorMsg() << std::endl;
            else if(!stringsMatch)
                std::cout << "Deduced var string was \"" << variablesString
                          << "\" instead of \""
                          << testData.paramString
                          << "\"." << std::endl;
            else
                std::cout << "Deduced variables amount was "
                          << variablesAmount << " instead of "
                          << testData.paramAmount << "."
                          << std::endl;
        }
        else
        {
            briefErrorMessages << "- " << testData.testName
                               << ": Failed ParseAndDeduceVariables().\n";
        }
        return false;
    }
    return true;
}

template<typename Value_t>
bool testVariableDeduction(FunctionParserBase<Value_t>& fp,
                           const TestType<Value_t>& testData,
                           std::ostream& briefErrorMessages)
{
    static std::string variablesString;
    static std::vector<std::string> variables;

    if(verbosityLevel >= 3)
        std::cout << "(Variable deduction)" << std::flush;

    int variablesAmount = -1;
    int retval = fp.ParseAndDeduceVariables
        (testData.funcString,
         &variablesAmount, testData.useDegrees);
    if(retval >= 0 || variablesAmount !=
       int(testData.paramAmount))
    {
        if(verbosityLevel >= 2)
        {
            std::cout <<
                "\nFirst ParseAndDeduceVariables() failed with function:\n\""
                      << testData.funcString << "\"\n";
            if(retval >= 0)
                std::cout << "Error index: " << retval
                          << ": " << fp.ErrorMsg() << std::endl;
            else
                std::cout << "Deduced variables amount was "
                          << variablesAmount << " instead of "
                          << testData.paramAmount << "."
                          << std::endl;
        }
        else
        {
            briefErrorMessages << "- " << testData.testName
                               << ": Failed ParseAndDeduceVariables().\n";
        }
        return false;
    }

    variablesAmount = -1;
    retval = fp.ParseAndDeduceVariables
        (testData.funcString,
         variablesString,
         &variablesAmount,
         testData.useDegrees);
    if(!checkVarString("Second", fp, testData, retval, variablesAmount,
                       variablesString, briefErrorMessages))
        return false;

    retval = fp.ParseAndDeduceVariables(testData.funcString,
                                        variables,
                                        testData.useDegrees);
    variablesAmount = int(variables.size());
    variablesString.clear();
    for(unsigned i = 0; i < variables.size(); ++i)
    {
        if(i > 0) variablesString += ',';
        variablesString += variables[i];
    }
    return checkVarString("Third", fp, testData, retval, variablesAmount,
                          variablesString, briefErrorMessages);
}


//=========================================================================
// Main test function
//=========================================================================
namespace
{
    template<typename Value_t>
    struct RegressionTests
    {
        static const TestType<Value_t> Tests[];
    };
    template<typename Value_t>
    const TestType<Value_t> RegressionTests<Value_t>::Tests[] = { TestType<Value_t>() };

#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
    inline MpfrFloat makeMF(const char* str)
    {
        MpfrFloat f;
        f.parseValue(str);
        return f;
    }
#endif

    /* These functions in fparser produce bool values. However,
     * the testing functions require that they produce Value_t's. */
    #define BoolProxy(Fname) \
    template<typename Value_t> \
    Value_t tb_##Fname(const Value_t& a, const Value_t& b) \
        { return Value_t(FUNCTIONPARSERTYPES::Fname(a,b)); }

    BoolProxy(fp_less)
    BoolProxy(fp_lessOrEq)
    BoolProxy(fp_greater)
    BoolProxy(fp_greaterOrEq)
    BoolProxy(fp_equal)
    BoolProxy(fp_nequal)

    template<typename Value_t>
    Value_t fp_truth(const Value_t& a)
    { return Value_t(FUNCTIONPARSERTYPES::fp_truth(a)); }

// Maybe these should be used in the test files instead...
#define fp_less tb_fp_less
#define fp_lessOrEq tb_fp_lessOrEq
#define fp_greater tb_fp_greater
#define fp_greaterOrEq tb_fp_greaterOrEq
#define fp_equal tb_fp_equal
#define fp_nequal tb_fp_nequal

#include "testbed_tests.inc"

#undef fp_less
#undef fp_lessOrEq
#undef fp_greater
#undef fp_greaterOrEq
#undef fp_equal
#undef fp_nequal
}

namespace
{
    template<typename Value_t>
    void testAgainstDouble(Value_t*, Value_t, const TestType<Value_t>&,
                           std::ostream&) {}

#if defined(FP_TEST_WANT_MPFR_FLOAT_TYPE) && defined(FP_TEST_WANT_DOUBLE_TYPE)
    void testAgainstDouble(MpfrFloat* vars, MpfrFloat parserValue,
                           const TestType<MpfrFloat>& testData,
                           std::ostream& error)
    {
        if(!testData.doubleFuncPtr) return;

        double doubleVars[10];
        for(unsigned i = 0; i < 10; ++i) doubleVars[i] = vars[i].toDouble();

        const double Eps = testbedEpsilon<double>();

        const double v1 = testData.doubleFuncPtr(doubleVars);
        const double v2 = parserValue.toDouble();

        /*
        using namespace FUNCTIONPARSERTYPES;
        const double scale = fp_pow(10.0, fp_floor(fp_log10(fp_abs(v1))));
        const double sv1 = fp_abs(v1) < Eps ? 0 : v1/scale;
        const double sv2 = fp_abs(v2) < Eps ? 0 : v2/scale;
        const double diff = fp_abs(sv2-sv1);
        */
        const double diff =
            std::fabs(v1) < Eps ?
            (std::fabs(v2) < Eps ? std::fabs(v1 - v2) :
             std::fabs((v1 - v2) / v2)) :
            std::fabs((v1 - v2) / v1);

        if(diff > Eps)
        {
            using namespace FUNCTIONPARSERTYPES;
            if(verbosityLevel >= 2)
                error << std::setprecision(16) << v2 << " instead of "
                      << std::setprecision(16) << v1
                      << "\n(Difference: "
                      << std::setprecision(16) << v2-v1
                      << ", epsilon: "
                      << std::setprecision(16) << Eps
                      << "; scaled diff "
                      << std::setprecision(16) << diff
                      << ")\nwhen tested against the double function.";
            else
                error << std::setprecision(16) << v2 << " vs "
                      << std::setprecision(16) << v1
                      << " (diff: "
                      << std::setprecision(16) << v2-v1
                      << ", sdiff "
                      << std::setprecision(16) << diff
                      << ") against double.";
        }
    }
#endif

    template<typename Value_t>
    void testAgainstLongInt(Value_t*, Value_t, const TestType<Value_t>&,
                            std::ostream&) {}

#if defined(FP_TEST_WANT_GMP_INT_TYPE) && defined(FP_TEST_WANT_LONG_INT_TYPE)
    void testAgainstLongInt(GmpInt* vars, GmpInt parserValue,
                            const TestType<GmpInt>& testData,
                            std::ostream& error)
    {
        if(!testData.longFuncPtr) return;

        long longVars[10];
        for(unsigned i = 0; i < 10; ++i) longVars[i] = vars[i].toInt();

        const long longValue = testData.longFuncPtr(longVars);
        if(longValue != parserValue)
        {
            if(verbosityLevel >= 2)
                error << parserValue << " instead of " << longValue
                      << "\nwhen tested against the long int function.";
            else
                error << parserValue << " vs " << longValue
                      << " against long.";
        }
    }
#endif
}

template<typename Value_t>
bool runRegressionTest(FunctionParserBase<Value_t>& fp,
                       const TestType<Value_t>& testData,
                       const std::string& valueType,
                       const Value_t Eps,
                       std::ostream& briefErrorMessages)
{
    Value_t vars[10];
    Value_t fp_vars[10];

    for(unsigned i = 0; i < testData.paramAmount; ++i)
        vars[i] = testData.paramMin;

    while(true)
    {
        unsigned paramInd = 0;
        while(paramInd < testData.paramAmount)
        {
            using namespace FUNCTIONPARSERTYPES;
            /* ^ Import a possible <= operator from that
             *   namespace for this particular comparison only */
            vars[paramInd] += testData.paramStep;
            if(vars[paramInd] <= testData.paramMax) break;
            vars[paramInd++] = testData.paramMin;
        }

        if(paramInd == testData.paramAmount) break;

        for(unsigned i = 0; i < testData.paramAmount; ++i)
            fp_vars[i] = vars[i];

        if(verbosityLevel >= 4)
        {
            std::cout << "Trying (";
            for(unsigned ind = 0; ind < testData.paramAmount; ++ind)
                std::cout << (ind>0 ? ", " : "") << vars[ind];
            std::cout << ")\n" << std::flush;
        }
        const Value_t v1 = testData.funcPtr(vars);
        if(true) /*test Eval() */
        {
            const Value_t v2 = fp.Eval(fp_vars);

            std::ostringstream error;

            if(fp.EvalError() > 0)
            {
                error << "EvalError " << fp.EvalError() << " ("
                      << getEvalErrorName(fp.EvalError()) << ")";
            }
            else if(FUNCTIONPARSERTYPES::IsIntType<Value_t>::result)
            {
                if(v1 != v2)
                {
                    using namespace FUNCTIONPARSERTYPES;
                    if(verbosityLevel >= 2)
                        error << v2 << " instead of " << v1;
                    else
                        error << v2 << " vs " << v1;
                }
                else
                    testAgainstLongInt(vars, v2, testData, error);
            }
            else
            {
                using namespace FUNCTIONPARSERTYPES;
                /*
                const Value_t scale =
                    fp_pow(Value_t(10.0), fp_floor(fp_log10(fp_abs(v1))));
                const Value_t sv1 = fp_abs(v1) < Eps ? 0 : v1/scale;
                const Value_t sv2 = fp_abs(v2) < Eps ? 0 : v2/scale;
                const Value_t diff = fp_abs(sv2-sv1);
                */
                const Value_t diff =
                    fp_abs(v1) < Eps ?
                    (fp_abs(v2) < Eps ? fp_abs(v1 - v2) :
                     fp_abs((v1 - v2) / v2)) :
                    fp_abs((v1 - v2) / v1);
                /*
                const Value_t diff =
                    v1 == Value_t(0) ?
                    (v2 == Value_t(0) ? Value_t(0) :
                     fp_abs((v1 - v2) / v2)) :
                    fp_abs((v1 - v2) / v1);
                */

                if(diff > Eps)
                {
                    using namespace FUNCTIONPARSERTYPES;
                    if(verbosityLevel >= 2)
                        error << std::setprecision(28) << v2 << " instead of "
                              << std::setprecision(28) << v1
                              << "\n(Difference: "
                              << std::setprecision(28) << v2-v1
                              << ", epsilon: "
                              << std::setprecision(28) << Eps
                              << "; scaled diff "
                              << std::setprecision(28) << diff
                              << ")";
                    else
                        error << std::setprecision(16) << v2 << " vs "
                              << std::setprecision(16) << v1
                              << " (diff: "
                              << std::setprecision(16) << v2-v1
                              << ", sdiff "
                              << std::setprecision(16) << diff
                              << ")";
                }
                else
                    testAgainstDouble(vars, v2, testData, error);
            }

            if(!error.str().empty())
            {
                if(verbosityLevel == 2)
                    std::cout << "\n****************************\nTest "
                              << testData.testName
                              << ", function:\n\"" << testData.funcString
                              << "\"\n(" << valueType << ")";

                if(verbosityLevel >= 2)
                {
                    using namespace FUNCTIONPARSERTYPES;
                    // ^ For output of complex numbers according to fparser practice

                    std::cout << std::endl << "Error: For (" << std::setprecision(20);
                    for(unsigned ind = 0; ind < testData.paramAmount; ++ind)
                        std::cout << (ind>0 ? ", " : "") << vars[ind];
                    std::cout << ")\nthe library returned " << error.str()
                              << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
                    fp.PrintByteCode(std::cout);
#endif
                }
                else
                {
                    using namespace FUNCTIONPARSERTYPES;

                    briefErrorMessages << "- " << testData.testName << " (";
                    for(unsigned ind = 0; ind < testData.paramAmount; ++ind)
                        briefErrorMessages << (ind>0 ? "," : "") << vars[ind];
                    briefErrorMessages << "): " << error.str() << "\n";
                }
                return false;
            }
        } /* test Eval() */
    }
    return true;
}

static bool WildMatch(const char *pattern, const char *what)
{
    for(; *what || *pattern; ++what, ++pattern)
        if(*pattern == '*')
        {
            while(*++pattern == '*') {}
            for(; *what; ++what)
                if(WildMatch(pattern, what))
                    return true;
            return !*pattern;
        }
        else if(*pattern != '?' && *pattern != *what)
            return false;
    return true;
}
static bool WildMatch_Dirmask(const char *pattern, const char *what)
{
    std::string testmask = pattern;
    if(testmask.find('/') == testmask.npos) testmask = "*/" + testmask;
    return WildMatch(testmask.c_str(), what);
}
bool IsSelectedTest(const char* testName)
{
    for(std::size_t a=0; a<selectedRegressionTests.size(); ++a)
        if(WildMatch_Dirmask(selectedRegressionTests[a], testName))
            return true;
    return false;
}
/* Asciibetical comparator, with in-string integer values sorted naturally */
bool natcomp(const std::string& a, const std::string& b)
{
    std::size_t ap=0, bp=0;
    while(ap < a.size() && bp < b.size())
    {
        if(a[ap] >= '0' && a[ap] <= '9'
        && b[bp] >= '0' && b[bp] <= '9')
        {
            unsigned long aval = (a[ap++] - '0');
            unsigned long bval = (b[bp++] - '0');
            while(ap < a.size() && a[ap] >= '0' && a[ap] <= '9')
                aval = aval*10ul + (a[ap++] - '0');
            while(bp < b.size() && b[bp] >= '0' && b[bp] <= '9')
                bval = bval*10ul + (b[bp++] - '0');
            if(aval != bval)
                return aval < bval;
        }
        else
        {
            if(a[ap] != b[ap]) return a[ap] < b[ap];
            ++ap; ++bp;
        }
    }
    return (bp < b.size() && ap >= a.size());
}


template<typename Value_t>
bool runRegressionTests(const std::string& valueType)
{
    // Setup the function parser for testing
    // -------------------------------------
    FunctionParserBase<Value_t> fp;

    if(verbosityLevel >= 1)
    {
        setAnsiBold();
        std::cout << "==================== Parser type \"" << valueType
                  << "\" ====================" << std::endl;
        resetAnsiColor();
    }

    bool ret = fp.AddConstant("pi",
                              FUNCTIONPARSERTYPES::fp_const_pi<Value_t>());
    ret = ret && fp.AddConstant("naturalnumber",
                                FUNCTIONPARSERTYPES::fp_const_e<Value_t>());
    ret = ret && fp.AddConstant("logtwo",
                                FUNCTIONPARSERTYPES::fp_const_log2<Value_t>());
    ret = ret && fp.AddConstant("logten",
                                FUNCTIONPARSERTYPES::fp_const_log10<Value_t>());
    ret = ret && fp.AddConstant("CONST", Value_t(CONST));
    if(!ret)
    {
        std::cout << "Ooops! AddConstant() didn't work" << std::endl;
        return false;
    }

    ret = fp.AddUnit("doubled", 2);
    ret = ret && fp.AddUnit("tripled", 3);
    if(!ret)
    {
        std::cout << "Ooops! AddUnit() didn't work" << std::endl;
        return false;
    }

    ret = fp.AddFunctionWrapper
        ("sub", UserDefFuncWrapper<Value_t>(userDefFuncSub<Value_t>), 2);
    ret = ret && fp.AddFunction("sqr", userDefFuncSqr<Value_t>, 1);
    ret = ret && fp.AddFunction("value", userDefFuncValue<Value_t>, 0);
    if(!ret)
    {
        std::cout << "Ooops! AddFunction(ptr) didn't work" << std::endl;
        return false;
    }

    UserDefFuncWrapper<Value_t>* wrapper =
        dynamic_cast<UserDefFuncWrapper<Value_t>*>
        (fp.GetFunctionWrapper("sub"));
    if(!wrapper || wrapper->counter() != 0)
    {
        std::cout << "Ooops! AddFunctionWrapper() didn't work" << std::endl;
        return false;
    }

    FunctionParserBase<Value_t> SqrFun, SubFun, ValueFun;
    if(verbosityLevel >= 3) std::cout << "Parsing SqrFun... ";
    SqrFun.Parse("x*x", "x");
    if(verbosityLevel >= 3) std::cout << "\nParsing SubFun... ";
    SubFun.Parse("x-y", "x,y");
    if(verbosityLevel >= 3) std::cout << "\nParsing ValueFun... ";
    ValueFun.Parse("5", "");
    if(verbosityLevel >= 3) std::cout << std::endl;

    ret = fp.AddFunction("psqr", SqrFun);
    ret = ret && fp.AddFunction("psub", SubFun);
    ret = ret && fp.AddFunction("pvalue", ValueFun);
    if(!ret)
    {
        std::cout << "Ooops! AddFunction(parser) didn't work" << std::endl;
        return false;
    }

    // Test repeated constant addition
    // -------------------------------
   {using namespace FUNCTIONPARSERTYPES; // For a possible custom < operator
    for(Value_t value = 0; value < Value_t(20); value += 1)
    {
        if(!fp.AddConstant("TestConstant", value))
        {
            std::cout << "Ooops2! AddConstant() didn't work" << std::endl;
            return false;
        }

        fp.Parse("TestConstant", "");
        if(fp.Eval(0) != value)
        {
            if(value == Value_t(0)) std::cout << "Usage of 'TestConstant' failed\n";
            else std::cout << "Changing the value of 'TestConstant' failed\n";
            return false;
        }
    }}

    bool allRegressionTestsOk = true;
    std::ostringstream briefErrorMessages;

    std::string prev_test_prefix;
    const unsigned maxtests = ~0U; // unknown
    /*    sizeof(RegressionTests<Value_t>::Tests)
      / sizeof(RegressionTests<Value_t>::Tests[0]); */
    for(unsigned i = 0; i < maxtests; ++i)
    {
        const TestType<Value_t>& testData = RegressionTests<Value_t>::Tests[i];
        if(!testData.testName) break;

        if(!IsSelectedTest(testData.testName)) continue;

        const int retval =
            fp.Parse(testData.funcString, testData.paramString,
                     testData.useDegrees);
        if(retval >= 0)
        {
            std::cout <<
                "With FunctionParserBase<" << valueType << ">"
                "\nin \"" << testData.funcString <<
                "\" (\"" << testData.paramString <<
                "\"), col " << retval <<
                ":\n" << fp.ErrorMsg() << std::endl;
            return false;
        }

        //fp.PrintByteCode(std::cout);
        if(verbosityLevel >= 3)
        {
            std::cout
                << /*std::right <<*/ std::setw(2)
                << testData.testName << ": \""
                << testData.funcString << "\" ("
                << FUNCTIONPARSERTYPES::fp_pow
                ((testData.paramMax - testData.paramMin) /
                 testData.paramStep,
                 Value_t( (int) testData.paramAmount))
                << " param. combinations): " << std::flush;
        }
        else if(verbosityLevel == 2)
        {
            const char* tn = testData.testName;
            const char* p = std::strrchr(tn, '/');
            if(!p)
                { prev_test_prefix = ""; std::cout << tn; }
            else
            {
                std::string path_prefix(tn, p-tn);
                if(path_prefix == prev_test_prefix)
                    std::cout << (p+1);
                else
                    { if(!prev_test_prefix.empty()) std::cout << std::endl;
                      std::cout << tn;
                      prev_test_prefix = path_prefix; }
            }
            std::cout << std::flush << " ";
        }

        bool thisTestOk =
            runRegressionTest(fp, testData, valueType + ", not optimized",
                              testbedEpsilon<Value_t>(), briefErrorMessages);

        if(thisTestOk)
        {
            if(verbosityLevel >= 3) std::cout << "Ok." << std::endl;

            fp.Optimize();
            //fp.PrintByteCode(std::cout);

            if(verbosityLevel >= 3)
                std::cout << "    Optimized: " << std::flush;

            thisTestOk =
                runRegressionTest(fp, testData,
                                  valueType + ", after optimization",
                                  testbedEpsilon<Value_t>(), briefErrorMessages);
            if(thisTestOk)
            {
                if(verbosityLevel >= 3)
                    std::cout << "(Calling Optimize() several times) "
                              << std::flush;

                for(int j = 0; j < 20; ++j)
                    fp.Optimize();

                /* Sometimes literals drift when the optimizer is run many
                   times, which can become significant with floats. The only
                   purpose to test running the optimizer several times is just
                   to see that it doesn't break. It's not intended to be called
                   several times normally. Hence just skip testing with floats,
                   because the drift just causes differences larger than
                   epsilon...
                */
                if(valueType != "float")
                {
                    thisTestOk =
                        runRegressionTest
                        (fp, testData,
                         valueType + ", after several optimization runs",
                         testbedEpsilon<Value_t>(), briefErrorMessages);
                }

                if(thisTestOk)
                {
                    thisTestOk =
                        testVariableDeduction(fp, testData, briefErrorMessages);

                    if(thisTestOk && verbosityLevel >= 3)
                        std::cout << "Ok." << std::endl;
                }
            }
        } // if(thisTestOk)

        if(!thisTestOk) allRegressionTestsOk = false;

        if(verbosityLevel == 1)
            std::cout << (thisTestOk ? "." : "!") << std::flush;
    } // for(unsigned i = 0; i < maxtests; ++i)

    if(allRegressionTestsOk)
    {
        if(verbosityLevel == 1 || verbosityLevel == 2)
            std::cout << std::endl;
    }
    else if(verbosityLevel <= 1)
    {
        if(verbosityLevel == 1) std::cout << "\n";
        std::cout << briefErrorMessages.str() << std::flush;
    }

    if(verbosityLevel >= 2)
        std::cout << "User-defined function \"sub\" was called "
                  << (dynamic_cast<UserDefFuncWrapper<Value_t>*>
                      (fp.GetFunctionWrapper("sub"))->counter())
                  << " times." << std::endl;

    return allRegressionTestsOk;
}

//=========================================================================
// Optimizer tests
//=========================================================================
namespace OptimizerTests
{
    // --------------------------------------------------------------------
    // Optimizer test 1
    // --------------------------------------------------------------------
    /* Tests functions of the form "A(x^B)^C op D(x^E)^F", where:
       - A,D = {sin,cos,tan,sinh,cosh,tanh,exp}
       - B,E = {1,2}
       - C,F = {-2,-1,0,1,2}
       - op = +, *
    */
    struct MathFuncData
    {
        DefaultValue_t (*mathFunc)(DefaultValue_t d);
        const char* funcName;
    };

    const MathFuncData mathFuncs[] =
    {
        { &std::sin, "sin" }, { &std::cos, "cos" }, { &std::tan, "tan" },
        { &std::sinh, "sinh" }, { &std::cosh, "cosh" }, { &std::tanh, "tanh" },
        { &std::exp, "exp" }
    };
    const unsigned mathFuncsAmount = sizeof(mathFuncs) / sizeof(mathFuncs[0]);

    unsigned mathFuncIndexA, mathFuncIndexD;
    int exponent_B, exponent_E;
    int exponent_C, exponent_F;
    unsigned operatorIndex;

    DefaultValue_t evaluateFunction(const DefaultValue_t* params)
    {
        const DefaultValue_t x = params[0];
        const MathFuncData& data1 = mathFuncs[mathFuncIndexA];
        const MathFuncData& data2 = mathFuncs[mathFuncIndexD];

        const DefaultValue_t angle1 =
            (exponent_B == 1 ? x : std::pow(x, exponent_B));
        const DefaultValue_t angle2 =
            (exponent_E == 1 ? x : std::pow(x, exponent_E));
        const DefaultValue_t part1 =
            std::pow(data1.mathFunc(angle1), exponent_C);
        const DefaultValue_t part2 =
            std::pow(data2.mathFunc(angle2), exponent_F);

        if(operatorIndex == 0) return part1 + part2;
        return part1 * part2;
    }

    bool runCurrentTrigCombinationTest()
    {
        const MathFuncData& data1 = mathFuncs[mathFuncIndexA];
        const MathFuncData& data2 = mathFuncs[mathFuncIndexD];

        std::ostringstream os;
        os << data1.funcName << "(x^" << exponent_B << ")^" << exponent_C;
        if(operatorIndex == 0) os << "+";
        else os << "*";
        os << data2.funcName << "(x^" << exponent_E << ")^" << exponent_F;
        const std::string funcString = os.str();

        const TestType<DefaultValue_t> testData =
        {
            1, -4.0, 4.0, 0.49, false, &evaluateFunction, DBL_ONLY(0)LNG_ONLY(0)
            "x", "'trig. combo optimizer test'", funcString.c_str()
        };

        DefaultParser parser;
        if(parser.Parse(funcString, "x") >= 0)
        {
            std::cout << "Oops: Function \"" << funcString
                      << "\" was malformed." << std::endl;
            return false;
        }

        std::ostringstream briefErrorMessages;

        if(!runRegressionTest(parser, testData, "DefaultValue_t",
                              testbedEpsilon<DefaultValue_t>(), briefErrorMessages))
        {
            if(verbosityLevel == 1)
                std::cout << "\n - " << briefErrorMessages.str() << std::flush;
            return false;
        }
        return true;
    }

    bool runTrigCombinationTests()
    {
        unsigned testCounter = 0;

        for(mathFuncIndexA = 0;
            mathFuncIndexA < mathFuncsAmount;
            ++mathFuncIndexA)
        {
            for(mathFuncIndexD = 0;
                mathFuncIndexD < mathFuncsAmount;
                ++mathFuncIndexD)
            {
                for(exponent_B = 1; exponent_B <= 2; ++exponent_B)
                {
                    for(exponent_E = 1; exponent_E <= 2; ++exponent_E)
                    {
                        for(exponent_C = -2; exponent_C <= 2; ++exponent_C)
                        {
                            for(exponent_F = -2; exponent_F <= 2; ++exponent_F)
                            {
                                for(operatorIndex = 0;
                                    operatorIndex < 2;
                                    ++operatorIndex)
                                {
                                    ++testCounter;
                                    if(!runCurrentTrigCombinationTest())
                                        return false;
                                }
                            }
                        }
                    }
                }
            }
        }

        if(verbosityLevel >= 1)
            std::cout << " (" << testCounter << ")" << std::flush;
        return true;
    }


    // --------------------------------------------------------------------
    // Optimizer test 2
    // --------------------------------------------------------------------
    /* Tests functions of the form "A op B [op C]", where
       A, B, C = { var, !var, !!var, var comp value }
       var = A -> x, B -> y, C -> z
       comp = { <, <=, =, !=, >, >= }
       value = { -1, -.5, 0, .5, 1 }
       op = { and, or, not and, not or }
    */
    // opIndex = 0-32 for doubles, 0-20 for ints
    const char* getOperandString(char varName, unsigned opIndex)
    {
        if(opIndex <= 2)
        {
            static char operand[] = "!!x";
            operand[2] = varName;
            return operand + (2 - opIndex);
        }

        opIndex -= 3;
        const unsigned compIndex = opIndex % 6, valueIndex = opIndex / 6;
        assert(valueIndex <= 4);

        static const char* const comp[] =
            { "< ", "<=", "= ", "!=", "> ", ">=" };
        static const char* const value[] =
            { "-1 ", "0  ", "1  ", ".5 ", "-.5" };
        static char expression[] = "(x<=-.5)";

        expression[1] = varName;
        expression[2] = comp[compIndex][0];
        expression[3] = comp[compIndex][1];
        expression[4] = value[valueIndex][0];
        expression[5] = value[valueIndex][1];
        expression[6] = value[valueIndex][2];
        return expression;
    }

    template<typename Value_t>
    Value_t getOperandValue(Value_t varValue, unsigned opIndex)
    {
        using namespace FUNCTIONPARSERTYPES;

        switch(opIndex)
        {
          case 0: return varValue;
          case 1: return fp_not(varValue);
          case 2: return fp_notNot(varValue);
        }

        opIndex -= 3;
        const unsigned compIndex = opIndex % 6, valueIndex = opIndex / 6;

        static const Value_t value[] =
            { -1, 0, 1, Value_t(.5), Value_t(-.5) };

        switch(compIndex)
        {
          case 0: return fp_less(varValue, value[valueIndex]);
          case 1: return fp_lessOrEq(varValue, value[valueIndex]);
          case 2: return fp_equal(varValue, value[valueIndex]);
          case 3: return fp_nequal(varValue, value[valueIndex]);
          case 4: return fp_greater(varValue, value[valueIndex]);
          case 5: return fp_greaterOrEq(varValue, value[valueIndex]);
        }
        assert(false);
        return 0;
    }

    // exprIndex = 0-3
    std::string getBooleanExpression(const std::string& operand1,
                                     const std::string& operand2,
                                     unsigned exprIndex)
    {
        switch(exprIndex)
        {
          case 0: return operand1 + "&" + operand2;
          case 1: return operand1 + "|" + operand2;
          case 2: return "!(" + operand1 + "&" + operand2 + ")";
          case 3: return "!(" + operand1 + "|" + operand2 + ")";
        }
        assert(false);
        return "";
    }

    template<typename Value_t>
    Value_t getBooleanValue(Value_t operand1Value, Value_t operand2Value,
                            unsigned exprIndex)
    {
        using namespace FUNCTIONPARSERTYPES;
        switch(exprIndex)
        {
          case 0: return fp_and(operand1Value, operand2Value);
          case 1: return fp_or(operand1Value, operand2Value);
          case 2: return fp_not(fp_and(operand1Value, operand2Value));
          case 3: return fp_not(fp_or(operand1Value, operand2Value));
        }
        assert(false);
        return 0;
    }

    bool updateIndices(unsigned* operandIndices, unsigned* exprIndices,
                       unsigned operands, const unsigned maxOperandIndex)
    {
        for(unsigned oi = 0; oi < operands; ++oi)
        {
            if(++operandIndices[oi] <= maxOperandIndex)
                return true;
            operandIndices[oi] = 0;
        }
        for(unsigned ei = 0; ei < operands-1; ++ei)
        {
            if(++exprIndices[ei] <= 3) return true;
            exprIndices[ei] = 0;
        }
        return false;
    }

    template<typename Value_t, unsigned varsAmount>
    bool runBooleanComparisonEvaluation(const unsigned* operandIndices,
                                        const unsigned* exprIndices,
                                        const unsigned operands,
                                        FunctionParserBase<Value_t>& fparser,
                                        const std::string& functionString,
                                        bool optimized)
    {
        const bool isIntegral = FUNCTIONPARSERTYPES::IsIntType<Value_t>::result;
        const unsigned varValuesToTest = isIntegral ? 3 : 4;

        static const Value_t values[] =
            { -1, 0, 1, Value_t(0.5), Value_t(-0.5) };
        static unsigned valueIndices[varsAmount];
        static Value_t variableValues[varsAmount];

        for(unsigned i = 0; i < operands; ++i) valueIndices[i] = 0;

        bool stop = false;
        while(!stop)
        {
            for(unsigned i = 0; i < operands; ++i)
                variableValues[i] = values[valueIndices[i]];

            const Value_t parserValue = fparser.Eval(variableValues);

            Value_t correctValue = getOperandValue(variableValues[0],
                                                   operandIndices[0]);

            for(unsigned i = 1; i < operands; ++i)
                correctValue =
                    getBooleanValue(correctValue,
                                    getOperandValue(variableValues[i],
                                                    operandIndices[i]),
                                    exprIndices[i-1]);

            if(FUNCTIONPARSERTYPES::fp_nequal(parserValue, correctValue))
            {
                const bool isIntegral =
                    FUNCTIONPARSERTYPES::IsIntType<Value_t>::result;
                if(verbosityLevel >= 2)
                {
                    using namespace FUNCTIONPARSERTYPES;
                    std::cout
                        << "\nFor function \"" << functionString
                        << "\" (";
                    for(unsigned i = 0; i < operands; ++i)
                        std::cout << (i>0 ? "," : "")
                                  << variableValues[i];
                    std::cout
                        << "): Parser<"
                        << (isIntegral ? "long" : "double")
                        << ">"
                        << (optimized ? " (optimized)" : "")
                        << "\nreturned " << parserValue
                        << " instead of " << correctValue
                        << std::endl;
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
                    fparser.PrintByteCode(std::cout);
#endif
                }
                else if(verbosityLevel >= 1)
                {
                    using namespace FUNCTIONPARSERTYPES;
                    std::cout << "<" << (isIntegral ? "long" : "double");
                    std::cout << (optimized ? ",optimized" : "");
                    std::cout << ">\"" << functionString
                              << "\"(";
                    for(unsigned i = 0; i < operands; ++i)
                        std::cout << (i>0 ? "," : "")
                                  << variableValues[i];
                    std::cout << "): ";
                    std::cout << parserValue << " vs " << correctValue;
                    std::cout << "\n";
                }
                return false;
            }

            stop = true;
            for(unsigned i = 0; i < operands; ++i)
            {
                if(++valueIndices[i] < varValuesToTest)
                { stop = false; break; }
                valueIndices[i] = 0;
            }
        }

        return true;
    }

    template<typename Value_t>
    bool runBooleanComparisonTestsForType()
    {
        const bool isIntegral = FUNCTIONPARSERTYPES::IsIntType<Value_t>::result;
        const unsigned maxOperandIndex = isIntegral ? 20 : 32;

        const char varNames[] = { 'x', 'y', 'z' };
        const char* const varString = "x,y,z";
        const unsigned varsAmount = sizeof(varNames) / sizeof(varNames[0]);

        unsigned operandIndices[varsAmount];
        unsigned exprIndices[varsAmount - 1];

        unsigned testCounter = 0;
        FunctionParserBase<Value_t> fparser;

        bool errors = false;

        for(unsigned operands = 2; operands <= varsAmount; ++operands)
        {
            for(unsigned i = 0; i < operands; ++i) operandIndices[i] = 0;
            for(unsigned i = 0; i < operands-1; ++i) exprIndices[i] = 0;

            do
            {
                // Generate function string:
                std::string functionString =
                    getOperandString(varNames[0], operandIndices[0]);

                for(unsigned i = 1; i < operands; ++i)
                    functionString =
                        getBooleanExpression
                        (i == 1 ? functionString : "(" + functionString + ")",
                         getOperandString(varNames[i], operandIndices[i]),
                         exprIndices[i-1]);

                //std::cout << '"' << functionString << "\"\n";

                // Parse function string:
                int errorIndex = fparser.Parse(functionString, varString);
                if(errorIndex >= 0)
                {
                    std::cout << "\nOops! Function \"" << functionString
                              << "\" was malformed.\n";
                    return false;
                }

                // Evaluate function and test for correctness:
                if(!runBooleanComparisonEvaluation<Value_t, varsAmount>
                   (operandIndices, exprIndices, operands,
                    fparser, functionString, false))
                {
                    if (verbosityLevel < 1) return false;
                    errors = true;
                }

                fparser.Optimize();

                if(!runBooleanComparisonEvaluation<Value_t, varsAmount>
                   (operandIndices, exprIndices, operands,
                    fparser, functionString, true))
                {
                    if (verbosityLevel < 1) return false;
                    errors = true;
                }

                ++testCounter;
            }
            while(updateIndices(operandIndices, exprIndices,
                                operands, maxOperandIndex));
        }
        if(errors) return false;

        if(verbosityLevel >= 1)
            std::cout << " (" << testCounter << ")" << std::flush;

        return true;
    }
}

int testOptimizer1()
{
    return OptimizerTests::runTrigCombinationTests();
}

int testOptimizer2()
{
    return OptimizerTests::runBooleanComparisonTestsForType<DefaultValue_t>();
}

int testOptimizer3()
{
#ifdef FP_SUPPORT_LONG_INT_TYPE
    return OptimizerTests::runBooleanComparisonTestsForType<long>();
#else
    return -1;
#endif
}


//=========================================================================
// Help output
//=========================================================================
void printAvailableTests(std::vector<std::string>& tests)
{
    std::cout << "Available tests:\n";
    std::size_t column=0;
    std::string prev_test_prefix;

    bool counting_tests = false;
    long last_count     = 0, count_length = 0;

    for(std::size_t a=0; a<tests.size(); ++a)
    {
        std::string tn = tests[a];
        std::size_t p = tn.rfind('/');
        if(p == tn.npos)
            prev_test_prefix = "";
        else
        {
            {
                std::string path_prefix(tn, 0, p);
                if(path_prefix != prev_test_prefix)
                {
                    if(counting_tests && count_length > 1)
                    {
                        std::ostringstream tmp; tmp << "-" << last_count;
                        std::cout << tmp.str(); column += tmp.str().size();
                    }
                    counting_tests = false;
                    if(column) { std::cout << std::endl; column=0; }
                    prev_test_prefix = path_prefix;
                    std::cout << "    " << path_prefix << "/\n";
                }
            }
            tn.erase(0, p+1);
        }
        if(column+tn.size() >= 76) { column=0; std::cout << "\n"; }
        if(column==0) { std::cout << "        "; column+=8; }
        else { std::cout << " "; column+=1; }

        /* TODO: Rewrite this such that backspaces are not needed,
         *       because they don't work with util-linux's "more"
         */
        char* endptr = 0;
        long val = strtol(tn.c_str(), &endptr, 10);
        if(!*endptr)
        {
            if(!counting_tests)
            {
                counting_tests = true; count_length = 1; last_count = val;
            }
            else if(val == last_count+1)
            {
                ++count_length;
                last_count = val; std::cout << "\b"; --column; continue;
            }
            else if(count_length > 1)
            {
                std::ostringstream tmp; tmp << "\b-" << last_count << " ";
                std::cout << tmp.str(); column += tmp.str().size();
                counting_tests = false;
            }
            else counting_tests = false;
        }
        else if(counting_tests && count_length > 1)
        {
            std::ostringstream tmp; tmp << "\b-" << last_count << " ";
            std::cout << tmp.str(); column += tmp.str().size();
            counting_tests = false;
        }
        else counting_tests = false;

        std::cout << tn;
        column += tn.size();
    }
    if(column) std::cout << std::endl;
}

//=========================================================================
// Main
//=========================================================================
int main(int argc, char* argv[])
{
    const char* const optionsHelpText =
        "    -q                Quiet (no progress, brief error reports)\n"
        "    -v                Verbose (progress, full error reports)\n"
        "    -vv               Very verbose\n"
        "    -tests <tests>    Select tests to perform, wildcards ok (implies -noalgo)\n"
        "                      Example: -tests 'cmp*'\n"
        "    -tests help       List available tests\n"
        "    -d                Test double datatype\n"
        "    -f                Test float datatype\n"
        "    -ld               Test long double datatype\n"
        "    -li               Test long int datatype\n"
        "    -mf, -mpfr        Test MpfrFloat datatype\n"
        "    -gi, -gmpint      Test GmpInt datatype\n"
        "    -cd               Test std::complex<double> datatype\n"
        "    -cf               Test std::complex<float> datatype\n"
        "    -cld              Test std::complex<long double> datatype\n"
        "    -algo <n>         Run only algorithmic test <n>\n"
        "    -noalgo           Skip all algorithmic tests\n"
        "    -skipSlowAlgo     Skip slow algorithmic tests\n"
        "    -h, --help        This help\n";

#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
    MpfrFloat::setDefaultMantissaBits(80);
#endif
#ifdef FP_SUPPORT_GMP_INT_TYPE
    GmpInt::setDefaultNumberOfBits(80);
#endif

    bool skipSlowAlgo = false;
    bool runAllTypes = true;
    bool runAlgoTests = true;
    bool run_d = false, run_f = false, run_ld = false;
    bool run_li = false, run_mf = false, run_gi = false;
    bool run_cd = false, run_cf = false, run_cld = false;
    unsigned runAlgoTest = 0;

    for(int i = 1; i < argc; ++i)
    {
        if(std::strcmp(argv[i], "-q") == 0) verbosityLevel = 0;
        else if(std::strcmp(argv[i], "-v") == 0) verbosityLevel = 2;
        else if(std::strcmp(argv[i], "-vv") == 0) verbosityLevel = 3;
        else if(std::strcmp(argv[i], "-vvv") == 0) verbosityLevel = 4;
        else if(std::strcmp(argv[i], "-noalgo") == 0) runAlgoTests = false;
        else if(std::strcmp(argv[i], "-skipSlowAlgo") == 0) skipSlowAlgo = true;
        else if(std::strcmp(argv[i], "-algo") == 0)
        {
            if(i+1 < argc) runAlgoTest = std::atoi(argv[++i]);
            runAlgoTests = true;
        }
        else if(std::strcmp(argv[i], "-tests") == 0)
        {
            runAlgoTests = false;

            std::vector<std::string> tests;
#ifndef FP_DISABLE_DOUBLE_TYPE
            for(unsigned a=0; RegressionTests<double>::Tests[a].testName; ++a)
                tests.push_back(RegressionTests<double>::Tests[a].testName);
#endif
#ifdef FP_SUPPORT_FLOAT_TYPE
            for(unsigned a=0; RegressionTests<float>::Tests[a].testName; ++a)
                tests.push_back(RegressionTests<float>::Tests[a].testName);
#endif
#ifdef FP_SUPPORT_LONG_DOUBLE_TYPE
            for(unsigned a=0; RegressionTests<long double>::Tests[a].testName; ++a)
                tests.push_back(RegressionTests<long double>::Tests[a].testName);
#endif
#ifdef FP_SUPPORT_LONG_INT_TYPE
            for(unsigned a=0; RegressionTests<long>::Tests[a].testName; ++a)
                tests.push_back(RegressionTests<long>::Tests[a].testName);
#endif
#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
            for(unsigned a=0; RegressionTests<MpfrFloat>::Tests[a].testName; ++a)
                tests.push_back(RegressionTests<MpfrFloat>::Tests[a].testName);
#endif
#ifdef FP_SUPPORT_GMP_INT_TYPE
            for(unsigned a=0; RegressionTests<GmpInt>::Tests[a].testName; ++a)
                tests.push_back(RegressionTests<GmpInt>::Tests[a].testName);
#endif
            std::sort(tests.begin(), tests.end(), natcomp);
            tests.erase(std::unique(tests.begin(), tests.end()), tests.end());

            if(std::strcmp(argv[i+1], "help") == 0)
            {
                printAvailableTests(tests);
                return 0;
            }
            while(i+1 < argc && argv[i+1][0] != '-')
            {
                const char* t = argv[++i];
                bool ok = false;
                for(std::size_t a=0; a<tests.size(); ++a)
                    if(WildMatch_Dirmask(t, tests[a].c_str()))
                    { ok=true; break; }
                if(!ok)
                {
                    std::cout << "No such test: " << t
                              << "\n\"testbed -tests help\" to list "
                              << "available tests.\n";
                    return -1;
                }
                selectedRegressionTests.push_back(t);
            }
        }
        else if(std::strcmp(argv[i], "-d") == 0
             || std::strcmp(argv[i], "-double") == 0)
            runAllTypes = false, run_d = true;
        else if(std::strcmp(argv[i], "-f") == 0
             || std::strcmp(argv[i], "-float") == 0)
            runAllTypes = false, run_f = true;
        else if(std::strcmp(argv[i], "-ld") == 0
             || std::strcmp(argv[i], "-longdouble") == 0)
            runAllTypes = false, run_ld = true;
        else if(std::strcmp(argv[i], "-li") == 0
             || std::strcmp(argv[i], "-longint") == 0)
            runAllTypes = false, run_li = true;
        else if(std::strcmp(argv[i], "-mf") == 0
             || std::strcmp(argv[i], "-mpfr") == 0)
            runAllTypes = false, run_mf = true;
        else if(std::strcmp(argv[i], "-gi") == 0
             || std::strcmp(argv[i], "-gmpint") == 0)
            runAllTypes = false, run_gi = true;
        else if(std::strcmp(argv[i], "-cd") == 0)
            runAllTypes = false, run_cd = true;
        else if(std::strcmp(argv[i], "-cf") == 0)
            runAllTypes = false, run_cf = true;
        else if(std::strcmp(argv[i], "-cld") == 0)
            runAllTypes = false, run_cld = true;

        else if(std::strcmp(argv[i], "--help") == 0
             || std::strcmp(argv[i], "-help") == 0
             || std::strcmp(argv[i], "-h") == 0
             || std::strcmp(argv[i], "/?") == 0)
        {
            std::cout <<
                "FunctionParser testbed " << kVersionNumber <<
                "\n\nUsage: " << argv[0] << " [<option> ...]\n"
                "\n" << optionsHelpText;
            return 0;
        }
        else if(std::strlen(argv[i]) > 0)
        {
            std::cout << "Unknown option: '" << argv[i] << "'\n";
            return 1;
        }
    }

    if(selectedRegressionTests.empty())
        selectedRegressionTests.push_back("*");

    DefaultParser fp0;

    // Test that the parser doesn't crash if Eval() is called before Parse():
    fp0.Eval(0);

    const char* const delimiterTestFunction = "x+y } ";
    fp0.setDelimiterChar('}');
    int res = fp0.Parse(delimiterTestFunction, "x,y");
    if(fp0.GetParseErrorType() != fp0.FP_NO_ERROR || res != 4)
    {
        std::cout << "Delimiter test \"" << delimiterTestFunction
                  << "\" failed at " << res << ": " << fp0.ErrorMsg()
                  << std::endl;
        return 1;
    }
    fp0.Parse("x+}y", "x,y");
    if(fp0.GetParseErrorType() == fp0.FP_NO_ERROR)
    {
        std::cout << "Erroneous function with delimiter didn't fail"
                  << std::endl;
        return 1;
    }

    bool allTestsOk = true;

    if(!runAllTypes || runAlgoTest == 0)
    {
#ifndef FP_DISABLE_DOUBLE_TYPE
        if(runAllTypes || run_d)
            if(!runRegressionTests<double>("double"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_FLOAT_TYPE
        if(runAllTypes || run_f)
            if(!runRegressionTests<float>("float"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_LONG_DOUBLE_TYPE
        if(runAllTypes || run_ld)
            if(!runRegressionTests<long double>("long double"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_LONG_INT_TYPE
        if(runAllTypes || run_li)
            if(!runRegressionTests<long>("long int"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_MPFR_FLOAT_TYPE
        if(runAllTypes || run_mf)
            if(!runRegressionTests<MpfrFloat>("MpfrFloat"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_GMP_INT_TYPE
        if(runAllTypes || run_gi)
            if(!runRegressionTests<GmpInt>("GmpInt"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_COMPLEX_DOUBLE_TYPE
        if(runAllTypes || run_cd)
            if(!runRegressionTests<std::complex<double> >("std::complex<double>"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_COMPLEX_FLOAT_TYPE
        if(runAllTypes || run_cf)
            if(!runRegressionTests<std::complex<float> >("std::complex<float>"))
                allTestsOk = false;
#endif
#ifdef FP_SUPPORT_COMPLEX_LONG_DOUBLE_TYPE
        if(runAllTypes || run_cld)
            if(!runRegressionTests<std::complex<long double> >("std::complex<long double>"))
                allTestsOk = false;
#endif
    }

////////////////////////////
////////////////////////////
////////////////////////////
////////////////////////////

    // Misc. tests
    // -----------
    const struct
    {
        const char* const testName;
        int(*testFunction)();
    }
    algorithmicTests[] =
    {
        { "Copy constructor and assignment", &TestCopying },
        { "Error situations", &TestErrorSituations },
        { "Whitespaces", &WhiteSpaceTest },
        { "Optimizer test 1 (trig. combinations)", &testOptimizer1 },
        { "Optimizer test 2 (bool combinations, double)",
          (skipSlowAlgo || (!runAllTypes && !run_d)) ? 0 : &testOptimizer2 },
        { "Optimizer test 3 (bool combinations, long)",
          (!runAllTypes && !run_li) ? 0 : &testOptimizer3 },
        { "Integral powers",  &TestIntPow },
        { "UTF8 test", skipSlowAlgo ? 0 : &UTF8Test },
        { "Identifier test", &TestIdentifiers },
        { "Used-defined functions", &testUserDefinedFunctions },
        { "Multithreading", &testMultithreadedEvaluation }
    };

    const unsigned algorithmicTestsAmount =
        sizeof(algorithmicTests) / sizeof(algorithmicTests[0]);

    if(runAlgoTests)
    {
        for(unsigned i = 0; i < algorithmicTestsAmount; ++i)
        {
            if(runAlgoTest >= 1 && runAlgoTest <= algorithmicTestsAmount &&
               runAlgoTest != i+1)
                continue;

            if(verbosityLevel >= 1)
                std::cout << "Algo test " << i+1 << ": "
                          << algorithmicTests[i].testName << std::flush;

            if(!algorithmicTests[i].testFunction)
            {
                if(verbosityLevel >= 1)
                    std::cout << ": Skipped." << std::endl;
                continue;
            }

            int result = algorithmicTests[i].testFunction();

            if(result == 0)
            {
                allTestsOk = false;
                if(verbosityLevel == 0)
                    std::cout << "Algo test " << i+1 << ": "
                              << algorithmicTests[i].testName;
                if(verbosityLevel <= 1)
                    std::cout << ": FAILED." << std::endl;
            }
            else if(verbosityLevel >= 1)
            {
                if(result < 0 )
                    std::cout << ": (No support)" << std::endl;
                else
                    std::cout << ": Ok." << std::endl;
            }
        }
    }

    if(!allTestsOk)
    {
        std::cout << "Some tests failed." << std::endl;
        return 1;
    }
    if(verbosityLevel == 1)
        std::cout << "================= All tests OK =================\n";
    else if(verbosityLevel >= 2)
        std::cout << "================================================\n"
                  << "================= All tests OK =================\n"
                  << "================================================\n";
    return 0;
}