]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/ucmptst.c
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / cintltst / ucmptst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1998-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ********************************************************************/
6 /*
7 * File test.c
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 07/28/2000 Madhu Creation
13 *******************************************************************************
14 */
15
16 #include "unicode/utypes.h"
17 #include "ucmp8.h"
18 #include "cmemory.h"
19 #include "cintltst.h"
20 #include "ucol_imp.h"
21
22
23 static void TestUCMP8API(void);
24
25 void addCompactArrayTest(TestNode** root);
26
27
28 void
29 addCompactArrayTest(TestNode** root)
30 {
31 addTest(root, &TestUCMP8API, "ucmptst/TestUCMP8API");
32 }
33
34 static void query(CompactByteArray *array) {
35 int32_t i = 0;
36 const uint8_t *valuesSet=(uint8_t *)ucmp8_getArray(array);
37 for(i =0 ; i< 10; i++ ){
38 if(valuesSet[0] != (uint8_t)0xFD ){
39 log_err("ERROR: did not get the values expected values\n");
40 break;
41 }
42 }
43 }
44
45 static void TestUCMP8API(){
46 UErrorCode status = U_ZERO_ERROR;
47 CompactByteArray* ucmp8Array=NULL;
48 CompactByteArray* pAliaser=NULL;
49 CompactByteArray* pAdopter=NULL;
50
51 CompactByteArray aliaser;
52 CompactByteArray adopter;
53
54
55 CompactByteArray ucmp8Array1;
56 CompactByteArray ucmp8Array2;
57 CompactByteArray ucmp8Clone;
58 int8_t *values;
59 uint8_t *valuesSet;
60 static const int8_t TEST_DEFAULT_VALUE = (int8_t)0xFF;
61
62
63 /*ucmp8_open*/
64 log_verbose("Testing ucmp8_open()\n");
65 ucmp8Array=ucmp8_open(TEST_DEFAULT_VALUE);
66 if(ucmp8Array == NULL){
67 log_err("ERROR: ucmp8_open() failed\n");
68 }
69 if( (int32_t)ucmp8_getCount(ucmp8Array) != (int32_t)ucmp8_getkUnicodeCount()) {
70 log_err("ERROR: ucmp8_open failed\n");
71 }
72
73 /*ucmp8_init*/
74 log_verbose("Testing ucmp8_init()\n");
75 ucmp8_init(&ucmp8Array1, TEST_DEFAULT_VALUE);
76 if( (int32_t)ucmp8_getCount(&ucmp8Array1) != (int32_t)ucmp8_getkUnicodeCount() ||
77 ucmp8_getIndex(&ucmp8Array1) == NULL ||
78 ucmp8_getArray(&ucmp8Array1) == NULL ||
79 ucmp8Array1.fBogus != FALSE){
80 log_err("Error: ucmp8_init() failed\n");
81 }
82 /*ucmp8_initBogus*/
83 log_verbose("Testing ucmp8_initBogus()\n");
84 ucmp8_initBogus(&ucmp8Array2);
85 if((int32_t)ucmp8_getCount(&ucmp8Array2) != ucmp8Array2.fCount ||
86 ucmp8_getIndex(&ucmp8Array2) != NULL ||
87 ucmp8_getArray(&ucmp8Array2) != NULL ||
88 ucmp8Array2.fBogus != TRUE){
89 log_err("Error: ucmp8_initBogus() failed\n");
90 }
91 /*ucmp8_getkBlockCount*/
92 if(ucmp8_getkBlockCount() != 128 ){
93 log_err("Error in ucmp8_getkBlockCount()\n");
94 }
95 values=(int8_t*)ucmp8_getArray(&ucmp8Array1);
96 if((uint8_t)values[0] != (uint8_t)TEST_DEFAULT_VALUE){
97 log_err("Error: getArray() or init failed\n");
98 }
99
100 /*ucmp8_compact*/
101 if(ucmp8Array1.fCompact == TRUE){
102 log_err("Error: ucmp8_open failed Got compact for expanded data\n");
103 }
104 ucmp8_compact(&ucmp8Array1, 1);
105 if(ucmp8Array1.fCompact != TRUE){
106 log_err("Error: ucmp8_compact failed\n");
107 }
108 /*ucmp8_set*/
109 ucmp8_set(&ucmp8Array1, 0, (uint8_t)0xFE);
110 valuesSet=(uint8_t*)ucmp8_getArray(&ucmp8Array1);
111 if(valuesSet[0] != (uint8_t)0xFE ){
112 log_err("ERROR: ucmp8_set() failed\n");
113 }
114 if(ucmp8Array1.fCompact == TRUE){
115 log_err("Error: ucmp8_set didn't expand the compact data \n");
116 }
117
118 /*ucmp8_set*/
119 ucmp8_compact(&ucmp8Array1, 1);
120 ucmp8_set(&ucmp8Array1, 0, (uint8_t)0xFD);
121 valuesSet=(uint8_t*)ucmp8_getArray(&ucmp8Array1);
122 if(valuesSet[0] != (uint8_t)0xFD ){
123 log_err("ERROR: ucmp8_set() failed\n");
124 }
125 if(ucmp8Array1.fCompact == TRUE){
126 log_err("Error: ucmp8_set didn't expand the compact data \n");
127 }
128 /*ucmp8_setRange*/
129 ucmp8_compact(&ucmp8Array1, 1);
130 ucmp8_setRange(&ucmp8Array1, 0, 10, (uint8_t)0xFD);
131 query(&ucmp8Array1);
132
133 log_verbose("Testing ucmp8_flattenMem()\n");
134 {
135 int32_t len = 0;
136 int32_t size = ucmp8_flattenMem(&ucmp8Array1, NULL);
137 uint8_t *buff = malloc(size);
138 uint8_t *buffLocation = buff;
139 len = ucmp8_flattenMem(&ucmp8Array1, buff);
140
141 if(size != len || size == 0 || len == 0 || buff == NULL) {
142 log_err("Unable to flatten!\n");
143 } else {
144 log_verbose("Testing ucmp8_initFromData()\n");
145 ucmp8_initFromData(&ucmp8Clone, (const uint8_t **)&buffLocation, &status);
146 if(U_FAILURE(status) || ucmp8_isBogus(&ucmp8Clone) == TRUE || (buffLocation-buff) != len){
147 log_err("ERROR: ucmp8_initFromData() failed\n");
148 status = U_ZERO_ERROR;
149 } else {
150 query(&ucmp8Clone);
151 ucmp8_close(&ucmp8Clone);
152 }
153 }
154 free(buff);
155 }
156
157 /*
158 openAdopt, initAdopt, openAlias, initAlias
159 */
160 log_verbose("Testing aliasers and adopters\n");
161 {
162 int32_t count = ucmp8_getCount(&ucmp8Array1);
163 const uint16_t *tIndex = ucmp8_getIndex(&ucmp8Array1);
164 const int8_t *tValues = ucmp8_getArray(&ucmp8Array1);
165 uint16_t *index = (uint16_t *)uprv_malloc(UCMP8_kIndexCount*sizeof(uint16_t));
166
167 values = (int8_t *)uprv_malloc(count);
168
169 memcpy(index, tIndex, UCMP8_kIndexCount*sizeof(uint16_t));
170 memcpy(values, tValues, count);
171
172 ucmp8_initAlias(&aliaser, index, values, count);
173 query(&aliaser);
174 ucmp8_close(&aliaser);
175
176 pAliaser = ucmp8_openAlias(index, values, count);
177 query(pAliaser);
178 ucmp8_close(pAliaser);
179
180 ucmp8_initAdopt(&adopter, index, values, count); /* TODO: BAD API. Adopted memory MUST be allocated with uprv_malloc */
181 query(&adopter);
182 ucmp8_close(&adopter);
183
184 index = (uint16_t *)uprv_malloc(UCMP8_kIndexCount*sizeof(uint16_t));
185 values = (int8_t *)uprv_malloc(count);
186
187 memcpy(index, tIndex, UCMP8_kIndexCount*sizeof(uint16_t));
188 memcpy(values, tValues, count);
189
190 pAdopter = ucmp8_openAdopt(index, values, count); /* TODO: BAD API */
191 query(pAdopter);
192 ucmp8_close(pAdopter);
193
194 }
195 ucmp8_close(&ucmp8Array1);
196 ucmp8_close(&ucmp8Array2);
197 ucmp8_close(ucmp8Array);
198
199 }
200
201