comparison mupdf-source/thirdparty/zint/backend/tests/test_2of5.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 libzint - the open source barcode library
3 Copyright (C) 2020-2024 Robin Stuart <rstuart114@gmail.com>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the project nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 SUCH DAMAGE.
29 */
30 /* SPDX-License-Identifier: BSD-3-Clause */
31
32 #include "testcommon.h"
33
34 static void test_large(const testCtx *const p_ctx) {
35 int debug = p_ctx->debug;
36
37 struct item {
38 int symbology;
39 int option_2;
40 char *pattern;
41 int length;
42 int ret;
43 int expected_rows;
44 int expected_width;
45 char *expected_errtxt;
46 };
47 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
48 static const struct item data[] = {
49 /* 0*/ { BARCODE_C25STANDARD, -1, "1", 112, 0, 1, 1137, "" },
50 /* 1*/ { BARCODE_C25STANDARD, -1, "1", 113, ZINT_ERROR_TOO_LONG, -1, -1, "Error 301: Input length 113 too long (maximum 112)" },
51 /* 2*/ { BARCODE_C25STANDARD, 1, "1", 112, 0, 1, 1147, "" },
52 /* 3*/ { BARCODE_C25STANDARD, 1, "1", 113, ZINT_ERROR_TOO_LONG, -1, -1, "Error 301: Input length 113 too long (maximum 112)" },
53 /* 4*/ { BARCODE_C25INTER, -1, "1", 125, 0, 1, 1143, "" },
54 /* 5*/ { BARCODE_C25INTER, -1, "1", 126, ZINT_ERROR_TOO_LONG, -1, -1, "Error 309: Input length 126 too long (maximum 125)" },
55 /* 6*/ { BARCODE_C25INTER, 1, "1", 125, 0, 1, 1143, "" },
56 /* 7*/ { BARCODE_C25INTER, 1, "1", 126, ZINT_ERROR_TOO_LONG, -1, -1, "Error 309: Input length 126 too long (maximum 125)" },
57 /* 8*/ { BARCODE_C25IATA, -1, "1", 80, 0, 1, 1129, "" },
58 /* 9*/ { BARCODE_C25IATA, -1, "1", 81, ZINT_ERROR_TOO_LONG, -1, -1, "Error 305: Input length 81 too long (maximum 80)" },
59 /* 10*/ { BARCODE_C25IATA, 1, "1", 80, 0, 1, 1143, "" },
60 /* 11*/ { BARCODE_C25IATA, 1, "1", 81, ZINT_ERROR_TOO_LONG, -1, -1, "Error 305: Input length 81 too long (maximum 80)" },
61 /* 12*/ { BARCODE_C25LOGIC, -1, "1", 113, 0, 1, 1139, "" },
62 /* 13*/ { BARCODE_C25LOGIC, -1, "1", 114, ZINT_ERROR_TOO_LONG, -1, -1, "Error 307: Input length 114 too long (maximum 113)" },
63 /* 14*/ { BARCODE_C25LOGIC, 1, "1", 113, 0, 1, 1149, "" },
64 /* 15*/ { BARCODE_C25LOGIC, 1, "1", 114, ZINT_ERROR_TOO_LONG, -1, -1, "Error 307: Input length 114 too long (maximum 113)" },
65 /* 16*/ { BARCODE_C25IND, -1, "1", 79, 0, 1, 1125, "" },
66 /* 17*/ { BARCODE_C25IND, -1, "1", 80, ZINT_ERROR_TOO_LONG, -1, -1, "Error 303: Input length 80 too long (maximum 79)" },
67 /* 18*/ { BARCODE_C25IND, 1, "1", 79, 0, 1, 1139, "" },
68 /* 19*/ { BARCODE_C25IND, 1, "1", 80, ZINT_ERROR_TOO_LONG, -1, -1, "Error 303: Input length 80 too long (maximum 79)" },
69 /* 20*/ { BARCODE_DPLEIT, -1, "1", 13, 0, 1, 135, "" },
70 /* 21*/ { BARCODE_DPLEIT, -1, "1", 14, ZINT_ERROR_TOO_LONG, -1, -1, "Error 313: Input length 14 too long (maximum 13)" },
71 /* 22*/ { BARCODE_DPIDENT, -1, "1", 11, 0, 1, 117, "" },
72 /* 23*/ { BARCODE_DPIDENT, -1, "1", 12, ZINT_ERROR_TOO_LONG, -1, -1, "Error 315: Input length 12 too long (maximum 11)" },
73 /* 24*/ { BARCODE_ITF14, -1, "1", 13, 0, 1, 135, "" },
74 /* 25*/ { BARCODE_ITF14, -1, "1", 14, ZINT_ERROR_TOO_LONG, -1, -1, "Error 311: Input length 14 too long (maximum 13)" },
75 };
76 const int data_size = ARRAY_SIZE(data);
77 int i, length, ret;
78 struct zint_symbol *symbol = NULL;
79
80 char data_buf[4096];
81
82 testStartSymbol("test_large", &symbol);
83
84 for (i = 0; i < data_size; i++) {
85
86 if (testContinue(p_ctx, i)) continue;
87
88 symbol = ZBarcode_Create();
89 assert_nonnull(symbol, "Symbol not created\n");
90
91 testUtilStrCpyRepeat(data_buf, data[i].pattern, data[i].length);
92 assert_equal(data[i].length, (int) strlen(data_buf), "i:%d length %d != strlen(data_buf) %d\n", i, data[i].length, (int) strlen(data_buf));
93
94 length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, data[i].option_2, -1, -1 /*output_options*/, data_buf, data[i].length, debug);
95
96 ret = ZBarcode_Encode(symbol, (unsigned char *) data_buf, length);
97 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
98 assert_equal(symbol->errtxt[0] == '\0', ret == 0, "i:%d symbol->errtxt not %s (%s)\n", i, ret ? "set" : "empty", symbol->errtxt);
99 assert_zero(strcmp(symbol->errtxt, data[i].expected_errtxt), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected_errtxt);
100
101 if (ret < ZINT_ERROR) {
102 assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d\n", i, symbol->rows, data[i].expected_rows);
103 assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d\n", i, symbol->width, data[i].expected_width);
104 }
105
106 ZBarcode_Delete(symbol);
107 }
108
109 testFinish();
110 }
111
112 static void test_hrt(const testCtx *const p_ctx) {
113 int debug = p_ctx->debug;
114
115 struct item {
116 int symbology;
117 int option_2;
118 char *data;
119 char *expected;
120 };
121 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
122 static const struct item data[] = {
123 /* 0*/ { BARCODE_C25STANDARD, -1, "123456789", "123456789" },
124 /* 1*/ { BARCODE_C25STANDARD, 1, "123456789", "1234567895" },
125 /* 2*/ { BARCODE_C25STANDARD, 2, "123456789", "123456789" }, /* Suppresses printing of check digit */
126 /* 3*/ { BARCODE_C25INTER, -1, "123456789", "0123456789" }, /* Adds leading zero if odd */
127 /* 4*/ { BARCODE_C25INTER, 1, "123456789", "1234567895" }, /* Unless check digit added when it becomes even */
128 /* 5*/ { BARCODE_C25INTER, 2, "123456789", "123456789" },
129 /* 6*/ { BARCODE_C25INTER, -1, "1234567890", "1234567890" }, /* No leading zero if even */
130 /* 7*/ { BARCODE_C25INTER, 1, "1234567890", "012345678905" }, /* Unless check digit added when it becomes odd */
131 /* 8*/ { BARCODE_C25INTER, 2, "1234567890", "01234567890" },
132 /* 9*/ { BARCODE_C25IATA, -1, "123456789", "123456789" },
133 /* 10*/ { BARCODE_C25IATA, 1, "123456789", "1234567895" },
134 /* 11*/ { BARCODE_C25IATA, 2, "123456789", "123456789" },
135 /* 12*/ { BARCODE_C25LOGIC, -1, "123456789", "123456789" },
136 /* 13*/ { BARCODE_C25LOGIC, 1, "123456789", "1234567895" },
137 /* 14*/ { BARCODE_C25LOGIC, 2, "123456789", "123456789" },
138 /* 15*/ { BARCODE_C25IND, -1, "123456789", "123456789" },
139 /* 16*/ { BARCODE_C25IND, 1, "123456789", "1234567895" },
140 /* 17*/ { BARCODE_C25IND, 2, "123456789", "123456789" },
141 /* 18*/ { BARCODE_DPLEIT, -1, "123456789", "00001.234.567.890" }, /* Leading zeroes added to make 13 + appended checksum */
142 /* 19*/ { BARCODE_DPLEIT, -1, "1234567890123", "12345.678.901.236" },
143 /* 20*/ { BARCODE_DPIDENT, -1, "123456789", "00.12 3.456.789 0" }, /* Leading zeroes added to make 11 + appended checksum */
144 /* 21*/ { BARCODE_DPIDENT, -1, "12345678901", "12.34 5.678.901 6" },
145 /* 22*/ { BARCODE_ITF14, -1, "123456789", "00001234567895" }, /* Leading zeroes added to make 13 + appended checksum */
146 /* 23*/ { BARCODE_ITF14, -1, "1234567890123", "12345678901231" },
147 };
148 const int data_size = ARRAY_SIZE(data);
149 int i, length, ret;
150 struct zint_symbol *symbol = NULL;
151
152 testStartSymbol("test_hrt", &symbol);
153
154 for (i = 0; i < data_size; i++) {
155
156 if (testContinue(p_ctx, i)) continue;
157
158 symbol = ZBarcode_Create();
159 assert_nonnull(symbol, "Symbol not created\n");
160
161 length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, data[i].option_2, -1, -1 /*output_options*/, data[i].data, -1, debug);
162
163 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
164 assert_zero(ret, "i:%d ZBarcode_Encode ret %d != 0 %s\n", i, ret, symbol->errtxt);
165
166 assert_zero(strcmp((char *) symbol->text, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->text, data[i].expected);
167
168 ZBarcode_Delete(symbol);
169 }
170
171 testFinish();
172 }
173
174 static void test_input(const testCtx *const p_ctx) {
175 int debug = p_ctx->debug;
176
177 struct item {
178 int symbology;
179 int input_mode;
180 char *data;
181 int ret;
182 int expected_rows;
183 int expected_width;
184 char *expected_errtxt;
185 };
186 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
187 static const struct item data[] = {
188 /* 0*/ { BARCODE_C25STANDARD, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 302: Invalid character at position 1 in input (digits only)" },
189 /* 1*/ { BARCODE_C25STANDARD, -1, "1234A6", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 302: Invalid character at position 5 in input (digits only)" },
190 /* 2*/ { BARCODE_C25STANDARD, ESCAPE_MODE, "\\d049234A6", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 302: Invalid character at position 5 in input (digits only)" }, /* TODO (maybe): position doesn't account for escapes */
191 /* 3*/ { BARCODE_C25INTER, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 310: Invalid character at position 1 in input (digits only)" },
192 /* 4*/ { BARCODE_C25IATA, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 306: Invalid character at position 1 in input (digits only)" },
193 /* 5*/ { BARCODE_C25LOGIC, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 308: Invalid character at position 1 in input (digits only)" },
194 /* 6*/ { BARCODE_C25IND, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 304: Invalid character at position 1 in input (digits only)" },
195 /* 7*/ { BARCODE_DPLEIT, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 314: Invalid character at position 1 in input (digits only)" },
196 /* 8*/ { BARCODE_DPIDENT, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 316: Invalid character at position 1 in input (digits only)" },
197 /* 9*/ { BARCODE_DPIDENT, -1, "1234567890A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 316: Invalid character at position 11 in input (digits only)" },
198 /* 10*/ { BARCODE_ITF14, -1, "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 312: Invalid character at position 1 in input (digits only)" },
199 };
200 const int data_size = ARRAY_SIZE(data);
201 int i, length, ret;
202 struct zint_symbol *symbol = NULL;
203
204 testStartSymbol("test_input", &symbol);
205
206 for (i = 0; i < data_size; i++) {
207
208 if (testContinue(p_ctx, i)) continue;
209
210 symbol = ZBarcode_Create();
211 assert_nonnull(symbol, "Symbol not created\n");
212
213 length = testUtilSetSymbol(symbol, data[i].symbology, data[i].input_mode, -1 /*eci*/, -1 /*option_1*/, -1 /*option_2*/, -1, -1 /*output_options*/, data[i].data, -1, debug);
214
215 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
216 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
217 assert_equal(symbol->errtxt[0] == '\0', ret == 0, "i:%d symbol->errtxt not %s (%s)\n", i, ret ? "set" : "empty", symbol->errtxt);
218 assert_zero(strcmp(symbol->errtxt, data[i].expected_errtxt), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected_errtxt);
219
220 if (ret < ZINT_ERROR) {
221 assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d\n", i, symbol->rows, data[i].expected_rows);
222 assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d\n", i, symbol->width, data[i].expected_width);
223 }
224
225 ZBarcode_Delete(symbol);
226 }
227
228 testFinish();
229 }
230
231 static void test_encode(const testCtx *const p_ctx) {
232 int debug = p_ctx->debug;
233
234 struct item {
235 int symbology;
236 int option_2;
237 char *data;
238 int ret;
239
240 int expected_rows;
241 int expected_width;
242 char *comment;
243 char *expected;
244 };
245 /* BARCODE_ITF14 examples verified manually against GS1 General Specifications 21.0.1 */
246 static const struct item data[] = {
247 /* 0*/ { BARCODE_C25STANDARD, -1, "87654321", 0, 1, 97, "Standard Code 2 of 5; note zint uses 4X start/end wides while BWIPP uses 3X",
248 "1111010101110100010101000111010001110101110111010101110111011100010101000101110111010111011110101"
249 },
250 /* 1*/ { BARCODE_C25STANDARD, 1, "87654321", 0, 1, 107, "With check digit",
251 "11110101011101000101010001110100011101011101110101011101110111000101010001011101110101110100010111011110101"
252 },
253 /* 2*/ { BARCODE_C25STANDARD, -1, "1234567890", 0, 1, 117, "",
254 "111101010111010111010001011101110001010101110111011101110101000111010101000111011101000101000100010101110001011110101"
255 },
256 /* 3*/ { BARCODE_C25INTER, -1, "87654321", 0, 1, 81, "Interleaved Code 2 of 5, even; verified manually against tec-it",
257 "101011101010111000100010001110111000101010001000111010111010001110101011100011101"
258 },
259 /* 4*/ { BARCODE_C25INTER, 1, "87654321", 0, 1, 99, "With check digit",
260 "101010001011101110001010100010001110111011101011100010100011101110001010100011101000101011100011101"
261 },
262 /* 5*/ { BARCODE_C25INTER, -1, "7654321", 0, 1, 81, "Interleaved Code 2 of 5, odd",
263 "101010101110111000100010001110111000101010001000111010111010001110101011100011101"
264 },
265 /* 6*/ { BARCODE_C25INTER, 1, "7654321", 0, 1, 81, "With check digit",
266 "101010100010001110111011101011100010100011101110001010100011101010001000111011101"
267 },
268 /* 7*/ { BARCODE_C25INTER, -1, "602003", 0, 1, 63, "DX cartridge barcode https://en.wikipedia.org/wiki/Interleaved_2_of_5 example",
269 "101010111011100010001010111010001000111010001000111011101011101"
270 },
271 /* 8*/ { BARCODE_C25IATA, -1, "87654321", 0, 1, 121, "IATA Code 2 of 5; verified manually against tec-it",
272 "1010111010101110101010101110111010111011101010111010111010101010111010111011101110101010101110101011101110101010111011101"
273 },
274 /* 9*/ { BARCODE_C25IATA, 1, "87654321", 0, 1, 135, "With check digit",
275 "101011101010111010101010111011101011101110101011101011101010101011101011101110111010101010111010101110111010101011101011101010111011101"
276 },
277 /* 10*/ { BARCODE_C25LOGIC, -1, "87654321", 0, 1, 89, "Code 2 of 5 Data Logic; verified manually against tec-it",
278 "10101110100010101000111010001110101110111010101110111011100010101000101110111010111011101"
279 },
280 /* 11*/ { BARCODE_C25LOGIC, 1, "87654321", 0, 1, 99, "With check digit",
281 "101011101000101010001110100011101011101110101011101110111000101010001011101110101110100010111011101"
282 },
283 /* 12*/ { BARCODE_C25IND, -1, "87654321", 0, 1, 131, "Industrial Code 2 of 5; verified manually against tec-it",
284 "11101110101110101011101010101011101110101110111010101110101110101010101110101110111011101010101011101010111011101010101110111010111"
285 },
286 /* 13*/ { BARCODE_C25IND, 1, "87654321", 0, 1, 145, "With check digit",
287 "1110111010111010101110101010101110111010111011101010111010111010101010111010111011101110101010101110101011101110101010111010111010101110111010111"
288 },
289 /* 14*/ { BARCODE_C25IND, -1, "1234567890", 0, 1, 159, "",
290 "111011101011101010101110101110101011101110111010101010101110101110111010111010101011101110101010101011101110111010101110101011101011101010101110111010111010111"
291 },
292 /* 15*/ { BARCODE_DPLEIT, -1, "0000087654321", 0, 1, 135, "Deutsche Post Leitcode; verified manually against tec-it",
293 "101010101110001110001010101110001110001010001011101110001010100010001110111011101011100010100011101110001010100011101000100010111011101"
294 },
295 /* 16*/ { BARCODE_DPLEIT, 1, "0000087654321", 0, 1, 135, "Check digit option ignored",
296 "101010101110001110001010101110001110001010001011101110001010100010001110111011101011100010100011101110001010100011101000100010111011101"
297 },
298 /* 17*/ { BARCODE_DPLEIT, -1, "2045703000360", 0, 1, 135, "Deutsche Post DIALOGPOST SCHWER brochure 3.1 example",
299 "101010111010001000111010001011100010111010101000111000111011101110100010001010101110001110001011101110001000101010001011100011101011101"
300 },
301 /* 18*/ { BARCODE_DPLEIT, -1, "5082300702800", 0, 1, 135, "Deutsche Post Leitcode https://de.wikipedia.org/wiki/Leitcode example",
302 "101011101011100010001011101000101110100011101110100010001010101110111000100010100011101110100011101010001110001010001011100011101011101"
303 },
304 /* 19*/ { BARCODE_DPIDENT, -1, "00087654321", 0, 1, 117, "Deutsche Post Identcode; verified manually against tec-it (HRT differently formatted)",
305 "101010101110001110001010001011101110001010100010001110111011101011100010100011101110001010100011101000100010111011101"
306 },
307 /* 20*/ { BARCODE_DPIDENT, -1, "00087654321", 0, 1, 117, "Check digit option ignored",
308 "101010101110001110001010001011101110001010100010001110111011101011100010100011101110001010100011101000100010111011101"
309 },
310 /* 21*/ { BARCODE_DPIDENT, -1, "80420000001", 0, 1, 117, "Deutsche Post DIALOGPOST SCHWER brochure 3.1 example",
311 "101011101010001110001010100011101011100010101110001110001010101110001110001010101110001110001011101010001000111011101"
312 },
313 /* 22*/ { BARCODE_DPIDENT, -1, "39601313414", 0, 1, 117, "Deutsche Post Identcode https://de.wikipedia.org/wiki/Leitcode example",
314 "101011101110001010001010111011100010001011100010001010111011100010001010111010001011101011100010101110001000111011101"
315 },
316 /* 23*/ { BARCODE_ITF14, -1, "0000087654321", 0, 1, 135, "ITF-14; verified manually against tec-it",
317 "101010101110001110001010101110001110001010001011101110001010100010001110111011101011100010100011101110001010100011101000101011100011101"
318 },
319 /* 24*/ { BARCODE_ITF14, 1, "0000087654321", 0, 1, 135, "Check digit option ignored",
320 "101010101110001110001010101110001110001010001011101110001010100010001110111011101011100010100011101110001010100011101000101011100011101"
321 },
322 /* 25*/ { BARCODE_ITF14, -1, "0950110153000", 0, 1, 135, "GS1 General Specifications Figure 5.1-2",
323 "101010100011101110001011101011100010001011100010101011100010001011101110100011100010001110101010101110001110001010001000111011101011101"
324 },
325 /* 26*/ { BARCODE_ITF14, -1, "1540014128876", 0, 1, 135, "GS1 General Specifications Figure 5.3.2.4-1",
326 "101011100010100010111010101110001000111010001011101110100010001011101011100010001110101000111011101010111000100010001110001110101011101"
327 },
328 /* 27*/ { BARCODE_ITF14, -1, "0950110153001", 0, 1, 135, "GS1 General Specifications Figure 5.3.6-1",
329 "101010100011101110001011101011100010001011100010101011100010001011101110100011100010001110101010101110001110001011101010001000111011101"
330 },
331 };
332 const int data_size = ARRAY_SIZE(data);
333 int i, length, ret;
334 struct zint_symbol *symbol = NULL;
335
336 char escaped[1024];
337 char cmp_buf[4096];
338 char cmp_msg[1024];
339
340 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
341 int do_zxingcpp = (debug & ZINT_DEBUG_TEST_ZXINGCPP) && testUtilHaveZXingCPPDecoder(); /* Only do ZXing-C++ test if asked, too slow otherwise */
342
343 testStartSymbol("test_encode", &symbol);
344
345 for (i = 0; i < data_size; i++) {
346
347 if (testContinue(p_ctx, i)) continue;
348
349 symbol = ZBarcode_Create();
350 assert_nonnull(symbol, "Symbol not created\n");
351
352 length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, data[i].option_2, -1, -1 /*output_options*/, data[i].data, -1, debug);
353
354 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
355 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
356
357 if (p_ctx->generate) {
358 printf(" /*%3d*/ { %s, %d, \"%s\", %s, %d, %d, \"%s\",\n",
359 i, testUtilBarcodeName(data[i].symbology), data[i].option_2, testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
360 testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].comment);
361 testUtilModulesPrint(symbol, " ", "\n");
362 printf(" },\n");
363 } else {
364 if (ret < ZINT_ERROR) {
365 int width, row;
366
367 assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d (%s)\n", i, symbol->rows, data[i].expected_rows, data[i].data);
368 assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d (%s)\n", i, symbol->width, data[i].expected_width, data[i].data);
369
370 ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
371 assert_zero(ret, "i:%d testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, ret, width, row, data[i].data);
372
373 if (do_bwipp && testUtilCanBwipp(i, symbol, -1, data[i].option_2, -1, debug)) {
374 ret = testUtilBwipp(i, symbol, -1, data[i].option_2, -1, data[i].data, length, NULL, cmp_buf, sizeof(cmp_buf), NULL);
375 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
376
377 ret = testUtilBwippCmp(symbol, cmp_msg, cmp_buf, data[i].expected);
378 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
379 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_buf, data[i].expected);
380 }
381 if (do_zxingcpp && testUtilCanZXingCPP(i, symbol, data[i].data, length, debug)) {
382 int cmp_len, ret_len;
383 char modules_dump[8192 + 1];
384 assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
385 ret = testUtilZXingCPP(i, symbol, data[i].data, length, modules_dump, cmp_buf, sizeof(cmp_buf), &cmp_len);
386 assert_zero(ret, "i:%d %s testUtilZXingCPP ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
387
388 ret = testUtilZXingCPPCmp(symbol, cmp_msg, cmp_buf, cmp_len, data[i].data, length, NULL /*primary*/, escaped, &ret_len);
389 assert_zero(ret, "i:%d %s testUtilZXingCPPCmp %d != 0 %s\n actual: %.*s\nexpected: %.*s\n",
390 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_len, cmp_buf, ret_len, escaped);
391 }
392 }
393 }
394
395 ZBarcode_Delete(symbol);
396 }
397
398 testFinish();
399 }
400
401 #include <time.h>
402
403 #define TEST_PERF_ITER_MILLES 5
404 #define TEST_PERF_ITERATIONS (TEST_PERF_ITER_MILLES * 1000)
405 #define TEST_PERF_TIME(arg) ((arg) * 1000.0 / CLOCKS_PER_SEC)
406
407 /* Not a real test, just performance indicator */
408 static void test_perf(const testCtx *const p_ctx) {
409 int debug = p_ctx->debug;
410
411 struct item {
412 int symbology;
413 int option_2;
414 char *data;
415 int ret;
416
417 int expected_rows;
418 int expected_width;
419 char *comment;
420 };
421 static const struct item data[] = {
422 /* 0*/ { BARCODE_C25INTER, -1, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 0, 1, 819, "C25INTER 90" },
423 /* 1*/ { BARCODE_C25INTER, -1, "1234567890", 0, 1, 99, "C25INTER 10" },
424 /* 2*/ { BARCODE_C25STANDARD, -1, "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 0, 1, 817, "C25STANDARD 80" },
425 /* 3*/ { BARCODE_C25STANDARD, -1, "1234567890", 0, 1, 117, "C25STANDARD 10" },
426 };
427 const int data_size = ARRAY_SIZE(data);
428 int i, length, ret;
429 struct zint_symbol *symbol;
430
431 clock_t start;
432 clock_t total_create = 0, total_encode = 0, total_buffer = 0, total_buf_inter = 0, total_print = 0;
433 clock_t diff_create, diff_encode, diff_buffer, diff_buf_inter, diff_print;
434 int comment_max = 0;
435
436 if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
437 return;
438 }
439
440 for (i = 0; i < data_size; i++) if ((int) strlen(data[i].comment) > comment_max) comment_max = (int) strlen(data[i].comment);
441
442 printf("Iterations %d\n", TEST_PERF_ITERATIONS);
443
444 for (i = 0; i < data_size; i++) {
445 int j;
446
447 if (testContinue(p_ctx, i)) continue;
448
449 diff_create = diff_encode = diff_buffer = diff_buf_inter = diff_print = 0;
450
451 for (j = 0; j < TEST_PERF_ITERATIONS; j++) {
452 start = clock();
453 symbol = ZBarcode_Create();
454 diff_create += clock() - start;
455 assert_nonnull(symbol, "Symbol not created\n");
456
457 length = testUtilSetSymbol(symbol, data[i].symbology, DATA_MODE, -1 /*eci*/, -1 /*option_1*/, data[i].option_2, -1, -1 /*output_options*/, data[i].data, -1, debug);
458
459 start = clock();
460 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
461 diff_encode += clock() - start;
462 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
463
464 assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d (%s)\n", i, symbol->rows, data[i].expected_rows, data[i].data);
465 assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d (%s)\n", i, symbol->width, data[i].expected_width, data[i].data);
466
467 start = clock();
468 ret = ZBarcode_Buffer(symbol, 0 /*rotate_angle*/);
469 diff_buffer += clock() - start;
470 assert_zero(ret, "i:%d ZBarcode_Buffer ret %d != 0 (%s)\n", i, ret, symbol->errtxt);
471
472 symbol->output_options |= OUT_BUFFER_INTERMEDIATE;
473 start = clock();
474 ret = ZBarcode_Buffer(symbol, 0 /*rotate_angle*/);
475 diff_buf_inter += clock() - start;
476 assert_zero(ret, "i:%d ZBarcode_Buffer OUT_BUFFER_INTERMEDIATE ret %d != 0 (%s)\n", i, ret, symbol->errtxt);
477 symbol->output_options &= ~OUT_BUFFER_INTERMEDIATE; /* Undo */
478
479 start = clock();
480 ret = ZBarcode_Print(symbol, 0 /*rotate_angle*/);
481 diff_print += clock() - start;
482 assert_zero(ret, "i:%d ZBarcode_Print ret %d != 0 (%s)\n", i, ret, symbol->errtxt);
483 assert_zero(testUtilRemove(symbol->outfile), "i:%d testUtilRemove(%s) != 0\n", i, symbol->outfile);
484
485 ZBarcode_Delete(symbol);
486 }
487
488 printf("%*s: encode % 8gms, buffer % 8gms, buf_inter % 8gms, print % 8gms, create % 8gms\n", comment_max, data[i].comment,
489 TEST_PERF_TIME(diff_encode), TEST_PERF_TIME(diff_buffer), TEST_PERF_TIME(diff_buf_inter), TEST_PERF_TIME(diff_print), TEST_PERF_TIME(diff_create));
490
491 total_create += diff_create;
492 total_encode += diff_encode;
493 total_buffer += diff_buffer;
494 total_buf_inter += diff_buf_inter;
495 total_print += diff_print;
496 }
497 if (p_ctx->index == -1) {
498 printf("%*s: encode % 8gms, buffer % 8gms, buf_inter % 8gms, print % 8gms, create % 8gms\n", comment_max, "totals",
499 TEST_PERF_TIME(total_encode), TEST_PERF_TIME(total_buffer), TEST_PERF_TIME(total_buf_inter), TEST_PERF_TIME(total_print), TEST_PERF_TIME(total_create));
500 }
501 }
502
503 int main(int argc, char *argv[]) {
504
505 testFunction funcs[] = { /* name, func */
506 { "test_large", test_large },
507 { "test_hrt", test_hrt },
508 { "test_input", test_input },
509 { "test_encode", test_encode },
510 { "test_perf", test_perf },
511 };
512
513 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
514
515 testReport();
516
517 return 0;
518 }
519
520 /* vim: set ts=4 sw=4 et : */