]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
374ca955 A |
3 | /* |
4 | ******************************************************************************* | |
5 | * | |
b331163b | 6 | * Copyright (C) 2003-2014, International Business Machines |
374ca955 A |
7 | * Corporation and others. All Rights Reserved. |
8 | * | |
9 | ******************************************************************************* | |
57a6839d | 10 | * file name: sorttest.c |
f3c0d7a5 | 11 | * encoding: UTF-8 |
374ca955 A |
12 | * tab size: 8 (not used) |
13 | * indentation:4 | |
14 | * | |
15 | * created on: 2003aug04 | |
16 | * created by: Markus W. Scherer | |
17 | * | |
18 | * Test internal sorting functions. | |
19 | */ | |
20 | ||
57a6839d A |
21 | #include <stdio.h> |
22 | ||
374ca955 | 23 | #include "unicode/utypes.h" |
57a6839d A |
24 | #include "unicode/ucol.h" |
25 | #include "unicode/ustring.h" | |
374ca955 A |
26 | #include "cmemory.h" |
27 | #include "cintltst.h" | |
28 | #include "uarrsort.h" | |
29 | ||
374ca955 | 30 | static void |
3d1f044b | 31 | SortTest(void) { |
374ca955 | 32 | uint16_t small[]={ 8, 1, 2, 5, 4, 3, 7, 6 }; |
73c04bcf | 33 | int32_t medium[]={ 10, 8, 1, 2, 5, 5, -1, 6, 4, 3, 9, 7, 5 }; |
374ca955 A |
34 | uint32_t large[]={ 21, 10, 20, 19, 11, 12, 13, 10, 10, 10, 10, |
35 | 8, 1, 2, 5, 10, 10, 4, 17, 18, 3, 9, 10, 7, 6, 14, 15, 16 }; | |
36 | ||
37 | int32_t i; | |
38 | UErrorCode errorCode; | |
39 | ||
40 | /* sort small array (stable) */ | |
41 | errorCode=U_ZERO_ERROR; | |
b331163b | 42 | uprv_sortArray(small, UPRV_LENGTHOF(small), sizeof(small[0]), uprv_uint16Comparator, NULL, TRUE, &errorCode); |
374ca955 A |
43 | if(U_FAILURE(errorCode)) { |
44 | log_err("uprv_sortArray(small) failed - %s\n", u_errorName(errorCode)); | |
45 | return; | |
46 | } | |
b331163b | 47 | for(i=1; i<UPRV_LENGTHOF(small); ++i) { |
374ca955 A |
48 | if(small[i-1]>small[i]) { |
49 | log_err("uprv_sortArray(small) mis-sorted [%d]=%u > [%d]=%u\n", i-1, small[i-1], i, small[i]); | |
50 | return; | |
51 | } | |
52 | } | |
53 | ||
54 | /* for medium, add bits that will not be compared, to test stability */ | |
b331163b | 55 | for(i=0; i<UPRV_LENGTHOF(medium); ++i) { |
f3c0d7a5 | 56 | medium[i]=(int32_t)((uint32_t)medium[i]<<4) | i; |
374ca955 A |
57 | } |
58 | ||
59 | /* sort medium array (stable) */ | |
b331163b | 60 | uprv_sortArray(medium, UPRV_LENGTHOF(medium), sizeof(medium[0]), uprv_int32Comparator, NULL, TRUE, &errorCode); |
374ca955 A |
61 | if(U_FAILURE(errorCode)) { |
62 | log_err("uprv_sortArray(medium) failed - %s\n", u_errorName(errorCode)); | |
63 | return; | |
64 | } | |
b331163b | 65 | for(i=1; i<UPRV_LENGTHOF(medium); ++i) { |
374ca955 A |
66 | if(medium[i-1]>=medium[i]) { |
67 | log_err("uprv_sortArray(medium) mis-sorted [%d]=%u > [%d]=%u\n", i-1, medium[i-1], i, medium[i]); | |
68 | return; | |
69 | } | |
70 | } | |
71 | ||
72 | /* sort large array (not stable) */ | |
73 | errorCode=U_ZERO_ERROR; | |
b331163b | 74 | uprv_sortArray(large, UPRV_LENGTHOF(large), sizeof(large[0]), uprv_uint32Comparator, NULL, FALSE, &errorCode); |
374ca955 A |
75 | if(U_FAILURE(errorCode)) { |
76 | log_err("uprv_sortArray(large) failed - %s\n", u_errorName(errorCode)); | |
77 | return; | |
78 | } | |
b331163b | 79 | for(i=1; i<UPRV_LENGTHOF(large); ++i) { |
374ca955 A |
80 | if(large[i-1]>large[i]) { |
81 | log_err("uprv_sortArray(large) mis-sorted [%d]=%u > [%d]=%u\n", i-1, large[i-1], i, large[i]); | |
82 | return; | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
57a6839d A |
87 | #if !UCONFIG_NO_COLLATION |
88 | ||
89 | /* | |
90 | * Fill an array with semi-random short strings. | |
91 | * Vary them enough to be interesting, but create duplicates. | |
92 | * With CYCLE=10 characters per STR_LEN=3 string positions there are only 1000 unique strings. | |
93 | * NUM_LINES should be larger than this. | |
94 | */ | |
95 | #define NUM_LINES 10000 | |
96 | #define STR_LEN 3 | |
97 | #define CYCLE 10 | |
98 | ||
99 | /* | |
100 | * Use characters beyond the Latin Extended A block to avoid a collator fastpath. | |
101 | * They should sort unique, so that we can later use a binary comparison for string equality. | |
102 | */ | |
103 | #define BASE_CHAR 0x200 | |
104 | ||
105 | typedef struct Line { | |
106 | UChar s[STR_LEN]; | |
107 | int32_t recordNumber; | |
108 | } Line; | |
109 | ||
110 | static void | |
111 | printLines(const Line *lines) { | |
112 | #if 0 | |
113 | int32_t i, j; | |
114 | for(i=0; i<NUM_LINES; ++i) { | |
115 | const Line *line=lines+i; | |
116 | for(j=0; j<STR_LEN; ++j) { | |
117 | printf("%04x ", line->s[j]); | |
118 | } | |
119 | printf(" #%5d\n", line->recordNumber); | |
120 | } | |
121 | #endif | |
122 | } | |
123 | ||
124 | /* Use a collator so that the comparisons are not essentially free, for simple benchmarking. */ | |
125 | static int32_t U_EXPORT2 | |
126 | linesComparator(const void *context, const void *left, const void *right) { | |
127 | const UCollator *coll=(const UCollator *)context; | |
128 | const Line *leftLine=(const Line *)left; | |
129 | const Line *rightLine=(const Line *)right; | |
130 | /* compare the strings but not the record number */ | |
131 | return ucol_strcoll(coll, leftLine->s, STR_LEN, rightLine->s, STR_LEN); | |
132 | } | |
133 | ||
3d1f044b | 134 | static void StableSortTest(void) { |
57a6839d A |
135 | UErrorCode errorCode=U_ZERO_ERROR; |
136 | UCollator *coll; | |
137 | Line *lines, *p; | |
138 | UChar s[STR_LEN]; | |
139 | int32_t i, j; | |
140 | ||
141 | coll=ucol_open("root", &errorCode); | |
142 | if(U_FAILURE(errorCode)) { | |
143 | log_data_err("ucol_open(root) failed - %s\n", u_errorName(errorCode)); | |
144 | return; | |
145 | } | |
146 | ||
147 | lines=p=(Line *)uprv_malloc(NUM_LINES*sizeof(Line)); | |
148 | uprv_memset(lines, 0, NUM_LINES*sizeof(Line)); /* avoid uninitialized memory */ | |
149 | ||
150 | for(j=0; j<STR_LEN; ++j) { s[j]=BASE_CHAR; } | |
151 | j=0; | |
152 | for(i=0; i<NUM_LINES; ++i) { | |
153 | UChar c; | |
154 | u_memcpy(p->s, s, STR_LEN); | |
155 | p->recordNumber=i; | |
156 | /* Modify the string for the next line. */ | |
157 | c=s[j]+1; | |
158 | if(c==BASE_CHAR+CYCLE) { c=BASE_CHAR; } | |
159 | s[j]=c; | |
160 | if(++j==STR_LEN) { j=0; } | |
161 | ++p; | |
162 | } | |
163 | puts("\n* lines before sorting"); | |
164 | printLines(lines); | |
165 | ||
166 | uprv_sortArray(lines, NUM_LINES, (int32_t)sizeof(Line), | |
167 | linesComparator, coll, TRUE, &errorCode); | |
168 | if(U_FAILURE(errorCode)) { | |
169 | log_err("uprv_sortArray() failed - %s\n", u_errorName(errorCode)); | |
170 | return; | |
171 | } | |
172 | puts("* lines after sorting"); | |
173 | printLines(lines); | |
174 | ||
175 | /* Verify that the array is sorted correctly. */ | |
176 | p=lines; | |
177 | for(i=1; i<NUM_LINES; ++i) { | |
178 | Line *q=p+1; /* =lines+i */ | |
179 | /* Binary comparison first, for speed. In this case, equal strings must be identical. */ | |
180 | int32_t diff=u_strCompare(p->s, STR_LEN, q->s, STR_LEN, FALSE); | |
181 | if(diff==0) { | |
182 | if(p->recordNumber>=q->recordNumber) { | |
183 | log_err("equal strings %d and %d out of order at sorted index %d\n", | |
184 | (int)p->recordNumber, (int)q->recordNumber, (int)i); | |
185 | break; | |
186 | } | |
187 | } else { | |
188 | /* Compare unequal strings with the collator. */ | |
189 | diff=ucol_strcoll(coll, p->s, STR_LEN, q->s, STR_LEN); | |
190 | if(diff>=0) { | |
191 | log_err("unequal strings %d and %d out of order at sorted index %d\n", | |
192 | (int)p->recordNumber, (int)q->recordNumber, (int)i); | |
193 | break; | |
194 | } | |
195 | } | |
196 | p=q; | |
197 | } | |
198 | ||
199 | uprv_free(lines); | |
200 | ucol_close(coll); | |
201 | } | |
202 | ||
203 | #endif /* !UCONFIG_NO_COLLATION */ | |
204 | ||
374ca955 A |
205 | void |
206 | addSortTest(TestNode** root); | |
207 | ||
208 | void | |
209 | addSortTest(TestNode** root) { | |
210 | addTest(root, &SortTest, "tsutil/sorttest/SortTest"); | |
57a6839d A |
211 | #if !UCONFIG_NO_COLLATION |
212 | addTest(root, &StableSortTest, "tsutil/sorttest/StableSortTest"); | |
213 | #endif | |
374ca955 | 214 | } |