]> git.sur5r.net Git - glabels/blob - qrencode-3.1.0/tests/common.h
Organized master branch to be top-level directory for glabels, instead of
[glabels] / qrencode-3.1.0 / tests / common.h
1 /*
2  * common part of test units.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #include <stdlib.h>
9 #include "../qrencode.h"
10 #include "../qrinput.h"
11 #include "../bitstream.h"
12 #include "../qrencode_inner.h"
13
14 #define testStart(__arg__) (testStartReal(__func__, __arg__))
15 #define testEndExp(__arg__) (testEnd(!(__arg__)))
16
17 static int tests = 0;
18 static int failed = 0;
19 static int assertionFailed = 0;
20 static int assertionNum = 0;
21 static const char *testName = NULL;
22 static const char *testFunc = NULL;
23 char levelChar[4] = {'L', 'M', 'Q', 'H'};
24 const char *modeStr[5] = {"nm", "an", "8", "kj", "st"};
25
26 void printQRinput(QRinput *input)
27 {
28         QRinput_List *list;
29         unsigned char *p;
30         int i;
31
32         list = input->head;
33         while(list != NULL) {
34                 p = list->data;
35                 for(i=0; i<list->size; i++) {
36                         printf("0x%02x,", list->data[i]);
37                 }
38                 list = list->next;
39         }
40         printf("\n");
41 }
42
43 void printQRinputInfo(QRinput *input)
44 {
45         QRinput_List *list;
46         BitStream *b;
47         int i;
48
49         printf("QRinput info:\n");
50         printf(" version: %d\n", input->version);
51         printf(" level  : %c\n", levelChar[input->level]);
52         list = input->head;
53         i = 0;
54         while(list != NULL) {
55                 i++;
56                 list = list->next;
57         }
58         printf("  chunks: %d\n", i);
59         b = QRinput_mergeBitStream(input);
60         if(b != NULL) {
61                 printf("  bitstream-size: %d\n", BitStream_size(b));
62                 BitStream_free(b);
63         }
64
65         list = input->head;
66         i = 0;
67         while(list != NULL) {
68                 printf("\t#%d: mode = %s, size = %d\n", i, modeStr[list->mode], list->size);
69                 i++;
70                 list = list->next;
71         }
72 }
73
74 void testStartReal(const char *func, const char *name)
75 {
76         tests++;
77         testName = name;
78         testFunc = func;
79         assertionFailed = 0;
80         assertionNum = 0;
81         printf("_____%d: %s: %s...\n", tests, func, name);
82 }
83
84 void testEnd(int result)
85 {
86         printf(".....%d: %s: %s, ", tests, testFunc, testName);
87         if(result) {
88                 puts("FAILED.");
89                 failed++;
90         } else {
91                 puts("PASSED.");
92         }
93 }
94
95 #define assert_exp(__exp__, ...) \
96 {assertionNum++;if(!(__exp__)) {assertionFailed++; printf(__VA_ARGS__);}}
97
98 #define assert_zero(__exp__, ...) assert_exp((__exp__) == 0, __VA_ARGS__)
99 #define assert_nonzero(__exp__, ...) assert_exp((__exp__) != 0, __VA_ARGS__)
100 #define assert_null(__ptr__, ...) assert_exp((__ptr__) == NULL, __VA_ARGS__)
101 #define assert_nonnull(__ptr__, ...) assert_exp((__ptr__) != NULL, __VA_ARGS__)
102 #define assert_equal(__e1__, __e2__, ...) assert_exp((__e1__) == (__e2__), __VA_ARGS__)
103 #define assert_notequal(__e1__, __e2__, ...) assert_exp((__e1__) != (__e2__), __VA_ARGS__)
104 #define assert_nothing(__exp__, ...) {printf(__VA_ARGS__); __exp__;}
105
106 void testFinish(void)
107 {
108         printf(".....%d: %s: %s, ", tests, testFunc, testName);
109         if(assertionFailed) {
110                 printf("FAILED. (%d assertions failed.)\n", assertionFailed);
111                 failed++;
112         } else {
113                 printf("PASSED. (%d assertions passed.)\n", assertionNum);
114         }
115 }
116
117 void report()
118 {
119         printf("Total %d tests, %d fails.\n", tests, failed);
120         if(failed) exit(-1);
121 }
122
123 int ncmpBin(char *correct, BitStream *bstream, int len)
124 {
125         int i, bit;
126
127         if(len != BitStream_size(bstream)) {
128                 printf("Length is not match: %d, %d expected.\n", BitStream_size(bstream), len);
129                 return -1;
130         }
131
132         for(i=0; i<len; i++) {
133                 bit = (correct[i] == '1')?1:0;
134                 if(bstream->data[i] != bit) return -1;
135         }
136
137         return 0;
138 }
139
140 int cmpBin(char *correct, BitStream *bstream)
141 {
142         int len;
143
144         len = strlen(correct);
145         return ncmpBin(correct, bstream, len);
146 }
147
148 char *sprintfBin(int size, unsigned char *data)
149 {
150         int i, j;
151         unsigned char mask;
152         int b, r;
153         char *str, *p;
154
155         str = (char *)malloc(size + 1);
156         p = str;
157         b = size / 8;
158         for(i=0; i<b; i++) {
159                 mask = 0x80;
160                 for(j=0; j<8; j++) {
161                         if(data[i] & mask) {
162                                 *p = '1';
163                         } else {
164                                 *p = '0';
165                         }
166                         p++;
167                         mask = mask >> 1;
168                 }
169         }
170         r = size - b * 8;
171         if(r) {
172                 mask = 1 << (r - 1);
173                 for(i=0; i<r; i++) {
174                         if(data[b] & mask) {
175                                 *p = '1';
176                         } else {
177                                 *p = '0';
178                         }
179                         p++;
180                         mask = mask >> 1;
181                 }
182         }
183         *p = '\0';
184
185         return str;
186 }
187
188 static char qrModeChar[4] = {'n', 'a', '8', 'k'};
189 void printQrinput(QRinput *input)
190 {
191         QRinput_List *list;
192
193         list = input->head;
194         while(list != NULL) {
195                 printf("%c(%d)\n", qrModeChar[list->mode], list->size);
196                 list = list->next;
197         }
198 }
199 #endif /* __COMMON_H__ */