1 /********************************************************************
3 * Copyright (c) 1998-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ********************************************************************/
9 * Modification History:
11 * Date Name Description
12 * 07/28/2000 Madhu Creation
13 *******************************************************************************
16 #include "unicode/utypes.h"
23 static void TestUCMP8API(void);
25 void addCompactArrayTest(TestNode
** root
);
29 addCompactArrayTest(TestNode
** root
)
31 addTest(root
, &TestUCMP8API
, "ucmptst/TestUCMP8API");
34 static void query(CompactByteArray
*array
) {
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");
45 static void TestUCMP8API(){
46 UErrorCode status
= U_ZERO_ERROR
;
47 CompactByteArray
* ucmp8Array
=NULL
;
48 CompactByteArray
* pAliaser
=NULL
;
49 CompactByteArray
* pAdopter
=NULL
;
51 CompactByteArray aliaser
;
52 CompactByteArray adopter
;
55 CompactByteArray ucmp8Array1
;
56 CompactByteArray ucmp8Array2
;
57 CompactByteArray ucmp8Clone
;
60 static const int8_t TEST_DEFAULT_VALUE
= (int8_t)0xFF;
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");
69 if( (int32_t)ucmp8_getCount(ucmp8Array
) != (int32_t)ucmp8_getkUnicodeCount()) {
70 log_err("ERROR: ucmp8_open failed\n");
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");
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");
91 /*ucmp8_getkBlockCount*/
92 if(ucmp8_getkBlockCount() != 128 ){
93 log_err("Error in ucmp8_getkBlockCount()\n");
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");
101 if(ucmp8Array1
.fCompact
== TRUE
){
102 log_err("Error: ucmp8_open failed Got compact for expanded data\n");
104 ucmp8_compact(&ucmp8Array1
, 1);
105 if(ucmp8Array1
.fCompact
!= TRUE
){
106 log_err("Error: ucmp8_compact failed\n");
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");
114 if(ucmp8Array1
.fCompact
== TRUE
){
115 log_err("Error: ucmp8_set didn't expand the compact data \n");
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");
125 if(ucmp8Array1
.fCompact
== TRUE
){
126 log_err("Error: ucmp8_set didn't expand the compact data \n");
129 ucmp8_compact(&ucmp8Array1
, 1);
130 ucmp8_setRange(&ucmp8Array1
, 0, 10, (uint8_t)0xFD);
133 log_verbose("Testing ucmp8_flattenMem()\n");
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
);
141 if(size
!= len
|| size
== 0 || len
== 0 || buff
== NULL
) {
142 log_err("Unable to flatten!\n");
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
;
151 ucmp8_close(&ucmp8Clone
);
158 openAdopt, initAdopt, openAlias, initAlias
160 log_verbose("Testing aliasers and adopters\n");
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));
167 values
= (int8_t *)uprv_malloc(count
);
169 memcpy(index
, tIndex
, UCMP8_kIndexCount
*sizeof(uint16_t));
170 memcpy(values
, tValues
, count
);
172 ucmp8_initAlias(&aliaser
, index
, values
, count
);
174 ucmp8_close(&aliaser
);
176 pAliaser
= ucmp8_openAlias(index
, values
, count
);
178 ucmp8_close(pAliaser
);
180 ucmp8_initAdopt(&adopter
, index
, values
, count
); /* TODO: BAD API. Adopted memory MUST be allocated with uprv_malloc */
182 ucmp8_close(&adopter
);
184 index
= (uint16_t *)uprv_malloc(UCMP8_kIndexCount
*sizeof(uint16_t));
185 values
= (int8_t *)uprv_malloc(count
);
187 memcpy(index
, tIndex
, UCMP8_kIndexCount
*sizeof(uint16_t));
188 memcpy(values
, tValues
, count
);
190 pAdopter
= ucmp8_openAdopt(index
, values
, count
); /* TODO: BAD API */
192 ucmp8_close(pAdopter
);
195 ucmp8_close(&ucmp8Array1
);
196 ucmp8_close(&ucmp8Array2
);
197 ucmp8_close(ucmp8Array
);