forked from unicode-org/icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitertst.c
1262 lines (1111 loc) · 41.3 KB
/
citertst.c
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
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
*
* File CITERTST.C
*
* Modification History:
* Date Name Description
* Madhu Katragadda Ported for C API
* 02/19/01 synwee Modified test case for new collation iterator
*********************************************************************************/
/*
* Collation Iterator tests.
* (Let me reiterate my position...)
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_COLLATION
#include "unicode/ucol.h"
#include "unicode/ucoleitr.h"
#include "unicode/uloc.h"
#include "unicode/uchar.h"
#include "unicode/ustring.h"
#include "unicode/utf16.h"
#include "unicode/putil.h"
#include "callcoll.h"
#include "cmemory.h"
#include "cintltst.h"
#include "citertst.h"
#include "ccolltst.h"
#include "filestrm.h"
#include "cstring.h"
#include "ucol_imp.h"
#include "uparse.h"
#include <stdbool.h>
#include <stdio.h>
extern uint8_t ucol_uprv_getCaseBits(const UChar *, uint32_t, UErrorCode *);
void addCollIterTest(TestNode** root)
{
addTest(root, &TestPrevious, "tscoll/citertst/TestPrevious");
addTest(root, &TestOffset, "tscoll/citertst/TestOffset");
addTest(root, &TestSetText, "tscoll/citertst/TestSetText");
addTest(root, &TestMaxExpansion, "tscoll/citertst/TestMaxExpansion");
addTest(root, &TestUnicodeChar, "tscoll/citertst/TestUnicodeChar");
addTest(root, &TestNormalizedUnicodeChar,
"tscoll/citertst/TestNormalizedUnicodeChar");
addTest(root, &TestNormalization, "tscoll/citertst/TestNormalization");
addTest(root, &TestBug672, "tscoll/citertst/TestBug672");
addTest(root, &TestBug672Normalize, "tscoll/citertst/TestBug672Normalize");
addTest(root, &TestSmallBuffer, "tscoll/citertst/TestSmallBuffer");
addTest(root, &TestDiscontiguos, "tscoll/citertst/TestDiscontiguos");
addTest(root, &TestSearchCollatorElements, "tscoll/citertst/TestSearchCollatorElements");
}
/* The locales we support */
static const char * LOCALES[] = {"en_AU", "en_BE", "en_CA"};
static void TestBug672() {
UErrorCode status = U_ZERO_ERROR;
UChar pattern[20];
UChar text[50];
int i;
int result[3][3];
u_uastrcpy(pattern, "resume");
u_uastrcpy(text, "Time to resume updating my resume.");
for (i = 0; i < 3; ++ i) {
UCollator *coll = ucol_open(LOCALES[i], &status);
UCollationElements *pitr = ucol_openElements(coll, pattern, -1,
&status);
UCollationElements *titer = ucol_openElements(coll, text, -1,
&status);
if (U_FAILURE(status)) {
log_err_status(status, "ERROR: in creation of either the collator or the collation iterator :%s\n",
myErrorName(status));
return;
}
log_verbose("locale tested %s\n", LOCALES[i]);
while (ucol_next(pitr, &status) != UCOL_NULLORDER &&
U_SUCCESS(status)) {
}
if (U_FAILURE(status)) {
log_err("ERROR: reversing collation iterator :%s\n",
myErrorName(status));
return;
}
ucol_reset(pitr);
ucol_setOffset(titer, u_strlen(pattern), &status);
if (U_FAILURE(status)) {
log_err("ERROR: setting offset in collator :%s\n",
myErrorName(status));
return;
}
result[i][0] = ucol_getOffset(titer);
log_verbose("Text iterator set to offset %d\n", result[i][0]);
/* Use previous() */
ucol_previous(titer, &status);
result[i][1] = ucol_getOffset(titer);
log_verbose("Current offset %d after previous\n", result[i][1]);
/* Add one to index */
log_verbose("Adding one to current offset...\n");
ucol_setOffset(titer, ucol_getOffset(titer) + 1, &status);
if (U_FAILURE(status)) {
log_err("ERROR: setting offset in collator :%s\n",
myErrorName(status));
return;
}
result[i][2] = ucol_getOffset(titer);
log_verbose("Current offset in text = %d\n", result[i][2]);
ucol_closeElements(pitr);
ucol_closeElements(titer);
ucol_close(coll);
}
if (uprv_memcmp(result[0], result[1], 3) != 0 ||
uprv_memcmp(result[1], result[2], 3) != 0) {
log_err("ERROR: Different locales have different offsets at the same character\n");
}
}
/* Running this test with normalization enabled showed up a bug in the incremental
normalization code. */
static void TestBug672Normalize() {
UErrorCode status = U_ZERO_ERROR;
UChar pattern[20];
UChar text[50];
int i;
int result[3][3];
u_uastrcpy(pattern, "resume");
u_uastrcpy(text, "Time to resume updating my resume.");
for (i = 0; i < 3; ++ i) {
UCollator *coll = ucol_open(LOCALES[i], &status);
UCollationElements *pitr = NULL;
UCollationElements *titer = NULL;
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
pitr = ucol_openElements(coll, pattern, -1, &status);
titer = ucol_openElements(coll, text, -1, &status);
if (U_FAILURE(status)) {
log_err_status(status, "ERROR: in creation of either the collator or the collation iterator :%s\n",
myErrorName(status));
return;
}
log_verbose("locale tested %s\n", LOCALES[i]);
while (ucol_next(pitr, &status) != UCOL_NULLORDER &&
U_SUCCESS(status)) {
}
if (U_FAILURE(status)) {
log_err("ERROR: reversing collation iterator :%s\n",
myErrorName(status));
return;
}
ucol_reset(pitr);
ucol_setOffset(titer, u_strlen(pattern), &status);
if (U_FAILURE(status)) {
log_err("ERROR: setting offset in collator :%s\n",
myErrorName(status));
return;
}
result[i][0] = ucol_getOffset(titer);
log_verbose("Text iterator set to offset %d\n", result[i][0]);
/* Use previous() */
ucol_previous(titer, &status);
result[i][1] = ucol_getOffset(titer);
log_verbose("Current offset %d after previous\n", result[i][1]);
/* Add one to index */
log_verbose("Adding one to current offset...\n");
ucol_setOffset(titer, ucol_getOffset(titer) + 1, &status);
if (U_FAILURE(status)) {
log_err("ERROR: setting offset in collator :%s\n",
myErrorName(status));
return;
}
result[i][2] = ucol_getOffset(titer);
log_verbose("Current offset in text = %d\n", result[i][2]);
ucol_closeElements(pitr);
ucol_closeElements(titer);
ucol_close(coll);
}
if (uprv_memcmp(result[0], result[1], 3) != 0 ||
uprv_memcmp(result[1], result[2], 3) != 0) {
log_err("ERROR: Different locales have different offsets at the same character\n");
}
}
/**
* Test for CollationElementIterator previous and next for the whole set of
* unicode characters.
*/
static void TestUnicodeChar()
{
UChar source[0x100];
UCollator *en_us;
UCollationElements *iter;
UErrorCode status = U_ZERO_ERROR;
UChar codepoint;
UChar *test;
en_us = ucol_open("en_US", &status);
if (U_FAILURE(status)){
log_err_status(status, "ERROR: in creation of collation data using ucol_open()\n %s\n",
myErrorName(status));
return;
}
for (codepoint = 1; codepoint < 0xFFFE;)
{
test = source;
while (codepoint % 0xFF != 0)
{
if (u_isdefined(codepoint))
*(test ++) = codepoint;
codepoint ++;
}
if (u_isdefined(codepoint))
*(test ++) = codepoint;
if (codepoint != 0xFFFF)
codepoint ++;
*test = 0;
iter=ucol_openElements(en_us, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
/* A basic test to see if it's working at all */
log_verbose("codepoint testing %x\n", codepoint);
backAndForth(iter);
ucol_closeElements(iter);
/* null termination test */
iter=ucol_openElements(en_us, source, -1, &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
/* A basic test to see if it's working at all */
backAndForth(iter);
ucol_closeElements(iter);
}
ucol_close(en_us);
}
/**
* Test for CollationElementIterator previous and next for the whole set of
* unicode characters with normalization on.
*/
static void TestNormalizedUnicodeChar()
{
UChar source[0x100];
UCollator *th_th;
UCollationElements *iter;
UErrorCode status = U_ZERO_ERROR;
UChar codepoint;
UChar *test;
/* thai should have normalization on */
th_th = ucol_open("th_TH", &status);
if (U_FAILURE(status)){
log_err_status(status, "ERROR: in creation of thai collation using ucol_open()\n %s\n",
myErrorName(status));
return;
}
for (codepoint = 1; codepoint < 0xFFFE;)
{
test = source;
while (codepoint % 0xFF != 0)
{
if (u_isdefined(codepoint))
*(test ++) = codepoint;
codepoint ++;
}
if (u_isdefined(codepoint))
*(test ++) = codepoint;
if (codepoint != 0xFFFF)
codepoint ++;
*test = 0;
iter=ucol_openElements(th_th, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(th_th);
return;
}
backAndForth(iter);
ucol_closeElements(iter);
iter=ucol_openElements(th_th, source, -1, &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(th_th);
return;
}
backAndForth(iter);
ucol_closeElements(iter);
}
ucol_close(th_th);
}
/**
* Test the incremental normalization
*/
static void TestNormalization()
{
UErrorCode status = U_ZERO_ERROR;
const char *str =
"&a < \\u0300\\u0315 < A\\u0300\\u0315 < \\u0316\\u0315B < \\u0316\\u0300\\u0315";
UCollator *coll;
UChar rule[50];
int rulelen = u_unescape(str, rule, 50);
int count = 0;
const char *testdata[] =
{"\\u1ED9", "o\\u0323\\u0302",
"\\u0300\\u0315", "\\u0315\\u0300",
"A\\u0300\\u0315B", "A\\u0315\\u0300B",
"A\\u0316\\u0315B", "A\\u0315\\u0316B",
"\\u0316\\u0300\\u0315", "\\u0315\\u0300\\u0316",
"A\\u0316\\u0300\\u0315B", "A\\u0315\\u0300\\u0316B",
"\\u0316\\u0315\\u0300", "A\\u0316\\u0315\\u0300B"};
int32_t srclen;
UChar source[10];
UCollationElements *iter;
coll = ucol_openRules(rule, rulelen, UCOL_ON, UCOL_TERTIARY, NULL, &status);
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
if (U_FAILURE(status)){
log_err_status(status, "ERROR: in creation of collator using ucol_openRules()\n %s\n",
myErrorName(status));
return;
}
srclen = u_unescape(testdata[0], source, 10);
iter = ucol_openElements(coll, source, srclen, &status);
backAndForth(iter);
ucol_closeElements(iter);
srclen = u_unescape(testdata[1], source, 10);
iter = ucol_openElements(coll, source, srclen, &status);
backAndForth(iter);
ucol_closeElements(iter);
while (count < 12) {
srclen = u_unescape(testdata[count], source, 10);
iter = ucol_openElements(coll, source, srclen, &status);
if (U_FAILURE(status)){
log_err("ERROR: in creation of collator element iterator\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
iter = ucol_openElements(coll, source, -1, &status);
if (U_FAILURE(status)){
log_err("ERROR: in creation of collator element iterator\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
count ++;
}
ucol_close(coll);
}
/**
* Test for CollationElementIterator.previous()
*
* @bug 4108758 - Make sure it works with contracting characters
*
*/
static void TestPrevious()
{
UCollator *coll=NULL;
UChar rule[50];
UChar *source;
UCollator *c1, *c2, *c3;
UCollationElements *iter;
UErrorCode status = U_ZERO_ERROR;
UChar test1[50];
UChar test2[50];
u_uastrcpy(test1, "What subset of all possible test cases?");
u_uastrcpy(test2, "has the highest probability of detecting");
coll = ucol_open("en_US", &status);
iter=ucol_openElements(coll, test1, u_strlen(test1), &status);
log_verbose("English locale testing back and forth\n");
if(U_FAILURE(status)){
log_err_status(status, "ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(coll);
return;
}
/* A basic test to see if it's working at all */
backAndForth(iter);
ucol_closeElements(iter);
ucol_close(coll);
/* Test with a contracting character sequence */
u_uastrcpy(rule, "&a,A < b,B < c,C, d,D < z,Z < ch,cH,Ch,CH");
c1 = ucol_openRules(rule, u_strlen(rule), UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
log_verbose("Contraction rule testing back and forth with no normalization\n");
if (c1 == NULL || U_FAILURE(status))
{
log_err("Couldn't create a RuleBasedCollator with a contracting sequence\n %s\n",
myErrorName(status));
return;
}
source=(UChar*)malloc(sizeof(UChar) * 20);
u_uastrcpy(source, "abchdcba");
iter=ucol_openElements(c1, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
ucol_close(c1);
/* Test with an expanding character sequence */
u_uastrcpy(rule, "&a < b < c/abd < d");
c2 = ucol_openRules(rule, u_strlen(rule), UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
log_verbose("Expansion rule testing back and forth with no normalization\n");
if (c2 == NULL || U_FAILURE(status))
{
log_err("Couldn't create a RuleBasedCollator with a contracting sequence.\n %s\n",
myErrorName(status));
return;
}
u_uastrcpy(source, "abcd");
iter=ucol_openElements(c2, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
ucol_close(c2);
/* Now try both */
u_uastrcpy(rule, "&a < b < c/aba < d < z < ch");
c3 = ucol_openRules(rule, u_strlen(rule), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH,NULL, &status);
log_verbose("Expansion/contraction rule testing back and forth with no normalization\n");
if (c3 == NULL || U_FAILURE(status))
{
log_err("Couldn't create a RuleBasedCollator with a contracting sequence.\n %s\n",
myErrorName(status));
return;
}
u_uastrcpy(source, "abcdbchdc");
iter=ucol_openElements(c3, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
ucol_close(c3);
source[0] = 0x0e41;
source[1] = 0x0e02;
source[2] = 0x0e41;
source[3] = 0x0e02;
source[4] = 0x0e27;
source[5] = 0x61;
source[6] = 0x62;
source[7] = 0x63;
source[8] = 0;
coll = ucol_open("th_TH", &status);
log_verbose("Thai locale testing back and forth with normalization\n");
iter=ucol_openElements(coll, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
ucol_close(coll);
/* prev test */
source[0] = 0x0061;
source[1] = 0x30CF;
source[2] = 0x3099;
source[3] = 0x30FC;
source[4] = 0;
coll = ucol_open("ja_JP", &status);
log_verbose("Japanese locale testing back and forth with normalization\n");
iter=ucol_openElements(coll, source, u_strlen(source), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
return;
}
backAndForth(iter);
ucol_closeElements(iter);
ucol_close(coll);
free(source);
}
/**
* Test for getOffset() and setOffset()
*/
static void TestOffset()
{
UErrorCode status= U_ZERO_ERROR;
UCollator *en_us=NULL;
UCollationElements *iter, *pristine;
int32_t offset;
OrderAndOffset *orders;
int32_t orderLength=0;
int count = 0;
UChar test1[50];
UChar test2[50];
u_uastrcpy(test1, "What subset of all possible test cases?");
u_uastrcpy(test2, "has the highest probability of detecting");
en_us = ucol_open("en_US", &status);
log_verbose("Testing getOffset and setOffset for collations\n");
iter = ucol_openElements(en_us, test1, u_strlen(test1), &status);
if(U_FAILURE(status)){
log_err_status(status, "ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
/* testing boundaries */
ucol_setOffset(iter, 0, &status);
if (U_FAILURE(status) || ucol_previous(iter, &status) != UCOL_NULLORDER) {
log_err("Error: After setting offset to 0, we should be at the end "
"of the backwards iteration");
}
ucol_setOffset(iter, u_strlen(test1), &status);
if (U_FAILURE(status) || ucol_next(iter, &status) != UCOL_NULLORDER) {
log_err("Error: After setting offset to end of the string, we should "
"be at the end of the backwards iteration");
}
/* Run all the way through the iterator, then get the offset */
orders = getOrders(iter, &orderLength);
offset = ucol_getOffset(iter);
if (offset != u_strlen(test1))
{
log_err("offset at end != length %d vs %d\n", offset,
u_strlen(test1) );
}
/* Now set the offset back to the beginning and see if it works */
pristine=ucol_openElements(en_us, test1, u_strlen(test1), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
status = U_ZERO_ERROR;
ucol_setOffset(iter, 0, &status);
if (U_FAILURE(status))
{
log_err("setOffset failed. %s\n", myErrorName(status));
}
else
{
assertEqual(iter, pristine);
}
ucol_closeElements(pristine);
ucol_closeElements(iter);
free(orders);
/* testing offsets in normalization buffer */
test1[0] = 0x61;
test1[1] = 0x300;
test1[2] = 0x316;
test1[3] = 0x62;
test1[4] = 0;
ucol_setAttribute(en_us, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
iter = ucol_openElements(en_us, test1, 4, &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
count = 0;
while (ucol_next(iter, &status) != UCOL_NULLORDER &&
U_SUCCESS(status)) {
switch (count) {
case 0:
if (ucol_getOffset(iter) != 1) {
log_err("ERROR: Offset of iteration should be 1\n");
}
break;
case 3:
if (ucol_getOffset(iter) != 4) {
log_err("ERROR: Offset of iteration should be 4\n");
}
break;
default:
if (ucol_getOffset(iter) != 3) {
log_err("ERROR: Offset of iteration should be 3\n");
}
}
count ++;
}
ucol_reset(iter);
count = 0;
while (ucol_previous(iter, &status) != UCOL_NULLORDER &&
U_SUCCESS(status)) {
switch (count) {
case 0:
case 1:
if (ucol_getOffset(iter) != 3) {
log_err("ERROR: Offset of iteration should be 3\n");
}
break;
case 2:
if (ucol_getOffset(iter) != 1) {
log_err("ERROR: Offset of iteration should be 1\n");
}
break;
default:
if (ucol_getOffset(iter) != 0) {
log_err("ERROR: Offset of iteration should be 0\n");
}
}
count ++;
}
if(U_FAILURE(status)){
log_err("ERROR: in iterating collation elements %s\n",
myErrorName(status));
}
ucol_closeElements(iter);
ucol_close(en_us);
}
/**
* Test for setText()
*/
static void TestSetText()
{
int32_t c,i;
UErrorCode status = U_ZERO_ERROR;
UCollator *en_us=NULL;
UCollationElements *iter1, *iter2;
UChar test1[50];
UChar test2[50];
u_uastrcpy(test1, "What subset of all possible test cases?");
u_uastrcpy(test2, "has the highest probability of detecting");
en_us = ucol_open("en_US", &status);
log_verbose("testing setText for Collation elements\n");
iter1=ucol_openElements(en_us, test1, u_strlen(test1), &status);
if(U_FAILURE(status)){
log_err_status(status, "ERROR: in creation of collation element iterator1 using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
iter2=ucol_openElements(en_us, test2, u_strlen(test2), &status);
if(U_FAILURE(status)){
log_err("ERROR: in creation of collation element iterator2 using ucol_openElements()\n %s\n",
myErrorName(status));
ucol_close(en_us);
return;
}
/* Run through the second iterator just to exercise it */
c = ucol_next(iter2, &status);
i = 0;
while ( ++i < 10 && (c != UCOL_NULLORDER))
{
if (U_FAILURE(status))
{
log_err("iter2->next() returned an error. %s\n", myErrorName(status));
ucol_closeElements(iter2);
ucol_closeElements(iter1);
ucol_close(en_us);
return;
}
c = ucol_next(iter2, &status);
}
/* Now set it to point to the same string as the first iterator */
ucol_setText(iter2, test1, u_strlen(test1), &status);
if (U_FAILURE(status))
{
log_err("call to iter2->setText(test1) failed. %s\n", myErrorName(status));
}
else
{
assertEqual(iter1, iter2);
}
/* Now set it to point to a null string with fake length*/
ucol_setText(iter2, NULL, 2, &status);
if (status != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("call to iter2->setText(null, 2) should yield an illegal-argument-error - %s\n",
myErrorName(status));
}
ucol_closeElements(iter2);
ucol_closeElements(iter1);
ucol_close(en_us);
}
/** @bug 4108762
* Test for getMaxExpansion()
*/
static void TestMaxExpansion()
{
UErrorCode status = U_ZERO_ERROR;
UCollator *coll ;/*= ucol_open("en_US", &status);*/
UChar ch = 0;
UChar32 unassigned = 0xEFFFD;
UChar supplementary[2];
uint32_t stringOffset = 0;
UBool isError = false;
uint32_t sorder = 0;
UCollationElements *iter ;/*= ucol_openElements(coll, &ch, 1, &status);*/
uint32_t temporder = 0;
UChar rule[256];
u_uastrcpy(rule, "&a < ab < c/aba < d < z < ch");
coll = ucol_openRules(rule, u_strlen(rule), UCOL_DEFAULT,
UCOL_DEFAULT_STRENGTH,NULL, &status);
if(U_SUCCESS(status) && coll) {
iter = ucol_openElements(coll, &ch, 1, &status);
while (ch < 0xFFFF && U_SUCCESS(status)) {
int count = 1;
uint32_t order;
int32_t size = 0;
ch ++;
ucol_setText(iter, &ch, 1, &status);
order = ucol_previous(iter, &status);
/* thai management */
if (order == 0)
order = ucol_previous(iter, &status);
while (U_SUCCESS(status) &&
ucol_previous(iter, &status) != UCOL_NULLORDER) {
count ++;
}
size = ucol_getMaxExpansion(iter, order);
if (U_FAILURE(status) || size < count) {
log_err("Failure at codepoint %d, maximum expansion count < %d\n",
ch, count);
}
}
/* testing for exact max expansion */
ch = 0;
while (ch < 0x61) {
uint32_t order;
int32_t size;
ucol_setText(iter, &ch, 1, &status);
order = ucol_previous(iter, &status);
size = ucol_getMaxExpansion(iter, order);
if (U_FAILURE(status) || size != 1) {
log_err("Failure at codepoint %d, maximum expansion count < %d\n",
ch, 1);
}
ch ++;
}
ch = 0x63;
ucol_setText(iter, &ch, 1, &status);
temporder = ucol_previous(iter, &status);
if (U_FAILURE(status) || ucol_getMaxExpansion(iter, temporder) != 3) {
log_err("Failure at codepoint %d, maximum expansion count != %d\n",
ch, 3);
}
ch = 0x64;
ucol_setText(iter, &ch, 1, &status);
temporder = ucol_previous(iter, &status);
if (U_FAILURE(status) || ucol_getMaxExpansion(iter, temporder) != 1) {
log_err("Failure at codepoint %d, maximum expansion count != %d\n",
ch, 3);
}
U16_APPEND(supplementary, stringOffset, 2, unassigned, isError);
(void)isError; /* Suppress set but not used warning. */
ucol_setText(iter, supplementary, 2, &status);
sorder = ucol_previous(iter, &status);
if (U_FAILURE(status) || ucol_getMaxExpansion(iter, sorder) != 2) {
log_err("Failure at codepoint %d, maximum expansion count < %d\n",
ch, 2);
}
/* testing jamo */
ch = 0x1165;
ucol_setText(iter, &ch, 1, &status);
temporder = ucol_previous(iter, &status);
if (U_FAILURE(status) || ucol_getMaxExpansion(iter, temporder) > 3) {
log_err("Failure at codepoint %d, maximum expansion count > %d\n",
ch, 3);
}
ucol_closeElements(iter);
ucol_close(coll);
/* testing special jamo &a<\u1160 */
rule[0] = 0x26;
rule[1] = 0x71;
rule[2] = 0x3c;
rule[3] = 0x1165;
rule[4] = 0x2f;
rule[5] = 0x71;
rule[6] = 0x71;
rule[7] = 0x71;
rule[8] = 0x71;
rule[9] = 0;
coll = ucol_openRules(rule, u_strlen(rule), UCOL_DEFAULT,
UCOL_DEFAULT_STRENGTH,NULL, &status);
iter = ucol_openElements(coll, &ch, 1, &status);
temporder = ucol_previous(iter, &status);
if (U_FAILURE(status) || ucol_getMaxExpansion(iter, temporder) != 6) {
log_err("Failure at codepoint %d, maximum expansion count > %d\n",
ch, 5);
}
ucol_closeElements(iter);
ucol_close(coll);
} else {
log_err_status(status, "Couldn't open collator -> %s\n", u_errorName(status));
}
}
static void assertEqual(UCollationElements *i1, UCollationElements *i2)
{
int32_t c1, c2;
int32_t count = 0;
UErrorCode status = U_ZERO_ERROR;
do
{
c1 = ucol_next(i1, &status);
c2 = ucol_next(i2, &status);
if (c1 != c2)
{
log_err("Error in iteration %d assetEqual between\n %d and %d, they are not equal\n", count, c1, c2);
break;
}
count += 1;
}
while (c1 != UCOL_NULLORDER);
}
/**
* Testing iterators with extremely small buffers
*/
static void TestSmallBuffer()
{
UErrorCode status = U_ZERO_ERROR;
UCollator *coll;
UCollationElements *testiter,
*iter;
int32_t count = 0;
OrderAndOffset *testorders,
*orders;
UChar teststr[500];
UChar str[] = {0x300, 0x31A, 0};
/*
creating a long string of decomposable characters,
since by default the writable buffer is of size 256
*/
while (count < 500) {
if ((count & 1) == 0) {
teststr[count ++] = 0x300;
}
else {
teststr[count ++] = 0x31A;
}
}
coll = ucol_open("th_TH", &status);
if(U_SUCCESS(status) && coll) {
testiter = ucol_openElements(coll, teststr, 500, &status);
iter = ucol_openElements(coll, str, 2, &status);
orders = getOrders(iter, &count);
if (count != 2) {
log_err("Error collation elements size is not 2 for \\u0300\\u031A\n");
}
/*
this will rearrange the string data to 250 characters of 0x300 first then
250 characters of 0x031A
*/
testorders = getOrders(testiter, &count);
if (count != 500) {
log_err("Error decomposition does not give the right sized collation elements\n");
}
while (count != 0) {
/* UCA collation element for 0x0F76 */
if ((count > 250 && testorders[-- count].order != orders[1].order) ||
(count <= 250 && testorders[-- count].order != orders[0].order)) {
log_err("Error decomposition does not give the right collation element at %d count\n", count);
break;
}
}
free(testorders);
free(orders);
ucol_reset(testiter);
/* ensures closing of elements done properly to clear writable buffer */
ucol_next(testiter, &status);
ucol_next(testiter, &status);
ucol_closeElements(testiter);
ucol_closeElements(iter);