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