]> git.sur5r.net Git - glabels/blob - qrencode-3.1.0/tests/test_bitstream.c
Organized master branch to be top-level directory for glabels, instead of
[glabels] / qrencode-3.1.0 / tests / test_bitstream.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "common.h"
4 #include "../bitstream.h"
5
6 void test_null(void)
7 {
8         BitStream *bstream;
9
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");
15         testFinish();
16
17         BitStream_free(bstream);
18 }
19
20 void test_num(void)
21 {
22         BitStream *bstream;
23         unsigned int data = 0x13579bdf;
24         char correct[] = "0010011010101111001101111011111";
25
26         testStart("New from num");
27         bstream = BitStream_new();
28         BitStream_appendNum(bstream, 31, data);
29         testEnd(cmpBin(correct, bstream));
30
31         BitStream_free(bstream);
32 }
33
34 void test_bytes(void)
35 {
36         BitStream *bstream;
37         unsigned char data[1] = {0x3a};
38         char correct[] = "00111010";
39
40         testStart("New from bytes");
41         bstream = BitStream_new();
42         BitStream_appendBytes(bstream, 1, data);
43         testEnd(cmpBin(correct, bstream));
44         BitStream_free(bstream);
45 }
46
47 void test_appendNum(void)
48 {
49         BitStream *bstream;
50         char correct[] = "10001010111111111111111100010010001101000101011001111000";
51
52         testStart("Append Num");
53         bstream = BitStream_new();
54
55         BitStream_appendNum(bstream,  8, 0x0000008a);
56         assert_zero(ncmpBin(correct, bstream, 8), "Internal data is incorrect.");
57
58         BitStream_appendNum(bstream, 16, 0x0000ffff);
59         assert_zero(ncmpBin(correct, bstream, 24), "Internal data is incorrect.");
60
61         BitStream_appendNum(bstream, 32, 0x12345678);
62
63         assert_zero(cmpBin(correct, bstream), "Internal data is incorrect.");
64         testFinish();
65
66         BitStream_free(bstream);
67 }
68
69 void test_appendBytes(void)
70 {
71         BitStream *bstream;
72         unsigned char data[8];
73         char correct[] = "10001010111111111111111100010010001101000101011001111000";
74
75         testStart("Append Bytes");
76         bstream = BitStream_new();
77
78         data[0] = 0x8a;
79         BitStream_appendBytes(bstream,  1, data);
80         assert_zero(ncmpBin(correct, bstream, 8), "Internal data is incorrect.");
81
82         data[0] = 0xff;
83         data[1] = 0xff;
84         BitStream_appendBytes(bstream, 2, data);
85         assert_zero(ncmpBin(correct, bstream, 24), "Internal data is incorrect.");
86
87         data[0] = 0x12;
88         data[1] = 0x34;
89         data[2] = 0x56;
90         data[3] = 0x78;
91         BitStream_appendBytes(bstream, 4, data);
92
93         assert_zero(cmpBin(correct, bstream), "Internal data is incorrect.");
94         testFinish();
95
96         BitStream_free(bstream);
97 }
98
99 void test_toByte(void)
100 {
101         BitStream *bstream;
102         unsigned char correct[] = {
103                 0x8a, 0xff, 0xff, 0x12, 0x34, 0x56, 0x78
104         };
105         unsigned char *result;
106
107         testStart("Convert to a byte array");
108         bstream = BitStream_new();
109
110         BitStream_appendBytes(bstream, 1, &correct[0]);
111         BitStream_appendBytes(bstream, 2, &correct[1]);
112         BitStream_appendBytes(bstream, 4, &correct[3]);
113
114         result = BitStream_toByte(bstream);
115         testEnd(memcmp(correct, result, 7));
116
117         BitStream_free(bstream);
118         free(result);
119 }
120
121 void test_size(void)
122 {
123         BitStream *bstream;
124
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)");
132         testFinish();
133
134         BitStream_free(bstream);
135 }
136
137 int main(int argc, char **argv)
138 {
139         test_null();
140         test_num();
141         test_bytes();
142         test_appendNum();
143         test_appendBytes();
144         test_toByte();
145         test_size();
146
147         report();
148
149         return 0;
150 }