]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/mstrmtst.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / cintltst / mstrmtst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1998-2001, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /*
7 * File memstrts.c (Tests the API in umemstrm)
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 07/19/2000 Madhu Creation
13 *******************************************************************************
14 */
15
16 #include <stdio.h>
17 #include "unicode/utypes.h"
18 #include "unicode/ustring.h"
19 #include "cmemory.h"
20 #include "cintltst.h"
21 #include "umemstrm.h"
22
23 static void TestMemoryStreamAPI(void);
24 static void printUSeqErr(const uint8_t *a, int len);
25
26 void addMemoryStreamTest(TestNode** root);
27
28 static void printUSeqErr(const uint8_t *a, int len)
29 {
30 int i=0;
31 fprintf(stderr, "{U+ ");
32 while (i<len)
33 fprintf(stderr, "0x%02x ", a[i++]);
34 fprintf(stderr,"}\n");
35 }
36
37 void
38 addMemoryStreamTest(TestNode** root)
39 {
40 addTest(root, &TestMemoryStreamAPI, "/tsutil/mstrmtst/TestMemoryStreamAPI");
41
42 }
43
44 static void TestMemoryStreamAPI(){
45 UMemoryStream *memStream=NULL;
46 int32_t size=999, x=0;
47 const uint8_t *gotBuffer=0;
48 uint8_t buffer[]={ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
49 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
50 };
51
52 log_verbose("Testing the function uprv_mstrm_openNew()\n");
53 memStream=uprv_mstrm_openNew(size);
54 if(memStream == NULL){
55 log_err("uprv_mstrm_openNew() failed\n");
56 }
57 uprv_mstrm_close(memStream);
58
59 log_verbose("Testing the function uprv_mstrm_openNew() with size=0\n");
60 memStream=uprv_mstrm_openNew(0);
61 if(memStream == NULL){
62 log_err("uprv_mstrm_openNew() failed with size=0\n");
63 }
64
65 log_verbose("Testing the function uprv_mstrm_write()\n");
66 x=uprv_mstrm_write(memStream, buffer, sizeof(buffer)/sizeof(buffer[0]) );
67 if(x == -1){
68 log_err("uprv_mstrm_write() failed\n");
69 }
70 if(x != sizeof(buffer)/sizeof(buffer[0])){
71 log_err("uprv_mstrm_write() wrote %d characters instead of %d\n", x, sizeof(buffer)/sizeof(buffer[0]));
72 }
73
74 log_verbose("Testing the function uprv_mstrm_getBuffer())\n");
75 x=0;
76 gotBuffer=uprv_mstrm_getBuffer(memStream, &x);
77 if(uprv_memcmp(buffer, gotBuffer, sizeof(buffer)/sizeof(buffer[0])) != 0){
78 log_err("uprv_mstrm_getBuffer() failed\n");
79 printf("\nGot:");
80 printUSeqErr(gotBuffer, sizeof(buffer)/sizeof(buffer[0]));
81 printf("\nExpected:");
82 printUSeqErr(buffer, sizeof(buffer)/sizeof(buffer[0]));
83 }
84
85 log_verbose("Testing the function uprv_mstrm_read()\n");
86 uprv_mstrm_read(memStream, (uint8_t*)gotBuffer, 1);
87 if(uprv_memcmp(buffer, gotBuffer, 1) != 0){
88 log_err("uprv_mstrm_read() failed\n");
89 printf("\nGot:");
90 printUSeqErr(gotBuffer, 1);
91 printf("\nExpected:");
92 printUSeqErr(buffer, 1);
93
94 }
95 uprv_mstrm_read(memStream, (uint8_t*)gotBuffer, 5);
96 if(uprv_memcmp(buffer+1, gotBuffer, 5) != 0){
97 log_err("uprv_mstrm_read() failed\n");
98 printf("\nGot:");
99 printUSeqErr(gotBuffer, 5);
100 printf("\nExpected:");
101 printUSeqErr(buffer+1, 5);
102
103 }
104 uprv_mstrm_read(memStream, (uint8_t*)gotBuffer, 8);
105 if(uprv_memcmp(buffer+6, gotBuffer, 8) != 0){
106 log_err("uprv_mstrm_read() failed\n");
107 printf("\nGot:");
108 printUSeqErr(gotBuffer, 8);
109 printf("\nExpected:");
110 printUSeqErr(buffer+6, 8);
111
112
113 }
114 /*try to read outside the limit*/
115 /*It just reads untill the limit and sets the error and eof flags*/
116 x=uprv_mstrm_read(memStream, (uint8_t*)gotBuffer, 5);
117 if(uprv_memcmp(buffer+14, gotBuffer, 2) != 0){
118 log_err("uprv_mstrm_read() failed\n");
119 printf("\nGot:");
120 printUSeqErr(gotBuffer, 2);
121 printf("\nExpected:");
122 printUSeqErr(buffer+14, 2);
123 }
124 if(uprv_mstrm_error(memStream) != TRUE || uprv_mstrm_eof(memStream) != TRUE){
125 log_err("Trying to read outside the limit should set the error and eof to TRUE\n");
126 }
127
128 uprv_mstrm_close(memStream);
129
130 log_verbose("Testing the function uprv_mstrm_openBuffer()\n");
131 memStream=uprv_mstrm_openBuffer(buffer, size);
132 if(memStream == NULL){
133 log_err("uprv_mstrm_openBuffer() failed\n");
134 }
135 log_verbose("Testing the function uprv_mstrm_getBuffer())\n");
136 x=0;
137 gotBuffer=uprv_mstrm_getBuffer(memStream, &x);
138 if(uprv_memcmp(buffer, gotBuffer, sizeof(buffer)/sizeof(buffer[0])) != 0){
139 log_err("uprv_mstrm_getBuffer() failed\n");
140 printf("\nGot:");
141 printUSeqErr(gotBuffer, sizeof(buffer)/sizeof(buffer[0]));
142 printf("\nExpected:");
143 printUSeqErr(buffer, sizeof(buffer)/sizeof(buffer[0]));
144 }
145
146 log_verbose("Test that function uprv_mstrm_openBuffer() opens it in the read only mode\n");
147 x=uprv_mstrm_write(memStream, gotBuffer, 2);
148 if(x !=0 || uprv_mstrm_error(memStream) != TRUE){
149 log_err("trying to write into a read only buffer should fail\n");
150 }
151 uprv_mstrm_close(memStream);
152
153
154 memStream=uprv_mstrm_openNew(1);
155 if(memStream == NULL){
156 log_err("uprv_mstrm_openNew() failed\n");
157 }
158 log_verbose("Testing the function uprv_mstrm_write() when position > size\n");
159 x=uprv_mstrm_write(memStream, buffer, sizeof(buffer)/sizeof(buffer[0]) );
160 if(x == -1){
161 log_err("uprv_mstrm_write() failed\n");
162 }
163 if(x != sizeof(buffer)/sizeof(buffer[0])){
164 log_err("uprv_mstrm_write() wrote %d characters instead of %d\n", x, sizeof(buffer)/sizeof(buffer[0]));
165 }
166
167 log_verbose("Testing how different functions behave when error is set to true using setError\n");
168 uprv_mstrm_setError(memStream);
169 gotBuffer=uprv_mstrm_getBuffer(memStream, &x);
170 if(gotBuffer != NULL || x !=0 ){
171 log_err("uprv_mstrm_getBuffer() should fail when the error is set to true using uprv_mstrm_setError()");
172 }
173 uprv_mstrm_close(memStream);
174
175 /*
176 Test the following APIs:
177 uprv_mstrm_write8
178 uprv_mstrm_write16
179 uprv_mstrm_write32
180 uprv_mstrm_writeString
181 uprv_mstrm_writeUString
182 uprv_mstrm_writePadding
183 uprv_mstrm_writeBlock
184 uprv_mstrm_getCurrentBuffer
185 uprv_mstrm_jump
186 uprv_mstrm_skip
187 */
188 {
189 uint8_t byteValue = 0x12;
190 uint16_t wordValue = 0x2112;
191 uint32_t wydeValue = 0x12211221;
192 uint32_t wydeRead = 0;
193 const char* stringVal = "This is a string";
194 UChar UCharBuff[256];
195 const UChar* ucharVal = UCharBuff;
196 const uint8_t *data = NULL;
197 int32_t bufLen = 0;
198
199 u_unescape("This is an Unicode String", UCharBuff, 256);
200
201 memStream=uprv_mstrm_openNew(size);
202 if(memStream == NULL){
203 log_err("uprv_mstrm_openNew() failed\n");
204 }
205 uprv_mstrm_write8(memStream, byteValue);
206 uprv_mstrm_writePadding(memStream, 3);
207 uprv_mstrm_write16(memStream, wordValue);
208 uprv_mstrm_writePadding(memStream, 2);
209 uprv_mstrm_write32(memStream, wydeValue);
210 uprv_mstrm_writeBlock(memStream, &wydeValue, 4);
211
212 uprv_mstrm_writeString(memStream, stringVal, -1);
213 uprv_mstrm_writeString(memStream, stringVal, strlen(stringVal));
214 uprv_mstrm_writeUString(memStream, ucharVal, -1);
215 uprv_mstrm_writeUString(memStream, ucharVal, u_strlen(ucharVal));
216
217 /* Now, lets get the values back */
218 data = uprv_mstrm_getBuffer(memStream, &bufLen);
219
220 if(data == NULL || bufLen == 0) {
221 log_err("get Buffer failed!\n");
222 } else {
223 if(byteValue != *(uint8_t *)data) {
224 log_err("Failed getting byte value\n");
225 }
226 data += 4; /* skip byte and 3 padding */
227 if(wordValue != *(uint16_t *)data) {
228 log_err("Failed getting word value\n");
229 }
230 data += 4; /* skip word and 2 padding */
231
232 if(wydeValue != *(uint32_t *)data) {
233 log_err("Failed getting word value\n");
234 }
235 data += 4; /* skip wyde */
236
237 if(wydeValue != *(uint32_t *)data) {
238 log_err("Failed getting word value\n");
239 }
240 data += 4; /* skip wyde */
241
242 if(strncmp(stringVal, (char *)data, strlen(stringVal)) != 0) {
243 log_err("String was not written correctly\n");
244 }
245 data += strlen(stringVal);
246
247 if(strncmp(stringVal, (char *)data, strlen(stringVal)) != 0) {
248 log_err("String was not written correctly\n");
249 }
250 data += strlen(stringVal);
251
252 if(u_strncmp(ucharVal, (UChar *)data, u_strlen(ucharVal)) != 0) {
253 log_err("UString was not written correctly\n");
254 }
255 data += u_strlen(ucharVal)*2;
256
257 if(u_strncmp(ucharVal, (UChar *)data, u_strlen(ucharVal)) != 0) {
258 log_err("UString was not written correctly\n");
259 }
260 data += u_strlen(ucharVal)*2;
261
262 uprv_mstrm_skip(memStream, 8); /* skip to first wyde */
263 bufLen = uprv_mstrm_read(memStream, &wydeRead, 4);
264 if(bufLen != 4 || wydeValue != wydeRead) {
265 log_err("Reading after skip failed\n");
266 }
267
268 /* this should get us to the second wyde */
269 data = uprv_mstrm_getCurrentBuffer(memStream, &bufLen);
270 if(wydeValue != *(uint32_t *)data) {
271 log_err("Failed getting wyde value after getCurrentBuffer\n");
272 }
273
274 uprv_mstrm_skip(memStream, -8);
275 data = uprv_mstrm_getCurrentBuffer(memStream, &bufLen);
276 uprv_mstrm_jump(memStream, data+4);
277
278 data = uprv_mstrm_getCurrentBuffer(memStream, &bufLen);
279 if(wydeValue != *(uint32_t *)data) {
280 log_err("Failed getting wyde value after getCurrentBuffer\n");
281 }
282
283
284
285
286
287
288 }
289
290
291 uprv_mstrm_close(memStream);
292
293
294 }
295
296 }