comparison mupdf-source/thirdparty/zint/backend/tests/test_imail.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) 2019-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 * Intelligent Mail barcode Encoder Test Case Reference Set (csv file)
33 * Copyright (C) 2009 U.S. Postal Service
34 */
35
36 #include "testcommon.h"
37
38 #include <time.h>
39
40 #define TEST_PERF_TIME(arg) (((arg) * 1000.0) / CLOCKS_PER_SEC)
41
42 #define TEST_CSV_PERF_ITERATIONS 100
43
44 #if 0
45 #define TEST_IMAIL_CSV_MAX 300
46 #endif
47
48 static void test_csv(const testCtx *const p_ctx) {
49 int debug = p_ctx->debug;
50
51 char csvfile[1024];
52 FILE *fd;
53 char buffer[1024];
54 char id[10];
55 char tracking_code[50];
56 char routing_code[50];
57 char data[102];
58 char expected_daft[70];
59 char return_code[10];
60 char actual_daft[70];
61
62 int ret;
63 int lc = 0;
64
65 int j;
66 clock_t start;
67 clock_t total = 0;
68 int test_performance = debug & ZINT_DEBUG_TEST_PERFORMANCE; /* -d 256 */
69 int perf_iterations = test_performance ? TEST_CSV_PERF_ITERATIONS : 1;
70
71 struct zint_symbol *symbol = NULL;
72
73 testStartSymbol("test_csv", &symbol);
74
75 if (test_performance) {
76 printf("test_csv perf iterations: %d\n", perf_iterations);
77 }
78
79 assert_nonzero(testUtilDataPath(csvfile, sizeof(csvfile),
80 "/backend/tests/data/imail/usps/", "uspsIMbEncoderTestCases.csv"), "testUtilDataPath == 0\n");
81
82 for (j = 0; j < perf_iterations; j++) {
83 fd = testUtilOpen(csvfile, "r");
84 assert_nonnull(fd, "testUtilOpen(%s) == NULL", csvfile);
85
86 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
87 const char *b;
88
89 lc++;
90
91 if (testContinue(p_ctx, lc - 1)) continue;
92
93 #ifdef TEST_IMAIL_CSV_MAX
94 if (lc > TEST_IMAIL_CSV_MAX && p_ctx->index == -1) {
95 break;
96 }
97 #endif
98
99 id[0] = tracking_code[0] = routing_code[0] = expected_daft[0] = return_code[0] = '\0';
100
101 b = testUtilReadCSVField(buffer, id, sizeof(id));
102 assert_nonnull(b, "lc:%d id b == NULL", lc);
103 assert_equal(*b, ',', "lc:%d id *b %c != ','", lc, *b);
104
105 b = testUtilReadCSVField(++b, tracking_code, sizeof(tracking_code));
106 assert_nonnull(b, "lc:%d tracking_code b == NULL", lc);
107 assert_equal(*b, ',', "lc:%d tracking_code *b %c != ','", lc, *b);
108
109 b = testUtilReadCSVField(++b, routing_code, sizeof(routing_code));
110 assert_nonnull(b, "lc:%d routing_code b == NULL", lc);
111 assert_equal(*b, ',', "lc:%d routing_code *b %c != ','", lc, *b);
112
113 b = testUtilReadCSVField(++b, expected_daft, sizeof(expected_daft));
114 assert_nonnull(b, "lc:%d expected_daft b == NULL", lc);
115 assert_equal(*b, ',', "lc:%d expected_daft *b %c != ','", lc, *b);
116
117 b = testUtilReadCSVField(++b, return_code, sizeof(return_code));
118 assert_nonnull(b, "lc:%d return_code b == NULL", lc);
119 assert_equal(*b, ',', "lc:%d return_code *b %c != ','", lc, *b);
120
121 strcpy(data, tracking_code);
122 strcat(data, "-");
123 strcat(data, routing_code);
124
125 assert_nonzero((int) strlen(data), "lc:%d strlen(data) == 0", lc);
126
127 symbol = ZBarcode_Create();
128 assert_nonnull(symbol, "Symbol not created\n");
129
130 symbol->symbology = BARCODE_USPS_IMAIL;
131 symbol->debug |= debug;
132
133 if (test_performance) {
134 start = clock();
135 }
136 ret = ZBarcode_Encode(symbol, (unsigned char *) data, (int) strlen(data));
137 if (test_performance) {
138 total += clock() - start;
139 }
140
141 if (strcmp(return_code, "00") == 0) {
142
143 assert_zero(ret, "lc:%d ZBarcode_Encode ret %d != 0\n", lc, ret);
144
145 assert_equal(symbol->rows, 3, "rows %d != 3", symbol->rows);
146
147 ret = testUtilDAFTConvert(symbol, actual_daft, sizeof(actual_daft));
148 assert_nonzero(ret, "lc:%d testUtilDAFTConvert == 0", lc);
149 assert_zero(strcmp(actual_daft, expected_daft), "lc:%d\n actual %s\nexpected %s\n", lc, actual_daft, expected_daft);
150 } else {
151 assert_nonzero(ret, "lc:%d ZBarcode_Encode ret %d == 0\n", lc, ret);
152 }
153
154 ZBarcode_Delete(symbol);
155 }
156
157 assert_zero(fclose(fd), "fclose != 0\n");
158 }
159
160 if (test_performance) {
161 printf("test_csv perf total: %8gms\n", TEST_PERF_TIME(total));
162 }
163 testFinish();
164 }
165
166 static void test_hrt(const testCtx *const p_ctx) {
167 int debug = p_ctx->debug;
168
169 struct item {
170 char *data;
171 char *expected;
172 };
173 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
174 static const struct item data[] = {
175 /* 0*/ { "53379777234994544928-51135759461", "" }, /* None */
176 };
177 const int data_size = ARRAY_SIZE(data);
178 int i, length, ret;
179 struct zint_symbol *symbol = NULL;
180
181 testStartSymbol("test_hrt", &symbol);
182
183 for (i = 0; i < data_size; i++) {
184
185 if (testContinue(p_ctx, i)) continue;
186
187 symbol = ZBarcode_Create();
188 assert_nonnull(symbol, "Symbol not created\n");
189
190 length = testUtilSetSymbol(symbol, BARCODE_USPS_IMAIL, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, -1, -1, -1 /*output_options*/, data[i].data, -1, debug);
191
192 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
193 assert_zero(ret, "i:%d ZBarcode_Encode ret %d != 0 %s\n", i, ret, symbol->errtxt);
194
195 assert_zero(strcmp((char *) symbol->text, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->text, data[i].expected);
196
197 ZBarcode_Delete(symbol);
198 }
199
200 testFinish();
201 }
202
203 static void test_input(const testCtx *const p_ctx) {
204 int debug = p_ctx->debug;
205
206 struct item {
207 char *data;
208 int ret;
209 int expected_rows;
210 int expected_width;
211 char *expected_errtxt;
212 };
213 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
214 static const struct item data[] = {
215 /* 0*/ { "53379777234994544928-51135759461", 0, 3, 129, "" },
216 /* 1*/ { "123456789012345678901234567890123", ZINT_ERROR_TOO_LONG, -1, -1, "Error 450: Input length 33 too long (maximum 32)" },
217 /* 2*/ { "A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 451: Invalid character at position 1 in input (digits and \"-\" only)" },
218 /* 3*/ { "12345678901234567890", 0, 3, 129, "" }, /* Tracker only, no ZIP */
219 /* 4*/ { "15345678901234567890", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 454: Barcode Identifier (second character) out of range (0 to 4)" }, /* Tracker 2nd char > 4 */
220 /* 5*/ { "1234567890123456789", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 452: Invalid length for tracking code (20 characters required)" }, /* Tracker 20 chars */
221 /* 6*/ { "12345678901234567890-1234", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 453: Invalid length for ZIP code (5, 9 or 11 characters required)" }, /* ZIP wrong len */
222 /* 7*/ { "12345678901234567890-12345", 0, 3, 129, "" },
223 /* 8*/ { "12345678901234567890-123456", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 453: Invalid length for ZIP code (5, 9 or 11 characters required)" }, /* ZIP wrong len */
224 /* 9*/ { "12345678901234567890-12345678", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 453: Invalid length for ZIP code (5, 9 or 11 characters required)" }, /* ZIP wrong len */
225 /* 10*/ { "12345678901234567890-123456789", 0, 3, 129, "" },
226 /* 11*/ { "12345678901234567890-1234567890", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 453: Invalid length for ZIP code (5, 9 or 11 characters required)" }, /* ZIP wrong len */
227 /* 12*/ { "12345678901234567890-12345678901", 0, 3, 129, "" },
228 };
229 const int data_size = ARRAY_SIZE(data);
230 int i, length, ret;
231 struct zint_symbol *symbol = NULL;
232
233 char cmp_buf[8192];
234 char cmp_msg[1024];
235
236 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
237
238 testStartSymbol("test_input", &symbol);
239
240 for (i = 0; i < data_size; i++) {
241
242 if (testContinue(p_ctx, i)) continue;
243
244 symbol = ZBarcode_Create();
245 assert_nonnull(symbol, "Symbol not created\n");
246
247 length = testUtilSetSymbol(symbol, BARCODE_USPS_IMAIL, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, -1, -1, -1 /*output_options*/, data[i].data, -1, debug);
248
249 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
250 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
251 assert_zero(strcmp(symbol->errtxt, data[i].expected_errtxt), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected_errtxt);
252
253 if (ret < ZINT_ERROR) {
254 assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d\n", i, symbol->rows, data[i].expected_rows);
255 assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d\n", i, symbol->width, data[i].expected_width);
256
257 if (do_bwipp && testUtilCanBwipp(i, symbol, -1, -1, -1, debug)) {
258 char modules_dump[4096];
259 assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
260 ret = testUtilBwipp(i, symbol, -1, -1, -1, data[i].data, length, NULL, cmp_buf, sizeof(cmp_buf), NULL);
261 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
262
263 ret = testUtilBwippCmp(symbol, cmp_msg, cmp_buf, modules_dump);
264 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
265 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_buf, modules_dump);
266 }
267 }
268
269 ZBarcode_Delete(symbol);
270 }
271
272 testFinish();
273 }
274
275 static void test_encode(const testCtx *const p_ctx) {
276 int debug = p_ctx->debug;
277
278 struct item {
279 char *data;
280 int ret;
281
282 int expected_rows;
283 int expected_width;
284 char *comment;
285 char *expected;
286 };
287 static const struct item data[] = {
288 /* 0*/ { "01234567094987654321-01234567891", 0, 3, 129, "USPS-B-3200 Rev. H (2015) Figure 5",
289 "101000001010001000001000001010001010001000000000101010000000000000001010100010000000001010100000000000100010101010001000001010001"
290 "101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"
291 "000010001010101000100010000000100000001010001010000000101000100000100010001000101010001010101010000000001010000000101000100000100"
292 },
293 };
294 const int data_size = ARRAY_SIZE(data);
295 int i, length, ret;
296 struct zint_symbol *symbol = NULL;
297
298 char escaped[1024];
299 char bwipp_buf[8192];
300 char bwipp_msg[1024];
301
302 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
303
304 testStartSymbol("test_encode", &symbol);
305
306 for (i = 0; i < data_size; i++) {
307
308 if (testContinue(p_ctx, i)) continue;
309
310 symbol = ZBarcode_Create();
311 assert_nonnull(symbol, "Symbol not created\n");
312
313 length = testUtilSetSymbol(symbol, BARCODE_USPS_IMAIL, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, -1, -1, -1 /*output_options*/, data[i].data, -1, debug);
314
315 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
316 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
317
318 if (p_ctx->generate) {
319 printf(" /*%3d*/ { \"%s\", %s, %d, %d, \"%s\",\n",
320 i, testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
321 testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].comment);
322 testUtilModulesPrint(symbol, " ", "\n");
323 printf(" },\n");
324 } else {
325 if (ret < ZINT_ERROR) {
326 int width, row;
327
328 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);
329 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);
330
331 ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
332 assert_zero(ret, "i:%d testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, ret, width, row, data[i].data);
333
334 if (do_bwipp && testUtilCanBwipp(i, symbol, -1, -1, -1, debug)) {
335 ret = testUtilBwipp(i, symbol, -1, -1, -1, data[i].data, length, NULL, bwipp_buf, sizeof(bwipp_buf), NULL);
336 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
337
338 ret = testUtilBwippCmp(symbol, bwipp_msg, bwipp_buf, data[i].expected);
339 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
340 i, testUtilBarcodeName(symbol->symbology), ret, bwipp_msg, bwipp_buf, data[i].expected);
341 }
342 }
343 }
344
345 ZBarcode_Delete(symbol);
346 }
347
348 testFinish();
349 }
350
351 int main(int argc, char *argv[]) {
352
353 testFunction funcs[] = { /* name, func */
354 { "test_csv", test_csv },
355 { "test_hrt", test_hrt },
356 { "test_input", test_input },
357 { "test_encode", test_encode },
358 };
359
360 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
361
362 testReport();
363
364 return 0;
365 }
366
367 /* vim: set ts=4 sw=4 et : */