]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/mstrmtst.c
1 /********************************************************************
3 * Copyright (c) 1998-2001, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 * File memstrts.c (Tests the API in umemstrm)
9 * Modification History:
11 * Date Name Description
12 * 07/19/2000 Madhu Creation
13 *******************************************************************************
17 #include "unicode/utypes.h"
18 #include "unicode/ustring.h"
23 static void TestMemoryStreamAPI(void);
24 static void printUSeqErr(const uint8_t *a
, int len
);
26 void addMemoryStreamTest(TestNode
** root
);
28 static void printUSeqErr(const uint8_t *a
, int len
)
31 fprintf(stderr
, "{U+ ");
33 fprintf(stderr
, "0x%02x ", a
[i
++]);
34 fprintf(stderr
,"}\n");
38 addMemoryStreamTest(TestNode
** root
)
40 addTest(root
, &TestMemoryStreamAPI
, "/tsutil/mstrmtst/TestMemoryStreamAPI");
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,
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");
57 uprv_mstrm_close(memStream
);
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");
65 log_verbose("Testing the function uprv_mstrm_write()\n");
66 x
=uprv_mstrm_write(memStream
, buffer
, sizeof(buffer
)/sizeof(buffer
[0]) );
68 log_err("uprv_mstrm_write() failed\n");
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]));
74 log_verbose("Testing the function uprv_mstrm_getBuffer())\n");
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");
80 printUSeqErr(gotBuffer
, sizeof(buffer
)/sizeof(buffer
[0]));
81 printf("\nExpected:");
82 printUSeqErr(buffer
, sizeof(buffer
)/sizeof(buffer
[0]));
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");
90 printUSeqErr(gotBuffer
, 1);
91 printf("\nExpected:");
92 printUSeqErr(buffer
, 1);
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");
99 printUSeqErr(gotBuffer
, 5);
100 printf("\nExpected:");
101 printUSeqErr(buffer
+1, 5);
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");
108 printUSeqErr(gotBuffer
, 8);
109 printf("\nExpected:");
110 printUSeqErr(buffer
+6, 8);
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");
120 printUSeqErr(gotBuffer
, 2);
121 printf("\nExpected:");
122 printUSeqErr(buffer
+14, 2);
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");
128 uprv_mstrm_close(memStream
);
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");
135 log_verbose("Testing the function uprv_mstrm_getBuffer())\n");
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");
141 printUSeqErr(gotBuffer
, sizeof(buffer
)/sizeof(buffer
[0]));
142 printf("\nExpected:");
143 printUSeqErr(buffer
, sizeof(buffer
)/sizeof(buffer
[0]));
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");
151 uprv_mstrm_close(memStream
);
154 memStream
=uprv_mstrm_openNew(1);
155 if(memStream
== NULL
){
156 log_err("uprv_mstrm_openNew() failed\n");
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]) );
161 log_err("uprv_mstrm_write() failed\n");
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]));
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()");
173 uprv_mstrm_close(memStream
);
176 Test the following APIs:
180 uprv_mstrm_writeString
181 uprv_mstrm_writeUString
182 uprv_mstrm_writePadding
183 uprv_mstrm_writeBlock
184 uprv_mstrm_getCurrentBuffer
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
;
199 u_unescape("This is an Unicode String", UCharBuff
, 256);
201 memStream
=uprv_mstrm_openNew(size
);
202 if(memStream
== NULL
){
203 log_err("uprv_mstrm_openNew() failed\n");
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);
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
));
217 /* Now, lets get the values back */
218 data
= uprv_mstrm_getBuffer(memStream
, &bufLen
);
220 if(data
== NULL
|| bufLen
== 0) {
221 log_err("get Buffer failed!\n");
223 if(byteValue
!= *(uint8_t *)data
) {
224 log_err("Failed getting byte value\n");
226 data
+= 4; /* skip byte and 3 padding */
227 if(wordValue
!= *(uint16_t *)data
) {
228 log_err("Failed getting word value\n");
230 data
+= 4; /* skip word and 2 padding */
232 if(wydeValue
!= *(uint32_t *)data
) {
233 log_err("Failed getting word value\n");
235 data
+= 4; /* skip wyde */
237 if(wydeValue
!= *(uint32_t *)data
) {
238 log_err("Failed getting word value\n");
240 data
+= 4; /* skip wyde */
242 if(strncmp(stringVal
, (char *)data
, strlen(stringVal
)) != 0) {
243 log_err("String was not written correctly\n");
245 data
+= strlen(stringVal
);
247 if(strncmp(stringVal
, (char *)data
, strlen(stringVal
)) != 0) {
248 log_err("String was not written correctly\n");
250 data
+= strlen(stringVal
);
252 if(u_strncmp(ucharVal
, (UChar
*)data
, u_strlen(ucharVal
)) != 0) {
253 log_err("UString was not written correctly\n");
255 data
+= u_strlen(ucharVal
)*2;
257 if(u_strncmp(ucharVal
, (UChar
*)data
, u_strlen(ucharVal
)) != 0) {
258 log_err("UString was not written correctly\n");
260 data
+= u_strlen(ucharVal
)*2;
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");
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");
274 uprv_mstrm_skip(memStream
, -8);
275 data
= uprv_mstrm_getCurrentBuffer(memStream
, &bufLen
);
276 uprv_mstrm_jump(memStream
, data
+4);
278 data
= uprv_mstrm_getCurrentBuffer(memStream
, &bufLen
);
279 if(wydeValue
!= *(uint32_t *)data
) {
280 log_err("Failed getting wyde value after getCurrentBuffer\n");
291 uprv_mstrm_close(memStream
);