summaryrefslogtreecommitdiff
path: root/icu4c/source/test/cintltst/cmsccoll.c
blob: 206daa20a3d6028724f7b8099b8a67bd78f7e42f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
 * COPYRIGHT:
 * Copyright (c) 2001-2016, International Business Machines Corporation and
 * others. All Rights Reserved.
 ********************************************************************/
/*******************************************************************************
*
* File cmsccoll.C
*
*******************************************************************************/
/**
 * These are the tests specific to ICU 1.8 and above, that I didn't know where
 * to fit.
 */

#include <stdio.h>

#include "unicode/utypes.h"

#if !UCONFIG_NO_COLLATION

#include "unicode/ucol.h"
#include "unicode/ucoleitr.h"
#include "unicode/uloc.h"
#include "cintltst.h"
#include "ccolltst.h"
#include "callcoll.h"
#include "unicode/ustring.h"
#include "string.h"
#include "ucol_imp.h"
#include "cmemory.h"
#include "cstring.h"
#include "uassert.h"
#include "unicode/parseerr.h"
#include "unicode/ucnv.h"
#include "unicode/ures.h"
#include "unicode/uscript.h"
#include "unicode/utf16.h"
#include "uparse.h"
#include "putilimp.h"


#define MAX_TOKEN_LEN 16

typedef UCollationResult tst_strcoll(void *collator, const int object,
                        const UChar *source, const int sLen,
                        const UChar *target, const int tLen);



const static char cnt1[][10] = {

  "AA",
  "AC",
  "AZ",
  "AQ",
  "AB",
  "ABZ",
  "ABQ",
  "Z",
  "ABC",
  "Q",
  "B"
};

const static char cnt2[][10] = {
  "DA",
  "DAD",
  "DAZ",
  "MAR",
  "Z",
  "DAVIS",
  "MARK",
  "DAV",
  "DAVI"
};

static void IncompleteCntTest(void)
{
  UErrorCode status = U_ZERO_ERROR;
  UChar temp[90];
  UChar t1[90];
  UChar t2[90];

  UCollator *coll =  NULL;
  uint32_t i = 0, j = 0;
  uint32_t size = 0;

  u_uastrcpy(temp, " & Z < ABC < Q < B");

  coll = ucol_openRules(temp, u_strlen(temp), UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL,&status);

  if(U_SUCCESS(status)) {
    size = UPRV_LENGTHOF(cnt1);
    for(i = 0; i < size-1; i++) {
      for(j = i+1; j < size; j++) {
        UCollationElements *iter;
        u_uastrcpy(t1, cnt1[i]);
        u_uastrcpy(t2, cnt1[j]);
        doTest(coll, t1, t2, UCOL_LESS);
        /* synwee : added collation element iterator test */
        iter = ucol_openElements(coll, t2, u_strlen(t2), &status);
        if (U_FAILURE(status)) {
          log_err("Creation of iterator failed\n");
          break;
        }
        backAndForth(iter);
        ucol_closeElements(iter);
      }
    }
  }

  ucol_close(coll);


  u_uastrcpy(temp, " & Z < DAVIS < MARK <DAV");
  coll = ucol_openRules(temp, u_strlen(temp), UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);

  if(U_SUCCESS(status)) {
    size = UPRV_LENGTHOF(cnt2);
    for(i = 0; i < size-1; i++) {
      for(j = i+1; j < size; j++) {
        UCollationElements *iter;
        u_uastrcpy(t1, cnt2[i]);
        u_uastrcpy(t2, cnt2[j]);
        doTest(coll, t1, t2, UCOL_LESS);

        /* synwee : added collation element iterator test */
        iter = ucol_openElements(coll, t2, u_strlen(t2), &status);
        if (U_FAILURE(status)) {
          log_err("Creation of iterator failed\n");
          break;
        }
        backAndForth(iter);
        ucol_closeElements(iter);
      }
    }
  }

  ucol_close(coll);


}

const static char shifted[][20] = {
  "black bird",
  "black-bird",
  "blackbird",
  "black Bird",
  "black-Bird",
  "blackBird",
  "black birds",
  "black-birds",
  "blackbirds"
};

const static UCollationResult shiftedTert[] = {
  UCOL_EQUAL,
  UCOL_EQUAL,
  UCOL_EQUAL,
  UCOL_LESS,
  UCOL_EQUAL,
  UCOL_EQUAL,
  UCOL_LESS,
  UCOL_EQUAL,
  UCOL_EQUAL
};

const static char nonignorable[][20] = {
  "black bird",
  "black Bird",
  "black birds",
  "black-bird",
  "black-Bird",
  "black-birds",
  "blackbird",
  "blackBird",
  "blackbirds"
};

static void BlackBirdTest(void) {
  UErrorCode status = U_ZERO_ERROR;
  UChar t1[90];
  UChar t2[90];

  uint32_t i = 0, j = 0;
  uint32_t size = 0;
  UCollator *coll = ucol_open("en_US", &status);

  ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
  ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);

  if(U_SUCCESS(status)) {
    size = UPRV_LENGTHOF(nonignorable);
    for(i = 0; i < size-1; i++) {
      for(j = i+1; j < size; j++) {
        u_uastrcpy(t1, nonignorable[i]);
        u_uastrcpy(t2, nonignorable[j]);
        doTest(coll, t1, t2, UCOL_LESS);
      }
    }
  }

  ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
  ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);

  if(U_SUCCESS(status)) {
    size = UPRV_LENGTHOF(shifted);
    for(i = 0; i < size-1; i++) {
      for(j = i+1; j < size; j++) {
        u_uastrcpy(t1, shifted[i]);
        u_uastrcpy(t2, shifted[j]);
        doTest(coll, t1, t2, UCOL_LESS);
      }
    }
  }

  ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
  if(U_SUCCESS(status)) {
    size = UPRV_LENGTHOF(shifted);
    for(i = 1; i < size; i++) {
      u_uastrcpy(t1, shifted[i-1]);
      u_uastrcpy(t2, shifted[i]);
      doTest(coll, t1, t2, shiftedTert[i]);
    }
  }

  ucol_close(coll);
}

const static UChar testSourceCases[][MAX_TOKEN_LEN] = {
    {0x0041/*'A'*/, 0x0300, 0x0301, 0x0000},
    {0x0041/*'A'*/, 0x0300, 0x0316, 0x0000},
    {0x0041/*'A'*/, 0x0300, 0x0000},
    {0x00C0, 0x0301, 0x0000},
    /* this would work with forced normalization */
    {0x00C0, 0x0316, 0x0000}
};

const static UChar testTargetCases[][MAX_TOKEN_LEN] = {
    {0x0041/*'A'*/, 0x0301, 0x0300, 0x0000},
    {0x0041/*'A'*/, 0x0316, 0x0300, 0x0000},
    {0x00C0, 0},
    {0x0041/*'A'*/, 0x0301, 0x0300, 0x0000},
    /* this would work with forced normalization */
    {0x0041/*'A'*/, 0x0316, 0x0300, 0x0000}
};

const static UCollationResult results[] = {
    UCOL_GREATER,
    UCOL_EQUAL,
    UCOL_EQUAL,
    UCOL_GREATER,
    UCOL_EQUAL
};

static void FunkyATest(void)
{

    int32_t i;
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    myCollation = ucol_open("en_US", &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }
    log_verbose("Testing some A letters, for some reason\n");
    ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
    ucol_setStrength(myCollation, UCOL_TERTIARY);
    for (i = 0; i < 4 ; i++)
    {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
    }
    ucol_close(myCollation);
}

UColAttributeValue caseFirst[] = {
    UCOL_OFF,
    UCOL_LOWER_FIRST,
    UCOL_UPPER_FIRST
};


UColAttributeValue alternateHandling[] = {
    UCOL_NON_IGNORABLE,
    UCOL_SHIFTED
};

UColAttributeValue caseLevel[] = {
    UCOL_OFF,
    UCOL_ON
};

UColAttributeValue strengths[] = {
    UCOL_PRIMARY,
    UCOL_SECONDARY,
    UCOL_TERTIARY,
    UCOL_QUATERNARY,
    UCOL_IDENTICAL
};

#if 0
static const char * strengthsC[] = {
    "UCOL_PRIMARY",
    "UCOL_SECONDARY",
    "UCOL_TERTIARY",
    "UCOL_QUATERNARY",
    "UCOL_IDENTICAL"
};

static const char * caseFirstC[] = {
    "UCOL_OFF",
    "UCOL_LOWER_FIRST",
    "UCOL_UPPER_FIRST"
};


static const char * alternateHandlingC[] = {
    "UCOL_NON_IGNORABLE",
    "UCOL_SHIFTED"
};

static const char * caseLevelC[] = {
    "UCOL_OFF",
    "UCOL_ON"
};

/* not used currently - does not test only prints */
static void PrintMarkDavis(void)
{
  UErrorCode status = U_ZERO_ERROR;
  UChar m[256];
  uint8_t sortkey[256];
  UCollator *coll = ucol_open("en_US", &status);
  uint32_t h,i,j,k, sortkeysize;
  uint32_t sizem = 0;
  char buffer[512];
  uint32_t len = 512;

  log_verbose("PrintMarkDavis");

  u_uastrcpy(m, "Mark Davis");
  sizem = u_strlen(m);


  m[1] = 0xe4;

  for(i = 0; i<sizem; i++) {
    fprintf(stderr, "\\u%04X ", m[i]);
  }
  fprintf(stderr, "\n");

  for(h = 0; h<UPRV_LENGTHOF(caseFirst); h++) {
    ucol_setAttribute(coll, UCOL_CASE_FIRST, caseFirst[i], &status);
    fprintf(stderr, "caseFirst: %s\n", caseFirstC[h]);

    for(i = 0; i<UPRV_LENGTHOF(alternateHandling); i++) {
      ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, alternateHandling[i], &status);
      fprintf(stderr, "  AltHandling: %s\n", alternateHandlingC[i]);

      for(j = 0; j<UPRV_LENGTHOF(caseLevel); j++) {
        ucol_setAttribute(coll, UCOL_CASE_LEVEL, caseLevel[j], &status);
        fprintf(stderr, "    caseLevel: %s\n", caseLevelC[j]);

        for(k = 0; k<UPRV_LENGTHOF(strengths); k++) {
          ucol_setAttribute(coll, UCOL_STRENGTH, strengths[k], &status);
          sortkeysize = ucol_getSortKey(coll, m, sizem, sortkey, 256);
          fprintf(stderr, "      strength: %s\n      Sortkey: ", strengthsC[k]);
          fprintf(stderr, "%s\n", ucol_sortKeyToString(coll, sortkey, buffer, &len));
        }

      }

    }

  }
}
#endif

static void BillFairmanTest(void) {
/*
** check for actual locale via ICU resource bundles
**
** lp points to the original locale ("fr_FR_....")
*/

    UResourceBundle *lr,*cr;
    UErrorCode              lec = U_ZERO_ERROR;
    const char *lp = "fr_FR_you_ll_never_find_this_locale";

    log_verbose("BillFairmanTest\n");

    lr = ures_open(NULL,lp,&lec);
    if (lr) {
        cr = ures_getByKey(lr,"collations",0,&lec);
        if (cr) {
            lp = ures_getLocaleByType(cr, ULOC_ACTUAL_LOCALE, &lec);
            if (lp) {
                if (U_SUCCESS(lec)) {
                    if(strcmp(lp, "fr") != 0) {
                        log_err("Wrong locale for French Collation Data, expected \"fr\" got %s", lp);
                    }
                }
            }
            ures_close(cr);
        }
        ures_close(lr);
    }
}

const static char chTest[][20] = {
  "c",
  "C",
  "ca", "cb", "cx", "cy", "CZ",
  "c\\u030C", "C\\u030C",
  "h",
  "H",
  "ha", "Ha", "harly", "hb", "HB", "hx", "HX", "hy", "HY",
  "ch", "cH", "Ch", "CH",
  "cha", "charly", "che", "chh", "chch", "chr",
  "i", "I", "iarly",
  "r", "R",
  "r\\u030C", "R\\u030C",
  "s",
  "S",
  "s\\u030C", "S\\u030C",
  "z", "Z",
  "z\\u030C", "Z\\u030C"
};

static void TestChMove(void) {
    UChar t1[256] = {0};
    UChar t2[256] = {0};

    uint32_t i = 0, j = 0;
    uint32_t size = 0;
    UErrorCode status = U_ZERO_ERROR;

    UCollator *coll = ucol_open("cs", &status);

    if(U_SUCCESS(status)) {
        size = UPRV_LENGTHOF(chTest);
        for(i = 0; i < size-1; i++) {
            for(j = i+1; j < size; j++) {
                u_unescape(chTest[i], t1, 256);
                u_unescape(chTest[j], t2, 256);
                doTest(coll, t1, t2, UCOL_LESS);
            }
        }
    }
    else {
        log_data_err("Can't open collator");
    }
    ucol_close(coll);
}




/*
const static char impTest[][20] = {
  "\\u4e00",
    "a",
    "A",
    "b",
    "B",
    "\\u4e01"
};
*/


static void TestImplicitTailoring(void) {
  static const struct {
    const char *rules;
    const char *data[10];
    const uint32_t len;
  } tests[] = {
      {
        /* Tailor b and c before U+4E00. */
        "&[before 1]\\u4e00 < b < c "
        /* Now, before U+4E00 is c; put d and e after that. */
        "&[before 1]\\u4e00 < d < e",
        { "b", "c", "d", "e", "\\u4e00"}, 5 },
      { "&\\u4e00 < a <<< A < b <<< B",   { "\\u4e00", "a", "A", "b", "B", "\\u4e01"}, 6 },
      { "&[before 1]\\u4e00 < \\u4e01 < \\u4e02", { "\\u4e01", "\\u4e02", "\\u4e00"}, 3},
      { "&[before 1]\\u4e01 < \\u4e02 < \\u4e03", { "\\u4e02", "\\u4e03", "\\u4e01"}, 3}
  };

  int32_t i = 0;

  for(i = 0; i < UPRV_LENGTHOF(tests); i++) {
      genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len);
  }

/*
  UChar t1[256] = {0};
  UChar t2[256] = {0};

  const char *rule = "&\\u4e00 < a <<< A < b <<< B";

  uint32_t i = 0, j = 0;
  uint32_t size = 0;
  uint32_t ruleLen = 0;
  UErrorCode status = U_ZERO_ERROR;
  UCollator *coll = NULL;
  ruleLen = u_unescape(rule, t1, 256);

  coll = ucol_openRules(t1, ruleLen, UCOL_OFF, UCOL_TERTIARY,NULL, &status);

  if(U_SUCCESS(status)) {
    size = UPRV_LENGTHOF(impTest);
    for(i = 0; i < size-1; i++) {
      for(j = i+1; j < size; j++) {
        u_unescape(impTest[i], t1, 256);
        u_unescape(impTest[j], t2, 256);
        doTest(coll, t1, t2, UCOL_LESS);
      }
    }
  }
  else {
    log_err("Can't open collator");
  }
  ucol_close(coll);
  */
}

static void TestFCDProblem(void) {
  UChar t1[256] = {0};
  UChar t2[256] = {0};

  const char *s1 = "\\u0430\\u0306\\u0325";
  const char *s2 = "\\u04D1\\u0325";

  UErrorCode status = U_ZERO_ERROR;
  UCollator *coll = ucol_open("", &status);
  u_unescape(s1, t1, 256);
  u_unescape(s2, t2, 256);

  ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
  doTest(coll, t1, t2, UCOL_EQUAL);

  ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
  doTest(coll, t1, t2, UCOL_EQUAL);

  ucol_close(coll);
}

/*
The largest normalization form is 18 for NFKC/NFKD, 4 for NFD and 3 for NFC
We're only using NFC/NFD in this test.
*/
#define NORM_BUFFER_TEST_LEN 18
typedef struct {
  UChar32 u;
  UChar NFC[NORM_BUFFER_TEST_LEN];
  UChar NFD[NORM_BUFFER_TEST_LEN];
} tester;

static void TestComposeDecompose(void) {
    /* [[:NFD_Inert=false:][:NFC_Inert=false:]] */
    static const UChar UNICODESET_STR[] = {
        0x5B,0x5B,0x3A,0x4E,0x46,0x44,0x5F,0x49,0x6E,0x65,0x72,0x74,0x3D,0x66,0x61,
        0x6C,0x73,0x65,0x3A,0x5D,0x5B,0x3A,0x4E,0x46,0x43,0x5F,0x49,0x6E,0x65,0x72,
        0x74,0x3D,0x66,0x61,0x6C,0x73,0x65,0x3A,0x5D,0x5D,0
    };
    int32_t noOfLoc;
    int32_t i = 0, j = 0;

    UErrorCode status = U_ZERO_ERROR;
    const char *locName = NULL;
    uint32_t nfcSize;
    uint32_t nfdSize;
    tester **t;
    uint32_t noCases = 0;
    UCollator *coll = NULL;
    UChar32 u = 0;
    UChar comp[NORM_BUFFER_TEST_LEN];
    uint32_t len = 0;
    UCollationElements *iter;
    USet *charsToTest = uset_openPattern(UNICODESET_STR, -1, &status);
    int32_t charsToTestSize;

    noOfLoc = uloc_countAvailable();

    coll = ucol_open("", &status);
    if (U_FAILURE(status)) {
        log_data_err("Error opening collator -> %s (Are you missing data?)\n", u_errorName(status));
        uset_close(charsToTest);
        return;
    }
    charsToTestSize = uset_size(charsToTest);
    if (charsToTestSize <= 0) {
        log_err("Set was zero. Missing data?\n");
        uset_close(charsToTest);
        return;
    }
    t = (tester **)malloc(charsToTestSize * sizeof(tester *));
    t[0] = (tester *)malloc(sizeof(tester));
    log_verbose("Testing UCA extensively for %d characters\n", charsToTestSize);

    for(u = 0; u < charsToTestSize; u++) {
        UChar32 ch = uset_charAt(charsToTest, u);
        len = 0;
        U16_APPEND_UNSAFE(comp, len, ch);
        nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status);
        nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status);

        if(nfcSize != nfdSize || (uprv_memcmp(t[noCases]->NFC, t[noCases]->NFD, nfcSize * sizeof(UChar)) != 0)
          || (len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0))) {
            t[noCases]->u = ch;
            if(len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0)) {
                u_strncpy(t[noCases]->NFC, comp, len);
                t[noCases]->NFC[len] = 0;
            }
            noCases++;
            t[noCases] = (tester *)malloc(sizeof(tester));
            uprv_memset(t[noCases], 0, sizeof(tester));
        }
    }
    log_verbose("Testing %d/%d of possible test cases\n", noCases, charsToTestSize);
    uset_close(charsToTest);
    charsToTest = NULL;

    for(u=0; u<(UChar32)noCases; u++) {
        if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) {
            log_err("Failure: codePoint %05X fails TestComposeDecompose in the UCA\n", t[u]->u);
            doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL);
        }
    }
    /*
    for(u = 0; u < charsToTestSize; u++) {
      if(!(u&0xFFFF)) {
        log_verbose("%08X ", u);
      }
      uprv_memset(t[noCases], 0, sizeof(tester));
      t[noCases]->u = u;
      len = 0;
      U16_APPEND_UNSAFE(comp, len, u);
      comp[len] = 0;
      nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status);
      nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status);
      doTest(coll, comp, t[noCases]->NFD, UCOL_EQUAL);
      doTest(coll, comp, t[noCases]->NFC, UCOL_EQUAL);
    }
    */

    ucol_close(coll);

    log_verbose("Testing locales, number of cases = %i\n", noCases);
    for(i = 0; i<noOfLoc; i++) {
        status = U_ZERO_ERROR;
        locName = uloc_getAvailable(i);
        if(hasCollationElements(locName)) {
            char cName[256];
            UChar name[256];
            int32_t nameSize = uloc_getDisplayName(locName, NULL, name, sizeof(cName), &status);

            for(j = 0; j<nameSize; j++) {
                cName[j] = (char)name[j];
            }
            cName[nameSize] = 0;
            log_verbose("\nTesting locale %s (%s)\n", locName, cName);

            coll = ucol_open(locName, &status);
            ucol_setStrength(coll, UCOL_IDENTICAL);
            iter = ucol_openElements(coll, t[u]->NFD, u_strlen(t[u]->NFD), &status);

            for(u=0; u<(UChar32)noCases; u++) {
                if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) {
                    log_err("Failure: codePoint %05X fails TestComposeDecompose for locale %s\n", t[u]->u, cName);
                    doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL);
                    log_verbose("Testing NFC\n");
                    ucol_setText(iter, t[u]->NFC, u_strlen(t[u]->NFC), &status);
                    backAndForth(iter);
                    log_verbose("Testing NFD\n");
                    ucol_setText(iter, t[u]->NFD, u_strlen(t[u]->NFD), &status);
                    backAndForth(iter);
                }
            }
            ucol_closeElements(iter);
            ucol_close(coll);
        }
    }
    for(u = 0; u <= (UChar32)noCases; u++) {
        free(t[u]);
    }
    free(t);
}

static void TestEmptyRule(void) {
  UErrorCode status = U_ZERO_ERROR;
  UChar rulez[] = { 0 };
  UCollator *coll = ucol_openRules(rulez, 0, UCOL_OFF, UCOL_TERTIARY,NULL, &status);

  ucol_close(coll);
}

static void TestUCARules(void) {
  UErrorCode status = U_ZERO_ERROR;
  UChar b[256];
  UChar *rules = b;
  uint32_t ruleLen = 0;
  UCollator *UCAfromRules = NULL;
  UCollator *coll = ucol_open("", &status);
  if(status == U_FILE_ACCESS_ERROR) {
    log_data_err("Is your data around?\n");
    return;
  } else if(U_FAILURE(status)) {
    log_err("Error opening collator\n");
    return;
  }
  ruleLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rules, 256);

  log_verbose("TestUCARules\n");
  if(ruleLen > 256) {
    rules = (UChar *)malloc((ruleLen+1)*sizeof(UChar));
    ruleLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rules, ruleLen);
  }
  log_verbose("Rules length is %d\n", ruleLen);
  UCAfromRules = ucol_openRules(rules, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
  if(U_SUCCESS(status)) {
    ucol_close(UCAfromRules);
  } else {
    log_verbose("Unable to create a collator from UCARules!\n");
  }
/*
  u_unescape(blah, b, 256);
  ucol_getSortKey(coll, b, 1, res, 256);
*/
  ucol_close(coll);
  if(rules != b) {
    free(rules);
  }
}


/* Pinyin tonal order */
/*
    A < .. (\u0101) < .. (\u00e1) < .. (\u01ce) < .. (\u00e0)
          (w/macron)<  (w/acute)<   (w/caron)<   (w/grave)
    E < .. (\u0113) < .. (\u00e9) < .. (\u011b) < .. (\u00e8)
    I < .. (\u012b) < .. (\u00ed) < .. (\u01d0) < .. (\u00ec)
    O < .. (\u014d) < .. (\u00f3) < .. (\u01d2) < .. (\u00f2)
    U < .. (\u016b) < .. (\u00fa) < .. (\u01d4) < .. (\u00f9)
      < .. (\u01d6) < .. (\u01d8) < .. (\u01da) < .. (\u01dc) <
.. (\u00fc)

However, in testing we got the following order:
    A < .. (\u00e1) < .. (\u00e0) < .. (\u01ce) < .. (\u0101)
          (w/acute)<   (w/grave)<   (w/caron)<   (w/macron)
    E < .. (\u00e9) < .. (\u00e8) < .. (\u00ea) < .. (\u011b) <
.. (\u0113)
    I < .. (\u00ed) < .. (\u00ec) < .. (\u01d0) < .. (\u012b)
    O < .. (\u00f3) < .. (\u00f2) < .. (\u01d2) < .. (\u014d)
    U < .. (\u00fa) < .. (\u00f9) < .. (\u01d4) < .. (\u00fc) <
.. (\u01d8)
      < .. (\u01dc) < .. (\u01da) < .. (\u01d6) < .. (\u016b)
*/

static void TestBefore(void) {
  const static char *data[] = {
      "\\u0101", "\\u00e1", "\\u01ce", "\\u00e0", "A",
      "\\u0113", "\\u00e9", "\\u011b", "\\u00e8", "E",
      "\\u012b", "\\u00ed", "\\u01d0", "\\u00ec", "I",
      "\\u014d", "\\u00f3", "\\u01d2", "\\u00f2", "O",
      "\\u016b", "\\u00fa", "\\u01d4", "\\u00f9", "U",
      "\\u01d6", "\\u01d8", "\\u01da", "\\u01dc", "\\u00fc"
  };
  genericRulesStarter(
    "&[before 1]a<\\u0101<\\u00e1<\\u01ce<\\u00e0"
    "&[before 1]e<\\u0113<\\u00e9<\\u011b<\\u00e8"
    "&[before 1]i<\\u012b<\\u00ed<\\u01d0<\\u00ec"
    "&[before 1]o<\\u014d<\\u00f3<\\u01d2<\\u00f2"
    "&[before 1]u<\\u016b<\\u00fa<\\u01d4<\\u00f9"
    "&u<\\u01d6<\\u01d8<\\u01da<\\u01dc<\\u00fc",
    data, UPRV_LENGTHOF(data));
}

#if 0
/* superceded by TestBeforePinyin */
static void TestJ784(void) {
  const static char *data[] = {
      "A", "\\u0101", "\\u00e1", "\\u01ce", "\\u00e0",
      "E", "\\u0113", "\\u00e9", "\\u011b", "\\u00e8",
      "I", "\\u012b", "\\u00ed", "\\u01d0", "\\u00ec",
      "O", "\\u014d", "\\u00f3", "\\u01d2", "\\u00f2",
      "U", "\\u016b", "\\u00fa", "\\u01d4", "\\u00f9",
      "\\u00fc",
           "\\u01d6", "\\u01d8", "\\u01da", "\\u01dc"
  };
  genericLocaleStarter("zh", data, UPRV_LENGTHOF(data));
}
#endif

static void TestUpperCaseFirst(void) {
  const static char *data[] = {
    "I",
      "i",
      "Y",
      "y"
  };
  genericLocaleStarter("da", data, UPRV_LENGTHOF(data));
}

static void TestJ815(void) {
  const static char *data[] = {
    "aa",
      "Aa",
      "ab",
      "Ab",
      "ad",
      "Ad",
      "ae",
      "Ae",
      "\\u00e6",
      "\\u00c6",
      "af",
      "Af",
      "b",
      "B"
  };
  genericLocaleStarter("fr", data, UPRV_LENGTHOF(data));
  genericRulesStarter("[backwards 2]&A<<\\u00e6/e<<<\\u00c6/E", data, UPRV_LENGTHOF(data));
}


static void TestCase(void)
{
    const static UChar gRules[MAX_TOKEN_LEN] =
    /*" & 0 < 1,\u2461<a,A"*/
    { 0x0026, 0x0030, 0x003C, 0x0031, 0x002C, 0x2460, 0x003C, 0x0061, 0x002C, 0x0041, 0x0000 };

    const static UChar testCase[][MAX_TOKEN_LEN] =
    {
        /*0*/ {0x0031 /*'1'*/, 0x0061/*'a'*/, 0x0000},
        /*1*/ {0x0031 /*'1'*/, 0x0041/*'A'*/, 0x0000},
        /*2*/ {0x2460 /*circ'1'*/, 0x0061/*'a'*/, 0x0000},
        /*3*/ {0x2460 /*circ'1'*/, 0x0041/*'A'*/, 0x0000}
    };

    const static UCollationResult caseTestResults[][9] =
    {
        { UCOL_LESS,    UCOL_LESS, UCOL_LESS,    UCOL_EQUAL, UCOL_LESS,    UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_LESS },
        { UCOL_GREATER, UCOL_LESS, UCOL_LESS,    UCOL_EQUAL, UCOL_LESS,    UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_GREATER },
        { UCOL_LESS,    UCOL_LESS, UCOL_LESS,    UCOL_EQUAL, UCOL_GREATER, UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_LESS },
        { UCOL_GREATER, UCOL_LESS, UCOL_GREATER, UCOL_EQUAL, UCOL_LESS,    UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_GREATER }
    };

    const static UColAttributeValue caseTestAttributes[][2] =
    {
        { UCOL_LOWER_FIRST, UCOL_OFF},
        { UCOL_UPPER_FIRST, UCOL_OFF},
        { UCOL_LOWER_FIRST, UCOL_ON},
        { UCOL_UPPER_FIRST, UCOL_ON}
    };
    int32_t i,j,k;
    UErrorCode status = U_ZERO_ERROR;
    UCollationElements *iter;
    UCollator  *myCollation;
    myCollation = ucol_open("en_US", &status);

    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }
    log_verbose("Testing different case settings\n");
    ucol_setStrength(myCollation, UCOL_TERTIARY);

    for(k = 0; k<4; k++) {
      ucol_setAttribute(myCollation, UCOL_CASE_FIRST, caseTestAttributes[k][0], &status);
      ucol_setAttribute(myCollation, UCOL_CASE_LEVEL, caseTestAttributes[k][1], &status);
      log_verbose("Case first = %d, Case level = %d\n", caseTestAttributes[k][0], caseTestAttributes[k][1]);
      for (i = 0; i < 3 ; i++) {
        for(j = i+1; j<4; j++) {
          doTest(myCollation, testCase[i], testCase[j], caseTestResults[k][3*i+j-1]);
        }
      }
    }
    ucol_close(myCollation);

    myCollation = ucol_openRules(gRules, u_strlen(gRules), UCOL_OFF, UCOL_TERTIARY,NULL, &status);
    if(U_FAILURE(status)){
        log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }
    log_verbose("Testing different case settings with custom rules\n");
    ucol_setStrength(myCollation, UCOL_TERTIARY);

    for(k = 0; k<4; k++) {
      ucol_setAttribute(myCollation, UCOL_CASE_FIRST, caseTestAttributes[k][0], &status);
      ucol_setAttribute(myCollation, UCOL_CASE_LEVEL, caseTestAttributes[k][1], &status);
      for (i = 0; i < 3 ; i++) {
        for(j = i+1; j<4; j++) {
          log_verbose("k:%d, i:%d, j:%d\n", k, i, j);
          doTest(myCollation, testCase[i], testCase[j], caseTestResults[k][3*i+j-1]);
          iter=ucol_openElements(myCollation, testCase[i], u_strlen(testCase[i]), &status);
          backAndForth(iter);
          ucol_closeElements(iter);
          iter=ucol_openElements(myCollation, testCase[j], u_strlen(testCase[j]), &status);
          backAndForth(iter);
          ucol_closeElements(iter);
        }
      }
    }
    ucol_close(myCollation);
    {
      const static char *lowerFirst[] = {
        "h",
        "H",
        "ch",
        "Ch",
        "CH",
        "cha",
        "chA",
        "Cha",
        "ChA",
        "CHa",
        "CHA",
        "i",
        "I"
      };

      const static char *upperFirst[] = {
        "H",
        "h",
        "CH",
        "Ch",
        "ch",
        "CHA",
        "CHa",
        "ChA",
        "Cha",
        "chA",
        "cha",
        "I",
        "i"
      };
      log_verbose("mixed case test\n");
      log_verbose("lower first, case level off\n");
      genericRulesStarter("[caseFirst lower]&H<ch<<<Ch<<<CH", lowerFirst, UPRV_LENGTHOF(lowerFirst));
      log_verbose("upper first, case level off\n");
      genericRulesStarter("[caseFirst upper]&H<ch<<<Ch<<<CH", upperFirst, UPRV_LENGTHOF(upperFirst));
      log_verbose("lower first, case level on\n");
      genericRulesStarter("[caseFirst lower][caseLevel on]&H<ch<<<Ch<<<CH", lowerFirst, UPRV_LENGTHOF(lowerFirst));
      log_verbose("upper first, case level on\n");
      genericRulesStarter("[caseFirst upper][caseLevel on]&H<ch<<<Ch<<<CH", upperFirst, UPRV_LENGTHOF(upperFirst));
    }

}

static void TestIncrementalNormalize(void) {

    /*UChar baseA     =0x61;*/
    UChar baseA     =0x41;
/*    UChar baseB     = 0x42;*/
    static const UChar ccMix[]   = {0x316, 0x321, 0x300};
    /*UChar ccMix[]   = {0x61, 0x61, 0x61};*/
    /*
        0x316 is combining grave accent below, cc=220
        0x321 is combining palatalized hook below, cc=202
        0x300 is combining grave accent, cc=230
    */

#define MAXSLEN 2000
    /*int          maxSLen   = 64000;*/
    int          sLen;
    int          i;

    UCollator        *coll;
    UErrorCode       status = U_ZERO_ERROR;
    UCollationResult result;

    int32_t myQ = getTestOption(QUICK_OPTION);

    if(getTestOption(QUICK_OPTION) < 0) {
        setTestOption(QUICK_OPTION, 1);
    }

    {
        /* Test 1.  Run very long unnormalized strings, to force overflow of*/
        /*          most buffers along the way.*/
        UChar            strA[MAXSLEN+1];
        UChar            strB[MAXSLEN+1];

        coll = ucol_open("en_US", &status);
        if(status == U_FILE_ACCESS_ERROR) {
          log_data_err("Is your data around?\n");
          return;
        } else if(U_FAILURE(status)) {
          log_err("Error opening collator\n");
          return;
        }
        ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);

        /*for (sLen = 257; sLen<MAXSLEN; sLen++) {*/
        /*for (sLen = 4; sLen<MAXSLEN; sLen++) {*/
        /*for (sLen = 1000; sLen<1001; sLen++) {*/
        for (sLen = 500; sLen<501; sLen++) {
        /*for (sLen = 40000; sLen<65000; sLen+=1000) {*/
            strA[0] = baseA;
            strB[0] = baseA;
            for (i=1; i<=sLen-1; i++) {
                strA[i] = ccMix[i % 3];
                strB[sLen-i] = ccMix[i % 3];
            }
            strA[sLen]   = 0;
            strB[sLen]   = 0;

            ucol_setStrength(coll, UCOL_TERTIARY);   /* Do test with default strength, which runs*/
            doTest(coll, strA, strB, UCOL_EQUAL);    /*   optimized functions in the impl*/
            ucol_setStrength(coll, UCOL_IDENTICAL);   /* Do again with the slow, general impl.*/
            doTest(coll, strA, strB, UCOL_EQUAL);
        }
    }

    setTestOption(QUICK_OPTION, myQ);


    /*  Test 2:  Non-normal sequence in a string that extends to the last character*/
    /*         of the string.  Checks a couple of edge cases.*/

    {
        static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0};
        static const UChar strB[] = {0x41, 0xc0, 0x316, 0};
        ucol_setStrength(coll, UCOL_TERTIARY);
        doTest(coll, strA, strB, UCOL_EQUAL);
    }

    /*  Test 3:  Non-normal sequence is terminated by a surrogate pair.*/

    {
      /* New UCA  3.1.1.
       * test below used a code point from Desseret, which sorts differently
       * than d800 dc00
       */
        /*UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0xD801, 0xDC00, 0};*/
        static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0xD800, 0xDC01, 0};
        static const UChar strB[] = {0x41, 0xc0, 0x316, 0xD800, 0xDC00, 0};
        ucol_setStrength(coll, UCOL_TERTIARY);
        doTest(coll, strA, strB, UCOL_GREATER);
    }

    /*  Test 4:  Embedded nulls do not terminate a string when length is specified.*/

    {
        static const UChar strA[] = {0x41, 0x00, 0x42, 0x00};
        static const UChar strB[] = {0x41, 0x00, 0x00, 0x00};
        char  sortKeyA[50];
        char  sortKeyAz[50];
        char  sortKeyB[50];
        char  sortKeyBz[50];
        int   r;

        /* there used to be -3 here. Hmmmm.... */
        /*result = ucol_strcoll(coll, strA, -3, strB, -3);*/
        result = ucol_strcoll(coll, strA, 3, strB, 3);
        if (result != UCOL_GREATER) {
            log_err("ERROR 1 in test 4\n");
        }
        result = ucol_strcoll(coll, strA, -1, strB, -1);
        if (result != UCOL_EQUAL) {
            log_err("ERROR 2 in test 4\n");
        }

        ucol_getSortKey(coll, strA,  3, (uint8_t *)sortKeyA, sizeof(sortKeyA));
        ucol_getSortKey(coll, strA, -1, (uint8_t *)sortKeyAz, sizeof(sortKeyAz));
        ucol_getSortKey(coll, strB,  3, (uint8_t *)sortKeyB, sizeof(sortKeyB));
        ucol_getSortKey(coll, strB, -1, (uint8_t *)sortKeyBz, sizeof(sortKeyBz));

        r = strcmp(sortKeyA, sortKeyAz);
        if (r <= 0) {
            log_err("Error 3 in test 4\n");
        }
        r = strcmp(sortKeyA, sortKeyB);
        if (r <= 0) {
            log_err("Error 4 in test 4\n");
        }
        r = strcmp(sortKeyAz, sortKeyBz);
        if (r != 0) {
            log_err("Error 5 in test 4\n");
        }

        ucol_setStrength(coll, UCOL_IDENTICAL);
        ucol_getSortKey(coll, strA,  3, (uint8_t *)sortKeyA, sizeof(sortKeyA));
        ucol_getSortKey(coll, strA, -1, (uint8_t *)sortKeyAz, sizeof(sortKeyAz));
        ucol_getSortKey(coll, strB,  3, (uint8_t *)sortKeyB, sizeof(sortKeyB));
        ucol_getSortKey(coll, strB, -1, (uint8_t *)sortKeyBz, sizeof(sortKeyBz));

        r = strcmp(sortKeyA, sortKeyAz);
        if (r <= 0) {
            log_err("Error 6 in test 4\n");
        }
        r = strcmp(sortKeyA, sortKeyB);
        if (r <= 0) {
            log_err("Error 7 in test 4\n");
        }
        r = strcmp(sortKeyAz, sortKeyBz);
        if (r != 0) {
            log_err("Error 8 in test 4\n");
        }
        ucol_setStrength(coll, UCOL_TERTIARY);
    }


    /*  Test 5:  Null characters in non-normal source strings.*/

    {
        static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42, 0x00};
        static const UChar strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00, 0x00};
        char  sortKeyA[50];
        char  sortKeyAz[50];
        char  sortKeyB[50];
        char  sortKeyBz[50];
        int   r;

        result = ucol_strcoll(coll, strA, 6, strB, 6);
        if (result != UCOL_GREATER) {
            log_err("ERROR 1 in test 5\n");
        }
        result = ucol_strcoll(coll, strA, -1, strB, -1);
        if (result != UCOL_EQUAL) {
            log_err("ERROR 2 in test 5\n");
        }

        ucol_getSortKey(coll, strA,  6, (uint8_t *)sortKeyA, sizeof(sortKeyA));
        ucol_getSortKey(coll, strA, -1, (uint8_t *)sortKeyAz, sizeof(sortKeyAz));
        ucol_getSortKey(coll, strB,  6, (uint8_t *)sortKeyB, sizeof(sortKeyB));
        ucol_getSortKey(coll, strB, -1, (uint8_t *)sortKeyBz, sizeof(sortKeyBz));

        r = strcmp(sortKeyA, sortKeyAz);
        if (r <= 0) {
            log_err("Error 3 in test 5\n");
        }
        r = strcmp(sortKeyA, sortKeyB);
        if (r <= 0) {
            log_err("Error 4 in test 5\n");
        }
        r = strcmp(sortKeyAz, sortKeyBz);
        if (r != 0) {
            log_err("Error 5 in test 5\n");
        }

        ucol_setStrength(coll, UCOL_IDENTICAL);
        ucol_getSortKey(coll, strA,  6, (uint8_t *)sortKeyA, sizeof(sortKeyA));
        ucol_getSortKey(coll, strA, -1, (uint8_t *)sortKeyAz, sizeof(sortKeyAz));
        ucol_getSortKey(coll, strB,  6, (uint8_t *)sortKeyB, sizeof(sortKeyB));
        ucol_getSortKey(coll, strB, -1, (uint8_t *)sortKeyBz, sizeof(sortKeyBz));

        r = strcmp(sortKeyA, sortKeyAz);
        if (r <= 0) {
            log_err("Error 6 in test 5\n");
        }
        r = strcmp(sortKeyA, sortKeyB);
        if (r <= 0) {
            log_err("Error 7 in test 5\n");
        }
        r = strcmp(sortKeyAz, sortKeyBz);
        if (r != 0) {
            log_err("Error 8 in test 5\n");
        }
        ucol_setStrength(coll, UCOL_TERTIARY);
    }


    /*  Test 6:  Null character as base of a non-normal combining sequence.*/

    {
        static const UChar strA[] = {0x41, 0x0, 0x300, 0x316, 0x41, 0x302, 0x00};
        static const UChar strB[] = {0x41, 0x0, 0x302, 0x316, 0x41, 0x300, 0x00};

        result = ucol_strcoll(coll, strA, 5, strB, 5);
        if (result != UCOL_LESS) {
            log_err("Error 1 in test 6\n");
        }
        result = ucol_strcoll(coll, strA, -1, strB, -1);
        if (result != UCOL_EQUAL) {
            log_err("Error 2 in test 6\n");
        }
    }

    ucol_close(coll);
}



#if 0
static void TestGetCaseBit(void) {
  static const char *caseBitData[] = {
    "a", "A", "ch", "Ch", "CH",
      "\\uFF9E", "\\u0009"
  };

  static const uint8_t results[] = {
    UCOL_LOWER_CASE, UCOL_UPPER_CASE, UCOL_LOWER_CASE, UCOL_MIXED_CASE, UCOL_UPPER_CASE,
      UCOL_UPPER_CASE, UCOL_LOWER_CASE
  };

  uint32_t i, blen = 0;
  UChar b[256] = {0};
  UErrorCode status = U_ZERO_ERROR;
  UCollator *UCA = ucol_open("", &status);
  uint8_t res = 0;

  for(i = 0; i<UPRV_LENGTHOF(results); i++) {
    blen = u_unescape(caseBitData[i], b, 256);
    res = ucol_uprv_getCaseBits(UCA, b, blen, &status);
    if(results[i] != res) {
      log_err("Expected case = %02X, got %02X for %04X\n", results[i], res, b[0]);
    }
  }
}
#endif

static void TestHangulTailoring(void) {
    static const char *koreanData[] = {
        "\\uac00", "\\u4f3d", "\\u4f73", "\\u5047", "\\u50f9", "\\u52a0", "\\u53ef", "\\u5475",
            "\\u54e5", "\\u5609", "\\u5ac1", "\\u5bb6", "\\u6687", "\\u67b6", "\\u67b7", "\\u67ef",
            "\\u6b4c", "\\u73c2", "\\u75c2", "\\u7a3c", "\\u82db", "\\u8304", "\\u8857", "\\u8888",
            "\\u8a36", "\\u8cc8", "\\u8dcf", "\\u8efb", "\\u8fe6", "\\u99d5",
            "\\u4EEE", "\\u50A2", "\\u5496", "\\u54FF", "\\u5777", "\\u5B8A", "\\u659D", "\\u698E",
            "\\u6A9F", "\\u73C8", "\\u7B33", "\\u801E", "\\u8238", "\\u846D", "\\u8B0C"
    };

    const char *rules =
        "&\\uac00 <<< \\u4f3d <<< \\u4f73 <<< \\u5047 <<< \\u50f9 <<< \\u52a0 <<< \\u53ef <<< \\u5475 "
        "<<< \\u54e5 <<< \\u5609 <<< \\u5ac1 <<< \\u5bb6 <<< \\u6687 <<< \\u67b6 <<< \\u67b7 <<< \\u67ef "
        "<<< \\u6b4c <<< \\u73c2 <<< \\u75c2 <<< \\u7a3c <<< \\u82db <<< \\u8304 <<< \\u8857 <<< \\u8888 "
        "<<< \\u8a36 <<< \\u8cc8 <<< \\u8dcf <<< \\u8efb <<< \\u8fe6 <<< \\u99d5 "
        "<<< \\u4EEE <<< \\u50A2 <<< \\u5496 <<< \\u54FF <<< \\u5777 <<< \\u5B8A <<< \\u659D <<< \\u698E "
        "<<< \\u6A9F <<< \\u73C8 <<< \\u7B33 <<< \\u801E <<< \\u8238 <<< \\u846D <<< \\u8B0C";


  UErrorCode status = U_ZERO_ERROR;
  UChar rlz[2048] = { 0 };
  uint32_t rlen = u_unescape(rules, rlz, 2048);

  UCollator *coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status);
  if(status == U_FILE_ACCESS_ERROR) {
    log_data_err("Is your data around?\n");
    return;
  } else if(U_FAILURE(status)) {
    log_err("Error opening collator\n");
    return;
  }

  log_verbose("Using start of korean rules\n");

  if(U_SUCCESS(status)) {
    genericOrderingTest(coll, koreanData, UPRV_LENGTHOF(koreanData));
  } else {
    log_err("Unable to open collator with rules %s\n", rules);
  }

  ucol_close(coll);

  log_verbose("Using ko__LOTUS locale\n");
  genericLocaleStarter("ko__LOTUS", koreanData, UPRV_LENGTHOF(koreanData));
}

/*
 * The secondary/tertiary compression middle byte
 * as used by the current implementation.
 * Subject to change as the sort key compression changes.
 * See class CollationKeys.
 */
enum {
    SEC_COMMON_MIDDLE = 0x25,  /* range 05..45 */
    TER_ONLY_COMMON_MIDDLE = 0x65  /* range 05..C5 */
};

static void TestCompressOverlap(void) {
    UChar       secstr[150];
    UChar       tertstr[150];
    UErrorCode  status = U_ZERO_ERROR;
    UCollator  *coll;
    uint8_t     result[500];
    uint32_t    resultlen;
    int         count = 0;
    uint8_t    *tempptr;

    coll = ucol_open("", &status);

    if (U_FAILURE(status)) {
        log_err_status(status, "Collator can't be created -> %s\n", u_errorName(status));
        return;
    }
    while (count < 149) {
        secstr[count] = 0x0020; /* [06, 05, 05] */
        tertstr[count] = 0x0020;
        count ++;
    }

    /* top down compression ----------------------------------- */
    secstr[count] = 0x0332; /* [, 87, 05] */
    tertstr[count] = 0x3000; /* [06, 05, 07] */

    /* no compression secstr should have 150 secondary bytes, tertstr should
    have 150 tertiary bytes.
    with correct compression, secstr should have 6 secondary
    bytes (149/33 rounded up + accent), tertstr should have > 2 tertiary bytes */
    resultlen = ucol_getSortKey(coll, secstr, 150, result, UPRV_LENGTHOF(result));
    (void)resultlen;    /* Suppress set but not used warning. */
    tempptr = (uint8_t *)uprv_strchr((char *)result, 1) + 1;
    while (*(tempptr + 1) != 1) {
        /* the last secondary collation element is not checked since it is not
        part of the compression */
        if (*tempptr < SEC_COMMON_MIDDLE) {
            log_err("Secondary top down compression overlapped\n");
        }
        tempptr ++;
    }

    /* tertiary top/bottom/common for en_US is similar to the secondary
    top/bottom/common */
    resultlen = ucol_getSortKey(coll, tertstr, 150, result, UPRV_LENGTHOF(result));
    tempptr = (uint8_t *)uprv_strrchr((char *)result, 1) + 1;
    while (*(tempptr + 1) != 0) {
        /* the last secondary collation element is not checked since it is not
        part of the compression */
        if (*tempptr < TER_ONLY_COMMON_MIDDLE) {
            log_err("Tertiary top down compression overlapped\n");
        }
        tempptr ++;
    }

    /* bottom up compression ------------------------------------- */
    secstr[count] = 0;
    tertstr[count] = 0;
    resultlen = ucol_getSortKey(coll, secstr, 150, result, UPRV_LENGTHOF(result));
    tempptr = (uint8_t *)uprv_strchr((char *)result, 1) + 1;
    while (*(tempptr + 1) != 1) {
        /* the last secondary collation element is not checked since it is not
        part of the compression */
        if (*tempptr > SEC_COMMON_MIDDLE) {
            log_err("Secondary bottom up compression overlapped\n");
        }
        tempptr ++;
    }

    /* tertiary top/bottom/common for en_US is similar to the secondary
    top/bottom/common */
    resultlen = ucol_getSortKey(coll, tertstr, 150, result, UPRV_LENGTHOF(result));
    tempptr = (uint8_t *)uprv_strrchr((char *)result, 1) + 1;
    while (*(tempptr + 1) != 0) {
        /* the last secondary collation element is not checked since it is not
        part of the compression */
        if (*tempptr > TER_ONLY_COMMON_MIDDLE) {
            log_err("Tertiary bottom up compression overlapped\n");
        }
        tempptr ++;
    }

    ucol_close(coll);
}

static void TestCyrillicTailoring(void) {
  static const char *test[] = {
    "\\u0410b",
      "\\u0410\\u0306a",
      "\\u04d0A"
  };

    /* Russian overrides contractions, so this test is not valid anymore */
    /*genericLocaleStarter("ru", test, 3);*/

    // Most of the following are commented out because UCA 8.0
    // drops most of the Cyrillic contractions from the default order.
    // See CLDR ticket #7246 "root collation: remove Cyrillic contractions".

    // genericLocaleStarter("root", test, 3);
    // genericRulesStarter("&\\u0410 = \\u0410", test, 3);
    // genericRulesStarter("&Z < \\u0410", test, 3);
    genericRulesStarter("&\\u0410 = \\u0410 < \\u04d0", test, 3);
    genericRulesStarter("&Z < \\u0410 < \\u04d0", test, 3);
    // genericRulesStarter("&\\u0410 = \\u0410 < \\u0410\\u0301", test, 3);
    // genericRulesStarter("&Z < \\u0410 < \\u0410\\u0301", test, 3);
}

static void TestSuppressContractions(void) {

  static const char *testNoCont2[] = {
      "\\u0410\\u0302a",
      "\\u0410\\u0306b",
      "\\u0410c"
  };
  static const char *testNoCont[] = {
      "a\\u0410",
      "A\\u0410\\u0306",
      "\\uFF21\\u0410\\u0302"
  };

  genericRulesStarter("[suppressContractions [\\u0400-\\u047f]]", testNoCont, 3);
  genericRulesStarter("[suppressContractions [\\u0400-\\u047f]]", testNoCont2, 3);
}

static void TestContraction(void) {
    const static char *testrules[] = {
        "&A = AB / B",
        "&A = A\\u0306/\\u0306",
        "&c = ch / h"
    };
    const static UChar testdata[][2] = {
        {0x0041 /* 'A' */, 0x0042 /* 'B' */},
        {0x0041 /* 'A' */, 0x0306 /* combining breve */},
        {0x0063 /* 'c' */, 0x0068 /* 'h' */}
    };
    const static UChar testdata2[][2] = {
        {0x0063 /* 'c' */, 0x0067 /* 'g' */},
        {0x0063 /* 'c' */, 0x0068 /* 'h' */},
        {0x0063 /* 'c' */, 0x006C /* 'l' */}
    };
#if 0
    /*
     * These pairs of rule strings are not guaranteed to yield the very same mappings.
     * In fact, LDML 24 recommends an improved way of creating mappings
     * which always yields different mappings for such pairs. See
     * http://www.unicode.org/reports/tr35/tr35-33/tr35-collation.html#Orderings
     */
    const static char *testrules3[] = {
        "&z < xyz &xyzw << B",
        "&z < xyz &xyz << B / w",
        "&z < ch &achm << B",
        "&z < ch &a << B / chm",
        "&\\ud800\\udc00w << B",
        "&\\ud800\\udc00 << B / w",
        "&a\\ud800\\udc00m << B",
        "&a << B / \\ud800\\udc00m",
    };
#endif

    UErrorCode  status   = U_ZERO_ERROR;
    UCollator  *coll;
    UChar       rule[256] = {0};
    uint32_t    rlen     = 0;
    int         i;

    for (i = 0; i < UPRV_LENGTHOF(testrules); i ++) {
        UCollationElements *iter1;
        int j = 0;
        log_verbose("Rule %s for testing\n", testrules[i]);
        rlen = u_unescape(testrules[i], rule, 32);
        coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
        if (U_FAILURE(status)) {
            log_err_status(status, "Collator creation failed %s -> %s\n", testrules[i], u_errorName(status));
            return;
        }
        iter1 = ucol_openElements(coll, testdata[i], 2, &status);
        if (U_FAILURE(status)) {
            log_err("Collation iterator creation failed\n");
            return;
        }
        while (j < 2) {
            UCollationElements *iter2 = ucol_openElements(coll,
                                                         &(testdata[i][j]),
                                                         1, &status);
            int32_t ce;
            if (U_FAILURE(status)) {
                log_err("Collation iterator creation failed\n");
                return;
            }
            ce = ucol_next(iter2, &status);
            while (ce != UCOL_NULLORDER) {
                if (ucol_next(iter1, &status) != ce) {
                    log_err("Collation elements in contraction split does not match\n");
                    return;
                }
                ce = ucol_next(iter2, &status);
            }
            j ++;
            ucol_closeElements(iter2);
        }
        if (ucol_next(iter1, &status) != UCOL_NULLORDER) {
            log_err("Collation elements not exhausted\n");
            return;
        }
        ucol_closeElements(iter1);
        ucol_close(coll);
    }

    rlen = u_unescape("& a < b < c < ch < d & c = ch / h", rule, 256);
    coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
    if (ucol_strcoll(coll, testdata2[0], 2, testdata2[1], 2) != UCOL_LESS) {
        log_err("Expected \\u%04x\\u%04x < \\u%04x\\u%04x\n",
                testdata2[0][0], testdata2[0][1], testdata2[1][0],
                testdata2[1][1]);
        return;
    }
    if (ucol_strcoll(coll, testdata2[1], 2, testdata2[2], 2) != UCOL_LESS) {
        log_err("Expected \\u%04x\\u%04x < \\u%04x\\u%04x\n",
                testdata2[1][0], testdata2[1][1], testdata2[2][0],
                testdata2[2][1]);
        return;
    }
    ucol_close(coll);
#if 0  /* see above */
    for (i = 0; i < UPRV_LENGTHOF(testrules3); i += 2) {
        log_verbose("testrules3 i==%d  \"%s\" vs. \"%s\"\n", i, testrules3[i], testrules3[i + 1]);
        UCollator          *coll1,
                           *coll2;
        UCollationElements *iter1,
                           *iter2;
        UChar               ch = 0x0042 /* 'B' */;
        uint32_t            ce;
        rlen = u_unescape(testrules3[i], rule, 32);
        coll1 = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
        rlen = u_unescape(testrules3[i + 1], rule, 32);
        coll2 = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
        if (U_FAILURE(status)) {
            log_err("Collator creation failed %s\n", testrules[i]);
            return;
        }
        iter1 = ucol_openElements(coll1, &ch, 1, &status);
        iter2 = ucol_openElements(coll2, &ch, 1, &status);
        if (U_FAILURE(status)) {
            log_err("Collation iterator creation failed\n");
            return;
        }
        ce = ucol_next(iter1, &status);
        if (U_FAILURE(status)) {
            log_err("Retrieving ces failed\n");
            return;
        }
        while (ce != UCOL_NULLORDER) {
            uint32_t ce2 = (uint32_t)ucol_next(iter2, &status);
            if (ce == ce2) {
                log_verbose("CEs match: %08x\n", ce);
            } else {
                log_err("CEs do not match: %08x vs. %08x\n", ce, ce2);
                return;
            }
            ce = ucol_next(iter1, &status);
            if (U_FAILURE(status)) {
                log_err("Retrieving ces failed\n");
                return;
            }
        }
        if (ucol_next(iter2, &status) != UCOL_NULLORDER) {
            log_err("CEs not exhausted\n");
            return;
        }
        ucol_closeElements(iter1);
        ucol_closeElements(iter2);
        ucol_close(coll1);
        ucol_close(coll2);
    }
#endif
}

static void TestExpansion(void) {
    const static char *testrules[] = {
#if 0
        /*
         * This seems to have tested that M was not mapped to an expansion.
         * I believe the old builder just did that because it computed the extension CEs
         * at the very end, which was a bug.
         * Among other problems, it violated the core tailoring principle
         * by making an earlier rule depend on a later one.
         * And, of course, if M did not get an expansion, then it was primary different from K,
         * unlike what the rule &K<<M says.
         */
        "&J << K / B & K << M",
#endif
        "&J << K / B << M"
    };
    const static UChar testdata[][3] = {
        {0x004A /*'J'*/, 0x0041 /*'A'*/, 0},
        {0x004D /*'M'*/, 0x0041 /*'A'*/, 0},
        {0x004B /*'K'*/, 0x0041 /*'A'*/, 0},
        {0x004B /*'K'*/, 0x0043 /*'C'*/, 0},
        {0x004A /*'J'*/, 0x0043 /*'C'*/, 0},
        {0x004D /*'M'*/, 0x0043 /*'C'*/, 0}
    };

    UErrorCode  status   = U_ZERO_ERROR;
    UCollator  *coll;
    UChar       rule[256] = {0};
    uint32_t    rlen     = 0;
    int         i;

    for (i = 0; i < UPRV_LENGTHOF(testrules); i ++) {
        int j = 0;
        log_verbose("Rule %s for testing\n", testrules[i]);
        rlen = u_unescape(testrules[i], rule, 32);
        coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
        if (U_FAILURE(status)) {
            log_err_status(status, "Collator creation failed %s -> %s\n", testrules[i], u_errorName(status));
            return;
        }

        for (j = 0; j < 5; j ++) {
            doTest(coll, testdata[j], testdata[j + 1], UCOL_LESS);
        }
        ucol_close(coll);
    }
}

#if 0
/* this test tests the current limitations of the engine */
/* it always fail, so it is disabled by default */
static void TestLimitations(void) {
  /* recursive expansions */
  {
    static const char *rule = "&a=b/c&d=c/e";
    static const char *tlimit01[] = {"add","b","adf"};
    static const char *tlimit02[] = {"aa","b","af"};
    log_verbose("recursive expansions\n");
    genericRulesStarter(rule, tlimit01, UPRV_LENGTHOF(tlimit01));
    genericRulesStarter(rule, tlimit02, UPRV_LENGTHOF(tlimit02));
  }
  /* contractions spanning expansions */
  {
    static const char *rule = "&a<<<c/e&g<<<eh";
    static const char *tlimit01[] = {"ad","c","af","f","ch","h"};
    static const char *tlimit02[] = {"ad","c","ch","af","f","h"};
    log_verbose("contractions spanning expansions\n");
    genericRulesStarter(rule, tlimit01, UPRV_LENGTHOF(tlimit01));
    genericRulesStarter(rule, tlimit02, UPRV_LENGTHOF(tlimit02));
  }
  /* normalization: nulls in contractions */
  {
    static const char *rule = "&a<<<\\u0000\\u0302";
    static const char *tlimit01[] = {"a","\\u0000\\u0302\\u0327"};
    static const char *tlimit02[] = {"\\u0000\\u0302\\u0327","a"};
    static const UColAttribute att[] = { UCOL_DECOMPOSITION_MODE };
    static const UColAttributeValue valOn[] = { UCOL_ON };
    static const UColAttributeValue valOff[] = { UCOL_OFF };

    log_verbose("NULL in contractions\n");
    genericRulesStarterWithOptions(rule, tlimit01, 2, att, valOn, 1);
    genericRulesStarterWithOptions(rule, tlimit02, 2, att, valOn, 1);
    genericRulesStarterWithOptions(rule, tlimit01, 2, att, valOff, 1);
    genericRulesStarterWithOptions(rule, tlimit02, 2, att, valOff, 1);

  }
  /* normalization: contractions spanning normalization */
  {
    static const char *rule = "&a<<<\\u0000\\u0302";
    static const char *tlimit01[] = {"a","\\u0000\\u0302\\u0327"};
    static const char *tlimit02[] = {"\\u0000\\u0302\\u0327","a"};
    static const UColAttribute att[] = { UCOL_DECOMPOSITION_MODE };
    static const UColAttributeValue valOn[] = { UCOL_ON };
    static const UColAttributeValue valOff[] = { UCOL_OFF };

    log_verbose("contractions spanning normalization\n");
    genericRulesStarterWithOptions(rule, tlimit01, 2, att, valOn, 1);
    genericRulesStarterWithOptions(rule, tlimit02, 2, att, valOn, 1);
    genericRulesStarterWithOptions(rule, tlimit01, 2, att, valOff, 1);
    genericRulesStarterWithOptions(rule, tlimit02, 2, att, valOff, 1);

  }
  /* variable top:  */
  {
    /*static const char *rule2 = "&\\u2010<x=[variable top]<z";*/
    static const char *rule = "&\\u2010<x<[variable top]=z";
    /*static const char *rule3 = "&' '<x<[variable top]=z";*/
    static const char *tlimit01[] = {" ", "z", "zb", "a", " b", "xb", "b", "c" };
    static const char *tlimit02[] = {"-", "-x", "x","xb", "-z", "z", "zb", "-a", "a", "-b", "b", "c"};
    static const char *tlimit03[] = {" ", "xb", "z", "zb", "a", " b", "b", "c" };
    static const UColAttribute att[] = { UCOL_ALTERNATE_HANDLING, UCOL_STRENGTH };
    static const UColAttributeValue valOn[] = { UCOL_SHIFTED, UCOL_QUATERNARY };
    static const UColAttributeValue valOff[] = { UCOL_NON_IGNORABLE, UCOL_TERTIARY };

    log_verbose("variable top\n");
    genericRulesStarterWithOptions(rule, tlimit03, UPRV_LENGTHOF(tlimit03), att, valOn, UPRV_LENGTHOF(att));
    genericRulesStarterWithOptions(rule, tlimit01, UPRV_LENGTHOF(tlimit01), att, valOn, UPRV_LENGTHOF(att));
    genericRulesStarterWithOptions(rule, tlimit02, UPRV_LENGTHOF(tlimit02), att, valOn, UPRV_LENGTHOF(att));
    genericRulesStarterWithOptions(rule, tlimit01, UPRV_LENGTHOF(tlimit01), att, valOff, UPRV_LENGTHOF(att));
    genericRulesStarterWithOptions(rule, tlimit02, UPRV_LENGTHOF(tlimit02), att, valOff, UPRV_LENGTHOF(att));

  }
  /* case level */
  {
    static const char *rule = "&c<ch<<<cH<<<Ch<<<CH";
    static const char *tlimit01[] = {"c","CH","Ch","cH","ch"};
    static const char *tlimit02[] = {"c","CH","cH","Ch","ch"};
    static const UColAttribute att[] = { UCOL_CASE_FIRST};
    static const UColAttributeValue valOn[] = { UCOL_UPPER_FIRST};
    /*static const UColAttributeValue valOff[] = { UCOL_OFF};*/
    log_verbose("case level\n");
    genericRulesStarterWithOptions(rule, tlimit01, UPRV_LENGTHOF(tlimit01), att, valOn, UPRV_LENGTHOF(att));
    genericRulesStarterWithOptions(rule, tlimit02, UPRV_LENGTHOF(tlimit02), att, valOn, UPRV_LENGTHOF(att));
    /*genericRulesStarterWithOptions(rule, tlimit01, UPRV_LENGTHOF(tlimit01), att, valOff, UPRV_LENGTHOF(att));*/
    /*genericRulesStarterWithOptions(rule, tlimit02, UPRV_LENGTHOF(tlimit02), att, valOff, UPRV_LENGTHOF(att));*/
  }

}
#endif

static void TestBocsuCoverage(void) {
  UErrorCode status = U_ZERO_ERROR;
  const char *testString = "\\u0041\\u0441\\u4441\\U00044441\\u4441\\u0441\\u0041";
  UChar       test[256] = {0};
  uint32_t    tlen     = u_unescape(testString, test, 32);
  uint8_t key[256]     = {0};
  uint32_t klen         = 0;

  UCollator *coll = ucol_open("", &status);
  if(U_SUCCESS(status)) {
  ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &status);

  klen = ucol_getSortKey(coll, test, tlen, key, 256);
  (void)klen;    /* Suppress set but not used warning. */

  ucol_close(coll);
  } else {
    log_data_err("Couldn't open UCA\n");
  }
}

static void TestVariableTopSetting(void) {
  UErrorCode status = U_ZERO_ERROR;
  uint32_t varTopOriginal = 0, varTop1, varTop2;
  UCollator *coll = ucol_open("", &status);
  if(U_SUCCESS(status)) {

  static const UChar nul = 0;
  static const UChar space = 0x20;
  static const UChar dot = 0x2e;  /* punctuation */
  static const UChar degree = 0xb0;  /* symbol */
  static const UChar dollar = 0x24;  /* currency symbol */
  static const UChar zero = 0x30;  /* digit */

  varTopOriginal = ucol_getVariableTop(coll, &status);
  log_verbose("ucol_getVariableTop(root) -> %08x\n", varTopOriginal);
  ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);

  varTop1 = ucol_setVariableTop(coll, &space, 1, &status);
  varTop2 = ucol_getVariableTop(coll, &status);
  log_verbose("ucol_setVariableTop(space) -> %08x\n", varTop1);
  if(U_FAILURE(status) || varTop1 != varTop2 ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      ucol_equal(coll, &nul, 0, &dot, 1) ||
      ucol_equal(coll, &nul, 0, &degree, 1) ||
      ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &space, 1, &dot, 1)) {
    log_err("ucol_setVariableTop(space) did not work - %s\n", u_errorName(status));
  }

  varTop1 = ucol_setVariableTop(coll, &dot, 1, &status);
  varTop2 = ucol_getVariableTop(coll, &status);
  log_verbose("ucol_setVariableTop(dot) -> %08x\n", varTop1);
  if(U_FAILURE(status) || varTop1 != varTop2 ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      !ucol_equal(coll, &nul, 0, &dot, 1) ||
      ucol_equal(coll, &nul, 0, &degree, 1) ||
      ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &dot, 1, &degree, 1)) {
    log_err("ucol_setVariableTop(dot) did not work - %s\n", u_errorName(status));
  }

  varTop1 = ucol_setVariableTop(coll, &degree, 1, &status);
  varTop2 = ucol_getVariableTop(coll, &status);
  log_verbose("ucol_setVariableTop(degree) -> %08x\n", varTop1);
  if(U_FAILURE(status) || varTop1 != varTop2 ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      !ucol_equal(coll, &nul, 0, &dot, 1) ||
      !ucol_equal(coll, &nul, 0, &degree, 1) ||
      ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &degree, 1, &dollar, 1)) {
    log_err("ucol_setVariableTop(degree) did not work - %s\n", u_errorName(status));
  }

  varTop1 = ucol_setVariableTop(coll, &dollar, 1, &status);
  varTop2 = ucol_getVariableTop(coll, &status);
  log_verbose("ucol_setVariableTop(dollar) -> %08x\n", varTop1);
  if(U_FAILURE(status) || varTop1 != varTop2 ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      !ucol_equal(coll, &nul, 0, &dot, 1) ||
      !ucol_equal(coll, &nul, 0, &degree, 1) ||
      !ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &dollar, 1, &zero, 1)) {
    log_err("ucol_setVariableTop(dollar) did not work - %s\n", u_errorName(status));
  }

  log_verbose("Testing setting variable top to contractions\n");
  {
    UChar first[4] = { 0 };
    first[0] = 0x0040;
    first[1] = 0x0050;
    first[2] = 0x0000;

    status = U_ZERO_ERROR;
    ucol_setVariableTop(coll, first, -1, &status);

    if(U_SUCCESS(status)) {
      log_err("Invalid contraction succeeded in setting variable top!\n");
    }

  }

  log_verbose("Test restoring variable top\n");

  status = U_ZERO_ERROR;
  ucol_restoreVariableTop(coll, varTopOriginal, &status);
  if(varTopOriginal != ucol_getVariableTop(coll, &status)) {
    log_err("Couldn't restore old variable top\n");
  }

  log_verbose("Testing calling with error set\n");

  status = U_INTERNAL_PROGRAM_ERROR;
  varTop1 = ucol_setVariableTop(coll, &space, 1, &status);
  varTop2 = ucol_getVariableTop(coll, &status);
  ucol_restoreVariableTop(coll, varTop2, &status);
  varTop1 = ucol_setVariableTop(NULL, &dot, 1, &status);
  varTop2 = ucol_getVariableTop(NULL, &status);
  ucol_restoreVariableTop(NULL, varTop2, &status);
  if(status != U_INTERNAL_PROGRAM_ERROR) {
    log_err("Bad reaction to passed error!\n");
  }
  ucol_close(coll);
  } else {
    log_data_err("Couldn't open UCA collator\n");
  }
}

static void TestMaxVariable(void) {
  UErrorCode status = U_ZERO_ERROR;
  UColReorderCode oldMax, max;
  UCollator *coll;

  static const UChar nul = 0;
  static const UChar space = 0x20;
  static const UChar dot = 0x2e;  /* punctuation */
  static const UChar degree = 0xb0;  /* symbol */
  static const UChar dollar = 0x24;  /* currency symbol */
  static const UChar zero = 0x30;  /* digit */

  coll = ucol_open("", &status);
  if(U_FAILURE(status)) {
    log_data_err("Couldn't open root collator\n");
    return;
  }

  oldMax = ucol_getMaxVariable(coll);
  log_verbose("ucol_getMaxVariable(root) -> %04x\n", oldMax);
  ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);

  ucol_setMaxVariable(coll, UCOL_REORDER_CODE_SPACE, &status);
  max = ucol_getMaxVariable(coll);
  log_verbose("ucol_setMaxVariable(space) -> %04x\n", max);
  if(U_FAILURE(status) || max != UCOL_REORDER_CODE_SPACE ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      ucol_equal(coll, &nul, 0, &dot, 1) ||
      ucol_equal(coll, &nul, 0, &degree, 1) ||
      ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &space, 1, &dot, 1)) {
    log_err("ucol_setMaxVariable(space) did not work - %s\n", u_errorName(status));
  }

  ucol_setMaxVariable(coll, UCOL_REORDER_CODE_PUNCTUATION, &status);
  max = ucol_getMaxVariable(coll);
  log_verbose("ucol_setMaxVariable(punctuation) -> %04x\n", max);
  if(U_FAILURE(status) || max != UCOL_REORDER_CODE_PUNCTUATION ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      !ucol_equal(coll, &nul, 0, &dot, 1) ||
      ucol_equal(coll, &nul, 0, &degree, 1) ||
      ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &dot, 1, &degree, 1)) {
    log_err("ucol_setMaxVariable(punctuation) did not work - %s\n", u_errorName(status));
  }

  ucol_setMaxVariable(coll, UCOL_REORDER_CODE_SYMBOL, &status);
  max = ucol_getMaxVariable(coll);
  log_verbose("ucol_setMaxVariable(symbol) -> %04x\n", max);
  if(U_FAILURE(status) || max != UCOL_REORDER_CODE_SYMBOL ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      !ucol_equal(coll, &nul, 0, &dot, 1) ||
      !ucol_equal(coll, &nul, 0, &degree, 1) ||
      ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &degree, 1, &dollar, 1)) {
    log_err("ucol_setMaxVariable(symbol) did not work - %s\n", u_errorName(status));
  }

  ucol_setMaxVariable(coll, UCOL_REORDER_CODE_CURRENCY, &status);
  max = ucol_getMaxVariable(coll);
  log_verbose("ucol_setMaxVariable(currency) -> %04x\n", max);
  if(U_FAILURE(status) || max != UCOL_REORDER_CODE_CURRENCY ||
      !ucol_equal(coll, &nul, 0, &space, 1) ||
      !ucol_equal(coll, &nul, 0, &dot, 1) ||
      !ucol_equal(coll, &nul, 0, &degree, 1) ||
      !ucol_equal(coll, &nul, 0, &dollar, 1) ||
      ucol_equal(coll, &nul, 0, &zero, 1) ||
      ucol_greaterOrEqual(coll, &dollar, 1, &zero, 1)) {
    log_err("ucol_setMaxVariable(currency) did not work - %s\n", u_errorName(status));
  }

  log_verbose("Test restoring maxVariable\n");
  status = U_ZERO_ERROR;
  ucol_setMaxVariable(coll, oldMax, &status);
  if(oldMax != ucol_getMaxVariable(coll)) {
    log_err("Couldn't restore old maxVariable\n");
  }

  log_verbose("Testing calling with error set\n");
  status = U_INTERNAL_PROGRAM_ERROR;
  ucol_setMaxVariable(coll, UCOL_REORDER_CODE_SPACE, &status);
  max = ucol_getMaxVariable(coll);
  if(max != oldMax || status != U_INTERNAL_PROGRAM_ERROR) {
    log_err("Bad reaction to passed error!\n");
  }
  ucol_close(coll);
}

static void TestNonChars(void) {
  static const char *test[] = {
      "\\u0000",  /* ignorable */
      "\\uFFFE",  /* special merge-sort character with minimum non-ignorable weights */
      "\\uFDD0", "\\uFDEF",
      "\\U0001FFFE", "\\U0001FFFF",  /* UCA 6.0: noncharacters are treated like unassigned, */
      "\\U0002FFFE", "\\U0002FFFF",  /* not like ignorable. */
      "\\U0003FFFE", "\\U0003FFFF",
      "\\U0004FFFE", "\\U0004FFFF",
      "\\U0005FFFE", "\\U0005FFFF",
      "\\U0006FFFE", "\\U0006FFFF",
      "\\U0007FFFE", "\\U0007FFFF",
      "\\U0008FFFE", "\\U0008FFFF",
      "\\U0009FFFE", "\\U0009FFFF",
      "\\U000AFFFE", "\\U000AFFFF",
      "\\U000BFFFE", "\\U000BFFFF",
      "\\U000CFFFE", "\\U000CFFFF",
      "\\U000DFFFE", "\\U000DFFFF",
      "\\U000EFFFE", "\\U000EFFFF",
      "\\U000FFFFE", "\\U000FFFFF",
      "\\U0010FFFE", "\\U0010FFFF",
      "\\uFFFF"  /* special character with maximum primary weight */
  };
  UErrorCode status = U_ZERO_ERROR;
  UCollator *coll = ucol_open("en_US", &status);

  log_verbose("Test non characters\n");

  if(U_SUCCESS(status)) {
    genericOrderingTestWithResult(coll, test, 35, UCOL_LESS);
  } else {
    log_err_status(status, "Unable to open collator\n");
  }

  ucol_close(coll);
}

static void TestExtremeCompression(void) {
  static char *test[4];
  int32_t j = 0, i = 0;

  for(i = 0; i<4; i++) {
    test[i] = (char *)malloc(2048*sizeof(char));
  }

  for(j = 20; j < 500; j++) {
    for(i = 0; i<4; i++) {
      uprv_memset(test[i], 'a', (j-1)*sizeof(char));
      test[i][j-1] = (char)('a'+i);
      test[i][j] = 0;
    }
    genericLocaleStarter("en_US", (const char **)test, 4);
  }


  for(i = 0; i<4; i++) {
    free(test[i]);
  }
}

#if 0
static void TestExtremeCompression(void) {
  static char *test[4];
  int32_t j = 0, i = 0;
  UErrorCode status = U_ZERO_ERROR;
  UCollator *coll = ucol_open("en_US", status);
  for(i = 0; i<4; i++) {
    test[i] = (char *)malloc(2048*sizeof(char));
  }
  for(j = 10; j < 2048; j++) {
    for(i = 0; i<4; i++) {
      uprv_memset(test[i], 'a', (j-2)*sizeof(char));
      test[i][j-1] = (char)('a'+i);
      test[i][j] = 0;
    }
  }
  genericLocaleStarter("en_US", (const char **)test, 4);

  for(j = 10; j < 2048; j++) {
    for(i = 0; i<1; i++) {
      uprv_memset(test[i], 'a', (j-1)*sizeof(char));
      test[i][j] = 0;
    }
  }
  for(i = 0; i<4; i++) {
    free(test[i]);
  }
}
#endif

static void TestSurrogates(void) {
  static const char *test[] = {
    "z","\\ud900\\udc25",  "\\ud805\\udc50",
       "\\ud800\\udc00y",  "\\ud800\\udc00r",
       "\\ud800\\udc00f",  "\\ud800\\udc00",
       "\\ud800\\udc00c", "\\ud800\\udc00b",
       "\\ud800\\udc00fa", "\\ud800\\udc00fb",
       "\\ud800\\udc00a",
       "c", "b"
  };

  static const char *rule =
    "&z < \\ud900\\udc25   < \\ud805\\udc50"
       "< \\ud800\\udc00y  < \\ud800\\udc00r"
       "< \\ud800\\udc00f  << \\ud800\\udc00"
       "< \\ud800\\udc00fa << \\ud800\\udc00fb"
       "< \\ud800\\udc00a  < c < b" ;

  genericRulesStarter(rule, test, 14);
}

/* This is a test for prefix implementation, used by JIS X 4061 collation rules */
static void TestPrefix(void) {
  uint32_t i;

  static const struct {
    const char *rules;
    const char *data[50];
    const uint32_t len;
  } tests[] = {
    { "&z <<< z|a",
      {"zz", "za"}, 2 },

    { "&z <<< z|   a",
      {"zz", "za"}, 2 },
    { "[strength I]"
      "&a=\\ud900\\udc25"
      "&z<<<\\ud900\\udc25|a",
      {"aa", "az", "\\ud900\\udc25z", "\\ud900\\udc25a", "zz"}, 4 },
  };


  for(i = 0; i<UPRV_LENGTHOF(tests); i++) {
    genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len);
  }
}

/* This test uses data supplied by Masashiko Maedera to test the implementation */
/* JIS X 4061 collation order implementation                                   */
static void TestNewJapanese(void) {

  static const char * const test1[] = {
      "\\u30b7\\u30e3\\u30fc\\u30ec",
      "\\u30b7\\u30e3\\u30a4",
      "\\u30b7\\u30e4\\u30a3",
      "\\u30b7\\u30e3\\u30ec",
      "\\u3061\\u3087\\u3053",
      "\\u3061\\u3088\\u3053",
      "\\u30c1\\u30e7\\u30b3\\u30ec\\u30fc\\u30c8",
      "\\u3066\\u30fc\\u305f",
      "\\u30c6\\u30fc\\u30bf",
      "\\u30c6\\u30a7\\u30bf",
      "\\u3066\\u3048\\u305f",
      "\\u3067\\u30fc\\u305f",
      "\\u30c7\\u30fc\\u30bf",
      "\\u30c7\\u30a7\\u30bf",
      "\\u3067\\u3048\\u305f",
      "\\u3066\\u30fc\\u305f\\u30fc",
      "\\u30c6\\u30fc\\u30bf\\u30a1",
      "\\u30c6\\u30a7\\u30bf\\u30fc",
      "\\u3066\\u3047\\u305f\\u3041",
      "\\u3066\\u3048\\u305f\\u30fc",
      "\\u3067\\u30fc\\u305f\\u30fc",
      "\\u30c7\\u30fc\\u30bf\\u30a1",
      "\\u3067\\u30a7\\u305f\\u30a1",
      "\\u30c7\\u3047\\u30bf\\u3041",
      "\\u30c7\\u30a8\\u30bf\\u30a2",
      "\\u3072\\u3086",
      "\\u3073\\u3085\\u3042",
      "\\u3074\\u3085\\u3042",
      "\\u3073\\u3085\\u3042\\u30fc",
      "\\u30d3\\u30e5\\u30a2\\u30fc",
      "\\u3074\\u3085\\u3042\\u30fc",
      "\\u30d4\\u30e5\\u30a2\\u30fc",
      "\\u30d2\\u30e5\\u30a6",
      "\\u30d2\\u30e6\\u30a6",
      "\\u30d4\\u30e5\\u30a6\\u30a2",
      "\\u3073\\u3085\\u30fc\\u3042\\u30fc",
      "\\u30d3\\u30e5\\u30fc\\u30a2\\u30fc",
      "\\u30d3\\u30e5\\u30a6\\u30a2\\u30fc",
      "\\u3072\\u3085\\u3093",
      "\\u3074\\u3085\\u3093",
      "\\u3075\\u30fc\\u308a",
      "\\u30d5\\u30fc\\u30ea",
      "\\u3075\\u3045\\u308a",
      "\\u3075\\u30a5\\u308a",
      "\\u3075\\u30a5\\u30ea",
      "\\u30d5\\u30a6\\u30ea",
      "\\u3076\\u30fc\\u308a",
      "\\u30d6\\u30fc\\u30ea",
      "\\u3076\\u3045\\u308a",
      "\\u30d6\\u30a5\\u308a",
      "\\u3077\\u3046\\u308a",
      "\\u30d7\\u30a6\\u30ea",
      "\\u3075\\u30fc\\u308a\\u30fc",
      "\\u30d5\\u30a5\\u30ea\\u30fc",
      "\\u3075\\u30a5\\u308a\\u30a3",
      "\\u30d5\\u3045\\u308a\\u3043",
      "\\u30d5\\u30a6\\u30ea\\u30fc",
      "\\u3075\\u3046\\u308a\\u3043",
      "\\u30d6\\u30a6\\u30ea\\u30a4",
      "\\u3077\\u30fc\\u308a\\u30fc",
      "\\u3077\\u30a5\\u308a\\u30a4",
      "\\u3077\\u3046\\u308a\\u30fc",
      "\\u30d7\\u30a6\\u30ea\\u30a4",
      "\\u30d5\\u30fd",
      "\\u3075\\u309e",
      "\\u3076\\u309d",
      "\\u3076\\u3075",
      "\\u3076\\u30d5",
      "\\u30d6\\u3075",
      "\\u30d6\\u30d5",
      "\\u3076\\u309e",
      "\\u3076\\u3077",
      "\\u30d6\\u3077",
      "\\u3077\\u309d",
      "\\u30d7\\u30fd",
      "\\u3077\\u3075",
};

  static const char *test2[] = {
    "\\u306f\\u309d", /* H\\u309d */
    "\\u30cf\\u30fd", /* K\\u30fd */
    "\\u306f\\u306f", /* HH */
    "\\u306f\\u30cf", /* HK */
    "\\u30cf\\u30cf", /* KK */
    "\\u306f\\u309e", /* H\\u309e */
    "\\u30cf\\u30fe", /* K\\u30fe */
    "\\u306f\\u3070", /* HH\\u309b */
    "\\u30cf\\u30d0", /* KK\\u309b */
    "\\u306f\\u3071", /* HH\\u309c */
    "\\u30cf\\u3071", /* KH\\u309c */
    "\\u30cf\\u30d1", /* KK\\u309c */
    "\\u3070\\u309d", /* H\\u309b\\u309d */
    "\\u30d0\\u30fd", /* K\\u309b\\u30fd */
    "\\u3070\\u306f", /* H\\u309bH */
    "\\u30d0\\u30cf", /* K\\u309bK */
    "\\u3070\\u309e", /* H\\u309b\\u309e */
    "\\u30d0\\u30fe", /* K\\u309b\\u30fe */
    "\\u3070\\u3070", /* H\\u309bH\\u309b */
    "\\u30d0\\u3070", /* K\\u309bH\\u309b */
    "\\u30d0\\u30d0", /* K\\u309bK\\u309b */
    "\\u3070\\u3071", /* H\\u309bH\\u309c */
    "\\u30d0\\u30d1", /* K\\u309bK\\u309c */
    "\\u3071\\u309d", /* H\\u309c\\u309d */
    "\\u30d1\\u30fd", /* K\\u309c\\u30fd */
    "\\u3071\\u306f", /* H\\u309cH */
    "\\u30d1\\u30cf", /* K\\u309cK */
    "\\u3071\\u3070", /* H\\u309cH\\u309b */
    "\\u3071\\u30d0", /* H\\u309cK\\u309b */
    "\\u30d1\\u30d0", /* K\\u309cK\\u309b */
    "\\u3071\\u3071", /* H\\u309cH\\u309c */
    "\\u30d1\\u30d1", /* K\\u309cK\\u309c */
  };
  /*
  static const char *test3[] = {
    "\\u221er\\u221e",
    "\\u221eR#",
    "\\u221et\\u221e",
    "#r\\u221e",
    "#R#",
    "#t%",
    "#T%",
    "8t\\u221e",
    "8T\\u221e",
    "8t#",
    "8T#",
    "8t%",
    "8T%",
    "8t8",
    "8T8",
    "\\u03c9r\\u221e",
    "\\u03a9R%",
    "rr\\u221e",
    "rR\\u221e",
    "Rr\\u221e",
    "RR\\u221e",
    "RT%",
    "rt8",
    "tr\\u221e",
    "tr8",
    "TR8",
    "tt8",
    "\\u30b7\\u30e3\\u30fc\\u30ec",
  };
  */
  static const UColAttribute att[] = { UCOL_STRENGTH };
  static const UColAttributeValue val[] = { UCOL_QUATERNARY };

  static const UColAttribute attShifted[] = { UCOL_STRENGTH, UCOL_ALTERNATE_HANDLING};
  static const UColAttributeValue valShifted[] = { UCOL_QUATERNARY, UCOL_SHIFTED };

  genericLocaleStarterWithOptions("ja", test1, UPRV_LENGTHOF(test1), att, val, 1);
  genericLocaleStarterWithOptions("ja", test2, UPRV_LENGTHOF(test2), att, val, 1);
  /*genericLocaleStarter("ja", test3, UPRV_LENGTHOF(test3));*/
  genericLocaleStarterWithOptions("ja", test1, UPRV_LENGTHOF(test1), attShifted, valShifted, 2);
  genericLocaleStarterWithOptions("ja", test2, UPRV_LENGTHOF(test2), attShifted, valShifted, 2);
}

static void TestStrCollIdenticalPrefix(void) {
  const char* rule = "&\\ud9b0\\udc70=\\ud9b0\\udc71";
  const char* test[] = {
    "ab\\ud9b0\\udc70",
    "ab\\ud9b0\\udc71"
  };
  genericRulesStarterWithResult(rule, test, UPRV_LENGTHOF(test), UCOL_EQUAL);
}
/* Contractions should have all their canonically equivalent */
/* strings included */
static void TestContractionClosure(void) {
  static const struct {
    const char *rules;
    const char *data[10];
    const uint32_t len;
  } tests[] = {
    {   "&b=\\u00e4\\u00e4",
      { "b", "\\u00e4\\u00e4", "a\\u0308a\\u0308", "\\u00e4a\\u0308", "a\\u0308\\u00e4" }, 5},
    {   "&b=\\u00C5",
      { "b", "\\u00C5", "A\\u030A", "\\u212B" }, 4},
  };
  uint32_t i;


  for(i = 0; i<UPRV_LENGTHOF(tests); i++) {
    genericRulesStarterWithResult(tests[i].rules, tests[i].data, tests[i].len, UCOL_EQUAL);
  }
}

/* This tests also fails*/
static void TestBeforePrefixFailure(void) {
  static const struct {
    const char *rules;
    const char *data[10];
    const uint32_t len;
  } tests[] = {
    { "&g <<< a"
      "&[before 3]\\uff41 <<< x",
      {"x", "\\uff41"}, 2 },
    {   "&\\u30A7=\\u30A7=\\u3047=\\uff6a"
        "&\\u30A8=\\u30A8=\\u3048=\\uff74"
        "&[before 3]\\u30a7<<<\\u30a9",
      {"\\u30a9", "\\u30a7"}, 2 },
    {   "&[before 3]\\u30a7<<<\\u30a9"
        "&\\u30A7=\\u30A7=\\u3047=\\uff6a"
        "&\\u30A8=\\u30A8=\\u3048=\\uff74",
      {"\\u30a9", "\\u30a7"}, 2 },
  };
  uint32_t i;


  for(i = 0; i<UPRV_LENGTHOF(tests); i++) {
    genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len);
  }

#if 0
  const char* rule1 =
        "&\\u30A7=\\u30A7=\\u3047=\\uff6a"
        "&\\u30A8=\\u30A8=\\u3048=\\uff74"
        "&[before 3]\\u30a7<<<\\u30c6|\\u30fc";
  const char* rule2 =
        "&[before 3]\\u30a7<<<\\u30c6|\\u30fc"
        "&\\u30A7=\\u30A7=\\u3047=\\uff6a"
        "&\\u30A8=\\u30A8=\\u3048=\\uff74";
  const char* test[] = {
      "\\u30c6\\u30fc\\u30bf",
      "\\u30c6\\u30a7\\u30bf",
  };
  genericRulesStarter(rule1, test, UPRV_LENGTHOF(test));
  genericRulesStarter(rule2, test, UPRV_LENGTHOF(test));
/* this piece of code should be in some sort of verbose mode     */
/* it gets the collation elements for elements and prints them   */
/* This is useful when trying to see whether the problem is      */
  {
    UErrorCode status = U_ZERO_ERROR;
    uint32_t i = 0;
    UCollationElements *it = NULL;
    uint32_t CE;
    UChar string[256];
    uint32_t uStringLen;
    UCollator *coll = NULL;

    uStringLen = u_unescape(rule1, string, 256);

    coll = ucol_openRules(string, uStringLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);

    /*coll = ucol_open("ja_JP_JIS", &status);*/
    it = ucol_openElements(coll, string, 0, &status);

    for(i = 0; i < UPRV_LENGTHOF(test); i++) {
      log_verbose("%s\n", test[i]);
      uStringLen = u_unescape(test[i], string, 256);
      ucol_setText(it, string, uStringLen, &status);

      while((CE=ucol_next(it, &status)) != UCOL_NULLORDER) {
        log_verbose("%08X\n", CE);
      }
      log_verbose("\n");

    }

    ucol_closeElements(it);
    ucol_close(coll);
  }
#endif
}

static void TestPrefixCompose(void) {
  const char* rule1 =
        "&\\u30a7<<<\\u30ab|\\u30fc=\\u30ac|\\u30fc";
  /*
  const char* test[] = {
      "\\u30c6\\u30fc\\u30bf",
      "\\u30c6\\u30a7\\u30bf",
  };
  */
  {
    UErrorCode status = U_ZERO_ERROR;
    /*uint32_t i = 0;*/
    /*UCollationElements *it = NULL;*/
/*    uint32_t CE;*/
    UChar string[256];
    uint32_t uStringLen;
    UCollator *coll = NULL;

    uStringLen = u_unescape(rule1, string, 256);

    coll = ucol_openRules(string, uStringLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
    ucol_close(coll);
  }


}

/*
[last variable] last variable value
[last primary ignorable] largest CE for primary ignorable
[last secondary ignorable] largest CE for secondary ignorable
[last tertiary ignorable] largest CE for tertiary ignorable
[top] guaranteed to be above all implicit CEs, for now and in the future (in 1.8)
*/

static void TestRuleOptions(void) {
  /* values here are hardcoded and are correct for the current UCA
   * when the UCA changes, one might be forced to change these
   * values.
   */

  /*
   * These strings contain the last character before [variable top]
   * and the first and second characters (by primary weights) after it.
   * See FractionalUCA.txt. For example:
      [last variable [0C FE, 05, 05]] # U+10A7F OLD SOUTH ARABIAN NUMERIC INDICATOR
      [variable top = 0C FE]
      [first regular [0D 0A, 05, 05]] # U+0060 GRAVE ACCENT
     and
      00B4; [0D 0C, 05, 05]
   *
   * Note: Starting with UCA 6.0, the [variable top] collation element
   * is not the weight of any character or string,
   * which means that LAST_VARIABLE_CHAR_STRING sorts before [last variable].
   */
#define LAST_VARIABLE_CHAR_STRING "\\U00010A7F"
#define FIRST_REGULAR_CHAR_STRING "\\u0060"
#define SECOND_REGULAR_CHAR_STRING "\\u00B4"

  /*
   * This string has to match the character that has the [last regular] weight
   * which changes with each UCA version.
   * See the bottom of FractionalUCA.txt which says something like
      [last regular [7A FE, 05, 05]] # U+1342E EGYPTIAN HIEROGLYPH AA032
   *
   * Note: Starting with UCA 6.0, the [last regular] collation element
   * is not the weight of any character or string,
   * which means that LAST_REGULAR_CHAR_STRING sorts before [last regular].
   */
#define LAST_REGULAR_CHAR_STRING "\\U0001342E"

  static const struct {
    const char *rules;
    const char *data[10];
    const uint32_t len;
  } tests[] = {
#if 0
    /* "you cannot go before ...": The parser now sets an error for such nonsensical rules. */
    /* - all befores here amount to zero */
    { "&[before 3][first tertiary ignorable]<<<a",
        { "\\u0000", "a"}, 2
    }, /* you cannot go before first tertiary ignorable */

    { "&[before 3][last tertiary ignorable]<<<a",
        { "\\u0000", "a"}, 2
    }, /* you cannot go before last tertiary ignorable */
#endif
    /*
     * However, there is a real secondary ignorable (artificial addition in FractionalUCA.txt),
     * and it *is* possible to "go before" that.
     */
    { "&[before 3][first secondary ignorable]<<<a",
        { "\\u0000", "a"}, 2
    },

    { "&[before 3][last secondary ignorable]<<<a",
        { "\\u0000", "a"}, 2
    },

    /* 'normal' befores */

    /*
     * Note: With a "SPACE first primary" boundary CE in FractionalUCA.txt,
     * it is not possible to tailor &[first primary ignorable]<a or &[last primary ignorable]<a
     * because there is no tailoring space before that boundary.
     * Made the tests work by tailoring to a space instead.
     */
    { "&[before 3][first primary ignorable]<<<c<<<b &' '<a",  /* was &[first primary ignorable]<a */
        {  "c", "b", "\\u0332", "a" }, 4
    },

    /* we don't have a code point that corresponds to
     * the last primary ignorable
     */
    { "&[before 3][last primary ignorable]<<<c<<<b &' '<a",  /* was &[last primary ignorable]<a */
        {  "\\u0332", "\\u20e3", "c", "b", "a" }, 5
    },

    { "&[before 3][first variable]<<<c<<<b &[first variable]<a",
        {  "c", "b", "\\u0009", "a", "\\u000a" }, 5
    },

    { "&[last variable]<a &[before 3][last variable]<<<c<<<b ",
        { LAST_VARIABLE_CHAR_STRING, "c", "b", /* [last variable] */ "a", FIRST_REGULAR_CHAR_STRING }, 5
    },

    { "&[first regular]<a"
      "&[before 1][first regular]<b",
      { "b", FIRST_REGULAR_CHAR_STRING, "a", SECOND_REGULAR_CHAR_STRING }, 4
    },

    { "&[before 1][last regular]<b"
      "&[last regular]<a",
        { LAST_REGULAR_CHAR_STRING, "b", /* [last regular] */ "a", "\\u4e00" }, 4
    },

    { "&[before 1][first implicit]<b"
      "&[first implicit]<a",
        { "b", "\\u4e00", "a", "\\u4e01"}, 4
    },
#if 0  /* The current builder does not support tailoring to unassigned-implicit CEs (seems unnecessary, adds complexity). */
    { "&[before 1][last implicit]<b"
      "&[last implicit]<a",
        { "b", "\\U0010FFFD", "a" }, 3
    },
#endif
    { "&[last variable]<z"
      "&' '<x"  /* was &[last primary ignorable]<x, see above */
      "&[last secondary ignorable]<<y"
      "&[last tertiary ignorable]<<<w"
      "&[top]<u",
      {"\\ufffb",  "w", "y", "\\u20e3", "x", LAST_VARIABLE_CHAR_STRING, "z", "u"}, 7
    }

  };
  uint32_t i;

  for(i = 0; i<UPRV_LENGTHOF(tests); i++) {
    genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len);
  }
}


static void TestOptimize(void) {
  /* this is not really a test - just trying out
   * whether copying of UCA contents will fail
   * Cannot really test, since the functionality
   * remains the same.
   */
  static const struct {
    const char *rules;
    const char *data[10];
    const uint32_t len;
  } tests[] = {
    /* - all befores here amount to zero */
    { "[optimize [\\uAC00-\\uD7FF]]",
    { "a", "b"}, 2}
  };
  uint32_t i;

  for(i = 0; i<UPRV_LENGTHOF(tests); i++) {
    genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len);
  }
}

/*
cycheng@ca.ibm.c... we got inconsistent results when using the UTF-16BE iterator and the UTF-8 iterator.
weiv    ucol_strcollIter?
cycheng@ca.ibm.c... e.g. s1 = 0xfffc0062, and s2 = d8000021
weiv    these are the input strings?
cycheng@ca.ibm.c... yes, using the utf-16 iterator and UCA with normalization on, we have s1 > s2
weiv    will check - could be a problem with utf-8 iterator
cycheng@ca.ibm.c... but if we use the utf-8 iterator, i.e. s1 = efbfbc62 and s2 = eda08021, we have s1 < s2
weiv    hmmm
cycheng@ca.ibm.c... note that we have a standalone high surrogate
weiv    that doesn't sound right
cycheng@ca.ibm.c... we got the same inconsistent results on AIX and Win2000
weiv    so you have two strings, you convert them to utf-8 and to utf-16BE
cycheng@ca.ibm.c... yes
weiv    and then do the comparison
cycheng@ca.ibm.c... in one case, the input strings are in utf8, and in the other case the input strings are in utf-16be
weiv    utf-16 strings look like a little endian ones in the example you sent me
weiv    It could be a bug - let me try to test it out
cycheng@ca.ibm.c... ok
cycheng@ca.ibm.c... we can wait till the conf. call
cycheng@ca.ibm.c... next weke
weiv    that would be great
weiv    hmmm
weiv    I might be wrong
weiv    let me play with it some more
cycheng@ca.ibm.c... ok
cycheng@ca.ibm.c... also please check s3 = 0x0e3a0062  and s4 = 0x0e400021. both are in utf-16be
cycheng@ca.ibm.c... seems with icu 2.2 we have s3 > s4, but not in icu 2.4 that's built for db2
cycheng@ca.ibm.c... also s1 & s2 that I sent you earlier are also in utf-16be
weiv    ok
cycheng@ca.ibm.c... i ask sherman to send you more inconsistent data
weiv    thanks
cycheng@ca.ibm.c... the 4 strings we sent are just samples
*/
#if 0
static void Alexis(void) {
  UErrorCode status = U_ZERO_ERROR;
  UCollator *coll = ucol_open("", &status);


  const char utf16be[2][4] = {
    { (char)0xd8, (char)0x00, (char)0x00, (char)0x21 },
    { (char)0xff, (char)0xfc, (char)0x00, (char)0x62 }
  };

  const char utf8[2][4] = {
    { (char)0xed, (char)0xa0, (char)0x80, (char)0x21 },
    { (char)0xef, (char)0xbf, (char)0xbc, (char)0x62 },
  };

  UCharIterator iterU161, iterU162;
  UCharIterator iterU81, iterU82;

  UCollationResult resU16, resU8;

  uiter_setUTF16BE(&iterU161, utf16be[0], 4);
  uiter_setUTF16BE(&iterU162, utf16be[1], 4);

  uiter_setUTF8(&iterU81, utf8[0], 4);
  uiter_setUTF8(&iterU82, utf8[1], 4);

  ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);

  resU16 = ucol_strcollIter(coll, &iterU161, &iterU162, &status);
  resU8 = ucol_strcollIter(coll, &iterU81, &iterU82, &status);


  if(resU16 != resU8) {
    log_err("different results\n");
  }

  ucol_close(coll);
}
#endif

#define CMSCOLL_ALEXIS2_BUFFER_SIZE 256
static void Alexis2(void) {
  UErrorCode status = U_ZERO_ERROR;
  UChar U16Source[CMSCOLL_ALEXIS2_BUFFER_SIZE], U16Target[CMSCOLL_ALEXIS2_BUFFER_SIZE];
  char U16BESource[CMSCOLL_ALEXIS2_BUFFER_SIZE], U16BETarget[CMSCOLL_ALEXIS2_BUFFER_SIZE];
  char U8Source[CMSCOLL_ALEXIS2_BUFFER_SIZE], U8Target[CMSCOLL_ALEXIS2_BUFFER_SIZE];
  int32_t U16LenS = 0, U16LenT = 0, U16BELenS = 0, U16BELenT = 0, U8LenS = 0, U8LenT = 0;

  UConverter *conv = NULL;

  UCharIterator U16BEItS, U16BEItT;
  UCharIterator U8ItS, U8ItT;

  UCollationResult resU16, resU16BE, resU8;

  static const char* const pairs[][2] = {
    { "\\ud800\\u0021", "\\uFFFC\\u0062"},
    { "\\u0435\\u0308\\u0334", "\\u0415\\u0334\\u0340" },
    { "\\u0E40\\u0021", "\\u00A1\\u0021"},
    { "\\u0E40\\u0021", "\\uFE57\\u0062"},
    { "\\u5F20", "\\u5F20\\u4E00\\u8E3F"},
    { "\\u0000\\u0020", "\\u0000\\u0020\\u0000"},
    { "\\u0020", "\\u0020\\u0000"}
/*
5F20 (my result here)
5F204E008E3F
5F20 (your result here)
*/
  };

  int32_t i = 0;

  UCollator *coll = ucol_open("", &status);
  if(status == U_FILE_ACCESS_ERROR) {
    log_data_err("Is your data around?\n");
    return;
  } else if(U_FAILURE(status)) {
    log_err("Error opening collator\n");
    return;
  }
  ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
  conv = ucnv_open("UTF16BE", &status);
  for(i = 0; i < UPRV_LENGTHOF(pairs); i++) {
    U16LenS = u_unescape(pairs[i][0], U16Source, CMSCOLL_ALEXIS2_BUFFER_SIZE);
    U16LenT = u_unescape(pairs[i][1], U16Target, CMSCOLL_ALEXIS2_BUFFER_SIZE);

    resU16 = ucol_strcoll(coll, U16Source, U16LenS, U16Target, U16LenT);

    log_verbose("Result of strcoll is %i\n", resU16);

    U16BELenS = ucnv_fromUChars(conv, U16BESource, CMSCOLL_ALEXIS2_BUFFER_SIZE, U16Source, U16LenS, &status);
    U16BELenT = ucnv_fromUChars(conv, U16BETarget, CMSCOLL_ALEXIS2_BUFFER_SIZE, U16Target, U16LenT, &status);
    (void)U16BELenS;    /* Suppress set but not used warnings. */
    (void)U16BELenT;

    /* use the original sizes, as the result from converter is in bytes */
    uiter_setUTF16BE(&U16BEItS, U16BESource, U16LenS);
    uiter_setUTF16BE(&U16BEItT, U16BETarget, U16LenT);

    resU16BE = ucol_strcollIter(coll, &U16BEItS, &U16BEItT, &status);

    log_verbose("Result of U16BE is %i\n", resU16BE);

    if(resU16 != resU16BE) {
      log_verbose("Different results between UTF16 and UTF16BE for %s & %s\n", pairs[i][0], pairs[i][1]);
    }

    u_strToUTF8(U8Source, CMSCOLL_ALEXIS2_BUFFER_SIZE, &U8LenS, U16Source, U16LenS, &status);
    u_strToUTF8(U8Target, CMSCOLL_ALEXIS2_BUFFER_SIZE, &U8LenT, U16Target, U16LenT, &status);

    uiter_setUTF8(&U8ItS, U8Source, U8LenS);
    uiter_setUTF8(&U8ItT, U8Target, U8LenT);

    resU8 = ucol_strcollIter(coll, &U8ItS, &U8ItT, &status);

    if(resU16 != resU8) {
      log_verbose("Different results between UTF16 and UTF8 for %s & %s\n", pairs[i][0], pairs[i][1]);
    }

  }

  ucol_close(coll);
  ucnv_close(conv);
}

static void TestHebrewUCA(void) {
  UErrorCode status = U_ZERO_ERROR;
  static const char *first[] = {
    "d790d6b8d79cd795d6bcd7a9",
    "d790d79cd79ed7a7d799d799d7a1",
    "d790d6b4d79ed795d6bcd7a9",
  };

  char utf8String[3][256];
  UChar utf16String[3][256];

  int32_t i = 0, j = 0;
  int32_t sizeUTF8[3];
  int32_t sizeUTF16[3];

  UCollator *coll = ucol_open("", &status);
  if (U_FAILURE(status)) {
      log_err_status(status, "Could not open UCA collation %s\n", u_errorName(status));
      return;
  }
  /*ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);*/

  for(i = 0; i < UPRV_LENGTHOF(first); i++) {
    sizeUTF8[i] = u_parseUTF8(first[i], -1, utf8String[i], 256, &status);
    u_strFromUTF8(utf16String[i], 256, &sizeUTF16[i], utf8String[i], sizeUTF8[i], &status);
    log_verbose("%i: ");
    for(j = 0; j < sizeUTF16[i]; j++) {
      /*log_verbose("\\u%04X", utf16String[i][j]);*/
      log_verbose("%04X", utf16String[i][j]);
    }
    log_verbose("\n");
  }
  for(i = 0; i < UPRV_LENGTHOF(first)-1; i++) {
    for(j = i + 1; j < UPRV_LENGTHOF(first); j++) {
      doTest(coll, utf16String[i], utf16String[j], UCOL_LESS);
    }
  }

  ucol_close(coll);

}

static void TestPartialSortKeyTermination(void) {
  static const char* cases[] = {
    "\\u1234\\u1234\\udc00",
    "\\udc00\\ud800\\ud800"
  };

  int32_t i;

  UErrorCode status = U_ZERO_ERROR;

  UCollator *coll = ucol_open("", &status);

  UCharIterator iter;

  UChar currCase[256];
  int32_t length = 0;
  int32_t pKeyLen = 0;

  uint8_t key[256];

  for(i = 0; i < UPRV_LENGTHOF(cases); i++) {
    uint32_t state[2] = {0, 0};
    length = u_unescape(cases[i], currCase, 256);
    uiter_setString(&iter, currCase, length);
    pKeyLen = ucol_nextSortKeyPart(coll, &iter, state, key, 256, &status);
    (void)pKeyLen;   /* Suppress set but not used warning. */

    log_verbose("Done\n");

  }
  ucol_close(coll);
}

static void TestSettings(void) {
  static const char* cases[] = {
    "apple",
      "Apple"
  };

  static const char* locales[] = {
    "",
      "en"
  };

  UErrorCode status = U_ZERO_ERROR;

  int32_t i = 0, j = 0;

  UChar source[256], target[256];
  int32_t sLen = 0, tLen = 0;

  UCollator *collateObject = NULL;
  for(i = 0; i < UPRV_LENGTHOF(locales); i++) {
    collateObject = ucol_open(locales[i], &status);
    ucol_setStrength(collateObject, UCOL_PRIMARY);
    ucol_setAttribute(collateObject, UCOL_CASE_LEVEL , UCOL_OFF, &status);
    for(j = 1; j < UPRV_LENGTHOF(cases); j++) {
      sLen = u_unescape(cases[j-1], source, 256);
      source[sLen] = 0;
      tLen = u_unescape(cases[j], target, 256);
      source[tLen] = 0;
      doTest(collateObject, source, target, UCOL_EQUAL);
    }
    ucol_close(collateObject);
  }
}

static int32_t TestEqualsForCollator(const char* locName, UCollator *source, UCollator *target) {
    UErrorCode status = U_ZERO_ERROR;
    int32_t errorNo = 0;
    const UChar *sourceRules = NULL;
    int32_t sourceRulesLen = 0;
    UParseError parseError;
    UColAttributeValue french = UCOL_OFF;

    if(!ucol_equals(source, target)) {
        log_err("Same collators, different address not equal\n");
        errorNo++;
    }
    ucol_close(target);
    if(uprv_strcmp(locName, ucol_getLocaleByType(source, ULOC_ACTUAL_LOCALE, &status)) == 0) {
        target = ucol_safeClone(source, NULL, NULL, &status);
        if(U_FAILURE(status)) {
            log_err("Error creating clone\n");
            errorNo++;
            return errorNo;
        }
        if(!ucol_equals(source, target)) {
            log_err("Collator different from it's clone\n");
            errorNo++;
        }
        french = ucol_getAttribute(source, UCOL_FRENCH_COLLATION, &status);
        if(french == UCOL_ON) {
            ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_OFF, &status);
        } else {
            ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
        }
        if(U_FAILURE(status)) {
            log_err("Error setting attributes\n");
            errorNo++;
            return errorNo;
        }
        if(ucol_equals(source, target)) {
            log_err("Collators same even when options changed\n");
            errorNo++;
        }
        ucol_close(target);

        sourceRules = ucol_getRules(source, &sourceRulesLen);
        target = ucol_openRules(sourceRules, sourceRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
        if(U_FAILURE(status)) {
            log_err("Error instantiating target from rules - %s\n", u_errorName(status));
            errorNo++;
            return errorNo;
        }
        /* Note: The tailoring rule string is an optional data item. */
        if(!ucol_equals(source, target) && sourceRulesLen != 0) {
            log_err("Collator different from collator that was created from the same rules\n");
            errorNo++;
        }
        ucol_close(target);
    }
    return errorNo;
}


static void TestEquals(void) {
    /* ucol_equals is not currently a public API. There is a chance that it will become
    * something like this.
    */
    /* test whether the two collators instantiated from the same locale are equal */
    UErrorCode status = U_ZERO_ERROR;
    UParseError parseError;
    int32_t noOfLoc = uloc_countAvailable();
    const char *locName = NULL;
    UCollator *source = NULL, *target = NULL;
    int32_t i = 0;

    const char* rules[] = {
        "&l < lj <<< Lj <<< LJ",
        "&n < nj <<< Nj <<< NJ",
        "&ae <<< \\u00e4",
        "&AE <<< \\u00c4"
    };
    /*
    const char* badRules[] = {
    "&l <<< Lj",
    "&n < nj <<< nJ <<< NJ",
    "&a <<< \\u00e4",
    "&AE <<< \\u00c4 <<< x"
    };
    */

    UChar sourceRules[1024], targetRules[1024];
    int32_t sourceRulesSize = 0, targetRulesSize = 0;
    int32_t rulesSize = UPRV_LENGTHOF(rules);

    for(i = 0; i < rulesSize; i++) {
        sourceRulesSize += u_unescape(rules[i], sourceRules+sourceRulesSize, 1024 - sourceRulesSize);
        targetRulesSize += u_unescape(rules[rulesSize-i-1], targetRules+targetRulesSize, 1024 - targetRulesSize);
    }

    source = ucol_openRules(sourceRules, sourceRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
    if(status == U_FILE_ACCESS_ERROR) {
        log_data_err("Is your data around?\n");
        return;
    } else if(U_FAILURE(status)) {
        log_err("Error opening collator\n");
        return;
    }
    target = ucol_openRules(targetRules, targetRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
    if(!ucol_equals(source, target)) {
        log_err("Equivalent collators not equal!\n");
    }
    ucol_close(source);
    ucol_close(target);

    source = ucol_open("root", &status);
    target = ucol_open("root", &status);
    log_verbose("Testing root\n");
    if(!ucol_equals(source, source)) {
        log_err("Same collator not equal\n");
    }
    if(TestEqualsForCollator("root", source, target)) {
        log_err("Errors for root\n");
    }
    ucol_close(source);

    for(i = 0; i<noOfLoc; i++) {
        status = U_ZERO_ERROR;
        locName = uloc_getAvailable(i);
        /*if(hasCollationElements(locName)) {*/
        log_verbose("Testing equality for locale %s\n", locName);
        source = ucol_open(locName, &status);
        target = ucol_open(locName, &status);
        if (U_FAILURE(status)) {
            log_err("Error opening collator for locale %s  %s\n", locName, u_errorName(status));
            continue;
        }
        if(TestEqualsForCollator(locName, source, target)) {
            log_err("Errors for locale %s\n", locName);
        }
        ucol_close(source);
        /*}*/
    }
}

static void TestJ2726(void) {
    UChar a[2] = { 0x61, 0x00 }; /*"a"*/
    UChar aSpace[3] = { 0x61, 0x20, 0x00 }; /*"a "*/
    UChar spaceA[3] = { 0x20, 0x61, 0x00 }; /*" a"*/
    UErrorCode status = U_ZERO_ERROR;
    UCollator *coll = ucol_open("en", &status);
    ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
    ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
    doTest(coll, a, aSpace, UCOL_EQUAL);
    doTest(coll, aSpace, a, UCOL_EQUAL);
    doTest(coll, a, spaceA, UCOL_EQUAL);
    doTest(coll, spaceA, a, UCOL_EQUAL);
    doTest(coll, spaceA, aSpace, UCOL_EQUAL);
    doTest(coll, aSpace, spaceA, UCOL_EQUAL);
    ucol_close(coll);
}

static void NullRule(void) {
    UChar r[3] = {0};
    UErrorCode status = U_ZERO_ERROR;
    UCollator *coll = ucol_openRules(r, 1, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
    if(U_SUCCESS(status)) {
        log_err("This should have been an error!\n");
        ucol_close(coll);
    } else {
        status = U_ZERO_ERROR;
    }
    coll = ucol_openRules(r, 0, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "Empty rules should have produced a valid collator -> %s\n", u_errorName(status));
    } else {
        ucol_close(coll);
    }
}

/**
 * Test for CollationElementIterator previous and next for the whole set of
 * unicode characters with normalization on.
 */
static void TestNumericCollation(void)
{
    UErrorCode status = U_ZERO_ERROR;

    const static char *basicTestStrings[]={
    "hello1",
    "hello2",
    "hello2002",
    "hello2003",
    "hello123456",
    "hello1234567",
    "hello10000000",
    "hello100000000",
    "hello1000000000",
    "hello10000000000",
    };

    const static char *preZeroTestStrings[]={
    "avery10000",
    "avery010000",
    "avery0010000",
    "avery00010000",
    "avery000010000",
    "avery0000010000",
    "avery00000010000",
    "avery000000010000",
    };

    const static char *thirtyTwoBitNumericStrings[]={
    "avery42949672960",
    "avery42949672961",
    "avery42949672962",
    "avery429496729610"
    };

     const static char *longNumericStrings[]={
     /* Some of these sort out of the order that would expected if digits-as-numbers handled arbitrarily-long digit strings.
        In fact, a single collation element can represent a maximum of 254 digits as a number. Digit strings longer than that
        are treated as multiple collation elements. */
    "num9234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123z", /*253digits, num + 9.23E252 + z */
    "num10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", /*254digits, num + 1.00E253 */
    "num100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", /*255digits, num + 1.00E253 + 0, out of numeric order but expected */
    "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", /*254digits, num + 1.23E253 */
    "num123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345", /*255digits, num + 1.23E253 + 5 */
    "num1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456", /*256digits, num + 1.23E253 + 56 */
    "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567", /*257digits, num + 1.23E253 + 567 */
    "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234a", /*254digits, num + 1.23E253 + a, out of numeric order but expected */
    "num92345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", /*254digits, num + 9.23E253, out of numeric order but expected */
    "num92345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234a", /*254digits, num + 9.23E253 + a, out of numeric order but expected */
    };

    const static char *supplementaryDigits[] = {
      "\\uD835\\uDFCE", /* 0 */
      "\\uD835\\uDFCF", /* 1 */
      "\\uD835\\uDFD0", /* 2 */
      "\\uD835\\uDFD1", /* 3 */
      "\\uD835\\uDFCF\\uD835\\uDFCE", /* 10 */
      "\\uD835\\uDFCF\\uD835\\uDFCF", /* 11 */
      "\\uD835\\uDFCF\\uD835\\uDFD0", /* 12 */
      "\\uD835\\uDFD0\\uD835\\uDFCE", /* 20 */
      "\\uD835\\uDFD0\\uD835\\uDFCF", /* 21 */
      "\\uD835\\uDFD0\\uD835\\uDFD0" /* 22 */
    };

    const static char *foreignDigits[] = {
      "\\u0661",
        "\\u0662",
        "\\u0663",
      "\\u0661\\u0660",
      "\\u0661\\u0662",
      "\\u0661\\u0663",
      "\\u0662\\u0660",
      "\\u0662\\u0662",
      "\\u0662\\u0663",
      "\\u0663\\u0660",
      "\\u0663\\u0662",
      "\\u0663\\u0663"
    };

    const static char *evenZeroes[] = {
      "2000",
      "2001",
        "2002",
        "2003"
    };

    UColAttribute att = UCOL_NUMERIC_COLLATION;
    UColAttributeValue val = UCOL_ON;

    /* Open our collator. */
    UCollator* coll = ucol_open("root", &status);
    if (U_FAILURE(status)){
        log_err_status(status, "ERROR: in using ucol_open() -> %s\n",
              myErrorName(status));
        return;
    }
    genericLocaleStarterWithOptions("root", basicTestStrings, UPRV_LENGTHOF(basicTestStrings), &att, &val, 1);
    genericLocaleStarterWithOptions("root", thirtyTwoBitNumericStrings, UPRV_LENGTHOF(thirtyTwoBitNumericStrings), &att, &val, 1);
    genericLocaleStarterWithOptions("root", longNumericStrings, UPRV_LENGTHOF(longNumericStrings), &att, &val, 1);
    genericLocaleStarterWithOptions("en_US", foreignDigits, UPRV_LENGTHOF(foreignDigits), &att, &val, 1);
    genericLocaleStarterWithOptions("root", supplementaryDigits, UPRV_LENGTHOF(supplementaryDigits), &att, &val, 1);
    genericLocaleStarterWithOptions("root", evenZeroes, UPRV_LENGTHOF(evenZeroes), &att, &val, 1);

    /* Setting up our collator to do digits. */
    ucol_setAttribute(coll, UCOL_NUMERIC_COLLATION, UCOL_ON, &status);
    if (U_FAILURE(status)){
        log_err("ERROR: in setting UCOL_NUMERIC_COLLATION as an attribute\n %s\n",
              myErrorName(status));
        return;
    }

    /*
       Testing that prepended zeroes still yield the correct collation behavior.
       We expect that every element in our strings array will be equal.
    */
    genericOrderingTestWithResult(coll, preZeroTestStrings, UPRV_LENGTHOF(preZeroTestStrings), UCOL_EQUAL);

    ucol_close(coll);
}

static void TestTibetanConformance(void)
{
    const char* test[] = {
        "\\u0FB2\\u0591\\u0F71\\u0061",
        "\\u0FB2\\u0F71\\u0061"
    };

    UErrorCode status = U_ZERO_ERROR;
    UCollator *coll = ucol_open("", &status);
    UChar source[100];
    UChar target[100];
    int result;
    ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
    if (U_SUCCESS(status)) {
        u_unescape(test[0], source, 100);
        u_unescape(test[1], target, 100);
        doTest(coll, source, target, UCOL_EQUAL);
        result = ucol_strcoll(coll, source, -1,   target, -1);
        log_verbose("result %d\n", result);
        if (UCOL_EQUAL != result) {
            log_err("Tibetan comparison error\n");
        }
    }
    ucol_close(coll);

    genericLocaleStarterWithResult("", test, 2, UCOL_EQUAL);
}

static void TestPinyinProblem(void) {
    static const char *test[] = { "\\u4E56\\u4E56\\u7761", "\\u4E56\\u5B69\\u5B50" };
    genericLocaleStarter("zh__PINYIN", test, UPRV_LENGTHOF(test));
}

/**
 * Iterate through the given iterator, checking to see that all the strings
 * in the expected array are present.
 * @param expected array of strings we expect to see, or NULL
 * @param expectedCount number of elements of expected, or 0
 */
static int32_t checkUEnumeration(const char* msg,
                                 UEnumeration* iter,
                                 const char** expected,
                                 int32_t expectedCount) {
    UErrorCode ec = U_ZERO_ERROR;
    int32_t i = 0, n, j, bit;
    int32_t seenMask = 0;

    U_ASSERT(expectedCount >= 0 && expectedCount < 31); /* [sic] 31 not 32 */
    n = uenum_count(iter, &ec);
    if (!assertSuccess("count", &ec)) return -1;
    log_verbose("%s = [", msg);
    for (;; ++i) {
        const char* s = uenum_next(iter, NULL, &ec);
        if (!assertSuccess("snext", &ec) || s == NULL) break;
        if (i != 0) log_verbose(",");
        log_verbose("%s", s);
        /* check expected list */
        for (j=0, bit=1; j<expectedCount; ++j, bit<<=1) {
            if ((seenMask&bit) == 0 &&
                uprv_strcmp(s, expected[j]) == 0) {
                seenMask |= bit;
                break;
            }
        }
    }
    log_verbose("] (%d)\n", i);
    assertTrue("count verified", i==n);
    /* did we see all expected strings? */
    for (j=0, bit=1; j<expectedCount; ++j, bit<<=1) {
        if ((seenMask&bit)!=0) {
            log_verbose("Ok: \"%s\" seen\n", expected[j]);
        } else {
            log_err("FAIL: \"%s\" not seen\n", expected[j]);
        }
    }
    return n;
}

/**
 * Test new API added for separate collation tree.
 */
static void TestSeparateTrees(void) {
    UErrorCode ec = U_ZERO_ERROR;
    UEnumeration *e = NULL;
    int32_t n = -1;
    UBool isAvailable;
    char loc[256];

    static const char* AVAIL[] = { "en", "de" };

    static const char* KW[] = { "collation" };

    static const char* KWVAL[] = { "phonebook", "stroke" };

#if !UCONFIG_NO_SERVICE
    e = ucol_openAvailableLocales(&ec);
    if (e != NULL) {
        assertSuccess("ucol_openAvailableLocales", &ec);
        assertTrue("ucol_openAvailableLocales!=0", e!=0);
        n = checkUEnumeration("ucol_openAvailableLocales", e, AVAIL, UPRV_LENGTHOF(AVAIL));
        (void)n;    /* Suppress set but not used warnings. */
        /* Don't need to check n because we check list */
        uenum_close(e);
    } else {
        log_data_err("Error calling ucol_openAvailableLocales() -> %s (Are you missing data?)\n", u_errorName(ec));
    }
#endif

    e = ucol_getKeywords(&ec);
    if (e != NULL) {
        assertSuccess("ucol_getKeywords", &ec);
        assertTrue("ucol_getKeywords!=0", e!=0);
        n = checkUEnumeration("ucol_getKeywords", e, KW, UPRV_LENGTHOF(KW));
        /* Don't need to check n because we check list */
        uenum_close(e);
    } else {
        log_data_err("Error calling ucol_getKeywords() -> %s (Are you missing data?)\n", u_errorName(ec));
    }

    e = ucol_getKeywordValues(KW[0], &ec);
    if (e != NULL) {
        assertSuccess("ucol_getKeywordValues", &ec);
        assertTrue("ucol_getKeywordValues!=0", e!=0);
        n = checkUEnumeration("ucol_getKeywordValues", e, KWVAL, UPRV_LENGTHOF(KWVAL));
        /* Don't need to check n because we check list */
        uenum_close(e);
    } else {
        log_data_err("Error calling ucol_getKeywordValues() -> %s (Are you missing data?)\n", u_errorName(ec));
    }

    /* Try setting a warning before calling ucol_getKeywordValues */
    ec = U_USING_FALLBACK_WARNING;
    e = ucol_getKeywordValues(KW[0], &ec);
    if (assertSuccess("ucol_getKeywordValues [with warning code set]", &ec)) {
        assertTrue("ucol_getKeywordValues!=0 [with warning code set]", e!=0);
        n = checkUEnumeration("ucol_getKeywordValues [with warning code set]", e, KWVAL, UPRV_LENGTHOF(KWVAL));
        /* Don't need to check n because we check list */
        uenum_close(e);
    }

    /*
U_CAPI int32_t U_EXPORT2
ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity,
                             const char* locale, UBool* isAvailable,
                             UErrorCode* status);
}
*/
    n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "de",
                                     &isAvailable, &ec);
    if (assertSuccess("getFunctionalEquivalent", &ec)) {
        assertEquals("getFunctionalEquivalent(de)", "root", loc);
        assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE",
                   isAvailable == TRUE);
    }

    n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "de_DE",
                                     &isAvailable, &ec);
    if (assertSuccess("getFunctionalEquivalent", &ec)) {
        assertEquals("getFunctionalEquivalent(de_DE)", "root", loc);
        assertTrue("getFunctionalEquivalent(de_DE).isAvailable==FALSE",
                   isAvailable == FALSE);
    }
}

/* supercedes TestJ784 */
static void TestBeforePinyin(void) {
    const static char rules[] = {
        "&[before 2]A<<\\u0101<<<\\u0100<<\\u00E1<<<\\u00C1<<\\u01CE<<<\\u01CD<<\\u00E0<<<\\u00C0"
        "&[before 2]e<<\\u0113<<<\\u0112<<\\u00E9<<<\\u00C9<<\\u011B<<<\\u011A<<\\u00E8<<<\\u00C8"
        "&[before 2]i<<\\u012B<<<\\u012A<<\\u00ED<<<\\u00CD<<\\u01D0<<<\\u01CF<<\\u00EC<<<\\u00CC"
        "&[before 2]o<<\\u014D<<<\\u014C<<\\u00F3<<<\\u00D3<<\\u01D2<<<\\u01D1<<\\u00F2<<<\\u00D2"
        "&[before 2]u<<\\u016B<<<\\u016A<<\\u00FA<<<\\u00DA<<\\u01D4<<<\\u01D3<<\\u00F9<<<\\u00D9"
        "&U<<\\u01D6<<<\\u01D5<<\\u01D8<<<\\u01D7<<\\u01DA<<<\\u01D9<<\\u01DC<<<\\u01DB<<\\u00FC"
    };

    const static char *test[] = {
        "l\\u0101",
        "la",
        "l\\u0101n",
        "lan ",
        "l\\u0113",
        "le",
        "l\\u0113n",
        "len"
    };

    const static char *test2[] = {
        "x\\u0101",
        "x\\u0100",
        "X\\u0101",
        "X\\u0100",
        "x\\u00E1",
        "x\\u00C1",
        "X\\u00E1",
        "X\\u00C1",
        "x\\u01CE",
        "x\\u01CD",
        "X\\u01CE",
        "X\\u01CD",
        "x\\u00E0",
        "x\\u00C0",
        "X\\u00E0",
        "X\\u00C0",
        "xa",
        "xA",
        "Xa",
        "XA",
        "x\\u0101x",
        "x\\u0100x",
        "x\\u00E1x",
        "x\\u00C1x",
        "x\\u01CEx",
        "x\\u01CDx",
        "x\\u00E0x",
        "x\\u00C0x",
        "xax",
        "xAx"
    };

    genericRulesStarter(rules, test, UPRV_LENGTHOF(test));
    genericLocaleStarter("zh", test, UPRV_LENGTHOF(test));
    genericRulesStarter(rules, test2, UPRV_LENGTHOF(test2));
    genericLocaleStarter("zh", test2, UPRV_LENGTHOF(test2));
}

static void TestBeforeTightening(void) {
    static const struct {
        const char *rules;
        UErrorCode expectedStatus;
    } tests[] = {
        { "&[before 1]a<x", U_ZERO_ERROR },
        { "&[before 1]a<<x", U_INVALID_FORMAT_ERROR },
        { "&[before 1]a<<<x", U_INVALID_FORMAT_ERROR },
        { "&[before 1]a=x", U_INVALID_FORMAT_ERROR },
        { "&[before 2]a<x",U_INVALID_FORMAT_ERROR },
        { "&[before 2]a<<x",U_ZERO_ERROR },
        { "&[before 2]a<<<x",U_INVALID_FORMAT_ERROR },
        { "&[before 2]a=x",U_INVALID_FORMAT_ERROR },
        { "&[before 3]a<x",U_INVALID_FORMAT_ERROR  },
        { "&[before 3]a<<x",U_INVALID_FORMAT_ERROR  },
        { "&[before 3]a<<<x",U_ZERO_ERROR },
        { "&[before 3]a=x",U_INVALID_FORMAT_ERROR  },
        { "&[before I]a = x",U_INVALID_FORMAT_ERROR }
    };

    int32_t i = 0;

    UErrorCode status = U_ZERO_ERROR;
    UChar rlz[RULE_BUFFER_LEN] = { 0 };
    uint32_t rlen = 0;

    UCollator *coll = NULL;


    for(i = 0; i < UPRV_LENGTHOF(tests); i++) {
        rlen = u_unescape(tests[i].rules, rlz, RULE_BUFFER_LEN);
        coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status);
        if(status != tests[i].expectedStatus) {
            log_err_status(status, "Opening a collator with rules %s returned error code %s, expected %s\n",
                tests[i].rules, u_errorName(status), u_errorName(tests[i].expectedStatus));
        }
        ucol_close(coll);
        status = U_ZERO_ERROR;
    }

}

/*
&m < a
&[before 1] a < x <<< X << q <<< Q < z
assert: m <<< M < x <<< X << q <<< Q < z < a < n

&m < a
&[before 2] a << x <<< X << q <<< Q < z
assert: m <<< M < x <<< X << q <<< Q << a < z < n

&m < a
&[before 3] a <<< x <<< X << q <<< Q < z
assert: m <<< M < x <<< X <<< a << q <<< Q < z < n


&m << a
&[before 1] a < x <<< X << q <<< Q < z
assert: x <<< X << q <<< Q < z < m <<< M << a < n

&m << a
&[before 2] a << x <<< X << q <<< Q < z
assert: m <<< M << x <<< X << q <<< Q << a < z < n

&m << a
&[before 3] a <<< x <<< X << q <<< Q < z
assert: m <<< M << x <<< X <<< a << q <<< Q < z < n


&m <<< a
&[before 1] a < x <<< X << q <<< Q < z
assert: x <<< X << q <<< Q < z < n < m <<< a <<< M

&m <<< a
&[before 2] a << x <<< X << q <<< Q < z
assert:  x <<< X << q <<< Q << m <<< a <<< M < z < n

&m <<< a
&[before 3] a <<< x <<< X << q <<< Q < z
assert: m <<< x <<< X <<< a <<< M  << q <<< Q < z < n


&[before 1] s < x <<< X << q <<< Q < z
assert: r <<< R < x <<< X << q <<< Q < z < s < n

&[before 2] s << x <<< X << q <<< Q < z
assert: r <<< R < x <<< X << q <<< Q << s < z < n

&[before 3] s <<< x <<< X << q <<< Q < z
assert: r <<< R < x <<< X <<< s << q <<< Q < z < n


&[before 1] \u24DC < x <<< X << q <<< Q < z
assert: x <<< X << q <<< Q < z < n < m <<< \u24DC <<< M

&[before 2] \u24DC << x <<< X << q <<< Q < z
assert:  x <<< X << q <<< Q << m <<< \u24DC <<< M < z < n

&[before 3] \u24DC <<< x <<< X << q <<< Q < z
assert: m <<< x <<< X <<< \u24DC <<< M  << q <<< Q < z < n
*/


#if 0
/* requires features not yet supported */
static void TestMoreBefore(void) {
    static const struct {
        const char* rules;
        const char* order[16];
        int32_t size;
    } tests[] = {
        { "&m < a &[before 1] a < x <<< X << q <<< Q < z",
        { "m","M","x","X","q","Q","z","a","n" }, 9},
        { "&m < a &[before 2] a << x <<< X << q <<< Q < z",
        { "m","M","x","X","q","Q","a","z","n" }, 9},
        { "&m < a &[before 3] a <<< x <<< X << q <<< Q < z",
        { "m","M","x","X","a","q","Q","z","n" }, 9},
        { "&m << a &[before 1] a < x <<< X << q <<< Q < z",
        { "x","X","q","Q","z","m","M","a","n" }, 9},
        { "&m << a &[before 2] a << x <<< X << q <<< Q < z",
        { "m","M","x","X","q","Q","a","z","n" }, 9},
        { "&m << a &[before 3] a <<< x <<< X << q <<< Q < z",
        { "m","M","x","X","a","q","Q","z","n" }, 9},
        { "&m <<< a &[before 1] a < x <<< X << q <<< Q < z",
        { "x","X","q","Q","z","n","m","a","M" }, 9},
        { "&m <<< a &[before 2] a << x <<< X << q <<< Q < z",
        { "x","X","q","Q","m","a","M","z","n" }, 9},
        { "&m <<< a &[before 3] a <<< x <<< X << q <<< Q < z",
        { "m","x","X","a","M","q","Q","z","n" }, 9},
        { "&[before 1] s < x <<< X << q <<< Q < z",
        { "r","R","x","X","q","Q","z","s","n" }, 9},
        { "&[before 2] s << x <<< X << q <<< Q < z",
        { "r","R","x","X","q","Q","s","z","n" }, 9},
        { "&[before 3] s <<< x <<< X << q <<< Q < z",
        { "r","R","x","X","s","q","Q","z","n" }, 9},
        { "&[before 1] \\u24DC < x <<< X << q <<< Q < z",
        { "x","X","q","Q","z","n","m","\\u24DC","M" }, 9},
        { "&[before 2] \\u24DC << x <<< X << q <<< Q < z",
        { "x","X","q","Q","m","\\u24DC","M","z","n" }, 9},
        { "&[before 3] \\u24DC <<< x <<< X << q <<< Q < z",
        { "m","x","X","\\u24DC","M","q","Q","z","n" }, 9}
    };

    int32_t i = 0;

    for(i = 0; i < UPRV_LENGTHOF(tests); i++) {
        genericRulesStarter(tests[i].rules, tests[i].order, tests[i].size);
    }
}
#endif

static void TestTailorNULL( void ) {
    const static char* rule = "&a <<< '\\u0000'";
    UErrorCode status = U_ZERO_ERROR;
    UChar rlz[RULE_BUFFER_LEN] = { 0 };
    uint32_t rlen = 0;
    UChar a = 1, null = 0;
    UCollationResult res = UCOL_EQUAL;

    UCollator *coll = NULL;


    rlen = u_unescape(rule, rlz, RULE_BUFFER_LEN);
    coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status);

    if(U_FAILURE(status)) {
        log_err_status(status, "Could not open default collator! -> %s\n", u_errorName(status));
    } else {
        res = ucol_strcoll(coll, &a, 1, &null, 1);

        if(res != UCOL_LESS) {
            log_err("NULL was not tailored properly!\n");
        }
    }

    ucol_close(coll);
}

static void
TestUpperFirstQuaternary(void)
{
  const char* tests[] = { "B", "b", "Bb", "bB" };
  UColAttribute att[] = { UCOL_STRENGTH, UCOL_CASE_FIRST };
  UColAttributeValue attVals[] = { UCOL_QUATERNARY, UCOL_UPPER_FIRST };
  genericLocaleStarterWithOptions("root", tests, UPRV_LENGTHOF(tests), att, attVals, UPRV_LENGTHOF(att));
}

static void
TestJ4960(void)
{
  const char* tests[] = { "\\u00e2T", "aT" };
  UColAttribute att[] = { UCOL_STRENGTH, UCOL_CASE_LEVEL };
  UColAttributeValue attVals[] = { UCOL_PRIMARY, UCOL_ON };
  const char* tests2[] = { "a", "A" };
  const char* rule = "&[first tertiary ignorable]=A=a";
  UColAttribute att2[] = { UCOL_CASE_LEVEL };
  UColAttributeValue attVals2[] = { UCOL_ON };
  /* Test whether we correctly ignore primary ignorables on case level when */
  /* we have only primary & case level */
  genericLocaleStarterWithOptionsAndResult("root", tests, UPRV_LENGTHOF(tests), att, attVals, UPRV_LENGTHOF(att), UCOL_EQUAL);
  /* Test whether ICU4J will make case level for sortkeys that have primary strength */
  /* and case level */
  genericLocaleStarterWithOptions("root", tests2, UPRV_LENGTHOF(tests2), att, attVals, UPRV_LENGTHOF(att));
  /* Test whether completely ignorable letters have case level info (they shouldn't) */
  genericRulesStarterWithOptionsAndResult(rule, tests2, UPRV_LENGTHOF(tests2), att2, attVals2, UPRV_LENGTHOF(att2), UCOL_EQUAL);
}

static void
TestJ5223(void)
{
  static const char *test = "this is a test string";
  UChar ustr[256];
  int32_t ustr_length = u_unescape(test, ustr, 256);
  unsigned char sortkey[256];
  int32_t sortkey_length;
  UErrorCode status = U_ZERO_ERROR;
  static UCollator *coll = NULL;
  coll = ucol_open("root", &status);
  if(U_FAILURE(status)) {
    log_err_status(status, "Couldn't open UCA -> %s\n", u_errorName(status));
    return;
  }
  ucol_setStrength(coll, UCOL_PRIMARY);
  ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
  ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
  if (U_FAILURE(status)) {
    log_err("Failed setting attributes\n");
    return;
  }
  sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, NULL, 0);
  if (sortkey_length > 256) return;

  /* we mark the position where the null byte should be written in advance */
  sortkey[sortkey_length-1] = 0xAA;

  /* we set the buffer size one byte higher than needed */
  sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, sortkey,
    sortkey_length+1);

  /* no error occurs (for me) */
  if (sortkey[sortkey_length-1] == 0xAA) {
    log_err("Hit bug at first try\n");
  }

  /* we mark the position where the null byte should be written again */
  sortkey[sortkey_length-1] = 0xAA;

  /* this time we set the buffer size to the exact amount needed */
  sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, sortkey,
    sortkey_length);

  /* now the trailing null byte is not written */
  if (sortkey[sortkey_length-1] == 0xAA) {
    log_err("Hit bug at second try\n");
  }

  ucol_close(coll);
}

/* Regression test for Thai partial sort key problem */
static void
TestJ5232(void)
{
    const static char *test[] = {
        "\\u0e40\\u0e01\\u0e47\\u0e1a\\u0e40\\u0e25\\u0e47\\u0e21",
        "\\u0e40\\u0e01\\u0e47\\u0e1a\\u0e40\\u0e25\\u0e48\\u0e21"
    };

    genericLocaleStarter("th", test, UPRV_LENGTHOF(test));
}

static void
TestJ5367(void)
{
    const static char *test[] = { "a", "y" };
    const char* rules = "&Ny << Y &[first secondary ignorable] <<< a";
    genericRulesStarter(rules, test, UPRV_LENGTHOF(test));
}

static void
TestVI5913(void)
{
    UErrorCode status = U_ZERO_ERROR;
    int32_t i, j;
    UCollator *coll =NULL;
    uint8_t  resColl[100], expColl[100];
    int32_t  rLen, tLen, ruleLen, sLen, kLen;
    UChar rule[256]={0x26, 0x62, 0x3c, 0x1FF3, 0};  /* &b<0x1FF3-omega with Ypogegrammeni*/
    UChar rule2[256]={0x26, 0x7a, 0x3c, 0x0161, 0};  /* &z<s with caron*/
    /*
     * Note: Just tailoring &z<ae^ does not work as expected:
     * The UCA spec requires for discontiguous contractions that they
     * extend an *existing match* by one combining mark at a time.
     * Therefore, ae must be a contraction so that the builder finds
     * discontiguous contractions for ae^, for example with an intervening underdot.
     * Only then do we get the expected tail closure with a\u1EC7, a\u1EB9\u0302, etc.
     */
    UChar rule3[256]={
        0x26, 0x78, 0x3c, 0x61, 0x65,      /* &x<ae */
        0x26, 0x7a, 0x3c, 0x0061, 0x00ea,  /* &z<a+e with circumflex.*/
        0};
    static const UChar tData[][20]={
        {0x1EAC, 0},
        {0x0041, 0x0323, 0x0302, 0},
        {0x1EA0, 0x0302, 0},
        {0x00C2, 0x0323, 0},
        {0x1ED8, 0},  /* O with dot and circumflex */
        {0x1ECC, 0x0302, 0},
        {0x1EB7, 0},
        {0x1EA1, 0x0306, 0},
    };
    static const UChar tailorData[][20]={
        {0x1FA2, 0},  /* Omega with 3 combining marks */
        {0x03C9, 0x0313, 0x0300, 0x0345, 0},
        {0x1FF3, 0x0313, 0x0300, 0},
        {0x1F60, 0x0300, 0x0345, 0},
        {0x1F62, 0x0345, 0},
        {0x1FA0, 0x0300, 0},
    };
    static const UChar tailorData2[][20]={
        {0x1E63, 0x030C, 0},  /* s with dot below + caron */
        {0x0073, 0x0323, 0x030C, 0},
        {0x0073, 0x030C, 0x0323, 0},
    };
    static const UChar tailorData3[][20]={
        {0x007a, 0},  /*  z */
        {0x0061, 0x0065, 0},  /*  a + e */
        {0x0061, 0x00ea, 0}, /* a + e with circumflex */
        {0x0061, 0x1EC7, 0},  /* a+ e with dot below and circumflex */
        {0x0061, 0x1EB9, 0x0302, 0}, /* a + e with dot below + combining circumflex */
        {0x0061, 0x00EA, 0x0323, 0},  /* a + e with circumflex + combining dot below */
        {0x00EA, 0x0323, 0},  /* e with circumflex + combining dot below */
        {0x00EA, 0},  /* e with circumflex  */
    };

    /* Test Vietnamese sort. */
    coll = ucol_open("vi", &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "Couldn't open collator -> %s\n", u_errorName(status));
        return;
    }
    log_verbose("\n\nVI collation:");
    if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[2], u_strlen(tData[2])) ) {
        log_err("\\u1EAC not equals to \\u1EA0+\\u0302\n");
    }
    if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[3], u_strlen(tData[3])) ) {
        log_err("\\u1EAC not equals to \\u00c2+\\u0323\n");
    }
    if ( !ucol_equal(coll, tData[5], u_strlen(tData[5]), tData[4], u_strlen(tData[4])) ) {
        log_err("\\u1ED8 not equals to \\u1ECC+\\u0302\n");
    }
    if ( !ucol_equal(coll, tData[7], u_strlen(tData[7]), tData[6], u_strlen(tData[6])) ) {
        log_err("\\u1EB7 not equals to \\u1EA1+\\u0306\n");
    }

    for (j=0; j<8; j++) {
        tLen = u_strlen(tData[j]);
        log_verbose("\n Data :%s  \tlen: %d key: ", tData[j], tLen);
        rLen = ucol_getSortKey(coll, tData[j], tLen, resColl, 100);
        for(i = 0; i<rLen; i++) {
            log_verbose(" %02X", resColl[i]);
        }
    }

    ucol_close(coll);

    /* Test Romanian sort. */
    coll = ucol_open("ro", &status);
    log_verbose("\n\nRO collation:");
    if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[1], u_strlen(tData[1])) ) {
        log_err("\\u1EAC not equals to \\u1EA0+\\u0302\n");
    }
    if ( !ucol_equal(coll, tData[4], u_strlen(tData[4]), tData[5], u_strlen(tData[5])) ) {
        log_err("\\u1EAC not equals to \\u00c2+\\u0323\n");
    }
    if ( !ucol_equal(coll, tData[6], u_strlen(tData[6]), tData[7], u_strlen(tData[7])) ) {
        log_err("\\u1EB7 not equals to \\u1EA1+\\u0306\n");
    }

    for (j=4; j<8; j++) {
        tLen = u_strlen(tData[j]);
        log_verbose("\n Data :%s  \tlen: %d key: ", tData[j], tLen);
        rLen = ucol_getSortKey(coll, tData[j], tLen, resColl, 100);
        for(i = 0; i<rLen; i++) {
            log_verbose(" %02X", resColl[i]);
        }
    }
    ucol_close(coll);

    /* Test the precomposed Greek character with 3 combining marks. */
    log_verbose("\n\nTailoring test: Greek character with 3 combining marks");
    ruleLen = u_strlen(rule);
    coll = ucol_openRules(rule, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
    if (U_FAILURE(status)) {
        log_err("ucol_openRules failed with %s\n", u_errorName(status));
        return;
    }
    sLen = u_strlen(tailorData[0]);
    for (j=1; j<6; j++) {
        tLen = u_strlen(tailorData[j]);
        if ( !ucol_equal(coll, tailorData[0], sLen, tailorData[j], tLen))  {
            log_err("\n \\u1FA2 not equals to data[%d]:%s\n", j, tailorData[j]);
        }
    }
    /* Test getSortKey. */
    tLen = u_strlen(tailorData[0]);
    kLen=ucol_getSortKey(coll, tailorData[0], tLen, expColl, 100);
    for (j=0; j<6; j++) {
        tLen = u_strlen(tailorData[j]);
        rLen = ucol_getSortKey(coll, tailorData[j], tLen, resColl, 100);
        if ( kLen!=rLen || uprv_memcmp(expColl, resColl, rLen*sizeof(uint8_t))!=0 ) {
            log_err("\n Data[%d] :%s  \tlen: %d key: ", j, tailorData[j], tLen);
            for(i = 0; i<rLen; i++) {
                log_err(" %02X", resColl[i]);
            }
        }
    }
    ucol_close(coll);

    log_verbose("\n\nTailoring test for s with caron:");
    ruleLen = u_strlen(rule2);
    coll = ucol_openRules(rule2, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
    tLen = u_strlen(tailorData2[0]);
    kLen=ucol_getSortKey(coll, tailorData2[0], tLen, expColl, 100);
    for (j=1; j<3; j++) {
        tLen = u_strlen(tailorData2[j]);
        rLen = ucol_getSortKey(coll, tailorData2[j], tLen, resColl, 100);
        if ( kLen!=rLen || uprv_memcmp(expColl, resColl, rLen*sizeof(uint8_t))!=0 ) {
            log_err("\n After tailoring Data[%d] :%s  \tlen: %d key: ", j, tailorData[j], tLen);
            for(i = 0; i<rLen; i++) {
                log_err(" %02X", resColl[i]);
            }
        }
    }
    ucol_close(coll);

    log_verbose("\n\nTailoring test for &z< ae with circumflex:");
    ruleLen = u_strlen(rule3);
    coll = ucol_openRules(rule3, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
    tLen = u_strlen(tailorData3[3]);
    kLen=ucol_getSortKey(coll, tailorData3[3], tLen, expColl, 100);
    log_verbose("\n Test Data[3] :%s  \tlen: %d key: ", aescstrdup(tailorData3[3], tLen), tLen);
    for(i = 0; i<kLen; i++) {
        log_verbose(" %02X", expColl[i]);
    }
    for (j=4; j<6; j++) {
        tLen = u_strlen(tailorData3[j]);
        rLen = ucol_getSortKey(coll, tailorData3[j], tLen, resColl, 100);

        if ( kLen!=rLen || uprv_memcmp(expColl, resColl, rLen*sizeof(uint8_t))!=0 ) {
            log_err("\n After tailoring Data[%d] :%s  \tlen: %d key: ", j, aescstrdup(tailorData3[j], tLen), tLen);
            for(i = 0; i<rLen; i++) {
                log_err(" %02X", resColl[i]);
            }
        }

        log_verbose("\n Test Data[%d] :%s  \tlen: %d key: ", j, aescstrdup(tailorData3[j], tLen), tLen);
         for(i = 0; i<rLen; i++) {
             log_verbose(" %02X", resColl[i]);
         }
    }
    ucol_close(coll);
}

static void
TestTailor6179(void)
{
    UErrorCode status = U_ZERO_ERROR;
    int32_t i;
    UCollator *coll =NULL;
    uint8_t  resColl[100];
    int32_t  rLen, tLen, ruleLen;
    /* &[last primary ignorable]<< a  &[first primary ignorable]<<b */
    static const UChar rule1[]={
            0x26,0x5B,0x6C,0x61,0x73,0x74,0x20,0x70,0x72,0x69,0x6D,0x61,0x72,0x79,
            0x20,0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,0x3C,0x20,0x61,0x20,
            0x26,0x5B,0x66,0x69,0x72,0x73,0x74,0x20,0x70,0x72,0x69,0x6D,0x61,0x72,0x79,0x20,
            0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,0x3C,0x62,0x20, 0};
    /* &[last secondary ignorable]<<< a &[first secondary ignorable]<<<b */
    static const UChar rule2[]={
            0x26,0x5B,0x6C,0x61,0x73,0x74,0x20,0x73,0x65,0x63,0x6F,0x6E,0x64,0x61,
            0x72,0x79,0x20,0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,0x3C,0x3C,
            0x61,0x20,0x26,0x5B,0x66,0x69,0x72,0x73,0x74,0x20,0x73,0x65,0x63,0x6F,0x6E,
            0x64,0x61,0x72,0x79,0x20,0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,
            0x3C,0x3C,0x20,0x62,0};

    static const UChar tData1[][4]={
        {0x61, 0},
        {0x62, 0},
        { 0xFDD0,0x009E, 0}
    };
    static const UChar tData2[][4]={
        {0x61, 0},
        {0x62, 0},
        { 0xFDD0,0x009E, 0}
     };

    /*
     * These values from FractionalUCA.txt will change,
     * and need to be updated here.
     * TODO: Make this not check for particular sort keys.
     * Instead, test that we get CEs before & after other ignorables; see ticket #6179.
     */
    static const uint8_t firstPrimaryIgnCE[]={1, 0x83, 1, 5, 0};
    static const uint8_t lastPrimaryIgnCE[]={1, 0xFC, 1, 5, 0};
    static const uint8_t firstSecondaryIgnCE[]={1, 1, 0xfe, 0};
    static const uint8_t lastSecondaryIgnCE[]={1, 1, 0xff, 0};

    UParseError parseError;

    /* Test [Last Primary ignorable] */

    log_verbose("Tailoring test: &[last primary ignorable]<<a  &[first primary ignorable]<<b\n");
    ruleLen = u_strlen(rule1);
    coll = ucol_openRules(rule1, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
    if (U_FAILURE(status)) {
        log_err_status(status, "Tailoring test: &[last primary ignorable] failed! -> %s\n", u_errorName(status));
        return;
    }
    tLen = u_strlen(tData1[0]);
    rLen = ucol_getSortKey(coll, tData1[0], tLen, resColl, 100);
    if (rLen != UPRV_LENGTHOF(lastPrimaryIgnCE) || uprv_memcmp(resColl, lastPrimaryIgnCE, rLen) != 0) {
        log_err("Bad result for &[lpi]<<a...: Data[%d] :%s  \tlen: %d key: ", 0, tData1[0], rLen);
        for(i = 0; i<rLen; i++) {
            log_err(" %02X", resColl[i]);
        }
        log_err("\n");
    }
    tLen = u_strlen(tData1[1]);
    rLen = ucol_getSortKey(coll, tData1[1], tLen, resColl, 100);
    if (rLen != UPRV_LENGTHOF(firstPrimaryIgnCE) || uprv_memcmp(resColl, firstPrimaryIgnCE, rLen) != 0) {
        log_err("Bad result for &[lpi]<<a...: Data[%d] :%s  \tlen: %d key: ", 1, tData1[1], rLen);
        for(i = 0; i<rLen; i++) {
            log_err(" %02X", resColl[i]);
        }
        log_err("\n");
    }
    ucol_close(coll);


    /* Test [Last Secondary ignorable] */
    log_verbose("Tailoring test: &[last secondary ignorable]<<<a  &[first secondary ignorable]<<<b\n");
    ruleLen = u_strlen(rule2);
    coll = ucol_openRules(rule2, ruleLen, UCOL_OFF, UCOL_TERTIARY, &parseError, &status);
    if (U_FAILURE(status)) {
        log_err("Tailoring test: &[last secondary ignorable] failed! -> %s\n", u_errorName(status));
        log_info("  offset=%d  \"%s\" | \"%s\"\n",
                 parseError.offset, aescstrdup(parseError.preContext, -1), aescstrdup(parseError.postContext, -1));
        return;
    }
    tLen = u_strlen(tData2[0]);
    rLen = ucol_getSortKey(coll, tData2[0], tLen, resColl, 100);
    if (rLen != UPRV_LENGTHOF(lastSecondaryIgnCE) || uprv_memcmp(resColl, lastSecondaryIgnCE, rLen) != 0) {
        log_err("Bad result for &[lsi]<<<a...: Data[%d] :%s  \tlen: %d key: ", 0, tData2[0], rLen);
        for(i = 0; i<rLen; i++) {
            log_err(" %02X", resColl[i]);
        }
        log_err("\n");
    }
    tLen = u_strlen(tData2[1]);
    rLen = ucol_getSortKey(coll, tData2[1], tLen, resColl, 100);
    if (rLen != UPRV_LENGTHOF(firstSecondaryIgnCE) || uprv_memcmp(resColl, firstSecondaryIgnCE, rLen) != 0) {
      log_err("Bad result for &[lsi]<<<a...: Data[%d] :%s  \tlen: %d key: ", 1, tData2[1], rLen);
      for(i = 0; i<rLen; i++) {
        log_err(" %02X", resColl[i]);
      }
      log_err("\n");
    }
    ucol_close(coll);
}

static void
TestUCAPrecontext(void)
{
    UErrorCode status = U_ZERO_ERROR;
    int32_t i, j;
    UCollator *coll =NULL;
    uint8_t  resColl[100], prevColl[100];
    int32_t  rLen, tLen, ruleLen;
    UChar rule1[256]= {0x26, 0xb7, 0x3c, 0x61, 0}; /* & middle-dot < a */
    UChar rule2[256]= {0x26, 0x4C, 0xb7, 0x3c, 0x3c, 0x61, 0};
    /* & l middle-dot << a  a is an expansion. */

    UChar tData1[][20]={
            { 0xb7, 0},  /* standalone middle dot(0xb7) */
            { 0x387, 0}, /* standalone middle dot(0x387) */
            { 0x61, 0},  /* a */
            { 0x6C, 0},  /* l */
            { 0x4C, 0x0332, 0},  /* l with [first primary ignorable] */
            { 0x6C, 0xb7, 0},  /* l with middle dot(0xb7) */
            { 0x6C, 0x387, 0}, /* l with middle dot(0x387) */
            { 0x4C, 0xb7, 0},  /* L with middle dot(0xb7) */
            { 0x4C, 0x387, 0}, /* L with middle dot(0x387) */
            { 0x6C, 0x61, 0x387, 0}, /* la  with middle dot(0x387) */
            { 0x4C, 0x61, 0xb7, 0},  /* La with middle dot(0xb7) */
     };

    log_verbose("\n\nEN collation:");
    coll = ucol_open("en", &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "Tailoring test: &z <<a|- failed! -> %s\n", u_errorName(status));
        return;
    }
    for (j=0; j<11; j++) {
        tLen = u_strlen(tData1[j]);
        rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
        if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
            log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
                    j, tData1[j]);
        }
        log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
        for(i = 0; i<rLen; i++) {
            log_verbose(" %02X", resColl[i]);
        }
        uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
     }
     ucol_close(coll);


     log_verbose("\n\nJA collation:");
     coll = ucol_open("ja", &status);
     if (U_FAILURE(status)) {
         log_err("Tailoring test: &z <<a|- failed!");
         return;
     }
     for (j=0; j<11; j++) {
         tLen = u_strlen(tData1[j]);
         rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
         if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
             log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
                     j, tData1[j]);
         }
         log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
         for(i = 0; i<rLen; i++) {
             log_verbose(" %02X", resColl[i]);
         }
         uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
      }
      ucol_close(coll);


      log_verbose("\n\nTailoring test: & middle dot < a ");
      ruleLen = u_strlen(rule1);
      coll = ucol_openRules(rule1, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
      if (U_FAILURE(status)) {
          log_err("Tailoring test: & middle dot < a failed!");
          return;
      }
      for (j=0; j<11; j++) {
          tLen = u_strlen(tData1[j]);
          rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
          if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
              log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
                      j, tData1[j]);
          }
          log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
          for(i = 0; i<rLen; i++) {
              log_verbose(" %02X", resColl[i]);
          }
          uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
       }
       ucol_close(coll);


       log_verbose("\n\nTailoring test: & l middle-dot << a ");
       ruleLen = u_strlen(rule2);
       coll = ucol_openRules(rule2, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
       if (U_FAILURE(status)) {
           log_err("Tailoring test: & l middle-dot << a failed!");
           return;
       }
       for (j=0; j<11; j++) {
           tLen = u_strlen(tData1[j]);
           rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
           if ((j>0) && (j!=3) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
               log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
                       j, tData1[j]);
           }
           if ((j==3)&&(strcmp((char *)resColl, (char *)prevColl)>0)) {
               log_err("\n Expecting smaller key than previous test case: Data[%d] :%s.",
                       j, tData1[j]);
           }
           log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
           for(i = 0; i<rLen; i++) {
               log_verbose(" %02X", resColl[i]);
           }
           uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
        }
        ucol_close(coll);
}

static void
TestOutOfBuffer5468(void)
{
    static const char *test = "\\u4e00";
    UChar ustr[256];
    int32_t ustr_length = u_unescape(test, ustr, 256);
    unsigned char shortKeyBuf[1];
    int32_t sortkey_length;
    UErrorCode status = U_ZERO_ERROR;
    static UCollator *coll = NULL;

    coll = ucol_open("root", &status);
    if(U_FAILURE(status)) {
      log_err_status(status, "Couldn't open UCA -> %s\n", u_errorName(status));
      return;
    }
    ucol_setStrength(coll, UCOL_PRIMARY);
    ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
    ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
    if (U_FAILURE(status)) {
      log_err("Failed setting attributes\n");
      return;
    }

    sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, shortKeyBuf, sizeof(shortKeyBuf));
    if (sortkey_length != 4) {
        log_err("expecting length of sortKey is 4  got:%d ", sortkey_length);
    }
    log_verbose("length of sortKey is %d", sortkey_length);
    ucol_close(coll);
}

#define TSKC_DATA_SIZE 5
#define TSKC_BUF_SIZE  50
static void
TestSortKeyConsistency(void)
{
    UErrorCode icuRC = U_ZERO_ERROR;
    UCollator* ucol;
    UChar data[] = { 0xFFFD, 0x0006, 0x0006, 0x0006, 0xFFFD};

    uint8_t bufFull[TSKC_DATA_SIZE][TSKC_BUF_SIZE];
    uint8_t bufPart[TSKC_DATA_SIZE][TSKC_BUF_SIZE];
    int32_t i, j, i2;

    ucol = ucol_openFromShortString("LEN_S4", FALSE, NULL, &icuRC);
    if (U_FAILURE(icuRC))
    {
        log_err_status(icuRC, "ucol_openFromShortString failed -> %s\n", u_errorName(icuRC));
        return;
    }

    for (i = 0; i < TSKC_DATA_SIZE; i++)
    {
        UCharIterator uiter;
        uint32_t state[2] = { 0, 0 };
        int32_t dataLen = i+1;
        for (j=0; j<TSKC_BUF_SIZE; j++)
            bufFull[i][j] = bufPart[i][j] = 0;

        /* Full sort key */
        ucol_getSortKey(ucol, data, dataLen, bufFull[i], TSKC_BUF_SIZE);

        /* Partial sort key */
        uiter_setString(&uiter, data, dataLen);
        ucol_nextSortKeyPart(ucol, &uiter, state, bufPart[i], TSKC_BUF_SIZE, &icuRC);
        if (U_FAILURE(icuRC))
        {
            log_err("ucol_nextSortKeyPart failed\n");
            ucol_close(ucol);
            return;
        }

        for (i2=0; i2<i; i2++)
        {
            UBool fullMatch = TRUE;
            UBool partMatch = TRUE;
            for (j=0; j<TSKC_BUF_SIZE; j++)
            {
                fullMatch = fullMatch && (bufFull[i][j] != bufFull[i2][j]);
                partMatch = partMatch && (bufPart[i][j] != bufPart[i2][j]);
            }
            if (fullMatch != partMatch) {
                log_err(fullMatch ? "full key was consistent, but partial key changed\n"
                                  : "partial key was consistent, but full key changed\n");
                ucol_close(ucol);
                return;
            }
        }
    }

    /*=============================================*/
   ucol_close(ucol);
}

/* ticket: 6101 */
static void TestCroatianSortKey(void) {
    const char* collString = "LHR_AN_CX_EX_FX_HX_NX_S3";
    UErrorCode status = U_ZERO_ERROR;
    UCollator *ucol;
    UCharIterator iter;

    static const UChar text[] = { 0x0044, 0xD81A };

    size_t length = UPRV_LENGTHOF(text);

    uint8_t textSortKey[32];
    size_t lenSortKey = 32;
    size_t actualSortKeyLen;
    uint32_t uStateInfo[2] = { 0, 0 };

    ucol = ucol_openFromShortString(collString, FALSE, NULL, &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ucol_openFromShortString error in Craotian test. -> %s\n", u_errorName(status));
        return;
    }

    uiter_setString(&iter, text, (int32_t)length);

    actualSortKeyLen = ucol_nextSortKeyPart(
        ucol, &iter, (uint32_t*)uStateInfo,
        textSortKey, (int32_t)lenSortKey, &status
        );

    if (actualSortKeyLen == lenSortKey) {
        log_err("ucol_nextSortKeyPart did not give correct result in Croatian test.\n");
    }

    ucol_close(ucol);
}

/* ticket: 6140 */
/* This test ensures that codepoints such as 0x3099 are flagged correctly by the collator since
 * they are both Hiragana and Katakana
 */
#define SORTKEYLEN 50
static void TestHiragana(void) {
    UErrorCode status = U_ZERO_ERROR;
    UCollator* ucol;
    UCollationResult strcollresult;
    UChar data1[] = { 0x3058, 0x30B8 }; /* Hiragana and Katakana letter Zi */
    UChar data2[] = { 0x3057, 0x3099, 0x30B7, 0x3099 };
    int32_t data1Len = UPRV_LENGTHOF(data1);
    int32_t data2Len = UPRV_LENGTHOF(data2);
    int32_t i, j;
    uint8_t sortKey1[SORTKEYLEN];
    uint8_t sortKey2[SORTKEYLEN];

    UCharIterator uiter1;
    UCharIterator uiter2;
    uint32_t state1[2] = { 0, 0 };
    uint32_t state2[2] = { 0, 0 };
    int32_t keySize1;
    int32_t keySize2;

    ucol = ucol_openFromShortString("LJA_AN_CX_EX_FX_HO_NX_S4", FALSE, NULL,
            &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "Error status: %s; Unable to open collator from short string.\n", u_errorName(status));
        return;
    }

    /* Start of full sort keys */
    /* Full sort key1 */
    keySize1 = ucol_getSortKey(ucol, data1, data1Len, sortKey1, SORTKEYLEN);
    /* Full sort key2 */
    keySize2 = ucol_getSortKey(ucol, data2, data2Len, sortKey2, SORTKEYLEN);
    if (keySize1 == keySize2) {
        for (i = 0; i < keySize1; i++) {
            if (sortKey1[i] != sortKey2[i]) {
                log_err("Full sort keys are different. Should be equal.");
            }
        }
    } else {
        log_err("Full sort keys sizes doesn't match: %d %d", keySize1, keySize2);
    }
    /* End of full sort keys */

    /* Start of partial sort keys */
    /* Partial sort key1 */
    uiter_setString(&uiter1, data1, data1Len);
    keySize1 = ucol_nextSortKeyPart(ucol, &uiter1, state1, sortKey1, SORTKEYLEN, &status);
    /* Partial sort key2 */
    uiter_setString(&uiter2, data2, data2Len);
    keySize2 = ucol_nextSortKeyPart(ucol, &uiter2, state2, sortKey2, SORTKEYLEN, &status);
    if (U_SUCCESS(status) && keySize1 == keySize2) {
        for (j = 0; j < keySize1; j++) {
            if (sortKey1[j] != sortKey2[j]) {
                log_err("Partial sort keys are different. Should be equal");
            }
        }
    } else {
        log_err("Error Status: %s or Partial sort keys sizes doesn't match: %d %d", u_errorName(status), keySize1, keySize2);
    }
    /* End of partial sort keys */

    /* Start of strcoll */
    /* Use ucol_strcoll() to determine ordering */
    strcollresult = ucol_strcoll(ucol, data1, data1Len, data2, data2Len);
    if (strcollresult != UCOL_EQUAL) {
        log_err("Result from ucol_strcoll() should be UCOL_EQUAL.");
    }

    ucol_close(ucol);
}

/* Convenient struct for running collation tests */
typedef struct {
  const UChar source[MAX_TOKEN_LEN];  /* String on left */
  const UChar target[MAX_TOKEN_LEN];  /* String on right */
  UCollationResult result;            /* -1, 0 or +1, depending on collation */
} OneTestCase;

/*
 * Utility function to test one collation test case.
 * @param testcases Array of test cases.
 * @param n_testcases Size of the array testcases.
 * @param str_rules Array of rules.  These rules should be specifying the same rule in different formats.
 * @param n_rules Size of the array str_rules.
 */
static void doTestOneTestCase(const OneTestCase testcases[],
                              int n_testcases,
                              const char* str_rules[],
                              int n_rules)
{
  int rule_no, testcase_no;
  UChar rule[500]; 
  int32_t length = 0;
  UErrorCode status = U_ZERO_ERROR;
  UParseError parse_error;
  UCollator  *myCollation;

  for (rule_no = 0; rule_no < n_rules; ++rule_no) {

    length = u_unescape(str_rules[rule_no], rule, 500);
    if (length == 0) {
        log_err("ERROR: The rule cannot be unescaped: %s\n");
        return;
    }
    myCollation = ucol_openRules(rule, length, UCOL_ON, UCOL_TERTIARY, &parse_error, &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        log_info("  offset=%d  \"%s\" | \"%s\"\n",
                 parse_error.offset,
                 aescstrdup(parse_error.preContext, -1),
                 aescstrdup(parse_error.postContext, -1));
        return;
    }
    log_verbose("Testing the <<* syntax\n");
    ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
    ucol_setStrength(myCollation, UCOL_TERTIARY);
    for (testcase_no = 0; testcase_no < n_testcases; ++testcase_no) {
      doTest(myCollation,
             testcases[testcase_no].source,
             testcases[testcase_no].target,
             testcases[testcase_no].result
             );
    }
    ucol_close(myCollation);
  }
}

const static OneTestCase rangeTestcases[] = {
  { {0x0061},                            {0x0062},                          UCOL_LESS }, /* "a" < "b" */
  { {0x0062},                            {0x0063},                          UCOL_LESS }, /* "b" < "c" */
  { {0x0061},                            {0x0063},                          UCOL_LESS }, /* "a" < "c" */

  { {0x0062},                            {0x006b},                          UCOL_LESS }, /* "b" << "k" */
  { {0x006b},                            {0x006c},                          UCOL_LESS }, /* "k" << "l" */
  { {0x0062},                            {0x006c},                          UCOL_LESS }, /* "b" << "l" */
  { {0x0061},                            {0x006c},                          UCOL_LESS }, /* "a" < "l" */
  { {0x0061},                            {0x006d},                          UCOL_LESS },  /* "a" < "m" */

  { {0x0079},                            {0x006d},                          UCOL_LESS },  /* "y" < "f" */
  { {0x0079},                            {0x0067},                          UCOL_LESS },  /* "y" < "g" */
  { {0x0061},                            {0x0068},                          UCOL_LESS },  /* "y" < "h" */
  { {0x0061},                            {0x0065},                          UCOL_LESS },  /* "g" < "e" */

  { {0x0061},                            {0x0031},                          UCOL_EQUAL }, /* "a" = "1" */
  { {0x0061},                            {0x0032},                          UCOL_EQUAL }, /* "a" = "2" */
  { {0x0061},                            {0x0033},                          UCOL_EQUAL }, /* "a" = "3" */
  { {0x0061},                            {0x0066},                          UCOL_LESS }, /* "a" < "f" */
  { {0x006c, 0x0061},                    {0x006b, 0x0062},                  UCOL_LESS },  /* "la" < "123" */
  { {0x0061, 0x0061, 0x0061},            {0x0031, 0x0032, 0x0033},          UCOL_EQUAL }, /* "aaa" = "123" */
  { {0x0062},                            {0x007a},                          UCOL_LESS },  /* "b" < "z" */
  { {0x0061, 0x007a, 0x0062},            {0x0032, 0x0079, 0x006d},          UCOL_LESS }, /* "azm" = "2yc" */
};

static int nRangeTestcases = UPRV_LENGTHOF(rangeTestcases);

const static OneTestCase rangeTestcasesSupplemental[] = {
  { {0x4e00},                            {0xfffb},                          UCOL_LESS }, /* U+4E00 < U+FFFB */
  { {0xfffb},                            {0xd800, 0xdc00},                  UCOL_LESS }, /* U+FFFB < U+10000 */
  { {0xd800, 0xdc00},                    {0xd800, 0xdc01},                  UCOL_LESS }, /* U+10000 < U+10001 */
  { {0x4e00},                            {0xd800, 0xdc01},                  UCOL_LESS }, /* U+4E00 < U+10001 */
  { {0xd800, 0xdc01},                    {0xd800, 0xdc02},                  UCOL_LESS }, /* U+10000 < U+10001 */
  { {0xd800, 0xdc01},                    {0xd800, 0xdc02},                  UCOL_LESS }, /* U+10000 < U+10001 */
  { {0x4e00},                            {0xd800, 0xdc02},                  UCOL_LESS }, /* U+4E00 < U+10001 */
};

static int nRangeTestcasesSupplemental = UPRV_LENGTHOF(rangeTestcasesSupplemental);

const static OneTestCase rangeTestcasesQwerty[] = {
  { {0x0071},                            {0x0077},                          UCOL_LESS }, /* "q" < "w" */
  { {0x0077},                            {0x0065},                          UCOL_LESS }, /* "w" < "e" */

  { {0x0079},                            {0x0075},                          UCOL_LESS }, /* "y" < "u" */
  { {0x0071},                            {0x0075},                          UCOL_LESS }, /* "q" << "u" */

  { {0x0074},                            {0x0069},                          UCOL_LESS }, /* "t" << "i" */
  { {0x006f},                            {0x0070},                          UCOL_LESS }, /* "o" << "p" */

  { {0x0079},                            {0x0065},                          UCOL_LESS },  /* "y" < "e" */
  { {0x0069},                            {0x0075},                          UCOL_LESS },  /* "i" < "u" */

  { {0x0071, 0x0075, 0x0065, 0x0073, 0x0074},
    {0x0077, 0x0065, 0x0072, 0x0065},                                       UCOL_LESS }, /* "quest" < "were" */
  { {0x0071, 0x0075, 0x0061, 0x0063, 0x006b},
    {0x0071, 0x0075, 0x0065, 0x0073, 0x0074},                               UCOL_LESS }, /* "quack" < "quest" */
};

static int nRangeTestcasesQwerty = UPRV_LENGTHOF(rangeTestcasesQwerty);

static void TestSameStrengthList(void)
{
  const char* strRules[] = {
    /* Normal */
    "&a<b<c<d &b<<k<<l<<m &k<<<x<<<y<<<z  &y<f<g<h<e &a=1=2=3", 

    /* Lists */
    "&a<*bcd &b<<*klm &k<<<*xyz &y<*fghe &a=*123", 
  };
  doTestOneTestCase(rangeTestcases, nRangeTestcases, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSameStrengthListQuoted(void)
{
  const char* strRules[] = {
    /* Lists with quoted characters */
    "&\\u0061<*bcd &b<<*klm &k<<<*xyz &y<*f\\u0067\\u0068e &a=*123",
    "&'\\u0061'<*bcd &b<<*klm &k<<<*xyz &y<*f'\\u0067\\u0068'e &a=*123",

    "&\\u0061<*b\\u0063d &b<<*klm &k<<<*xyz &\\u0079<*fgh\\u0065 &a=*\\u0031\\u0032\\u0033",
    "&'\\u0061'<*b'\\u0063'd &b<<*klm &k<<<*xyz &'\\u0079'<*fgh'\\u0065' &a=*'\\u0031\\u0032\\u0033'",

    "&\\u0061<*\\u0062c\\u0064 &b<<*klm &k<<<*xyz  &y<*fghe &a=*\\u0031\\u0032\\u0033", 
    "&'\\u0061'<*'\\u0062'c'\\u0064' &b<<*klm &k<<<*xyz  &y<*fghe &a=*'\\u0031\\u0032\\u0033'", 
  };
  doTestOneTestCase(rangeTestcases, nRangeTestcases, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSameStrengthListSupplemental(void)
{
  const char* strRules[] = {
    "&\\u4e00<\\ufffb<\\U00010000<\\U00010001<\\U00010002",
    "&\\u4e00<\\ufffb<\\ud800\\udc00<\\ud800\\udc01<\\ud800\\udc02",
    "&\\u4e00<*\\ufffb\\U00010000\\U00010001\\U00010002",
    "&\\u4e00<*\\ufffb\\ud800\\udc00\\ud800\\udc01\\ud800\\udc02",
  };
  doTestOneTestCase(rangeTestcasesSupplemental, nRangeTestcasesSupplemental, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSameStrengthListQwerty(void)
{
  const char* strRules[] = {
    "&q<w<e<r &w<<t<<y<<u &t<<<i<<<o<<<p &o=a=s=d",   /* Normal */
    "&q<*wer &w<<*tyu &t<<<*iop &o=*asd",             /* Lists  */
    "&\\u0071<\\u0077<\\u0065<\\u0072 &\\u0077<<\\u0074<<\\u0079<<\\u0075 &\\u0074<<<\\u0069<<<\\u006f<<<\\u0070 &\\u006f=\\u0061=\\u0073=\\u0064",
    "&'\\u0071'<\\u0077<\\u0065<\\u0072 &\\u0077<<'\\u0074'<<\\u0079<<\\u0075 &\\u0074<<<\\u0069<<<'\\u006f'<<<\\u0070 &\\u006f=\\u0061='\\u0073'=\\u0064",
    "&\\u0071<*\\u0077\\u0065\\u0072 &\\u0077<<*\\u0074\\u0079\\u0075 &\\u0074<<<*\\u0069\\u006f\\u0070 &\\u006f=*\\u0061\\u0073\\u0064",

    /* Quoted characters also will work if two quoted characters are not consecutive.  */
    "&\\u0071<*'\\u0077'\\u0065\\u0072 &\\u0077<<*\\u0074'\\u0079'\\u0075 &\\u0074<<<*\\u0069\\u006f'\\u0070' &'\\u006f'=*\\u0061\\u0073\\u0064",

    /* Consecutive quoted characters do not work, because a '' will be treated as a quote character. */
    /* "&\\u0071<*'\\u0077''\\u0065''\\u0072' &\\u0077<<*'\\u0074''\\u0079''\\u0075' &\\u0074<<<*'\\u0069''\\u006f''\\u0070' &'\\u006f'=*\\u0061\\u0073\\u0064",*/

 };
  doTestOneTestCase(rangeTestcasesQwerty, nRangeTestcasesQwerty, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSameStrengthListQuotedQwerty(void)
{
  const char* strRules[] = {
    "&q<w<e<r &w<<t<<y<<u &t<<<i<<<o<<<p &o=a=s=d",   /* Normal */
    "&q<*wer &w<<*tyu &t<<<*iop &o=*asd",             /* Lists  */
    "&q<*w'e'r &w<<*'t'yu &t<<<*io'p' &o=*'a's'd'",   /* Lists with quotes */

    /* Lists with continuous quotes may not work, because '' will be treated as a quote character. */
    /* "&q<*'w''e''r' &w<<*'t''y''u' &t<<<*'i''o''p' &o=*'a''s''d'", */
   };
  doTestOneTestCase(rangeTestcasesQwerty, nRangeTestcasesQwerty, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSameStrengthListRanges(void)
{
  const char* strRules[] = {
    "&a<*b-d &b<<*k-m &k<<<*x-z &y<*f-he &a=*1-3",
  };
  doTestOneTestCase(rangeTestcases, nRangeTestcases, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSameStrengthListSupplementalRanges(void)
{
  const char* strRules[] = {
    /* Note: U+FFFD..U+FFFF are not tailorable, so a range cannot include them. */
    "&\\u4e00<*\\ufffb\\U00010000-\\U00010002",
  };
  doTestOneTestCase(rangeTestcasesSupplemental, nRangeTestcasesSupplemental, strRules, UPRV_LENGTHOF(strRules));
}

static void TestSpecialCharacters(void)
{
  const char* strRules[] = {
    /* Normal */
    "&';'<'+'<','<'-'<'&'<'*'",

    /* List */
    "&';'<*'+,-&*'",

    /* Range */
    "&';'<*'+'-'-&*'", 
  };

  const static OneTestCase specialCharacterStrings[] = {
    { {0x003b}, {0x002b}, UCOL_LESS },  /* ; < + */
    { {0x002b}, {0x002c}, UCOL_LESS },  /* + < , */
    { {0x002c}, {0x002d}, UCOL_LESS },  /* , < - */
    { {0x002d}, {0x0026}, UCOL_LESS },  /* - < & */
  };
  doTestOneTestCase(specialCharacterStrings, UPRV_LENGTHOF(specialCharacterStrings), strRules, UPRV_LENGTHOF(strRules));
}

static void TestPrivateUseCharacters(void)
{
  const char* strRules[] = {
    /* Normal */
    "&'\\u5ea7'<'\\uE2D8'<'\\uE2D9'<'\\uE2DA'<'\\uE2DB'<'\\uE2DC'<'\\u4e8d'",
    "&\\u5ea7<\\uE2D8<\\uE2D9<\\uE2DA<\\uE2DB<\\uE2DC<\\u4e8d", 
  };

  const static OneTestCase privateUseCharacterStrings[] = {
    { {0x5ea7}, {0xe2d8}, UCOL_LESS },
    { {0xe2d8}, {0xe2d9}, UCOL_LESS },
    { {0xe2d9}, {0xe2da}, UCOL_LESS },
    { {0xe2da}, {0xe2db}, UCOL_LESS },
    { {0xe2db}, {0xe2dc}, UCOL_LESS },
    { {0xe2dc}, {0x4e8d}, UCOL_LESS },
  };
  doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));
}

static void TestPrivateUseCharactersInList(void)
{
  const char* strRules[] = {
    /* List */
    "&'\\u5ea7'<*'\\uE2D8\\uE2D9\\uE2DA\\uE2DB\\uE2DC\\u4e8d'",
    /* "&'\\u5ea7'<*\\uE2D8'\\uE2D9\\uE2DA'\\uE2DB'\\uE2DC\\u4e8d'", */
    "&\\u5ea7<*\\uE2D8\\uE2D9\\uE2DA\\uE2DB\\uE2DC\\u4e8d",
  };

  const static OneTestCase privateUseCharacterStrings[] = {
    { {0x5ea7}, {0xe2d8}, UCOL_LESS },
    { {0xe2d8}, {0xe2d9}, UCOL_LESS },
    { {0xe2d9}, {0xe2da}, UCOL_LESS },
    { {0xe2da}, {0xe2db}, UCOL_LESS },
    { {0xe2db}, {0xe2dc}, UCOL_LESS },
    { {0xe2dc}, {0x4e8d}, UCOL_LESS },
  };
  doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));
}

static void TestPrivateUseCharactersInRange(void)
{
  const char* strRules[] = {
    /* Range */
    "&'\\u5ea7'<*'\\uE2D8'-'\\uE2DC\\u4e8d'",
    "&\\u5ea7<*\\uE2D8-\\uE2DC\\u4e8d",
    /* "&\\u5ea7<\\uE2D8'\\uE2D8'-'\\uE2D9'\\uE2DA-\\uE2DB\\uE2DC\\u4e8d", */
  };

  const static OneTestCase privateUseCharacterStrings[] = {
    { {0x5ea7}, {0xe2d8}, UCOL_LESS },
    { {0xe2d8}, {0xe2d9}, UCOL_LESS },
    { {0xe2d9}, {0xe2da}, UCOL_LESS },
    { {0xe2da}, {0xe2db}, UCOL_LESS },
    { {0xe2db}, {0xe2dc}, UCOL_LESS },
    { {0xe2dc}, {0x4e8d}, UCOL_LESS },
  };
  doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));
}

static void TestInvalidListsAndRanges(void)
{
  const char* invalidRules[] = {
    /* Range not in starred expression */
    "&\\ufffe<\\uffff-\\U00010002",

    /* Range without start */
    "&a<*-c",

    /* Range without end */
    "&a<*b-",

    /* More than one hyphen */
    "&a<*b-g-l",

    /* Range in the wrong order */
    "&a<*k-b",

  };

  UChar rule[500];
  UErrorCode status = U_ZERO_ERROR;
  UParseError parse_error;
  int n_rules = UPRV_LENGTHOF(invalidRules);
  int rule_no;
  int length;
  UCollator  *myCollation;

  for (rule_no = 0; rule_no < n_rules; ++rule_no) {

    length = u_unescape(invalidRules[rule_no], rule, 500);
    if (length == 0) {
        log_err("ERROR: The rule cannot be unescaped: %s\n");
        return;
    }
    myCollation = ucol_openRules(rule, length, UCOL_ON, UCOL_TERTIARY, &parse_error, &status);
    (void)myCollation;      /* Suppress set but not used warning. */
    if(!U_FAILURE(status)){
      log_err("ERROR: Could not cause a failure as expected: \n");
    }
    status = U_ZERO_ERROR;
  }
}

/*
 * This test ensures that characters placed before a character in a different script have the same lead byte
 * in their collation key before and after script reordering.
 */
static void TestBeforeRuleWithScriptReordering(void)
{
    UParseError error;
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    char srules[500] = "&[before 1]\\u03b1 < \\u0e01";
    UChar rules[500];
    uint32_t rulesLength = 0;
    int32_t reorderCodes[1] = {USCRIPT_GREEK};
    UCollationResult collResult;

    uint8_t baseKey[256];
    uint32_t baseKeyLength;
    uint8_t beforeKey[256];
    uint32_t beforeKeyLength;

    UChar base[] = { 0x03b1 }; /* base */
    int32_t baseLen = UPRV_LENGTHOF(base);

    UChar before[] = { 0x0e01 }; /* ko kai */
    int32_t beforeLen = UPRV_LENGTHOF(before);

    /*UChar *data[] = { before, base };
    genericRulesStarter(srules, data, 2);*/
    
    log_verbose("Testing the &[before 1] rule with [reorder grek]\n");

    (void)beforeKeyLength;   /* Suppress set but not used warnings. */
    (void)baseKeyLength;

    /* build collator */
    log_verbose("Testing the &[before 1] rule with [scriptReorder grek]\n");

    rulesLength = u_unescape(srules, rules, UPRV_LENGTHOF(rules));
    myCollation = ucol_openRules(rules, rulesLength, UCOL_ON, UCOL_TERTIARY, &error, &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }

    /* check collation results - before rule applied but not script reordering */
    collResult = ucol_strcoll(myCollation, base, baseLen, before, beforeLen);
    if (collResult != UCOL_GREATER) {
        log_err("Collation result not correct before script reordering = %d\n", collResult);
    }

    /* check the lead byte of the collation keys before script reordering */
    baseKeyLength = ucol_getSortKey(myCollation, base, baseLen, baseKey, 256);
    beforeKeyLength = ucol_getSortKey(myCollation, before, beforeLen, beforeKey, 256);
    if (baseKey[0] != beforeKey[0]) {
      log_err("Different lead byte for sort keys using before rule and before script reordering. base character lead byte = %02x, before character lead byte = %02x\n", baseKey[0], beforeKey[0]);
   }

    /* reorder the scripts */
    ucol_setReorderCodes(myCollation, reorderCodes, 1, &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: while setting script order: %s\n", myErrorName(status));
        return;
    }

    /* check collation results - before rule applied and after script reordering */
    collResult = ucol_strcoll(myCollation, base, baseLen, before, beforeLen);
    if (collResult != UCOL_GREATER) {
        log_err("Collation result not correct after script reordering = %d\n", collResult);
    }
    
    /* check the lead byte of the collation keys after script reordering */
    ucol_getSortKey(myCollation, base, baseLen, baseKey, 256);
    ucol_getSortKey(myCollation, before, beforeLen, beforeKey, 256);
    if (baseKey[0] != beforeKey[0]) {
        log_err("Different lead byte for sort keys using before rule and after script reordering. base character lead byte = %02x, before character lead byte = %02x\n", baseKey[0], beforeKey[0]);
    }

    ucol_close(myCollation);
}

/*
 * Test that in a primary-compressed sort key all bytes except the first one are unchanged under script reordering.
 */
static void TestNonLeadBytesDuringCollationReordering(void)
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    int32_t reorderCodes[1] = {USCRIPT_GREEK};

    uint8_t baseKey[256];
    uint32_t baseKeyLength;
    uint8_t reorderKey[256];
    uint32_t reorderKeyLength;

    UChar testString[] = { 0x03b1, 0x03b2, 0x03b3 };
    
    uint32_t i;


    log_verbose("Testing non-lead bytes in a sort key with and without reordering\n");

    /* build collator tertiary */
    myCollation = ucol_open("", &status);
    ucol_setStrength(myCollation, UCOL_TERTIARY);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }
    baseKeyLength = ucol_getSortKey(myCollation, testString, UPRV_LENGTHOF(testString), baseKey, 256);

    ucol_setReorderCodes(myCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
        return;
    }
    reorderKeyLength = ucol_getSortKey(myCollation, testString, UPRV_LENGTHOF(testString), reorderKey, 256);
    
    if (baseKeyLength != reorderKeyLength) {
        log_err("Key lengths not the same during reordering.\n");
        return;
    }
    
    for (i = 1; i < baseKeyLength; i++) {
        if (baseKey[i] != reorderKey[i]) {
            log_err("Collation key bytes not the same at position %d.\n", i);
            return;
        }
    } 
    ucol_close(myCollation);

    /* build collator quaternary */
    myCollation = ucol_open("", &status);
    ucol_setStrength(myCollation, UCOL_QUATERNARY);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }
    baseKeyLength = ucol_getSortKey(myCollation, testString, UPRV_LENGTHOF(testString), baseKey, 256);

    ucol_setReorderCodes(myCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
        return;
    }
    reorderKeyLength = ucol_getSortKey(myCollation, testString, UPRV_LENGTHOF(testString), reorderKey, 256);
    
    if (baseKeyLength != reorderKeyLength) {
        log_err("Key lengths not the same during reordering.\n");
        return;
    }
    
    for (i = 1; i < baseKeyLength; i++) {
        if (baseKey[i] != reorderKey[i]) {
            log_err("Collation key bytes not the same at position %d.\n", i);
            return;
        }
    }
    ucol_close(myCollation);
}

/*
 * Test reordering API.
 */
static void TestReorderingAPI(void)
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    int32_t reorderCodes[3] = {USCRIPT_GREEK, USCRIPT_HAN, UCOL_REORDER_CODE_PUNCTUATION};
    int32_t duplicateReorderCodes[] = {USCRIPT_HIRAGANA, USCRIPT_GREEK, UCOL_REORDER_CODE_CURRENCY, USCRIPT_KATAKANA};
    int32_t reorderCodesStartingWithDefault[] = {UCOL_REORDER_CODE_DEFAULT, USCRIPT_GREEK, USCRIPT_HAN, UCOL_REORDER_CODE_PUNCTUATION};
    int32_t reorderCodeNone = UCOL_REORDER_CODE_NONE;
    UCollationResult collResult;
    int32_t retrievedReorderCodesLength;
    int32_t retrievedReorderCodes[10];
    UChar greekString[] = { 0x03b1 };
    UChar punctuationString[] = { 0x203e };
    int loopIndex;

    log_verbose("Testing non-lead bytes in a sort key with and without reordering\n");

    /* build collator tertiary */
    myCollation = ucol_open("", &status);
    ucol_setStrength(myCollation, UCOL_TERTIARY);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }

    /* set the reorderding */
    ucol_setReorderCodes(myCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
        return;
    }
    
    /* get the reordering */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
    if (status != U_BUFFER_OVERFLOW_ERROR) {
        log_err_status(status, "ERROR: getting error codes should have returned U_BUFFER_OVERFLOW_ERROR : %s\n", myErrorName(status));
        return;
    }
    status = U_ZERO_ERROR;
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(reorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(reorderCodes));
        return;
    }
    /* now let's really get it */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, retrievedReorderCodes, UPRV_LENGTHOF(retrievedReorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: getting reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(reorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(reorderCodes));
        return;
    }
    for (loopIndex = 0; loopIndex < retrievedReorderCodesLength; loopIndex++) {
        if (retrievedReorderCodes[loopIndex] != reorderCodes[loopIndex]) {
            log_err_status(status, "ERROR: retrieved reorder code doesn't match set reorder code at index %d\n", loopIndex);
            return;
        }
    }
    collResult = ucol_strcoll(myCollation, greekString, UPRV_LENGTHOF(greekString), punctuationString, UPRV_LENGTHOF(punctuationString));
    if (collResult != UCOL_LESS) {
        log_err_status(status, "ERROR: collation result should have been UCOL_LESS\n");
        return;
    }
    
    /* clear the reordering */
    ucol_setReorderCodes(myCollation, NULL, 0, &status);    
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes to NULL: %s\n", myErrorName(status));
        return;
    }

    /* get the reordering again */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
    if (retrievedReorderCodesLength != 0) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, 0);
        return;
    }

    collResult = ucol_strcoll(myCollation, greekString, UPRV_LENGTHOF(greekString), punctuationString, UPRV_LENGTHOF(punctuationString));
    if (collResult != UCOL_GREATER) {
        log_err_status(status, "ERROR: collation result should have been UCOL_GREATER\n");
        return;
    }

    /* clear the reordering using [NONE] */
    ucol_setReorderCodes(myCollation, &reorderCodeNone, 1, &status);    
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes to [NONE]: %s\n", myErrorName(status));
        return;
    }

    /* get the reordering again */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
    if (retrievedReorderCodesLength != 0) {
        log_err_status(status,
                       "ERROR: [NONE] retrieved reorder codes length was %d but should have been 0\n",
                       retrievedReorderCodesLength);
        return;
    }

    /* test for error condition on duplicate reorder codes */
    ucol_setReorderCodes(myCollation, duplicateReorderCodes, UPRV_LENGTHOF(duplicateReorderCodes), &status);
    if (!U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting duplicate reorder codes did not generate a failure\n");
        return;
    }
    
    status = U_ZERO_ERROR;
    /* test for reorder codes after a reset code */
    ucol_setReorderCodes(myCollation, reorderCodesStartingWithDefault, UPRV_LENGTHOF(reorderCodesStartingWithDefault), &status);
    if (!U_FAILURE(status)) {
        log_err_status(status, "ERROR: reorderd code sequence starting with default and having following codes didn't cause an error\n");
        return;
    }
    
    ucol_close(myCollation);
}

/*
 * Test reordering API.
 */
static void TestReorderingAPIWithRuleCreatedCollator(void)
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    UChar rules[90];
    static const int32_t rulesReorderCodes[2] = {USCRIPT_HAN, USCRIPT_GREEK};
    static const int32_t reorderCodes[3] = {USCRIPT_GREEK, USCRIPT_HAN, UCOL_REORDER_CODE_PUNCTUATION};
    static const int32_t onlyDefault[1] = {UCOL_REORDER_CODE_DEFAULT};
    UCollationResult collResult;
    int32_t retrievedReorderCodesLength;
    int32_t retrievedReorderCodes[10];
    static const UChar greekString[] = { 0x03b1 };
    static const UChar punctuationString[] = { 0x203e };
    static const UChar hanString[] = { 0x65E5, 0x672C };
    int loopIndex;

    log_verbose("Testing non-lead bytes in a sort key with and without reordering\n");

    /* build collator from rules */
    u_uastrcpy(rules, "[reorder Hani Grek]");
    myCollation = ucol_openRules(rules, u_strlen(rules), UCOL_DEFAULT, UCOL_TERTIARY, NULL, &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }

    /* get the reordering */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, retrievedReorderCodes, UPRV_LENGTHOF(retrievedReorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: getting reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(rulesReorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(rulesReorderCodes));
        return;
    }
    for (loopIndex = 0; loopIndex < retrievedReorderCodesLength; loopIndex++) {
        if (retrievedReorderCodes[loopIndex] != rulesReorderCodes[loopIndex]) {
            log_err_status(status, "ERROR: retrieved reorder code doesn't match set reorder code at index %d\n", loopIndex);
            return;
        }
    }
    collResult = ucol_strcoll(myCollation, greekString, UPRV_LENGTHOF(greekString), hanString, UPRV_LENGTHOF(hanString));
    if (collResult != UCOL_GREATER) {
        log_err_status(status, "ERROR: collation result should have been UCOL_GREATER\n");
        return;
    }

    /* set the reordering */
    ucol_setReorderCodes(myCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
        return;
    }

    /* get the reordering */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
    if (status != U_BUFFER_OVERFLOW_ERROR) {
        log_err_status(status, "ERROR: getting error codes should have returned U_BUFFER_OVERFLOW_ERROR : %s\n", myErrorName(status));
        return;
    }
    status = U_ZERO_ERROR;
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(reorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(reorderCodes));
        return;
    }
    /* now let's really get it */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, retrievedReorderCodes, UPRV_LENGTHOF(retrievedReorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: getting reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(reorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(reorderCodes));
        return;
    }
    for (loopIndex = 0; loopIndex < retrievedReorderCodesLength; loopIndex++) {
        if (retrievedReorderCodes[loopIndex] != reorderCodes[loopIndex]) {
            log_err_status(status, "ERROR: retrieved reorder code doesn't match set reorder code at index %d\n", loopIndex);
            return;
        }
    }
    collResult = ucol_strcoll(myCollation, greekString, UPRV_LENGTHOF(greekString), punctuationString, UPRV_LENGTHOF(punctuationString));
    if (collResult != UCOL_LESS) {
        log_err_status(status, "ERROR: collation result should have been UCOL_LESS\n");
        return;
    }

    /* clear the reordering */
    ucol_setReorderCodes(myCollation, NULL, 0, &status);    
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes to NULL: %s\n", myErrorName(status));
        return;
    }

    /* get the reordering again */
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
    if (retrievedReorderCodesLength != 0) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, 0);
        return;
    }

    collResult = ucol_strcoll(myCollation, greekString, UPRV_LENGTHOF(greekString), punctuationString, UPRV_LENGTHOF(punctuationString));
    if (collResult != UCOL_GREATER) {
        log_err_status(status, "ERROR: collation result should have been UCOL_GREATER\n");
        return;
    }

    /* reset the reordering */
    ucol_setReorderCodes(myCollation, onlyDefault, 1, &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes to {default}: %s\n", myErrorName(status));
        return;
    }
    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, retrievedReorderCodes, UPRV_LENGTHOF(retrievedReorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: getting reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(rulesReorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(rulesReorderCodes));
        return;
    }
    for (loopIndex = 0; loopIndex < retrievedReorderCodesLength; loopIndex++) {
        if (retrievedReorderCodes[loopIndex] != rulesReorderCodes[loopIndex]) {
            log_err_status(status, "ERROR: retrieved reorder code doesn't match set reorder code at index %d\n", loopIndex);
            return;
        }
    }

    ucol_close(myCollation);
}

static UBool containsExpectedScript(const int32_t scripts[], int32_t length, int32_t expectedScript) {
    int32_t i;
    for (i = 0; i < length; ++i) {
        if (expectedScript == scripts[i]) { return TRUE; }
    }
    return FALSE;
}

static void TestEquivalentReorderingScripts(void) {
    // Beginning with ICU 55, collation reordering moves single scripts
    // rather than groups of scripts,
    // except where scripts share a range and sort primary-equal.
    UErrorCode status = U_ZERO_ERROR;
    int32_t equivalentScripts[100];
    int32_t length;
    int i;
    int32_t prevScript;
    /* These scripts are expected to be equivalent. */
    static const int32_t expectedScripts[] = {
        USCRIPT_HIRAGANA,
        USCRIPT_KATAKANA,
        USCRIPT_KATAKANA_OR_HIRAGANA
    };

    equivalentScripts[0] = 0;
    length = ucol_getEquivalentReorderCodes(
            USCRIPT_GOTHIC, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR/Gothic: retrieving equivalent reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (length != 1 || equivalentScripts[0] != USCRIPT_GOTHIC) {
        log_err("ERROR/Gothic: retrieved equivalent scripts wrong: "
                "length expected 1, was = %d; expected [%d] was [%d]\n",
                length, USCRIPT_GOTHIC, equivalentScripts[0]);
    }

    length = ucol_getEquivalentReorderCodes(
            USCRIPT_HIRAGANA, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR/Hiragana: retrieving equivalent reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (length != UPRV_LENGTHOF(expectedScripts)) {
        log_err("ERROR/Hiragana: retrieved equivalent script length wrong: "
                "expected %d, was = %d\n",
                UPRV_LENGTHOF(expectedScripts), length);
    }
    prevScript = -1;
    for (i = 0; i < length; ++i) {
        int32_t script = equivalentScripts[i];
        if (script <= prevScript) {
            log_err("ERROR/Hiragana: equivalent scripts out of order at index %d\n", i);
        }
        prevScript = script;
    }
    for (i = 0; i < UPRV_LENGTHOF(expectedScripts); i++) {
        if (!containsExpectedScript(equivalentScripts, length, expectedScripts[i])) {
            log_err("ERROR/Hiragana: equivalent scripts do not contain %d\n",
                    expectedScripts[i]);
        }
    }

    length = ucol_getEquivalentReorderCodes(
            USCRIPT_KATAKANA, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR/Katakana: retrieving equivalent reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (length != UPRV_LENGTHOF(expectedScripts)) {
        log_err("ERROR/Katakana: retrieved equivalent script length wrong: "
                "expected %d, was = %d\n",
                UPRV_LENGTHOF(expectedScripts), length);
    }
    for (i = 0; i < UPRV_LENGTHOF(expectedScripts); i++) {
        if (!containsExpectedScript(equivalentScripts, length, expectedScripts[i])) {
            log_err("ERROR/Katakana: equivalent scripts do not contain %d\n",
                    expectedScripts[i]);
        }
    }

    length = ucol_getEquivalentReorderCodes(
            USCRIPT_KATAKANA_OR_HIRAGANA, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status) || length != UPRV_LENGTHOF(expectedScripts)) {
        log_err("ERROR/Hrkt: retrieved equivalent script length wrong: "
                "expected %d, was = %d\n",
                UPRV_LENGTHOF(expectedScripts), length);
    }

    length = ucol_getEquivalentReorderCodes(
            USCRIPT_HAN, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status) || length != 3) {
        log_err("ERROR/Hani: retrieved equivalent script length wrong: "
                "expected 3, was = %d\n", length);
    }
    length = ucol_getEquivalentReorderCodes(
            USCRIPT_SIMPLIFIED_HAN, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status) || length != 3) {
        log_err("ERROR/Hans: retrieved equivalent script length wrong: "
                "expected 3, was = %d\n", length);
    }
    length = ucol_getEquivalentReorderCodes(
            USCRIPT_TRADITIONAL_HAN, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status) || length != 3) {
        log_err("ERROR/Hant: retrieved equivalent script length wrong: "
                "expected 3, was = %d\n", length);
    }

    length = ucol_getEquivalentReorderCodes(
            USCRIPT_MEROITIC_CURSIVE, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status) || length != 2) {
        log_err("ERROR/Merc: retrieved equivalent script length wrong: "
                "expected 2, was = %d\n", length);
    }
    length = ucol_getEquivalentReorderCodes(
            USCRIPT_MEROITIC_HIEROGLYPHS, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status);
    if (U_FAILURE(status) || length != 2) {
        log_err("ERROR/Mero: retrieved equivalent script length wrong: "
                "expected 2, was = %d\n", length);
    }
}

static void TestReorderingAcrossCloning(void) 
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    int32_t reorderCodes[3] = {USCRIPT_GREEK, USCRIPT_HAN, UCOL_REORDER_CODE_PUNCTUATION};
    UCollator *clonedCollation;
    int32_t retrievedReorderCodesLength;
    int32_t retrievedReorderCodes[10];
    int loopIndex;

    log_verbose("Testing non-lead bytes in a sort key with and without reordering\n");

    /* build collator tertiary */
    myCollation = ucol_open("", &status);
    ucol_setStrength(myCollation, UCOL_TERTIARY);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }

    /* set the reorderding */
    ucol_setReorderCodes(myCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
        return;
    }
    
    /* clone the collator */
    clonedCollation = ucol_safeClone(myCollation, NULL, NULL, &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: cloning collator: %s\n", myErrorName(status));
        return;
    }
    
    /* get the reordering */
    retrievedReorderCodesLength = ucol_getReorderCodes(clonedCollation, retrievedReorderCodes, UPRV_LENGTHOF(retrievedReorderCodes), &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: getting reorder codes: %s\n", myErrorName(status));
        return;
    }
    if (retrievedReorderCodesLength != UPRV_LENGTHOF(reorderCodes)) {
        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(reorderCodes));
        return;
    }
    for (loopIndex = 0; loopIndex < retrievedReorderCodesLength; loopIndex++) {
        if (retrievedReorderCodes[loopIndex] != reorderCodes[loopIndex]) {
            log_err_status(status, "ERROR: retrieved reorder code doesn't match set reorder code at index %d\n", loopIndex);
            return;
        }
    }
    
    /*uprv_free(buffer);*/
    ucol_close(myCollation);
    ucol_close(clonedCollation);
}

/*
 * Utility function to test one collation reordering test case set.
 * @param testcases Array of test cases.
 * @param n_testcases Size of the array testcases.
 * @param reorderTokens Array of reordering codes.
 * @param reorderTokensLen Size of the array reorderTokens.
 */
static void doTestOneReorderingAPITestCase(const OneTestCase testCases[], uint32_t testCasesLen, const int32_t reorderTokens[], int32_t reorderTokensLen)
{
    uint32_t testCaseNum;
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;

    myCollation = ucol_open("", &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }
    ucol_setReorderCodes(myCollation, reorderTokens, reorderTokensLen, &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: while setting script order: %s\n", myErrorName(status));
        return;
    }
        
    for (testCaseNum = 0; testCaseNum < testCasesLen; ++testCaseNum) {
        doTest(myCollation,
            testCases[testCaseNum].source,
            testCases[testCaseNum].target,
            testCases[testCaseNum].result
        );
    }
    ucol_close(myCollation);
}

static void TestGreekFirstReorder(void)
{
    const char* strRules[] = {
        "[reorder Grek]"
    };

    const int32_t apiRules[] = {
        USCRIPT_GREEK
    };
    
    const static OneTestCase privateUseCharacterStrings[] = {
        { {0x0391}, {0x0391}, UCOL_EQUAL },
        { {0x0041}, {0x0391}, UCOL_GREATER },
        { {0x03B1, 0x0041}, {0x03B1, 0x0391}, UCOL_GREATER },
        { {0x0060}, {0x0391}, UCOL_LESS },
        { {0x0391}, {0xe2dc}, UCOL_LESS },
        { {0x0391}, {0x0060}, UCOL_GREATER },
    };

    /* Test rules creation */
    doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));

    /* Test collation reordering API */
    doTestOneReorderingAPITestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), apiRules, UPRV_LENGTHOF(apiRules));
}

static void TestGreekLastReorder(void)
{
    const char* strRules[] = {
        "[reorder Zzzz Grek]"
    };

    const int32_t apiRules[] = {
        USCRIPT_UNKNOWN, USCRIPT_GREEK
    };
    
    const static OneTestCase privateUseCharacterStrings[] = {
        { {0x0391}, {0x0391}, UCOL_EQUAL },
        { {0x0041}, {0x0391}, UCOL_LESS },
        { {0x03B1, 0x0041}, {0x03B1, 0x0391}, UCOL_LESS },
        { {0x0060}, {0x0391}, UCOL_LESS },
        { {0x0391}, {0xe2dc}, UCOL_GREATER },
    };
    
    /* Test rules creation */
    doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));

    /* Test collation reordering API */
    doTestOneReorderingAPITestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), apiRules, UPRV_LENGTHOF(apiRules));
}

static void TestNonScriptReorder(void)
{
    const char* strRules[] = {
        "[reorder Grek Symbol DIGIT Latn Punct space Zzzz cURRENCy]"
    };

    const int32_t apiRules[] = {
        USCRIPT_GREEK, UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_DIGIT, USCRIPT_LATIN, 
        UCOL_REORDER_CODE_PUNCTUATION, UCOL_REORDER_CODE_SPACE, USCRIPT_UNKNOWN, 
        UCOL_REORDER_CODE_CURRENCY
    };

    const static OneTestCase privateUseCharacterStrings[] = {
        { {0x0391}, {0x0041}, UCOL_LESS },
        { {0x0041}, {0x0391}, UCOL_GREATER },
        { {0x0060}, {0x0041}, UCOL_LESS },
        { {0x0060}, {0x0391}, UCOL_GREATER },
        { {0x0024}, {0x0041}, UCOL_GREATER },
    };
    
    /* Test rules creation */
    doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));

    /* Test collation reordering API */
    doTestOneReorderingAPITestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), apiRules, UPRV_LENGTHOF(apiRules));
}

static void TestHaniReorder(void)
{
    const char* strRules[] = {
        "[reorder Hani]"
    };
    const int32_t apiRules[] = {
        USCRIPT_HAN
    };

    const static OneTestCase privateUseCharacterStrings[] = {
        { {0x4e00}, {0x0041}, UCOL_LESS },
        { {0x4e00}, {0x0060}, UCOL_GREATER },
        { {0xD86D, 0xDF40}, {0x0041}, UCOL_LESS },
        { {0xD86D, 0xDF40}, {0x0060}, UCOL_GREATER },
        { {0x4e00}, {0xD86D, 0xDF40}, UCOL_LESS },
        { {0xfa27}, {0x0041}, UCOL_LESS },
        { {0xD869, 0xDF00}, {0x0041}, UCOL_LESS },
    };
    
    /* Test rules creation */
    doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));

    /* Test collation reordering API */
    doTestOneReorderingAPITestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), apiRules, UPRV_LENGTHOF(apiRules));
}

static void TestHaniReorderWithOtherRules(void)
{
    const char* strRules[] = {
        "[reorder Hani] &b<a"
    };
    /*const int32_t apiRules[] = {
        USCRIPT_HAN
    };*/

    const static OneTestCase privateUseCharacterStrings[] = {
        { {0x4e00}, {0x0041}, UCOL_LESS },
        { {0x4e00}, {0x0060}, UCOL_GREATER },
        { {0xD86D, 0xDF40}, {0x0041}, UCOL_LESS },
        { {0xD86D, 0xDF40}, {0x0060}, UCOL_GREATER },
        { {0x4e00}, {0xD86D, 0xDF40}, UCOL_LESS },
        { {0xfa27}, {0x0041}, UCOL_LESS },
        { {0xD869, 0xDF00}, {0x0041}, UCOL_LESS },
        { {0x0062}, {0x0061}, UCOL_LESS },
    };
    
    /* Test rules creation */
    doTestOneTestCase(privateUseCharacterStrings, UPRV_LENGTHOF(privateUseCharacterStrings), strRules, UPRV_LENGTHOF(strRules));
}

static void TestMultipleReorder(void)
{
    const char* strRules[] = {
        "[reorder Grek Zzzz DIGIT Latn Hani]"
    };

    const int32_t apiRules[] = {
        USCRIPT_GREEK, USCRIPT_UNKNOWN, UCOL_REORDER_CODE_DIGIT, USCRIPT_LATIN, USCRIPT_HAN
    };
        
    const static OneTestCase collationTestCases[] = {
        { {0x0391}, {0x0041}, UCOL_LESS},
        { {0x0031}, {0x0041}, UCOL_LESS},
        { {0x0041}, {0x4e00}, UCOL_LESS},
    };

    /* Test rules creation */
    doTestOneTestCase(collationTestCases, UPRV_LENGTHOF(collationTestCases), strRules, UPRV_LENGTHOF(strRules));

    /* Test collation reordering API */
    doTestOneReorderingAPITestCase(collationTestCases, UPRV_LENGTHOF(collationTestCases), apiRules, UPRV_LENGTHOF(apiRules));
}

/*
 * Test that covers issue reported in ticket 8814
 */
static void TestReorderWithNumericCollation(void)
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator  *myCollation;
    UCollator  *myReorderCollation;
    int32_t reorderCodes[] = {UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION, UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_DIGIT, USCRIPT_GREEK,USCRIPT_LATIN, USCRIPT_HEBREW, UCOL_REORDER_CODE_OTHERS};
    /* UChar fortyS[] = { 0x0034, 0x0030, 0x0053 };
    UChar fortyThreeP[] = { 0x0034, 0x0033, 0x0050 }; */
    UChar fortyS[] = { 0x0053 };
    UChar fortyThreeP[] = { 0x0050 };
    uint8_t fortyS_sortKey[128];
    int32_t fortyS_sortKey_Length;
    uint8_t fortyThreeP_sortKey[128];
    int32_t fortyThreeP_sortKey_Length;
    uint8_t fortyS_sortKey_reorder[128];
    int32_t fortyS_sortKey_reorder_Length;
    uint8_t fortyThreeP_sortKey_reorder[128];
    int32_t fortyThreeP_sortKey_reorder_Length;
    UCollationResult collResult;
    UCollationResult collResultReorder;

    log_verbose("Testing reordering with and without numeric collation\n");

    /* build collator tertiary with numeric */
    myCollation = ucol_open("", &status);
    /*
    ucol_setStrength(myCollation, UCOL_TERTIARY);
    */
    ucol_setAttribute(myCollation, UCOL_NUMERIC_COLLATION, UCOL_ON, &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }
    
    /* build collator tertiary with numeric and reordering */
    myReorderCollation = ucol_open("", &status);
    /*
    ucol_setStrength(myReorderCollation, UCOL_TERTIARY);
    */
    ucol_setAttribute(myReorderCollation, UCOL_NUMERIC_COLLATION, UCOL_ON, &status);
    ucol_setReorderCodes(myReorderCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status);
    if(U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
        return;
    }
    
    fortyS_sortKey_Length = ucol_getSortKey(myCollation, fortyS, UPRV_LENGTHOF(fortyS), fortyS_sortKey, 128);
    fortyThreeP_sortKey_Length = ucol_getSortKey(myCollation, fortyThreeP, UPRV_LENGTHOF(fortyThreeP), fortyThreeP_sortKey, 128);
    fortyS_sortKey_reorder_Length = ucol_getSortKey(myReorderCollation, fortyS, UPRV_LENGTHOF(fortyS), fortyS_sortKey_reorder, 128);
    fortyThreeP_sortKey_reorder_Length = ucol_getSortKey(myReorderCollation, fortyThreeP, UPRV_LENGTHOF(fortyThreeP), fortyThreeP_sortKey_reorder, 128);

    if (fortyS_sortKey_Length < 0 || fortyThreeP_sortKey_Length < 0 || fortyS_sortKey_reorder_Length < 0 || fortyThreeP_sortKey_reorder_Length < 0) {
        log_err_status(status, "ERROR: couldn't generate sort keys\n");
        return;
    }
    collResult = ucol_strcoll(myCollation, fortyS, UPRV_LENGTHOF(fortyS), fortyThreeP, UPRV_LENGTHOF(fortyThreeP));
    collResultReorder = ucol_strcoll(myReorderCollation, fortyS, UPRV_LENGTHOF(fortyS), fortyThreeP, UPRV_LENGTHOF(fortyThreeP));
    /*
    fprintf(stderr, "\tcollResult = %x\n", collResult);
    fprintf(stderr, "\tcollResultReorder = %x\n", collResultReorder);
    fprintf(stderr, "\nfortyS\n");
    for (i = 0; i < fortyS_sortKey_Length; i++) {
        fprintf(stderr, "%x --- %x\n", fortyS_sortKey[i], fortyS_sortKey_reorder[i]);
    }
    fprintf(stderr, "\nfortyThreeP\n");
    for (i = 0; i < fortyThreeP_sortKey_Length; i++) {
        fprintf(stderr, "%x --- %x\n", fortyThreeP_sortKey[i], fortyThreeP_sortKey_reorder[i]);
    }
    */
    if (collResult != collResultReorder) {
        log_err_status(status, "ERROR: collation results should have been the same.\n");
        return;
    }
    
    ucol_close(myCollation);
    ucol_close(myReorderCollation);
}

static int compare_uint8_t_arrays(const uint8_t* a, const uint8_t* b)
{
  for (; *a == *b; ++a, ++b) {
    if (*a == 0) {
      return 0;
    }
  }
  return (*a < *b ? -1 : 1);
}

static void TestImportRulesDeWithPhonebook(void)
{
  const char* normalRules[] = {
    "&a<\\u00e6<\\u00c6<\\u00dc<\\u00fc",
    "&a<<\\u00e6<<\\u00c6<<\\u00dc<<\\u00fc",
    "&a<<\\u00e6<<<\\u00c6<<\\u00dc<<\\u00fc",
  };
  const OneTestCase normalTests[] = {
    { {0x00e6}, {0x00c6}, UCOL_LESS},
    { {0x00fc}, {0x00dc}, UCOL_GREATER},
  };

  const char* importRules[] = {
    "&a<\\u00e6<\\u00c6<\\u00dc<\\u00fc[import de-u-co-phonebk]",
    "&a<<\\u00e6<<\\u00c6<<\\u00dc<<\\u00fc[import de-u-co-phonebk]",
    "&a<<\\u00e6<<<\\u00c6<<\\u00dc<<\\u00fc[import de-u-co-phonebk]",
  };
  const OneTestCase importTests[] = {
    { {0x00e6}, {0x00c6}, UCOL_LESS},
    { {0x00fc}, {0x00dc}, UCOL_LESS},
  };

  doTestOneTestCase(normalTests, UPRV_LENGTHOF(normalTests), normalRules, UPRV_LENGTHOF(normalRules));
  doTestOneTestCase(importTests, UPRV_LENGTHOF(importTests), importRules, UPRV_LENGTHOF(importRules));
}

#if 0
static void TestImportRulesFiWithEor(void)
{
  /* DUCET. */
  const char* defaultRules[] = {
    "&a<b",                                    /* Dummy rule. */
  };

  const OneTestCase defaultTests[] = {
    { {0x0110}, {0x00F0}, UCOL_LESS},
    { {0x00a3}, {0x00a5}, UCOL_LESS},
    { {0x0061}, {0x0061, 0x00a3}, UCOL_LESS},
  };

  /* European Ordering rules: ignore currency characters. */
  const char* eorRules[] = {
    "[import root-u-co-eor]",
  };

  const OneTestCase eorTests[] = {
    { {0x0110}, {0x00F0}, UCOL_LESS},
    { {0x00a3}, {0x00a5}, UCOL_EQUAL},
    { {0x0061}, {0x0061, 0x00a3}, UCOL_EQUAL},
  };

  const char* fiStdRules[] = {
    "[import fi-u-co-standard]",
  };

  const OneTestCase fiStdTests[] = {
    { {0x0110}, {0x00F0}, UCOL_GREATER},
    { {0x00a3}, {0x00a5}, UCOL_LESS},
    { {0x0061}, {0x0061, 0x00a3}, UCOL_LESS},
  };

  /* Both European Ordering Rules and Fi Standard Rules. */
  const char* eorFiStdRules[] = {
    "[import root-u-co-eor][import fi-u-co-standard]",
  };

  /* This is essentially same as the one before once fi.txt is updated with import. */
  const char* fiEorRules[] = {
    "[import fi-u-co-eor]",
  };

  const OneTestCase fiEorTests[] = {
    { {0x0110}, {0x00F0}, UCOL_GREATER},
    { {0x00a3}, {0x00a5}, UCOL_EQUAL},
    { {0x0061}, {0x0061, 0x00a3}, UCOL_EQUAL},
  };

  doTestOneTestCase(defaultTests, UPRV_LENGTHOF(defaultTests), defaultRules, UPRV_LENGTHOF(defaultRules));
  doTestOneTestCase(eorTests, UPRV_LENGTHOF(eorTests), eorRules, UPRV_LENGTHOF(eorRules));
  doTestOneTestCase(fiStdTests, UPRV_LENGTHOF(fiStdTests), fiStdRules, UPRV_LENGTHOF(fiStdRules));
  doTestOneTestCase(fiEorTests, UPRV_LENGTHOF(fiEorTests), eorFiStdRules, UPRV_LENGTHOF(eorFiStdRules));

  log_knownIssue("8962", NULL);
  /* TODO: Fix ICU ticket #8962 by uncommenting the following test after fi.txt is updated with the following rule:
        eor{
            Sequence{
                "[import root-u-co-eor][import fi-u-co-standard]"
            }
            Version{"21.0"}
        }
  */
  /* doTestOneTestCase(fiEorTests, UPRV_LENGTHOF(fiEorTests), fiEorRules, UPRV_LENGTHOF(fiEorRules)); */

}
#endif

#if 0
/*
 * This test case tests inclusion with the unihan rules, but this cannot be included now, unless
 * the resource files are built with -includeUnihanColl option.
 * TODO: Uncomment this function and make it work when unihan rules are built by default.
 */
static void TestImportRulesCJKWithUnihan(void)
{
  /* DUCET. */
  const char* defaultRules[] = {
    "&a<b",                                    /* Dummy rule. */
  };

  const OneTestCase defaultTests[] = {
    { {0x3402}, {0x4e1e}, UCOL_GREATER},
  };

  /* European Ordering rules: ignore currency characters. */
  const char* unihanRules[] = {
    "[import ko-u-co-unihan]",
  };

  const OneTestCase unihanTests[] = {
    { {0x3402}, {0x4e1e}, UCOL_LESS},
  };

  doTestOneTestCase(defaultTests, UPRV_LENGTHOF(defaultTests), defaultRules, UPRV_LENGTHOF(defaultRules));
  doTestOneTestCase(unihanTests, UPRV_LENGTHOF(unihanTests), unihanRules, UPRV_LENGTHOF(unihanRules));

}
#endif

static void TestImport(void)
{
    UCollator* vicoll;
    UCollator* escoll;
    UCollator* viescoll;
    UCollator* importviescoll;
    UParseError error;
    UErrorCode status = U_ZERO_ERROR;
    UChar* virules;
    int32_t viruleslength;
    UChar* esrules;
    int32_t esruleslength;
    UChar* viesrules;
    int32_t viesruleslength;
    char srules[500] = "[import vi][import es]";
    UChar rules[500];
    uint32_t length = 0;
    int32_t itemCount;
    int32_t i, k;
    UChar32 start;
    UChar32 end;
    UChar str[500];
    int32_t strLength;

    uint8_t sk1[500];
    uint8_t sk2[500];

    UBool b;
    USet* tailoredSet;
    USet* importTailoredSet;


    vicoll = ucol_open("vi", &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: Call ucol_open(\"vi\", ...): %s\n", myErrorName(status));
        return;
    }

    virules = (UChar*) ucol_getRules(vicoll, &viruleslength);
    if(viruleslength == 0) {
        log_data_err("missing vi tailoring rule string\n");
        ucol_close(vicoll);
        return;
    }
    escoll = ucol_open("es", &status);
    esrules = (UChar*) ucol_getRules(escoll, &esruleslength);
    viesrules = (UChar*)uprv_malloc((viruleslength+esruleslength+1)*sizeof(UChar*));
    viesrules[0] = 0;
    u_strcat(viesrules, virules);
    u_strcat(viesrules, esrules);
    viesruleslength = viruleslength + esruleslength;
    viescoll = ucol_openRules(viesrules, viesruleslength, UCOL_ON, UCOL_TERTIARY, &error, &status);

    /* u_strFromUTF8(rules, 500, &length, srules, strlen(srules), &status); */
    length = u_unescape(srules, rules, 500);
    importviescoll = ucol_openRules(rules, length, UCOL_ON, UCOL_TERTIARY, &error, &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }

    tailoredSet = ucol_getTailoredSet(viescoll, &status);
    importTailoredSet = ucol_getTailoredSet(importviescoll, &status);

    if(!uset_equals(tailoredSet, importTailoredSet)){
        log_err("Tailored sets not equal");
    }

    uset_close(importTailoredSet);

    itemCount = uset_getItemCount(tailoredSet);

    for( i = 0; i < itemCount; i++){
        strLength = uset_getItem(tailoredSet, i, &start, &end, str, 500, &status);
        if(strLength < 2){
            for (; start <= end; start++){
                k = 0;
                U16_APPEND(str, k, 500, start, b);
                (void)b;    /* Suppress set but not used warning. */
                ucol_getSortKey(viescoll, str, 1, sk1, 500);
                ucol_getSortKey(importviescoll, str, 1, sk2, 500);
                if(compare_uint8_t_arrays(sk1, sk2) != 0){
                    log_err("Sort key for %s not equal\n", str);
                    break;
                }
            }
        }else{
            ucol_getSortKey(viescoll, str, strLength, sk1, 500);
            ucol_getSortKey(importviescoll, str, strLength, sk2, 500);
            if(compare_uint8_t_arrays(sk1, sk2) != 0){
                log_err("ZZSort key for %s not equal\n", str);
                break;
            }

        }
    }

    uset_close(tailoredSet);

    uprv_free(viesrules);

    ucol_close(vicoll);
    ucol_close(escoll);
    ucol_close(viescoll);
    ucol_close(importviescoll);
}

static void TestImportWithType(void)
{
    UCollator* vicoll;
    UCollator* decoll;
    UCollator* videcoll;
    UCollator* importvidecoll;
    UParseError error;
    UErrorCode status = U_ZERO_ERROR;
    const UChar* virules;
    int32_t viruleslength;
    const UChar* derules;
    int32_t deruleslength;
    UChar* viderules;
    int32_t videruleslength;
    const char srules[500] = "[import vi][import de-u-co-phonebk]";
    UChar rules[500];
    uint32_t length = 0;
    int32_t itemCount;
    int32_t i, k;
    UChar32 start;
    UChar32 end;
    UChar str[500];
    int32_t strLength;

    uint8_t sk1[500];
    uint8_t sk2[500];

    USet* tailoredSet;
    USet* importTailoredSet;

    vicoll = ucol_open("vi", &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }
    virules = ucol_getRules(vicoll, &viruleslength);
    if(viruleslength == 0) {
        log_data_err("missing vi tailoring rule string\n");
        ucol_close(vicoll);
        return;
    }
    /* decoll = ucol_open("de@collation=phonebook", &status); */
    decoll = ucol_open("de-u-co-phonebk", &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }


    derules = ucol_getRules(decoll, &deruleslength);
    viderules = (UChar*)uprv_malloc((viruleslength+deruleslength+1)*sizeof(UChar*));
    viderules[0] = 0;
    u_strcat(viderules, virules);
    u_strcat(viderules, derules);
    videruleslength = viruleslength + deruleslength;
    videcoll = ucol_openRules(viderules, videruleslength, UCOL_ON, UCOL_TERTIARY, &error, &status);

    /* u_strFromUTF8(rules, 500, &length, srules, strlen(srules), &status); */
    length = u_unescape(srules, rules, 500);
    importvidecoll = ucol_openRules(rules, length, UCOL_ON, UCOL_TERTIARY, &error, &status);
    if(U_FAILURE(status)){
        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
        return;
    }

    tailoredSet = ucol_getTailoredSet(videcoll, &status);
    importTailoredSet = ucol_getTailoredSet(importvidecoll, &status);

    if(!uset_equals(tailoredSet, importTailoredSet)){
        log_err("Tailored sets not equal");
    }

    uset_close(importTailoredSet);

    itemCount = uset_getItemCount(tailoredSet);

    for( i = 0; i < itemCount; i++){
        strLength = uset_getItem(tailoredSet, i, &start, &end, str, 500, &status);
        if(strLength < 2){
            for (; start <= end; start++){
                k = 0;
                U16_APPEND_UNSAFE(str, k, start);
                ucol_getSortKey(videcoll, str, 1, sk1, 500);
                ucol_getSortKey(importvidecoll, str, 1, sk2, 500);
                if(compare_uint8_t_arrays(sk1, sk2) != 0){
                    log_err("Sort key for %s not equal\n", str);
                    break;
                }
            }
        }else{
            ucol_getSortKey(videcoll, str, strLength, sk1, 500);
            ucol_getSortKey(importvidecoll, str, strLength, sk2, 500);
            if(compare_uint8_t_arrays(sk1, sk2) != 0){
                log_err("Sort key for %s not equal\n", str);
                break;
            }

        }
    }

    uset_close(tailoredSet);

    uprv_free(viderules);

    ucol_close(videcoll);
    ucol_close(importvidecoll);
    ucol_close(vicoll);
    ucol_close(decoll);
}

/* 'IV INTERNATIONAL SCIENTIFIC - PRACTICAL CONFERENCE "GEOPOLITICS, GEOECONOMICS AND INTERNATIONAL RELATIONS PROBLEMS" 22-23 June 2010, St. Petersburg, Russia' */
static const UChar longUpperStr1[]= { /* 155 chars */
    0x49, 0x56, 0x20, 0x49, 0x4E, 0x54, 0x45, 0x52, 0x4E, 0x41, 0x54, 0x49, 0x4F, 0x4E, 0x41, 0x4C,
    0x20, 0x53, 0x43, 0x49, 0x45, 0x4E, 0x54, 0x49, 0x46, 0x49, 0x43, 0x20, 0x2D, 0x20, 0x50, 0x52,
    0x41, 0x43, 0x54, 0x49, 0x43, 0x41, 0x4C, 0x20, 0x43, 0x4F, 0x4E, 0x46, 0x45, 0x52, 0x45, 0x4E,
    0x43, 0x45, 0x20, 0x22, 0x47, 0x45, 0x4F, 0x50, 0x4F, 0x4C, 0x49, 0x54, 0x49, 0x43, 0x53, 0x2C,
    0x20, 0x47, 0x45, 0x4F, 0x45, 0x43, 0x4F, 0x4E, 0x4F, 0x4D, 0x49, 0x43, 0x53, 0x20, 0x41, 0x4E,
    0x44, 0x20, 0x49, 0x4E, 0x54, 0x45, 0x52, 0x4E, 0x41, 0x54, 0x49, 0x4F, 0x4E, 0x41, 0x4C, 0x20,
    0x52, 0x45, 0x4C, 0x41, 0x54, 0x49, 0x4F, 0x4E, 0x53, 0x20, 0x50, 0x52, 0x4F, 0x42, 0x4C, 0x45,
    0x4D, 0x53, 0x22, 0x20, 0x32, 0x32, 0x2D, 0x32, 0x33, 0x20, 0x4A, 0x75, 0x6E, 0x65, 0x20, 0x32,
    0x30, 0x31, 0x30, 0x2C, 0x20, 0x53, 0x74, 0x2E, 0x20, 0x50, 0x65, 0x74, 0x65, 0x72, 0x73, 0x62,
    0x75, 0x72, 0x67, 0x2C, 0x20, 0x52, 0x75, 0x73, 0x73, 0x69, 0x61
};

/* 'BACEDIFOGUHAJEKILOMUNAPE ' with diacritics on vowels, repeated 5 times */
static const UChar longUpperStr2[]= { /* 125 chars, > 128 collation elements */
    0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20,
    0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20,
    0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20,
    0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20,
    0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20
};

/* 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' repeated 12 times */
static const UChar longUpperStr3[]= { /* 324 chars */
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20,
    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20
};

typedef struct {
    const UChar * longUpperStrPtr;
    int32_t       longUpperStrLen;
} LongUpperStrItem;

/* String pointers must be in reverse collation order of the corresponding strings */
static const LongUpperStrItem longUpperStrItems[] = {
    { longUpperStr1, UPRV_LENGTHOF(longUpperStr1) },
    { longUpperStr2, UPRV_LENGTHOF(longUpperStr2) },
    { longUpperStr3, UPRV_LENGTHOF(longUpperStr3) },
    { NULL,          0                           }
};

enum { kCollKeyLenMax = 850 }; /* may change with collation changes */

/* Text fix for #8445; without fix, could have crash due to stack or heap corruption */
static void TestCaseLevelBufferOverflow(void)
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator * ucol = ucol_open("root", &status);
    if ( U_SUCCESS(status) ) {
        ucol_setAttribute(ucol, UCOL_CASE_LEVEL, UCOL_ON, &status);
        if ( U_SUCCESS(status) ) {
            const LongUpperStrItem * itemPtr;
            uint8_t sortKeyA[kCollKeyLenMax], sortKeyB[kCollKeyLenMax];
            for ( itemPtr = longUpperStrItems; itemPtr->longUpperStrPtr != NULL; itemPtr++ ) {
                int32_t sortKeyLen;
                if (itemPtr > longUpperStrItems) {
                    uprv_strcpy((char *)sortKeyB, (char *)sortKeyA);
                }
                sortKeyLen = ucol_getSortKey(ucol, itemPtr->longUpperStrPtr, itemPtr->longUpperStrLen, sortKeyA, kCollKeyLenMax);
                if (sortKeyLen <= 0 || sortKeyLen > kCollKeyLenMax) {
                    log_err("ERROR sort key length from ucol_getSortKey is %d\n", sortKeyLen);
                    break;
                }
                if ( itemPtr > longUpperStrItems ) {
                    int compareResult = uprv_strcmp((char *)sortKeyA, (char *)sortKeyB);
                    if (compareResult >= 0) {
                        log_err("ERROR in sort key comparison result, expected -1, got %d\n", compareResult);
                    }
                }
            }
        } else {
            log_err_status(status, "ERROR in ucol_setAttribute UCOL_CASE_LEVEL on: %s\n", myErrorName(status));
        }
        ucol_close(ucol);
    } else {
        log_err_status(status, "ERROR in ucol_open for root: %s\n", myErrorName(status));
    }
}

/* Test for #10595 */
static const UChar testJapaneseName[] = {0x4F50, 0x3005, 0x6728, 0x002C, 0x6B66, 0}; /* Sa sa Ki, Takeshi */
#define KEY_PART_SIZE 16

static void TestNextSortKeyPartJaIdentical(void)
{
    UErrorCode status = U_ZERO_ERROR;
    UCollator *coll;
    uint8_t keyPart[KEY_PART_SIZE];
    UCharIterator iter;
    uint32_t state[2] = {0, 0};
    int32_t keyPartLen;
    
    coll = ucol_open("ja", &status);
    ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &status);
    if (U_FAILURE(status)) {
        log_err_status(status, "ERROR: in creation of Japanese collator with identical strength: %s\n", myErrorName(status));
        return;
    }

    uiter_setString(&iter, testJapaneseName, 5);
    keyPartLen = KEY_PART_SIZE;
    while (keyPartLen == KEY_PART_SIZE) {
        keyPartLen = ucol_nextSortKeyPart(coll, &iter, state, keyPart, KEY_PART_SIZE, &status);
        if (U_FAILURE(status)) {
            log_err_status(status, "ERROR: in iterating next sort key part: %s\n", myErrorName(status));
            break;
        }
    }

    ucol_close(coll);
}

#define TEST(x) addTest(root, &x, "tscoll/cmsccoll/" # x)

void addMiscCollTest(TestNode** root)
{
    TEST(TestRuleOptions);
    TEST(TestBeforePrefixFailure);
    TEST(TestContractionClosure);
    TEST(TestPrefixCompose);
    TEST(TestStrCollIdenticalPrefix);
    TEST(TestPrefix);
    TEST(TestNewJapanese);
    /*TEST(TestLimitations);*/
    TEST(TestNonChars);
    TEST(TestExtremeCompression);
    TEST(TestSurrogates);
    TEST(TestVariableTopSetting);
    TEST(TestMaxVariable);
    TEST(TestBocsuCoverage);
    TEST(TestCyrillicTailoring);
    TEST(TestCase);
    TEST(IncompleteCntTest);
    TEST(BlackBirdTest);
    TEST(FunkyATest);
    TEST(BillFairmanTest);
    TEST(TestChMove);
    TEST(TestImplicitTailoring);
    TEST(TestFCDProblem);
    TEST(TestEmptyRule);
    /*TEST(TestJ784);*/ /* 'zh' locale has changed - now it is getting tested by TestBeforePinyin */
    TEST(TestJ815);
    TEST(TestUpperCaseFirst);
    TEST(TestBefore);
    TEST(TestHangulTailoring);
    TEST(TestUCARules);
    TEST(TestIncrementalNormalize);
    TEST(TestComposeDecompose);
    TEST(TestCompressOverlap);
    TEST(TestContraction);
    TEST(TestExpansion);
    /*TEST(PrintMarkDavis);*/ /* this test doesn't test - just prints sortkeys */
    /*TEST(TestGetCaseBit);*/ /*this one requires internal things to be exported */
    TEST(TestOptimize);
    TEST(TestSuppressContractions);
    TEST(Alexis2);
    TEST(TestHebrewUCA);
    TEST(TestPartialSortKeyTermination);
    TEST(TestSettings);
    TEST(TestEquals);
    TEST(TestJ2726);
    TEST(NullRule);
    TEST(TestNumericCollation);
    TEST(TestTibetanConformance);
    TEST(TestPinyinProblem);
    TEST(TestSeparateTrees);
    TEST(TestBeforePinyin);
    TEST(TestBeforeTightening);
    /*TEST(TestMoreBefore);*/
    TEST(TestTailorNULL);
    TEST(TestUpperFirstQuaternary);
    TEST(TestJ4960);
    TEST(TestJ5223);
    TEST(TestJ5232);
    TEST(TestJ5367);
    TEST(TestHiragana);
    TEST(TestSortKeyConsistency);
    TEST(TestVI5913);  /* VI, RO tailored rules */
    TEST(TestCroatianSortKey);
    TEST(TestTailor6179);
    TEST(TestUCAPrecontext);
    TEST(TestOutOfBuffer5468);
    TEST(TestSameStrengthList);

    TEST(TestSameStrengthListQuoted);
    TEST(TestSameStrengthListSupplemental);
    TEST(TestSameStrengthListQwerty);
    TEST(TestSameStrengthListQuotedQwerty);
    TEST(TestSameStrengthListRanges);
    TEST(TestSameStrengthListSupplementalRanges);
    TEST(TestSpecialCharacters);
    TEST(TestPrivateUseCharacters);
    TEST(TestPrivateUseCharactersInList);
    TEST(TestPrivateUseCharactersInRange);
    TEST(TestInvalidListsAndRanges);
    TEST(TestImportRulesDeWithPhonebook);
    /* TEST(TestImportRulesFiWithEor); EOR rules removed from CLDR 21 */
    /* TEST(TestImportRulesCJKWithUnihan); */
    TEST(TestImport);
    TEST(TestImportWithType);

    TEST(TestBeforeRuleWithScriptReordering);
    TEST(TestNonLeadBytesDuringCollationReordering);
    TEST(TestReorderingAPI);
    TEST(TestReorderingAPIWithRuleCreatedCollator);
    TEST(TestEquivalentReorderingScripts);
    TEST(TestGreekFirstReorder);
    TEST(TestGreekLastReorder);
    TEST(TestNonScriptReorder);
    TEST(TestHaniReorder);
    TEST(TestHaniReorderWithOtherRules);
    TEST(TestMultipleReorder);
    TEST(TestReorderingAcrossCloning);
    TEST(TestReorderWithNumericCollation);
    
    TEST(TestCaseLevelBufferOverflow);
    TEST(TestNextSortKeyPartJaIdentical);
}

#endif /* #if !UCONFIG_NO_COLLATION */