4 #include "../bitstream.h"
10 testStart("Empty stream");
11 bstream = BitStream_new();
12 assert_zero(BitStream_size(bstream), "Size of empty BitStream is not 0.");
13 assert_null(BitStream_toByte(bstream), "BitStream_toByte returned non-NULL.");
14 assert_nothing(BitStream_free(NULL), "Check BitStream_free(NULL).\n");
17 BitStream_free(bstream);
23 unsigned int data = 0x13579bdf;
24 char correct[] = "0010011010101111001101111011111";
26 testStart("New from num");
27 bstream = BitStream_new();
28 BitStream_appendNum(bstream, 31, data);
29 testEnd(cmpBin(correct, bstream));
31 BitStream_free(bstream);
37 unsigned char data[1] = {0x3a};
38 char correct[] = "00111010";
40 testStart("New from bytes");
41 bstream = BitStream_new();
42 BitStream_appendBytes(bstream, 1, data);
43 testEnd(cmpBin(correct, bstream));
44 BitStream_free(bstream);
47 void test_appendNum(void)
50 char correct[] = "10001010111111111111111100010010001101000101011001111000";
52 testStart("Append Num");
53 bstream = BitStream_new();
55 BitStream_appendNum(bstream, 8, 0x0000008a);
56 assert_zero(ncmpBin(correct, bstream, 8), "Internal data is incorrect.");
58 BitStream_appendNum(bstream, 16, 0x0000ffff);
59 assert_zero(ncmpBin(correct, bstream, 24), "Internal data is incorrect.");
61 BitStream_appendNum(bstream, 32, 0x12345678);
63 assert_zero(cmpBin(correct, bstream), "Internal data is incorrect.");
66 BitStream_free(bstream);
69 void test_appendBytes(void)
72 unsigned char data[8];
73 char correct[] = "10001010111111111111111100010010001101000101011001111000";
75 testStart("Append Bytes");
76 bstream = BitStream_new();
79 BitStream_appendBytes(bstream, 1, data);
80 assert_zero(ncmpBin(correct, bstream, 8), "Internal data is incorrect.");
84 BitStream_appendBytes(bstream, 2, data);
85 assert_zero(ncmpBin(correct, bstream, 24), "Internal data is incorrect.");
91 BitStream_appendBytes(bstream, 4, data);
93 assert_zero(cmpBin(correct, bstream), "Internal data is incorrect.");
96 BitStream_free(bstream);
99 void test_toByte(void)
102 unsigned char correct[] = {
103 0x8a, 0xff, 0xff, 0x12, 0x34, 0x56, 0x78
105 unsigned char *result;
107 testStart("Convert to a byte array");
108 bstream = BitStream_new();
110 BitStream_appendBytes(bstream, 1, &correct[0]);
111 BitStream_appendBytes(bstream, 2, &correct[1]);
112 BitStream_appendBytes(bstream, 4, &correct[3]);
114 result = BitStream_toByte(bstream);
115 testEnd(memcmp(correct, result, 7));
117 BitStream_free(bstream);
125 testStart("size check");
126 bstream = BitStream_new();
127 assert_equal(BitStream_size(bstream), 0, "Initialized BitStream is not 0 length");
128 BitStream_appendNum(bstream, 1, 0);
129 assert_equal(BitStream_size(bstream), 1, "Size incorrect. (first append)");
130 BitStream_appendNum(bstream, 2, 0);
131 assert_equal(BitStream_size(bstream), 3, "Size incorrect. (second append)");
134 BitStream_free(bstream);
137 int main(int argc, char **argv)