1 | #include <iostream> |
---|
2 | #include <fstream> |
---|
3 | #include "RpBuffer.h" |
---|
4 | |
---|
5 | #define BUFF 30 |
---|
6 | |
---|
7 | int |
---|
8 | testInit() |
---|
9 | { |
---|
10 | int passed = 0; |
---|
11 | int tests = 0; |
---|
12 | |
---|
13 | /* =========================================================== */ |
---|
14 | tests++; |
---|
15 | Rappture::Buffer *buffer1 = NULL; |
---|
16 | buffer1 = new Rappture::Buffer(); |
---|
17 | |
---|
18 | if ( (buffer1 != NULL) && |
---|
19 | (buffer1->size() == 0) ) { |
---|
20 | passed++; |
---|
21 | delete buffer1; |
---|
22 | } |
---|
23 | else { |
---|
24 | delete buffer1; |
---|
25 | printf("Error testInit: test1"); |
---|
26 | return (tests == passed); |
---|
27 | } |
---|
28 | /* =========================================================== */ |
---|
29 | |
---|
30 | /* =========================================================== */ |
---|
31 | tests++; |
---|
32 | Rappture::Buffer buffer2; |
---|
33 | if (buffer2.size() == 0) { |
---|
34 | passed++; |
---|
35 | } |
---|
36 | else { |
---|
37 | printf("Error testInit: test2"); |
---|
38 | return (tests == passed); |
---|
39 | } |
---|
40 | /* =========================================================== */ |
---|
41 | |
---|
42 | /* =========================================================== */ |
---|
43 | tests++; |
---|
44 | Rappture::Buffer buffer3 = Rappture::Buffer("hi my name is derrick",22); |
---|
45 | if (buffer3.size() == 22) { |
---|
46 | passed++; |
---|
47 | } |
---|
48 | else { |
---|
49 | printf("Error testInit: test3"); |
---|
50 | return (tests == passed); |
---|
51 | } |
---|
52 | /* =========================================================== */ |
---|
53 | |
---|
54 | /* =========================================================== */ |
---|
55 | tests++; |
---|
56 | Rappture::Buffer buffer4 = Rappture::Buffer("hi my name is derrick",-1); |
---|
57 | if (buffer4.size() == 21) { |
---|
58 | passed++; |
---|
59 | } |
---|
60 | else { |
---|
61 | printf("Error testInit: test4"); |
---|
62 | return (tests == passed); |
---|
63 | } |
---|
64 | /* =========================================================== */ |
---|
65 | |
---|
66 | /* =========================================================== */ |
---|
67 | tests++; |
---|
68 | Rappture::Buffer buffer5 = Rappture::Buffer("hi my name is derrick"); |
---|
69 | if (buffer5.size() == 21) { |
---|
70 | passed++; |
---|
71 | } |
---|
72 | else { |
---|
73 | printf("Error testInit: test5"); |
---|
74 | return (tests == passed); |
---|
75 | } |
---|
76 | /* =========================================================== */ |
---|
77 | |
---|
78 | /* =========================================================== */ |
---|
79 | tests++; |
---|
80 | Rappture::Buffer buffer6 = Rappture::Buffer("hi my name is derrick"); |
---|
81 | Rappture::Buffer buffer7 = buffer6; |
---|
82 | if ( (buffer6.size() == 21) && (buffer7.size() == buffer6.size()) ) { |
---|
83 | passed++; |
---|
84 | } |
---|
85 | else { |
---|
86 | printf("Error testInit: test6"); |
---|
87 | return (tests == passed); |
---|
88 | } |
---|
89 | /* =========================================================== */ |
---|
90 | |
---|
91 | /* =========================================================== */ |
---|
92 | tests++; |
---|
93 | Rappture::Buffer buffer8 = Rappture::Buffer("hi my name is derrick"); |
---|
94 | Rappture::Buffer buffer9 = Rappture::Buffer(buffer8); |
---|
95 | if ( (buffer8.size() == 21) && |
---|
96 | (buffer9.size() == buffer8.size()) && |
---|
97 | (buffer8.bytes() != buffer9.bytes()) ) { |
---|
98 | passed++; |
---|
99 | } |
---|
100 | else { |
---|
101 | printf("Error testInit: test7"); |
---|
102 | return (tests == passed); |
---|
103 | } |
---|
104 | /* =========================================================== */ |
---|
105 | |
---|
106 | /* =========================================================== */ |
---|
107 | tests++; |
---|
108 | Rappture::Buffer buffer10 = Rappture::Buffer("hi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrickhi my name is derrick"); |
---|
109 | Rappture::Buffer buffer11 = Rappture::Buffer(buffer10); |
---|
110 | if ( (buffer10.size() == 210) && |
---|
111 | (buffer10.size() == buffer11.size()) && |
---|
112 | (buffer10.bytes() != buffer11.bytes()) ) { |
---|
113 | passed++; |
---|
114 | } |
---|
115 | else { |
---|
116 | printf("Error testInit: test8"); |
---|
117 | return (tests == passed); |
---|
118 | } |
---|
119 | /* =========================================================== */ |
---|
120 | |
---|
121 | return (tests == passed); |
---|
122 | } |
---|
123 | |
---|
124 | int testAppend() |
---|
125 | { |
---|
126 | int passed = 0; |
---|
127 | int tests = 0; |
---|
128 | |
---|
129 | /* =========================================================== */ |
---|
130 | tests++; |
---|
131 | Rappture::Buffer buffer1; |
---|
132 | buffer1.append("hi my name is derrick",21); |
---|
133 | if (buffer1.size() == 21) { |
---|
134 | passed++; |
---|
135 | } |
---|
136 | else { |
---|
137 | printf("Error testAppend: length1 = %d\n",buffer1.size()); |
---|
138 | return (tests == passed); |
---|
139 | } |
---|
140 | /* =========================================================== */ |
---|
141 | |
---|
142 | /* =========================================================== */ |
---|
143 | tests++; |
---|
144 | Rappture::Buffer buffer2 = Rappture::Buffer("hi my name is derrick",190); |
---|
145 | buffer2.append("adding more text to the buffer",30); |
---|
146 | if (buffer2.size() == 220) { |
---|
147 | passed++; |
---|
148 | // printDBufferDetails(dbPtr); |
---|
149 | } |
---|
150 | else { |
---|
151 | printf("Error testAppend: length2 = %d\n",buffer2.size()); |
---|
152 | return (tests == passed); |
---|
153 | } |
---|
154 | /* =========================================================== */ |
---|
155 | |
---|
156 | /* =========================================================== */ |
---|
157 | |
---|
158 | return (tests == passed); |
---|
159 | } |
---|
160 | |
---|
161 | int testRead() |
---|
162 | { |
---|
163 | int passed = 0; |
---|
164 | int tests = 0; |
---|
165 | char b[BUFF]; |
---|
166 | int bytesRead = 0; |
---|
167 | |
---|
168 | |
---|
169 | /* =========================================================== */ |
---|
170 | tests++; |
---|
171 | Rappture::Buffer buffer1; |
---|
172 | buffer1.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
173 | bytesRead = buffer1.read(b, BUFF); |
---|
174 | |
---|
175 | /* |
---|
176 | std::cout << "num bytes read = :" << bytesRead << ":" << std::endl; |
---|
177 | std::cout << "value read in = :"; |
---|
178 | for (int i = 0; i < bytesRead; i++) { |
---|
179 | std::cout << b[i]; |
---|
180 | } |
---|
181 | std::cout << ":" << std::endl; |
---|
182 | */ |
---|
183 | |
---|
184 | if (bytesRead == 26) { |
---|
185 | passed++; |
---|
186 | } |
---|
187 | else { |
---|
188 | printf("Error testRead 1\n"); |
---|
189 | // printDBufferDetails(dbPtr); |
---|
190 | return (tests == passed); |
---|
191 | } |
---|
192 | |
---|
193 | /* =========================================================== */ |
---|
194 | |
---|
195 | return (tests == passed); |
---|
196 | } |
---|
197 | |
---|
198 | int testCompress() |
---|
199 | { |
---|
200 | int passed = 0; |
---|
201 | int tests = 0; |
---|
202 | int bytesRead = 0; |
---|
203 | |
---|
204 | /* =========================================================== */ |
---|
205 | tests++; |
---|
206 | Rappture::Buffer buffer1; |
---|
207 | Rappture::Outcome status1; |
---|
208 | buffer1.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
209 | buffer1.encode(status1, true, false); |
---|
210 | bytesRead = buffer1.size(); |
---|
211 | |
---|
212 | // std::cout << status1.remark() << std::endl << status1.context() << std::endl; |
---|
213 | |
---|
214 | if (bytesRead == 46) { |
---|
215 | passed++; |
---|
216 | } |
---|
217 | else { |
---|
218 | printf("Error testCompress 1\n"); |
---|
219 | // printDBufferDetails(dbPtr); |
---|
220 | return (tests == passed); |
---|
221 | } |
---|
222 | /* =========================================================== */ |
---|
223 | /* =========================================================== */ |
---|
224 | tests++; |
---|
225 | Rappture::Buffer buffer2; |
---|
226 | Rappture::Outcome status2; |
---|
227 | buffer2.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
228 | buffer2.encode(status2, false, true); |
---|
229 | bytesRead = buffer2.size(); |
---|
230 | |
---|
231 | // std::cout << status2.remark() << std::endl << status2.context() << std::endl; |
---|
232 | |
---|
233 | if (bytesRead == 37) { |
---|
234 | passed++; |
---|
235 | } |
---|
236 | else { |
---|
237 | printf("Error testCompress 2\n"); |
---|
238 | // printDBufferDetails(dbPtr); |
---|
239 | return (tests == passed); |
---|
240 | } |
---|
241 | /* =========================================================== */ |
---|
242 | /* =========================================================== */ |
---|
243 | tests++; |
---|
244 | Rappture::Buffer buffer3; |
---|
245 | Rappture::Outcome status3; |
---|
246 | buffer3.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
247 | buffer3.encode(status3, true, true); |
---|
248 | bytesRead = buffer3.size(); |
---|
249 | |
---|
250 | // std::cout << status3.remark() << std::endl << status3.context() << std::endl; |
---|
251 | |
---|
252 | if (bytesRead == 65) { |
---|
253 | passed++; |
---|
254 | } |
---|
255 | else { |
---|
256 | printf("Error testCompress 3\n"); |
---|
257 | // printDBufferDetails(dbPtr); |
---|
258 | return (tests == passed); |
---|
259 | } |
---|
260 | /* =========================================================== */ |
---|
261 | |
---|
262 | return (tests == passed); |
---|
263 | } |
---|
264 | |
---|
265 | int testDeCompress() |
---|
266 | { |
---|
267 | int passed = 0; |
---|
268 | int tests = 0; |
---|
269 | int bytesRead = 0; |
---|
270 | |
---|
271 | /* =========================================================== */ |
---|
272 | tests++; |
---|
273 | Rappture::Buffer buffer1; |
---|
274 | Rappture::Outcome status1; |
---|
275 | buffer1.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
276 | buffer1.encode(status1, RPENC_Z); |
---|
277 | buffer1.decode(status1, RPENC_Z); |
---|
278 | bytesRead = buffer1.size(); |
---|
279 | |
---|
280 | // std::cout << status1.remark() << std::endl << status1.context() << std::endl; |
---|
281 | |
---|
282 | if (bytesRead == 26) { |
---|
283 | passed++; |
---|
284 | } |
---|
285 | else { |
---|
286 | printf("Error testDeCompress 1\n"); |
---|
287 | // printDBufferDetails(dbPtr); |
---|
288 | return (tests == passed); |
---|
289 | } |
---|
290 | /* =========================================================== */ |
---|
291 | /* =========================================================== */ |
---|
292 | tests++; |
---|
293 | Rappture::Buffer buffer2; |
---|
294 | Rappture::Outcome status2; |
---|
295 | buffer2.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
296 | buffer2.encode(status2, RPENC_B64); |
---|
297 | buffer2.decode(status2, RPENC_B64); |
---|
298 | bytesRead = buffer2.size(); |
---|
299 | |
---|
300 | // std::cout << status2.remark() << std::endl << status2.context() << std::endl; |
---|
301 | |
---|
302 | if (bytesRead == 26) { |
---|
303 | passed++; |
---|
304 | } |
---|
305 | else { |
---|
306 | printf("Error testDeCompress 2\n"); |
---|
307 | // printDBufferDetails(dbPtr); |
---|
308 | return (tests == passed); |
---|
309 | } |
---|
310 | /* =========================================================== */ |
---|
311 | /* =========================================================== */ |
---|
312 | tests++; |
---|
313 | Rappture::Buffer buffer3; |
---|
314 | Rappture::Outcome status3; |
---|
315 | buffer3.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
316 | buffer3.encode(status3, RPENC_B64 | RPENC_Z); |
---|
317 | buffer3.decode(status3, RPENC_B64 | RPENC_Z); |
---|
318 | bytesRead = buffer3.size(); |
---|
319 | |
---|
320 | // std::cout << status3.remark() << std::endl << status3.context() << std::endl; |
---|
321 | |
---|
322 | if (bytesRead == 26) { |
---|
323 | passed++; |
---|
324 | } |
---|
325 | else { |
---|
326 | printf("Error testDeCompress 3\n"); |
---|
327 | // printDBufferDetails(dbPtr); |
---|
328 | return (tests == passed); |
---|
329 | } |
---|
330 | /* =========================================================== */ |
---|
331 | |
---|
332 | return (tests == passed); |
---|
333 | } |
---|
334 | |
---|
335 | int testDump() |
---|
336 | { |
---|
337 | int passed = 0; |
---|
338 | int tests = 0; |
---|
339 | |
---|
340 | /* =========================================================== */ |
---|
341 | tests++; |
---|
342 | char* filePath = "buffer1.txt"; |
---|
343 | Rappture::Buffer buffer1; |
---|
344 | buffer1.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
345 | Rappture::Outcome status1; |
---|
346 | buffer1.dump(status1, filePath); |
---|
347 | |
---|
348 | std::ifstream inFile; |
---|
349 | std::ifstream::pos_type size = 0; |
---|
350 | char* memblock = NULL; |
---|
351 | |
---|
352 | inFile.open(filePath, std::ios::in | std::ios::ate | std::ios::binary); |
---|
353 | if (!inFile.is_open()) { |
---|
354 | std::cout << "Error testDump1 1" << std::endl; |
---|
355 | std::cout << "error while opening file" << std::endl; |
---|
356 | return (tests == passed); |
---|
357 | } |
---|
358 | |
---|
359 | size = inFile.tellg(); |
---|
360 | memblock = new char [size]; |
---|
361 | if (memblock == NULL) { |
---|
362 | std::cout << "Error testDump1 1" << std::endl; |
---|
363 | std::cout << "error allocating memory" << std::endl; |
---|
364 | inFile.close(); |
---|
365 | return (tests == passed); |
---|
366 | } |
---|
367 | |
---|
368 | inFile.seekg(0,std::ios::beg); |
---|
369 | inFile.read(memblock,size); |
---|
370 | inFile.close(); |
---|
371 | |
---|
372 | const char* buffer1bytes = buffer1.bytes(); |
---|
373 | for (int i = 0; i < size; i++) { |
---|
374 | if (buffer1bytes[i] != memblock[i]) { |
---|
375 | std::cout << "Error testDump1 1" << std::endl; |
---|
376 | std::cout << "buffers not equal" << std::endl; |
---|
377 | return (tests == passed); |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | delete [] memblock; |
---|
382 | passed++; |
---|
383 | /* =========================================================== */ |
---|
384 | |
---|
385 | return (tests == passed); |
---|
386 | } |
---|
387 | |
---|
388 | int testLoad() |
---|
389 | { |
---|
390 | int passed = 0; |
---|
391 | int tests = 0; |
---|
392 | |
---|
393 | /* =========================================================== */ |
---|
394 | tests++; |
---|
395 | char* filePath = "buffer1.txt"; |
---|
396 | Rappture::Buffer buffer1; |
---|
397 | Rappture::Buffer buffer1out; |
---|
398 | Rappture::Outcome status1; |
---|
399 | buffer1.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
400 | buffer1.dump(status1, filePath); |
---|
401 | buffer1out.load(status1, filePath); |
---|
402 | int size = buffer1out.size(); |
---|
403 | const char* b1bytes = buffer1.bytes(); |
---|
404 | const char* b1obytes = buffer1out.bytes(); |
---|
405 | |
---|
406 | if (size == 0) { |
---|
407 | std::cout << "Error testLoad1" << std::endl; |
---|
408 | std::cout << "zero bytes read from file" << std::endl; |
---|
409 | return (tests == passed); |
---|
410 | } |
---|
411 | |
---|
412 | |
---|
413 | for (int i = 0; i < size; i++) { |
---|
414 | if (b1bytes[i] != b1obytes[i]) { |
---|
415 | printf("Error testLoad1\n"); |
---|
416 | std::cout << "buffers not equal" << std::endl; |
---|
417 | return (tests == passed); |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | passed++; |
---|
422 | /* =========================================================== */ |
---|
423 | |
---|
424 | tests++; |
---|
425 | char* filePath1 = "output.dx"; |
---|
426 | char* filePath2 = "output.dx.mime"; |
---|
427 | char* filePath3 = "output.dx.again"; |
---|
428 | |
---|
429 | Rappture::Buffer buffer2; |
---|
430 | Rappture::Buffer buffer2out; |
---|
431 | Rappture::Outcome status2; |
---|
432 | buffer2.load(status2, filePath1); |
---|
433 | buffer2.encode(status2); |
---|
434 | buffer2.dump(status2, filePath2); |
---|
435 | |
---|
436 | buffer2out.load(status2, filePath2); |
---|
437 | buffer2out.decode(status2); |
---|
438 | buffer2out.dump(status2, filePath3); |
---|
439 | |
---|
440 | buffer2.clear(); |
---|
441 | buffer2out.clear(); |
---|
442 | |
---|
443 | passed++; |
---|
444 | /* =========================================================== */ |
---|
445 | |
---|
446 | return (tests == passed); |
---|
447 | } |
---|
448 | |
---|
449 | int testClear() |
---|
450 | { |
---|
451 | int passed = 0; |
---|
452 | int tests = 0; |
---|
453 | |
---|
454 | |
---|
455 | /* =========================================================== */ |
---|
456 | tests++; |
---|
457 | Rappture::Buffer buffer1; |
---|
458 | buffer1.append("abcdefghijklmnopqrstuvwxyz",26); |
---|
459 | int size1before = buffer1.size(); |
---|
460 | buffer1.clear(); |
---|
461 | int size1after = buffer1.size(); |
---|
462 | |
---|
463 | if (size1before != 26) { |
---|
464 | std::cout << "Error testClear1" << std::endl; |
---|
465 | std::cout << "incorrect buffer size" << std::endl; |
---|
466 | return (tests == passed); |
---|
467 | } |
---|
468 | if (size1after != 0) { |
---|
469 | std::cout << "Error testClear1" << std::endl; |
---|
470 | std::cout << "clear failed buffer size" << std::endl; |
---|
471 | return (tests == passed); |
---|
472 | } |
---|
473 | |
---|
474 | passed++; |
---|
475 | /* =========================================================== */ |
---|
476 | |
---|
477 | return (tests == passed); |
---|
478 | } |
---|
479 | |
---|
480 | int testFile() |
---|
481 | { |
---|
482 | int passed = 0; |
---|
483 | int tests = 0; |
---|
484 | |
---|
485 | /* =========================================================== */ |
---|
486 | tests++; |
---|
487 | const char* inFile = "out.dx"; |
---|
488 | const char* outFile = "out.dx.gz"; |
---|
489 | Rappture::Buffer buffer1; |
---|
490 | Rappture::Outcome status1; |
---|
491 | buffer1.load(status1, inFile); |
---|
492 | |
---|
493 | // compress, dont encode |
---|
494 | buffer1.encode(status1, RPENC_Z); |
---|
495 | std::remove(outFile); |
---|
496 | buffer1.dump(status1, outFile); |
---|
497 | buffer1.decode(status1, RPENC_Z); |
---|
498 | |
---|
499 | /* |
---|
500 | if (size1before != 26) { |
---|
501 | std::cout << "Error testClear1" << std::endl; |
---|
502 | std::cout << "incorrect buffer size" << std::endl; |
---|
503 | return (tests == passed); |
---|
504 | } |
---|
505 | if (size1after != 0) { |
---|
506 | std::cout << "Error testClear1" << std::endl; |
---|
507 | std::cout << "clear failed buffer size" << std::endl; |
---|
508 | return (tests == passed); |
---|
509 | } |
---|
510 | */ |
---|
511 | |
---|
512 | passed++; |
---|
513 | /* =========================================================== */ |
---|
514 | |
---|
515 | return (tests == passed); |
---|
516 | } |
---|
517 | |
---|
518 | int main() |
---|
519 | { |
---|
520 | int passed = 0; |
---|
521 | |
---|
522 | passed += testInit(); |
---|
523 | passed += testAppend(); |
---|
524 | passed += testRead(); |
---|
525 | passed += testCompress(); |
---|
526 | passed += testDeCompress(); |
---|
527 | passed += testDump(); |
---|
528 | passed += testLoad(); |
---|
529 | passed += testClear(); |
---|
530 | passed += testFile(); |
---|
531 | |
---|
532 | if (passed != 9) { |
---|
533 | printf("failed: %d\n", passed); |
---|
534 | } |
---|
535 | else { |
---|
536 | printf("sucessful\n"); |
---|
537 | } |
---|
538 | |
---|
539 | return 0; |
---|
540 | } |
---|