1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2009-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 */
11
12 #include <unicode/coll.h>
13 #include <unicode/uclean.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18
19 /* String to use. */
20 const UChar stuff[] = { 0x30BB, 0x0d4c, 0x53, 0x74, 0x75, 0x66, 0x66, 0x00 }; /* Stuff */
21
22 #include "provider_version.h"
23
24
25 #define LOCALE_COUNT 4
26 const char *locale[LOCALE_COUNT] = { "fi", "en_US", "ja", "ml" }; /* List of locales to test */
27
28 /**
29 * Set up ICU, print # of available collators
30 */
setup(UErrorCode & status)31 void setup(UErrorCode &status) {
32 u_init(&status);
33
34 fprintf(stderr, "ICU %s init: %s\n", U_ICU_VERSION, u_errorName(status));
35
36 int32_t count;
37 StringEnumeration *se = Collator::getAvailableLocales();
38 count = se->count(status);
39 fprintf(stderr, "# Collators now available: %d,\t%s - %d providers expected.\n", count, u_errorName(status), (int32_t)PROVIDER_COUNT);
40 }
41
main(int,const char * [])42 int main(int /* argc*/ , const char * /*argv*/ []) {
43 UErrorCode status = U_ZERO_ERROR;
44 int diffs = 0;
45 int gbaddiffs =0;
46 setup(status);
47 if(U_FAILURE(status)) return 1;
48
49 int expected = PROVIDER_COUNT;
50
51 for(int l=0;l<LOCALE_COUNT;l++) {
52 printf("\n");
53 uint8_t oldBytes[200];
54 int32_t oldLen = -1;
55 for(int v=0;v<=expected;v++) {
56
57 // Construct the locale ID
58 char locID[200];
59 strcpy(locID, locale[l]);
60 if((v!=expected)) { // -1 = no version
61 strcat(locID, "@sp=icu");
62 strcat(locID, provider_version[v]);
63 }
64
65 printf("%-28s = ", locID);
66
67 UErrorCode subStatus = U_ZERO_ERROR;
68 uint8_t bytes[200];
69 uint8_t bytesb[200];
70 #define USE_CXX 0
71
72 #if USE_CXX
73 Collator *col = Collator::createInstance(Locale(locID),subStatus);
74 if(U_FAILURE(subStatus)) {
75 printf("ERR: %s\n", u_errorName(subStatus));
76 continue;
77 }
78 int32_t len = col->getSortKey(stuff, -1, bytes, 200);
79 #else
80 #if 1
81 char xbuf2[200];
82 strcpy(xbuf2,"X/");
83 strcat(xbuf2,locID);
84 strcat(xbuf2,"/");
85 //printf(" -> %s\n", xbuf2);
86 UCollator *col = ucol_openFromShortString(xbuf2, FALSE,NULL, &subStatus);
87 #else
88 UCollator *col = ucol_open(locID, &subStatus);
89 #endif
90 if(U_FAILURE(subStatus)) {
91 printf("ERR: %s\n", u_errorName(subStatus));
92 continue;
93 }
94
95
96 char xbuf3[200];
97 {
98 int32_t def = ucol_getShortDefinitionString(col,locID/*NULL*/,xbuf3,200,&subStatus);
99 if(U_FAILURE(subStatus)) {
100 printf("Err getting short string name: %s\n", u_errorName(subStatus));
101 } else {
102 printf(" --> %s\n", xbuf3);
103 }
104 }
105
106 int32_t len = ucol_getSortKey(col, stuff, -1, bytes, 200);
107 #endif
108
109 printf(" ");
110
111 int tdiffs=0;
112
113 for(int i=0;i<len;i++) {
114 if(i<oldLen&&bytes[i]!=oldBytes[i]) {
115 diffs++;
116 printf("*");
117 } else {
118 printf(" ");
119 }
120 printf("%02X", (0xFF&bytes[i]));
121 }
122 printf("\n");
123
124 char xbuf4[200];
125 UCollator *col2 = ucol_openFromShortString(xbuf3, FALSE, NULL, &subStatus);
126 if(U_FAILURE(subStatus)) {
127 printf("Err opening from new short string : %s\n", u_errorName(subStatus));
128 continue;
129 } else {
130 int32_t def4 = ucol_getShortDefinitionString(col,locID/*NULL*/,xbuf4,200,&subStatus);
131 if(strcmp(xbuf4,xbuf3)) {
132 printf(" --> reopened = %s (%s)\n", xbuf4, u_errorName(subStatus));
133 }
134 }
135 int32_t len2 = ucol_getSortKey(col2, stuff, -1, bytesb, 200);
136
137 int baddiffs=0;
138 for(int i=0;i<len;i++) {
139 if(i<len&&bytes[i]!=bytesb[i]) {
140 baddiffs++;
141 printf("!");
142 } else {
143 // printf(" ");
144 }
145 // printf("%02X", (0xFF&bytesb[i]));
146 }
147 if(baddiffs>0) {
148 printf(" - ERR! Diffs from %s in %d places\n", xbuf2,baddiffs);
149 gbaddiffs+=baddiffs;
150 } else {
151 //printf(" OK.\n");
152 }
153 // printf("\n");
154
155
156
157 #if USE_CXX
158 delete col;
159 #else
160 ucol_close(col);
161 #endif
162
163 oldLen = len;
164 memcpy(oldBytes, bytes, len);
165 }
166 }
167
168 if(diffs==0) {
169 #if (U_ICU_VERSION_MAJOR_NUM < 49)
170 printf("ERROR: 0 differences found between platforms. ICU " U_ICU_VERSION " does not support collator plugins properly (not until 49)\n");
171 #else
172 printf("ERROR: 0 differences found between platforms.. are the platforms installed? Try 'icuinfo -L'\n");
173 #endif
174 return 1;
175 } else {
176 printf("%d differences found among provider versions!\n", diffs);
177 }
178
179 if(gbaddiffs>0) {
180 printf("ERROR: %d diffs found between a collator and it's reopened (from shortstring) variant.\n", gbaddiffs);
181 return 2;
182 } else {
183 printf("Collator and reopened (shortstring) are OK.\n");
184 }
185
186 printf("Success!\n");
187
188 return 0;
189 }
190