]> git.sur5r.net Git - glabels/blob - qrencode-3.1.0/tests/test_estimatebit.c
4e65eb630cccd6526fbdeac6f397b9746b7c1a7b
[glabels] / qrencode-3.1.0 / tests / test_estimatebit.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "common.h"
5 #include "../qrinput.h"
6
7 QRinput *gstream;
8
9 void test_numbit(void)
10 {
11         QRinput *stream;
12         char num[9]="01234567";
13         int bits;
14
15         testStart("Estimation of Numeric stream (8 digits)");
16         stream = QRinput_new();
17         QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num);
18         bits = QRinput_estimateBitStreamSize(stream, 0);
19         testEndExp(bits == 41);
20
21         QRinput_append(gstream, QR_MODE_NUM, 8, (unsigned char *)num);
22         QRinput_free(stream);
23 }
24
25 void test_numbit2(void)
26 {
27         QRinput *stream;
28         char num[17]="0123456789012345";
29         int bits;
30
31         testStart("Estimation of Numeric stream (16 digits)");
32         stream = QRinput_new();
33         QRinput_append(stream, QR_MODE_NUM, 16, (unsigned char *)num);
34         bits = QRinput_estimateBitStreamSize(stream, 0);
35         testEndExp(bits == 68);
36
37         QRinput_append(gstream, QR_MODE_NUM, 16, (unsigned char *)num);
38         QRinput_free(stream);
39 }
40
41 void test_numbit3(void)
42 {
43         QRinput *stream;
44         char *num;
45         int bits;
46
47         testStart("Estimation of Numeric stream (400 digits)");
48         stream = QRinput_new();
49         num = (char *)malloc(401);
50         memset(num, '1', 400);
51         num[400] = '\0';
52         QRinput_append(stream, QR_MODE_NUM, 400, (unsigned char *)num);
53         bits = QRinput_estimateBitStreamSize(stream, 0);
54         /* 4 + 10 + 133*10 + 4 = 1348 */
55         testEndExp(bits == 1348);
56
57         QRinput_append(gstream, QR_MODE_NUM, 400, (unsigned char *)num);
58         QRinput_free(stream);
59         free(num);
60 }
61
62 void test_an(void)
63 {
64         QRinput *stream;
65         char str[6]="AC-42";
66         int bits;
67
68         testStart("Estimation of Alphabet-Numeric stream (5 chars)");
69         stream = QRinput_new();
70         QRinput_append(stream, QR_MODE_AN, 5, (unsigned char *)str);
71         bits = QRinput_estimateBitStreamSize(stream, 0);
72         testEndExp(bits == 41);
73
74         QRinput_append(gstream, QR_MODE_AN, 5, (unsigned char *)str);
75         QRinput_free(stream);
76 }
77
78 void test_8(void)
79 {
80         QRinput *stream;
81         char str[9]="12345678";
82         int bits;
83
84         testStart("Estimation of 8 bit data stream (8 bytes)");
85         stream = QRinput_new();
86         QRinput_append(stream, QR_MODE_8, 8, (unsigned char *)str);
87         bits = QRinput_estimateBitStreamSize(stream, 0);
88         testEndExp(bits == 76);
89
90         QRinput_append(gstream, QR_MODE_8, 8, (unsigned char *)str);
91         QRinput_free(stream);
92 }
93
94 void test_structure(void)
95 {
96         QRinput *stream;
97         int bits;
98
99         testStart("Estimation of a structure-append header");
100         stream = QRinput_new();
101         QRinput_insertStructuredAppendHeader(stream, 10, 1, 0);
102         bits = QRinput_estimateBitStreamSize(stream, 1);
103         testEndExp(bits == 20);
104
105         QRinput_insertStructuredAppendHeader(gstream, 10, 1, 0);
106         QRinput_free(stream);
107 }
108
109 void test_kanji(void)
110 {
111         int res;
112
113         QRinput *stream;
114         unsigned char str[4]= {0x93, 0x5f,0xe4, 0xaa};
115         int bits;
116
117         testStart("Estimation of Kanji stream (2 chars)");
118         stream = QRinput_new();
119         res = QRinput_append(stream, QR_MODE_KANJI, 4, (unsigned char *)str);
120         if(res < 0) {
121                 printf("Failed to add.\n");
122                 testEnd(1);
123         } else {
124                 bits = QRinput_estimateBitStreamSize(stream, 0);
125                 testEndExp(bits == 38);
126                 QRinput_append(gstream, QR_MODE_KANJI, 4, (unsigned char *)str);
127         }
128
129         QRinput_free(stream);
130 }
131
132 void test_mix(void)
133 {
134         int bits;
135
136         testStart("Estimation of Mixed stream");
137         bits = QRinput_estimateBitStreamSize(gstream, 0);
138         testEndExp(bits == (41 + 68 + 1348 + 41 + 76 + 38 + 20));
139         QRinput_free(gstream);
140 }
141
142 int main(int argc, char **argv)
143 {
144         gstream = QRinput_new();
145
146         test_numbit();
147         test_numbit2();
148         test_numbit3();
149         test_an();
150         test_8();
151         test_kanji();
152         test_structure();
153         test_mix();
154
155         report();
156
157         return 0;
158 }