exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
check_streamIO.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
19 #include <stdlib.h>
20 #include <check.h>
21 #include "streamRead.h"
22 #include "streamWrite.h"
23 #include "streamDecode.h"
24 #include "streamEncode.h"
25 #include "procTypes.h"
26 #include "errorHandle.h"
27 #include "stringManipulate.h"
28 #include "memManagement.h"
29 #include "ioUtil.h"
30 
31 /* BEGIN: streamRead tests */
32 
33 START_TEST (test_readNextBit)
34 {
35  EXIStream testStream;
36 
37  char buf[2];
38  boolean bit_val = 0;
40 
41  testStream.context.bitPointer = 0;
42  buf[0] = (char) 0xD4; /* 0b11010100 */
43  buf[1] = (char) 0x60; /* 0b01100000 */
44  testStream.buffer.buf = buf;
45  testStream.buffer.bufLen = 2;
46  testStream.buffer.bufContent = 2;
47  testStream.buffer.ioStrm.readWriteToStream = NULL;
48  testStream.buffer.ioStrm.stream = NULL;
49  testStream.context.bufferIndx = 0;
50  initAllocList(&testStream.memList);
51 
52  err = readNextBit(&testStream, &bit_val);
53 
54  fail_unless (bit_val == 1,
55  "The bit 1 from the stream is read as 0");
56  fail_unless (err == EXIP_OK,
57  "readNextBit returns error code %d", err);
58  fail_unless (testStream.context.bitPointer == 1,
59  "The readNextBit function did not move the bit Pointer of the stream correctly");
60 
61  // Set the bit pointer to the first byte boundary
62  testStream.context.bitPointer = 7;
63 
64  err = readNextBit(&testStream, &bit_val);
65 
66  fail_unless (bit_val == 0,
67  "The bit 0 from the stream is read as 1");
68  fail_unless (err == EXIP_OK,
69  "readNextBit returns error code %d", err);
70  fail_unless (testStream.context.bitPointer == 0 && testStream.context.bufferIndx == 1,
71  "The readNextBit function did not move the bit Pointer of the stream correctly");
72 
73  // Set the bit pointer to the second byte boundary
74  testStream.context.bitPointer = 7;
75 
76  err = readNextBit(&testStream, &bit_val);
77 
78  fail_unless (err == EXIP_OK,
79  "readNextBit returns error code %d", err);
80  fail_unless (testStream.context.bitPointer == 0 && testStream.context.bufferIndx == 2,
81  "The readNextBit function did not move the bit Pointer of the stream correctly");
82 
83  err = readNextBit(&testStream, &bit_val);
84  fail_unless (err == EXIP_BUFFER_END_REACHED, "Incorrect error code");
85 
86 }
88 
89 START_TEST (test_readBits)
90 {
91  EXIStream testStream;
92  char buf[2];
93  unsigned int bits_val = 0;
95 
96  testStream.context.bitPointer = 0;
97  buf[0] = (char) 0xD4; /* 0b11010100 */
98  buf[1] = (char) 0x60; /* 0b01100000 */
99  testStream.buffer.buf = buf;
100  testStream.buffer.bufLen = 2;
101  testStream.buffer.bufContent = 2;
102  testStream.buffer.ioStrm.readWriteToStream = NULL;
103  testStream.buffer.ioStrm.stream = NULL;
104  testStream.context.bufferIndx = 0;
105  initAllocList(&testStream.memList);
106 
107  err = readBits(&testStream, 4, &bits_val);
108 
109  fail_unless (bits_val == 13,
110  "The bits 1101 from the stream are read as %d", bits_val);
111  fail_unless (err == EXIP_OK,
112  "readBits returns error code %d", err);
113  fail_unless (testStream.context.bitPointer == 4,
114  "The readBits function did not move the bit Pointer of the stream correctly");
115 
116  // Set the bit pointer to the first byte boundary
117  testStream.context.bitPointer = 7;
118 
119  err = readBits(&testStream, 5, &bits_val);
120 
121  fail_unless (bits_val == 6,
122  "The bits 00110 from the stream are read as %d", bits_val);
123  fail_unless (err == EXIP_OK,
124  "readNextBit returns error code %d", err);
125  fail_unless (testStream.context.bitPointer == 4 && testStream.context.bufferIndx == 1,
126  "The readBits function did not move the bit Pointer of the stream correctly");
127 
128  err = readBits(&testStream, 5, &bits_val);
129  fail_unless (err == EXIP_BUFFER_END_REACHED, "Incorrect error code");
130 }
131 END_TEST
132 
133 /* END: streamRead tests */
134 
135 /* BEGIN: streamWrite tests */
136 
137 START_TEST (test_writeNextBit)
138 {
139  EXIStream testStream;
140  char buf[2];
142  int test;
143 
144  testStream.context.bitPointer = 0;
145  buf[0] = (char) 0x01; /* 0b00000001 */
146  buf[1] = (char) 0x00; /* 0b00000000 */
147  testStream.buffer.buf = buf;
148  testStream.buffer.bufLen = 2;
149  testStream.buffer.bufContent = 2;
150  testStream.buffer.ioStrm.readWriteToStream = NULL;
151  testStream.buffer.ioStrm.stream = NULL;
152  testStream.context.bufferIndx = 0;
153  initAllocList(&testStream.memList);
154 
155  err = writeNextBit(&testStream, 1);
156 
157  test = (buf[0] & 0x80 /* 0b10000000 */ ) != 0;
158 
159  fail_unless (test == 1,
160  "The bit 1 was written as 0");
161  fail_unless (err == EXIP_OK,
162  "writeNextBit returns error code %d", err);
163  fail_unless (testStream.context.bitPointer == 1,
164  "The writeNextBit function did not move the bit Pointer of the stream correctly");
165 
166  // Set the bit pointer to the first byte boundary
167  testStream.context.bitPointer = 7;
168 
169  err = writeNextBit(&testStream, 0);
170 
171  test = (buf[0] & 0x01) != 0;
172 
173  fail_unless (test == 0,
174  "The bit 0 was written as 1");
175  fail_unless (err == EXIP_OK,
176  "writeNextBit returns error code %d", err);
177  fail_unless (testStream.context.bitPointer == 0 && testStream.context.bufferIndx == 1,
178  "The writeNextBit function did not move the bit Pointer of the stream correctly");
179 
180  testStream.context.bufferIndx = 2;
181  testStream.context.bitPointer = 0;
182 
183  err = writeNextBit(&testStream, 0);
184  fail_unless (err == EXIP_BUFFER_END_REACHED, "Incorrect error code");
185 
186 }
187 END_TEST
188 
189 START_TEST (test_writeNBits)
190 {
191  EXIStream testStream;
192  char buf[2];
194  int test, test1;
195 
196  testStream.context.bitPointer = 0;
197  buf[0] = (char) 0xA0; /* 0b10100000 */
198  buf[1] = (char) 0xE0; /* 0b11100000 */
199  testStream.buffer.buf = buf;
200  testStream.buffer.bufLen = 2;
201  testStream.buffer.ioStrm.readWriteToStream = NULL;
202  testStream.buffer.ioStrm.stream = NULL;
203  testStream.buffer.bufContent = 2;
204  testStream.context.bufferIndx = 0;
205  initAllocList(&testStream.memList);
206 
207  err = writeNBits(&testStream, 7, 19);
208 
209  test = ((unsigned int) buf[0]) >> 1;
210 
211  fail_unless (test == 19,
212  "The number 19 was written as %d", test);
213  fail_unless (err == EXIP_OK,
214  "writeNBits returns error code %d", err);
215  fail_unless (testStream.context.bitPointer == 7,
216  "The writeNBits function did not move the bit Pointer of the stream correctly");
217 
218  // Set the bit pointer to the first byte boundary
219  testStream.context.bitPointer = 7;
220 
221  err = writeNBits(&testStream, 5, 9);
222 
223  test = (buf[0] & 0x01 ) != 0;
224 
225  test1 = (buf[1] & 0xF0 /* 0b11110000 */ ) >> 4;
226 
227  fail_unless (test == 0 && test1 == 9,
228  "writeNBits function doesn't write correctly");
229  fail_unless (err == EXIP_OK,
230  "writeNBits returns error code %d", err);
231  fail_unless (testStream.context.bitPointer == 4 && testStream.context.bufferIndx == 1,
232  "The writeNBits function did not move the bit Pointer of the stream correctly");
233 
234  err = writeNBits(&testStream, 5, 16);
235  fail_unless (err == EXIP_BUFFER_END_REACHED, "Incorrect error code");
236 }
237 END_TEST
238 
239 /* END: streamWrite tests */
240 
241 /* BEGIN: streamDecode tests */
242 
243 START_TEST (test_decodeNBitUnsignedInteger)
244 {
245  EXIStream testStream;
246 
247  char buf[2];
248  unsigned int bit_val = 0;
250 
251  testStream.context.bitPointer = 0;
252  makeDefaultOpts(&testStream.header.opts);
253 
254  buf[0] = (char) 0xD4; /* 0b11010100 */
255  buf[1] = (char) 0x60; /* 0b01100000 */
256  testStream.buffer.buf = buf;
257  testStream.buffer.bufLen = 2;
258  testStream.buffer.bufContent = 2;
259  testStream.buffer.ioStrm.readWriteToStream = NULL;
260  testStream.buffer.ioStrm.stream = NULL;
261  testStream.context.bufferIndx = 0;
262  initAllocList(&testStream.memList);
263 
264  err = decodeNBitUnsignedInteger(&testStream, 6, &bit_val);
265 
266  fail_unless (bit_val == 53,
267  "The 110101 from the stream is read as %d", bit_val);
268  fail_unless (err == EXIP_OK,
269  "decodeNBitUnsignedInteger returns error code %d", err);
270  fail_unless (testStream.context.bitPointer == 6,
271  "The decodeNBitUnsignedInteger function did not move the bit Pointer of the stream correctly");
272 
273 }
274 END_TEST
275 
276 START_TEST (test_decodeBoolean)
277 {
278  EXIStream testStream;
279  char buf[2];
280  boolean bit_val = 0;
282 
283  testStream.context.bitPointer = 0;
284  makeDefaultOpts(&testStream.header.opts);
285 
286  buf[0] = (char) 0xD4; /* 0b11010100 */
287  buf[1] = (char) 0x60; /* 0b01100000 */
288  testStream.buffer.buf = buf;
289  testStream.buffer.bufLen = 2;
290  testStream.buffer.bufContent = 2;
291  testStream.buffer.ioStrm.readWriteToStream = NULL;
292  testStream.buffer.ioStrm.stream = NULL;
293  testStream.context.bufferIndx = 0;
294  initAllocList(&testStream.memList);
295 
296  err = decodeBoolean(&testStream, &bit_val);
297 
298  fail_unless (bit_val == 1,
299  "The the bit 1 from the stream is read as %d", bit_val);
300  fail_unless (err == EXIP_OK,
301  "decodeBoolean returns error code %d", err);
302  fail_unless (testStream.context.bitPointer == 1,
303  "The decodeBoolean function did not move the bit Pointer of the stream correctly");
304 }
305 END_TEST
306 
307 START_TEST (test_decodeUnsignedInteger)
308 {
309  EXIStream testStream;
310  char buf[3];
311  UnsignedInteger bit_val = 0;
313 
314  testStream.context.bitPointer = 0;
315  makeDefaultOpts(&testStream.header.opts);
316 
317  buf[0] = (char) 0xD4; /* 0b11010100 */
318  buf[1] = (char) 0x60; /* 0b01100000 */
319  buf[2] = (char) 0x48; /* 0b01001000 */
320  testStream.buffer.buf = buf;
321  testStream.buffer.bufLen = 3;
322  testStream.buffer.bufContent = 3;
323  testStream.buffer.ioStrm.readWriteToStream = NULL;
324  testStream.buffer.ioStrm.stream = NULL;
325  testStream.context.bufferIndx = 0;
326  initAllocList(&testStream.memList);
327 
328  err = decodeUnsignedInteger(&testStream, &bit_val);
329 
330  fail_unless (bit_val == 12372,
331  "The UnsignedInteger 12372 from the stream is read as %d", bit_val);
332  fail_unless (err == EXIP_OK,
333  "decodeUnsignedInteger returns error code %d", err);
334  fail_unless (testStream.context.bitPointer == 0,
335  "The decodeUnsignedInteger function did not move the bit Pointer of the stream correctly");
336  fail_unless (testStream.context.bufferIndx == 2,
337  "The decodeUnsignedInteger function did not move the byte Pointer of the stream correctly");
338 
339 }
340 END_TEST
341 
342 
343 START_TEST (test_decodeString)
344 {
345  EXIStream testStream;
346  char buf[4];
347  String bit_val;
348  CharType cht[100];
350 
351  testStream.context.bitPointer = 0;
352  makeDefaultOpts(&testStream.header.opts);
353 
354  buf[0] = (char) 0x02; /* 0b00000010 */
355  buf[1] = (char) 0x65; /* 0b01100101 */ // e - ASCII
356  buf[2] = (char) 0x54; /* 0b01010100 */ // T - ASCII
357  buf[3] = (char) 0x52; /* 0b01010010 */
358  testStream.buffer.buf = buf;
359  testStream.buffer.bufLen = 4;
360  testStream.buffer.bufContent = 4;
361  testStream.buffer.ioStrm.readWriteToStream = NULL;
362  testStream.buffer.ioStrm.stream = NULL;
363  testStream.context.bufferIndx = 0;
364  initAllocList(&testStream.memList);
365  bit_val.length = 0;
366  bit_val.str = cht;
367 
368  err = decodeString(&testStream, &bit_val);
369 
370  fail_unless (bit_val.length == 2,
371  "The String length of 2 is reported as %d from decodeString", bit_val.length);
372  fail_unless (bit_val.str[0] == 'e' && bit_val.str[1] == 'T',
373  "The String \"eT\" is decoded wrong by decodeString");
374  fail_unless (err == EXIP_OK,
375  "decodeString returns error code %d", err);
376  fail_unless (testStream.context.bitPointer == 0,
377  "The decodeString function did not move the bit Pointer of the stream correctly");
378  fail_unless (testStream.context.bufferIndx == 3,
379  "The decodeString function did not move the byte Pointer of the stream correctly");
380 
381 }
382 END_TEST
383 
384 START_TEST (test_decodeBinary)
385 {
386  EXIStream testStream;
387  char buf[20];
388  char testbuf[20];
389  int i;
390  char* res;
391  Index bytes = 0;
393  int same=1;
394 
395  testStream.context.bitPointer = 0;
396  makeDefaultOpts(&testStream.header.opts);
397 
398  buf[0] = (char) 0x05; /* 0b00000101 */ //5
399  buf[1] = (char) 0xF0; /* 0b11110000 */
400  buf[2] = (char) 0xCC; /* 0b11001100 */
401  buf[3] = (char) 0xAA; /* 0b10101010 */
402  buf[4] = (char) 0x55; /* 0b01010101 */
403  buf[5] = (char) 0x33; /* 0b00110011 */
404  buf[6] = (char) 0x08; /* 0b00001000 */ //8
405  buf[7] = (char) 0x6E; /* 0b01101110 */
406  buf[8] = (char) 0xCA; /* 0b11001010 */
407  buf[9] = (char) 0x59; /* 0b01011001 */
408  buf[10] = (char) 0xD8; /* 0b11011000 */
409  buf[11] = (char) 0x59; /* 0b01011001 */
410  buf[12] = (char) 0xCA; /* 0b11001010 */
411  buf[13] = (char) 0x6C; /* 0b01101100 */
412  buf[14] = (char) 0xD8; /* 0b11011000 */
413  buf[15] = (char) 0x07; /* 0b00000111 */
414  testStream.buffer.buf = buf;
415  testStream.buffer.bufLen = 20;
416  testStream.buffer.bufContent = 20;
417  testStream.buffer.ioStrm.readWriteToStream = NULL;
418  testStream.buffer.ioStrm.stream = NULL;
419  initAllocList(&testStream.memList);
420  for(i=0;i<20;i++) testbuf[i]=buf[i];
421 
422  testStream.context.bufferIndx = 0;
423 
424  //Test1:
425  err = decodeBinary(&testStream, &res, &bytes);
426 
427  for(i=0;i<5;i++)
428  {
429  if(res[i]!=testbuf[i+1])
430  {
431  same=0;
432  break;
433  }
434  }
435 
436  fail_unless (err == EXIP_OK,
437  "decodeBinary returns error code %d", err);
438  fail_unless (bytes == 5,
439  "The length of the binary content is read as %d (actual : %d)", bytes,5);
440  fail_unless (same == 1,
441  "The binary content is read wrongly");
442  fail_unless (testStream.context.bitPointer == 0,
443  "The decodeBinary function did not move the bit Pointer of the stream correctly");
444  fail_unless (testStream.context.bufferIndx == 6,
445  "The decodeBinary function did not move the byte Pointer of the stream correctly");
446 
447 //Test2:
448  bytes=0;
449  err = EXIP_UNEXPECTED_ERROR;
450 
451  err = decodeBinary(&testStream, &res, &bytes);
452 
453  same = 1;
454  for(i=0;i<8;i++)
455  {
456  if(res[i]!=testbuf[i+7])
457  {
458  same=0;
459  break;
460  }
461  }
462 
463  fail_unless (err == EXIP_OK,
464  "decodeBinary returns error code %d", err);
465  fail_unless (bytes == 8,
466  "The length of the binary content is read as %d (actual : %d)", bytes,8);
467  fail_unless (same == 1,
468  "The binary content is read wrongly");
469  fail_unless (testStream.context.bitPointer == 0,
470  "The decodeBinary function did not move the bit Pointer of the stream correctly");
471  fail_unless (testStream.context.bufferIndx == 15,
472  "The decodeBinary function did not move the byte Pointer of the stream correctly");
473 
474 }
475 END_TEST
476 
477 START_TEST (test_decodeFloat)
478 {
479  EXIStream testStream;
480  char buf[3];
481  Float fl_val;
482  double expected_res = 500; // 5 x 10^2
484  double actual_res = 0;
485 
486  makeDefaultOpts(&testStream.header.opts);
487 
488  fl_val.exponent = 0;
489  fl_val.mantissa = 0;
490  buf[0] = (char) 0x02; /* 0b00000010 */
491  buf[1] = (char) 0x80; /* 0b10000000 */
492  buf[2] = (char) 0x92; /* 0b10010010 */
493  testStream.buffer.buf = buf;
494  testStream.buffer.bufLen = 3;
495  testStream.buffer.bufContent = 3;
496  testStream.buffer.ioStrm.readWriteToStream = NULL;
497  testStream.buffer.ioStrm.stream = NULL;
498  testStream.context.bufferIndx = 0;
499  testStream.context.bitPointer = 0;
500  initAllocList(&testStream.memList);
501 
502  err = decodeFloatValue(&testStream, &fl_val);
503 
504  actual_res = fl_val.mantissa;
505  while(fl_val.exponent)
506  {
507  if(fl_val.exponent > 0)
508  {
509  fl_val.exponent--;
510  }
511  else
512  {
513  fl_val.exponent++;
514  }
515  actual_res *= 10;
516  }
517 
518  fail_unless (err == EXIP_OK,
519  "decodeFloat returns error code %d", err);
520  fail_unless (actual_res == expected_res,
521  "The float value is read as %f (actual : %f)", actual_res, expected_res);
522  fail_unless (testStream.context.bitPointer == 2,
523  "The decodeBinary function did not move the bit Pointer of the stream correctly");
524  fail_unless (testStream.context.bufferIndx == 2,
525  "The decodeBinary function did not move the byte Pointer of the stream correctly");
526 
527 }
528 END_TEST
529 
530 START_TEST (test_decodeIntegerValue)
531 {
532  EXIStream testStream;
533  char buf[3];
534  Integer bit_val = 0;
536 
537  testStream.context.bitPointer = 0;
538  makeDefaultOpts(&testStream.header.opts);
539 
540  buf[0] = (char) 0x94; /* 0b10010100 */
541  buf[1] = (char) 0x60; /* 0b01100000 */
542  buf[2] = (char) 0x48; /* 0b01001000 */
543  testStream.buffer.buf = buf;
544  testStream.buffer.bufLen = 3;
545  testStream.buffer.bufContent = 3;
546  testStream.buffer.ioStrm.readWriteToStream = NULL;
547  testStream.buffer.ioStrm.stream = NULL;
548  testStream.context.bufferIndx = 0;
549  initAllocList(&testStream.memList);
550 
551  err = decodeIntegerValue(&testStream, &bit_val);
552 
553  fail_unless (bit_val == -41,
554  "The IntegerValue -41 from the stream is read as %d", bit_val);
555  fail_unless (err == EXIP_OK,
556  "decodeIntegerValue returns error code %d", err);
557  fail_unless (testStream.context.bitPointer == 1,
558  "The decodeIntegerValue function did not move the bit Pointer of the stream correctly");
559  fail_unless (testStream.context.bufferIndx == 1,
560  "The decodeIntegerValue function did not move the byte Pointer of the stream correctly");
561 
562  buf[0] = (char) 0x14; /* 0b00010100 */
563  testStream.context.bitPointer = 0;
564  testStream.context.bufferIndx = 0;
565 
566  err = decodeIntegerValue(&testStream, &bit_val);
567 
568  fail_unless (bit_val == 40,
569  "The IntegerValue 40 from the stream is read as %d", bit_val);
570  fail_unless (err == EXIP_OK,
571  "decodeIntegerValue returns error code %d", err);
572  fail_unless (testStream.context.bitPointer == 1,
573  "The decodeIntegerValue function did not move the bit Pointer of the stream correctly");
574  fail_unless (testStream.context.bufferIndx == 1,
575  "The decodeIntegerValue function did not move the byte Pointer of the stream correctly");
576 
577 }
578 END_TEST
579 
580 START_TEST (test_decodeDecimalValue)
581 {
582  EXIStream testStream;
583  char buf[3];
585  Decimal dec_val;
586  Decimal res;
587 
588  dec_val.mantissa = 0;
589  dec_val.exponent = 0;
590 
591  res.mantissa = 5001;
592  res.exponent = -3;
593 
594  makeDefaultOpts(&testStream.header.opts);
595 
596  buf[0] = (char) 0x02; /* 0b00000010 */
597  buf[1] = (char) 0xB2; /* 0b10110010 */
598  buf[2] = (char) 0x12; /* 0b00010010 */
599  testStream.buffer.buf = buf;
600  testStream.buffer.bufLen = 3;
601  testStream.buffer.bufContent = 3;
602  testStream.buffer.ioStrm.readWriteToStream = NULL;
603  testStream.buffer.ioStrm.stream = NULL;
604  testStream.context.bufferIndx = 0;
605  testStream.context.bitPointer = 0;
606  initAllocList(&testStream.memList);
607 
608  err = decodeDecimalValue(&testStream, &dec_val);
609 
610  fail_unless (res.mantissa == dec_val.mantissa && res.exponent == dec_val.exponent, "The value 5.001 is decoded as %d*10^%d", dec_val.mantissa, dec_val.exponent);
611  fail_unless (err == EXIP_OK,
612  "decodeDecimalValue returns error code %d", err);
613  fail_unless (testStream.context.bitPointer == 1,
614  "The decodeIntegerValue function did not move the bit Pointer of the stream correctly");
615  fail_unless (testStream.context.bufferIndx == 2,
616  "The decodeIntegerValue function did not move the byte Pointer of the stream correctly");
617 
618 }
619 END_TEST
620 
621 /* END: streamDecode tests */
622 
623 /* BEGIN: streamEncode tests */
624 
625 START_TEST (test_encodeNBitUnsignedInteger)
626 {
627  EXIStream testStream;
628  char buf[2];
630  unsigned char test, test2;
631 
632  testStream.context.bitPointer = 0;
633  makeDefaultOpts(&testStream.header.opts);
634 
635  buf[0] = (char) 0xCE; /* 0b11001110 */
636  buf[1] = (char) 0xE0; /* 0b11100000 */
637  testStream.buffer.buf = buf;
638  testStream.buffer.bufLen = 2;
639  testStream.buffer.bufContent = 2;
640  testStream.buffer.ioStrm.readWriteToStream = NULL;
641  testStream.buffer.ioStrm.stream = NULL;
642  testStream.context.bufferIndx = 0;
643  initAllocList(&testStream.memList);
644 
645  err = encodeNBitUnsignedInteger(&testStream, 9, 412);
646 
647  test = buf[0] | 0;
648  test2 = (unsigned char) buf[1] >> 7;
649 
650  fail_unless (err == EXIP_OK,
651  "encodeNBitUnsignedInteger returns error code %d", err);
652  fail_unless (test == 206 && test2 == 0,
653  "encodeNBitUnsignedInteger does not encode correctly");
654  fail_unless (testStream.context.bitPointer == 1 && testStream.context.bufferIndx == 1,
655  "The encodeNBitUnsignedInteger function did not move the bit Pointer of the stream correctly");
656 
657 }
658 END_TEST
659 
660 START_TEST (test_encodeBoolean)
661 {
662  EXIStream testStream;
663  char buf[2];
665  unsigned char bit_val = 0;
666 
667  testStream.context.bitPointer = 0;
668  makeDefaultOpts(&testStream.header.opts);
669 
670  buf[0] = (char) 0x54; /* 0b01010100 */
671  buf[1] = (char) 0x60; /* 0b01100000 */
672  testStream.buffer.buf = buf;
673  testStream.buffer.bufLen = 2;
674  testStream.buffer.bufContent = 2;
675  testStream.buffer.ioStrm.readWriteToStream = NULL;
676  testStream.buffer.ioStrm.stream = NULL;
677  testStream.context.bufferIndx = 0;
678  initAllocList(&testStream.memList);
679 
680  err = encodeBoolean(&testStream, 1);
681 
682  bit_val = (unsigned char) buf[0] >> 7;
683 
684  fail_unless (err == EXIP_OK,
685  "encodeBoolean returns error code %d", err);
686  fail_unless (bit_val == 1,
687  "encodeBoolean does not write correctly");
688  fail_unless (testStream.context.bitPointer == 1,
689  "The encodeBoolean function did not move the bit Pointer of the stream correctly");
690 
691  err = encodeBoolean(&testStream, 0);
692 
693  bit_val = (unsigned char) buf[0] >> 6;
694 
695  fail_unless (err == EXIP_OK,
696  "encodeBoolean returns error code %d", err);
697  fail_unless (bit_val == 2,
698  "encodeBoolean does not write correctly");
699  fail_unless (testStream.context.bitPointer == 2,
700  "The encodeBoolean function did not move the bit Pointer of the stream correctly");
701 }
702 END_TEST
703 
704 START_TEST (test_encodeUnsignedInteger)
705 {
706  EXIStream testStream;
707  char buf[3];
709  unsigned char test1, test2;
710 
711  testStream.context.bitPointer = 0;
712  makeDefaultOpts(&testStream.header.opts);
713 
714  buf[0] = (char) 0xD4; /* 0b11010100 */
715  buf[1] = (char) 0x00; /* 0b00000000 */
716  buf[2] = (char) 0x00; /* 0b00000000 */
717  testStream.buffer.buf = buf;
718  testStream.buffer.bufLen = 3;
719  testStream.buffer.bufContent = 3;
720  testStream.buffer.ioStrm.readWriteToStream = NULL;
721  testStream.buffer.ioStrm.stream = NULL;
722  testStream.context.bufferIndx = 0;
723  initAllocList(&testStream.memList);
724 
725  err = encodeUnsignedInteger(&testStream, 421);
726 
727  test1 = (unsigned char) buf[0];
728  test2 = (unsigned char) buf[1];
729 
730  fail_unless (err == EXIP_OK,
731  "encodeUnsignedInteger returns error code %d", err);
732  fail_unless (test1 == 165 && test2 == 3,
733  "The encodeUnsignedInteger function doesn't work correctly");
734 
735  fail_unless (testStream.context.bitPointer == 0,
736  "The encodeUnsignedInteger function did not move the bit Pointer of the stream correctly");
737  fail_unless (testStream.context.bufferIndx == 2,
738  "The encodeUnsignedInteger function did not move the byte Pointer of the stream correctly");
739 
740  buf[0] = (char) 0x10; /* 0b00010000 */
741  buf[1] = (char) 0x00; /* 0b00000000 */
742  buf[2] = (char) 0x00; /* 0b00000000 */
743  testStream.context.bufferIndx = 0;
744  testStream.context.bitPointer = 0;
745  err = EXIP_UNEXPECTED_ERROR;
746 
747  err = encodeUnsignedInteger(&testStream, 0);
748 
749  test1 = (unsigned char) buf[0];
750  test2 = (unsigned char) buf[1];
751 
752  fail_unless (err == EXIP_OK,
753  "encodeUnsignedInteger returns error code %d", err);
754  fail_unless (test1 == 0 && test2 == 0,
755  "The encodeUnsignedInteger function doesn't work correctly");
756  fail_unless (testStream.context.bitPointer == 0,
757  "The encodeUnsignedInteger function did not move the bit Pointer of the stream correctly");
758  fail_unless (testStream.context.bufferIndx == 1,
759  "The encodeUnsignedInteger function did not move the byte Pointer of the stream correctly");
760 }
761 END_TEST
762 
763 
764 START_TEST (test_encodeString)
765 {
766  EXIStream testStream;
767  char buf[50];
768  String testStr;
770  unsigned char str_len;
771 
772  testStream.context.bitPointer = 0;
773  makeDefaultOpts(&testStream.header.opts);
774 
775  buf[0] = (char) 0x02; /* 0b00000010 */
776  buf[1] = (char) 0x65; /* 0b01100101 */
777  buf[2] = (char) 0x64; /* 0b01010100 */
778  buf[3] = (char) 0x62; /* 0b01010010 */
779  initAllocList(&testStream.memList);
780  testStream.buffer.buf = buf;
781  testStream.buffer.bufLen = 50;
782  testStream.buffer.bufContent = 50;
783  testStream.buffer.ioStrm.readWriteToStream = NULL;
784  testStream.buffer.ioStrm.stream = NULL;
785  testStream.context.bufferIndx = 0;
786  asciiToString("TEST encodeString()", &testStr, &testStream.memList, FALSE);
787 
788  err = encodeString(&testStream, &testStr);
789 
790  str_len = buf[0];
791 
792  fail_unless (err == EXIP_OK,
793  "encodeString returns error code %d", err);
794  fail_unless (str_len == 19,
795  "The String length is not encoded correctly");
796  fail_unless (buf[1] == 'T' && buf[2] == 'E',
797  "encodeString doesn't encode correctly");
798  fail_unless (testStream.context.bitPointer == 0,
799  "The encodeString function did not move the bit Pointer of the stream correctly");
800  fail_unless (testStream.context.bufferIndx == 20,
801  "The encodeString function did not move the byte Pointer of the stream correctly");
802 
803 }
804 END_TEST
805 
806 START_TEST (test_encodeBinary)
807 {
808  EXIStream testStream;
809  char buf[50];
810  char bin_data[50];
812 
813  makeDefaultOpts(&testStream.header.opts);
814 
815  bin_data[0] = (char) 0x22; /* 0b00100010 */
816  bin_data[1] = (char) 0x65; /* 0b01100101 */
817  bin_data[2] = (char) 0xD4; /* 0b11010100 */
818  bin_data[3] = (char) 0x5A; /* 0b01011010 */
819  bin_data[4] = (char) 0xD7; /* 0b11010111 */
820  initAllocList(&testStream.memList);
821  testStream.buffer.buf = buf;
822  testStream.buffer.bufLen = 50;
823  testStream.buffer.bufContent = 50;
824  testStream.buffer.ioStrm.readWriteToStream = NULL;
825  testStream.buffer.ioStrm.stream = NULL;
826  testStream.context.bufferIndx = 0;
827  testStream.context.bitPointer = 0;
828 
829  err = encodeBinary(&testStream, bin_data, 5);
830 
831  fail_unless (err == EXIP_OK,
832  "encodeBinary returns error code %d", err);
833  fail_unless (testStream.context.bitPointer == 0,
834  "The encodeBinary function did not move the bit Pointer of the stream correctly");
835  fail_unless (testStream.context.bufferIndx == 6,
836  "The encodeBinary function did not move the byte Pointer of the stream correctly");
837 
838  fail_unless(testStream.buffer.buf[0] == 5, "Incorrect encoding during encodeBinary 1");
839  fail_unless(testStream.buffer.buf[1] == (signed char) 0x22, "Incorrect encoding during encodeBinary 2");
840  fail_unless(testStream.buffer.buf[2] == (signed char) 0x65, "Incorrect encoding during encodeBinary 3");
841  fail_unless(testStream.buffer.buf[3] == (signed char) 0xD4, "Incorrect encoding during encodeBinary 4");
842  fail_unless(testStream.buffer.buf[4] == (signed char) 0x5A, "Incorrect encoding during encodeBinary 5");
843  fail_unless(testStream.buffer.buf[5] == (signed char) 0xD7, "Incorrect encoding during encodeBinary 6");
844 }
845 END_TEST
846 
847 START_TEST (test_encodeFloatValue)
848 {
849  EXIStream testStream;
850  char buf[10];
851  Float test_val;
852  Float test_dec;
854 
855  // 5 x 10^2
856  test_val.mantissa = 5;
857  test_val.exponent = 2;
858 
859  makeDefaultOpts(&testStream.header.opts);
860 
861  testStream.buffer.buf = buf;
862  testStream.buffer.bufLen = 10;
863  testStream.buffer.bufContent = 10;
864  testStream.buffer.ioStrm.readWriteToStream = NULL;
865  testStream.buffer.ioStrm.stream = NULL;
866  testStream.context.bufferIndx = 0;
867  testStream.context.bitPointer = 0;
868  initAllocList(&testStream.memList);
869 
870  err = encodeFloatValue(&testStream, test_val);
871 
872  fail_unless (err == EXIP_OK,
873  "encodeFloatValue returns error code %d", err);
874 
875  fail_unless (testStream.context.bitPointer == 2,
876  "The encodeFloatValue function did not move the bit Pointer of the stream correctly");
877  fail_unless (testStream.context.bufferIndx == 2,
878  "The encodeFloatValue function did not move the byte Pointer of the stream correctly");
879 
880  testStream.context.bitPointer = 0;
881  testStream.context.bufferIndx = 0;
882 
883  test_dec.mantissa = 0;
884  test_dec.exponent = 0;
885  err = decodeFloatValue(&testStream, &test_dec);
886 
887  fail_unless(test_val.exponent == test_dec.exponent && test_val.mantissa == test_dec.mantissa
888  , "Incorrect encoding of float value");
889 
890 }
891 END_TEST
892 
893 START_TEST (test_encodeIntegerValue)
894 {
895  EXIStream testStream;
896  char buf[5];
898  Integer test_dec = 0;
899 
900  makeDefaultOpts(&testStream.header.opts);
901 
902  testStream.buffer.buf = buf;
903  testStream.buffer.bufLen = 5;
904  testStream.buffer.bufContent = 5;
905  testStream.buffer.ioStrm.readWriteToStream = NULL;
906  testStream.buffer.ioStrm.stream = NULL;
907  testStream.context.bufferIndx = 0;
908  testStream.context.bitPointer = 0;
909  initAllocList(&testStream.memList);
910 
911  err = encodeIntegerValue(&testStream, -913);
912 
913  fail_unless (err == EXIP_OK,
914  "encodeIntegerValue returns error code %d", err);
915  fail_unless (testStream.context.bitPointer == 1,
916  "The encodeIntegerValue function did not move the bit Pointer of the stream correctly");
917  fail_unless (testStream.context.bufferIndx == 2,
918  "The encodeIntegerValue function did not move the byte Pointer of the stream correctly");
919 
920  testStream.context.bitPointer = 0;
921  testStream.context.bufferIndx = 0;
922 
923  err = decodeIntegerValue(&testStream, &test_dec);
924 
925  fail_unless (test_dec == -913,
926  "The encodeIntegerValue encodes correctly");
927 }
928 END_TEST
929 
930 START_TEST (test_encodeDecimalValue)
931 {
932  EXIStream testStream;
933  char buf[30];
935  Decimal dec_val;
936  Decimal res;
937 
938  dec_val.mantissa = 0;
939  dec_val.exponent = 0;
940 
941  res.mantissa = 5001;
942  res.exponent = -3;
943 
944  makeDefaultOpts(&testStream.header.opts);
945 
946  testStream.buffer.buf = buf;
947  testStream.buffer.bufLen = 30;
948  testStream.buffer.bufContent = 30;
949  testStream.buffer.ioStrm.readWriteToStream = NULL;
950  testStream.buffer.ioStrm.stream = NULL;
951  testStream.context.bufferIndx = 0;
952  testStream.context.bitPointer = 0;
953  initAllocList(&testStream.memList);
954 
955  err = encodeDecimalValue(&testStream, res);
956 
957  fail_unless (err == EXIP_OK,
958  "encodeDecimalValue returns error code %d", err);
959 
960  fail_unless (testStream.context.bitPointer == 1,
961  "The encodeDecimalValue function did not move the bit Pointer of the stream correctly");
962  fail_unless (testStream.context.bufferIndx == 2,
963  "The encodeDecimalValue function did not move the byte Pointer of the stream correctly");
964 
965  testStream.context.bitPointer = 0;
966  testStream.context.bufferIndx = 0;
967 
968  err = decodeDecimalValue(&testStream, &dec_val);
969 
970  fail_unless (res.mantissa == dec_val.mantissa && res.exponent == dec_val.exponent, "The value 5.001 is decoded as %d*10^%d", dec_val.mantissa, dec_val.exponent);
971 
972 }
973 END_TEST
974 
975 /* END: streamEncode tests */
976 
977 
978 /* START: ioUtil tests */
979 
980 START_TEST (test_moveBitPointer)
981 {
982  EXIStream strm;
983 
984  strm.context.bitPointer = 3;
985  strm.context.bufferIndx = 0;
986  moveBitPointer(&strm, 13);
987  fail_unless(strm.context.bitPointer == 0 && strm.context.bufferIndx == 2, "incorrect moving of the BitPointer");
988 
989  strm.context.bitPointer = 7;
990  strm.context.bufferIndx = 0;
991  moveBitPointer(&strm, 1);
992  fail_unless(strm.context.bitPointer == 0 && strm.context.bufferIndx == 1, "incorrect moving of the BitPointer");
993 
994  strm.context.bitPointer = 3;
995  strm.context.bufferIndx = 0;
996  moveBitPointer(&strm, 12);
997  fail_unless(strm.context.bitPointer == 7 && strm.context.bufferIndx == 1, "incorrect moving of the BitPointer");
998 }
999 END_TEST
1000 
1001 START_TEST (test_getBitsNumber)
1002 {
1003  fail_unless(getBitsNumber(99) == 7);
1004  fail_unless(getBitsNumber(63) == 6);
1005  fail_unless(getBitsNumber(64) == 7);
1006  fail_unless(getBitsNumber(4095) == 12);
1007  fail_unless(getBitsNumber(824) == 10);
1008  fail_unless(getBitsNumber(16383) == 14);
1009  fail_unless(getBitsNumber(7234) == 13);
1010 }
1011 END_TEST
1012 
1013 START_TEST (test_log2INT)
1014 {
1015  fail_unless(log2INT(99) == 6);
1016  fail_unless(log2INT(63) == 5);
1017  fail_unless(log2INT(64) == 6);
1018  fail_unless(log2INT(4095) == 11);
1019  fail_unless(log2INT(824) == 9);
1020  fail_unless(log2INT(16383) == 13);
1021  fail_unless(log2INT(7234) == 12);
1022 }
1023 END_TEST
1024 
1025 /* END: ioUtil tests */
1026 
1027 
1028 
1030 {
1031  Suite *s = suite_create ("StreamIO");
1032 
1033  {
1034  /* StreamRead test case */
1035  TCase *tc_sRead = tcase_create ("StreamRead");
1036  tcase_add_test (tc_sRead, test_readNextBit);
1037  tcase_add_test (tc_sRead, test_readBits);
1038  suite_add_tcase (s, tc_sRead);
1039  }
1040 
1041  {
1042  /* StreamWrite test case */
1043  TCase *tc_sWrite = tcase_create ("StreamWrite");
1044  tcase_add_test (tc_sWrite, test_writeNextBit);
1045  tcase_add_test (tc_sWrite, test_writeNBits);
1046  suite_add_tcase (s, tc_sWrite);
1047  }
1048 
1049  {
1050  /* StreamDecode test case */
1051  TCase *tc_sDecode = tcase_create ("StreamDecode");
1052  tcase_add_test (tc_sDecode, test_decodeNBitUnsignedInteger);
1053  tcase_add_test (tc_sDecode, test_decodeBoolean);
1054  tcase_add_test (tc_sDecode, test_decodeUnsignedInteger);
1055  tcase_add_test (tc_sDecode, test_decodeString);
1056  tcase_add_test (tc_sDecode, test_decodeBinary);
1057  tcase_add_test (tc_sDecode, test_decodeFloat);
1058  tcase_add_test (tc_sDecode, test_decodeIntegerValue);
1059  tcase_add_test (tc_sDecode, test_decodeDecimalValue);
1060  suite_add_tcase (s, tc_sDecode);
1061  }
1062 
1063  {
1064  /* StreamEncode test case */
1065  TCase *tc_sEncode = tcase_create ("StreamEncode");
1066  tcase_add_test (tc_sEncode, test_encodeNBitUnsignedInteger);
1067  tcase_add_test (tc_sEncode, test_encodeBoolean);
1068  tcase_add_test (tc_sEncode, test_encodeUnsignedInteger);
1069  tcase_add_test (tc_sEncode, test_encodeString);
1070  tcase_add_test (tc_sEncode, test_encodeBinary);
1071  tcase_add_test (tc_sEncode, test_encodeFloatValue);
1072  tcase_add_test (tc_sEncode, test_encodeIntegerValue);
1073  tcase_add_test (tc_sEncode, test_encodeDecimalValue);
1074  suite_add_tcase (s, tc_sEncode);
1075  }
1076 
1077  {
1078  /* ioUtil test case */
1079  TCase *tc_ioUtil = tcase_create ("ioUtil");
1080  tcase_add_test (tc_ioUtil, test_moveBitPointer);
1081  tcase_add_test (tc_ioUtil, test_getBitsNumber);
1082  tcase_add_test (tc_ioUtil, test_log2INT);
1083  suite_add_tcase (s, tc_ioUtil);
1084  }
1085 
1086  return s;
1087 }
1088 
1089 int main (void)
1090 {
1091  int number_failed;
1092  Suite *s = streamIO_suite();
1093  SRunner *sr = srunner_create (s);
1094 #ifdef _MSC_VER
1096 #endif
1097  srunner_run_all (sr, CK_NORMAL);
1098  number_failed = srunner_ntests_failed (sr);
1099  srunner_free (sr);
1100  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1101 }