comparison mupdf-source/thirdparty/zint/backend/tests/test_bc412.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) 2022-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_input(const testCtx *const p_ctx) {
35 int debug = p_ctx->debug;
36
37 struct item {
38 int input_mode;
39 int option_1;
40 int option_2;
41 char *data;
42 int length;
43 int ret;
44 int expected_rows;
45 int expected_width;
46 char *expected;
47 char *comment;
48 };
49 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
50 static const struct item data[] = {
51 /* 0*/ { UNICODE_MODE, -1, -1, "123456", -1, ZINT_ERROR_TOO_LONG, -1, -1, "Error 792: Input length 6 too short (minimum 7)", "" },
52 /* 1*/ { UNICODE_MODE, -1, -1, "1234567890123456789", -1, ZINT_ERROR_TOO_LONG, -1, -1, "Error 790: Input length 19 too long (maximum 18)", "" },
53 /* 2*/ { UNICODE_MODE, -1, -1, "1234567", -1, 0, 1, 102, "1U234567", "" },
54 /* 3*/ { UNICODE_MODE, -1, -1, "1234567 ", -1, ZINT_ERROR_INVALID_DATA, -1, -1, "Error 791: Invalid character at position 8 in input (alphanumerics only, excluding \"O\")", "" },
55 /* 4*/ { UNICODE_MODE, -1, -1, "ABCDEFGHIJKLMNOPQR", -1, ZINT_ERROR_INVALID_DATA, -1, -1, "Error 791: Invalid character at position 15 in input (alphanumerics only, excluding \"O\")", "" },
56 /* 5*/ { UNICODE_MODE, -1, -1, "ABCDEFGHIJKLMNPQRS", -1, 0, 1, 234, "AQBCDEFGHIJKLMNPQRS", "" },
57 /* 6*/ { UNICODE_MODE, -1, -1, "STUVWXYZ1234567890", -1, 0, 1, 234, "SCTUVWXYZ1234567890", "" },
58 /* 7*/ { UNICODE_MODE, -1, -1, "abcdefghijklmnpqrs", -1, 0, 1, 234, "AQBCDEFGHIJKLMNPQRS", "" },
59 /* 8*/ { UNICODE_MODE, -1, -1, "123456789012345678", -1, 0, 1, 234, "1223456789012345678", "" },
60 /* 9*/ { UNICODE_MODE, -1, -1, "MMMMMMMMMMMMMMMMMM", -1, 0, 1, 234, "MTMMMMMMMMMMMMMMMMM", "" },
61 /* 10*/ { UNICODE_MODE, -1, -1, "00000000", -1, 0, 1, 114, "000000000", "" },
62 /* 11*/ { UNICODE_MODE, -1, -1, "000000000", -1, 0, 1, 126, "0000000000", "" },
63 /* 12*/ { UNICODE_MODE, -1, -1, "000S0S000S", -1, 0, 1, 138, "0S00S0S000S", "" },
64 /* 13*/ { UNICODE_MODE, -1, -1, "ZYXWVUTSRQP", -1, 0, 1, 150, "Z0YXWVUTSRQP", "" },
65 /* 14*/ { UNICODE_MODE, -1, -1, "0R9GLVHA8EZ4", -1, 0, 1, 162, "0DR9GLVHA8EZ4", "" },
66 /* 15*/ { UNICODE_MODE, -1, -1, "NTS1J2Q6C7DYK", -1, 0, 1, 174, "NRTS1J2Q6C7DYK", "" },
67 /* 16*/ { UNICODE_MODE, -1, -1, "TS1J2Q6C7DYKBU", -1, 0, 1, 186, "TWS1J2Q6C7DYKBU", "" },
68 /* 17*/ { UNICODE_MODE, -1, -1, "IX3FWP5M0R9GLVH", -1, 0, 1, 198, "IBX3FWP5M0R9GLVH", "" },
69 /* 18*/ { UNICODE_MODE, -1, -1, "R9GLVHA8EZ4NTS1J", -1, 0, 1, 210, "RY9GLVHA8EZ4NTS1J", "" },
70 /* 19*/ { UNICODE_MODE, -1, -1, "M5PWF3XIUBKYD7C6Q", -1, 0, 1, 222, "M35PWF3XIUBKYD7C6Q", "" },
71 /* 20*/ { UNICODE_MODE, -1, -1, "2J1STN4ZE8AHVLG90R", -1, 0, 1, 234, "2PJ1STN4ZE8AHVLG90R", "" },
72 /* 21*/ { UNICODE_MODE, -1, -1, "JJJJJJJJJJJJJJJJJJ", -1, 0, 1, 234, "J9JJJJJJJJJJJJJJJJJ", "" },
73 };
74 const int data_size = ARRAY_SIZE(data);
75 int i, length, ret;
76 struct zint_symbol *symbol = NULL;
77
78 char bwipp_buf[8192];
79 char bwipp_msg[1024];
80
81 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
82
83 testStartSymbol("test_input", &symbol);
84
85 for (i = 0; i < data_size; i++) {
86
87 if (testContinue(p_ctx, i)) continue;
88
89 symbol = ZBarcode_Create();
90 assert_nonnull(symbol, "Symbol not created\n");
91
92 length = testUtilSetSymbol(symbol, BARCODE_BC412, data[i].input_mode, -1 /*eci*/, data[i].option_1, data[i].option_2, -1, -1 /*output_options*/, data[i].data, data[i].length, debug);
93
94 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
95 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
96
97 if (ret < ZINT_ERROR) {
98 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);
99 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);
100 assert_zero(strcmp((char *) symbol->text, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->text, data[i].expected);
101
102 if (do_bwipp && testUtilCanBwipp(i, symbol, data[i].option_1, -1, -1, debug)) {
103 char modules_dump[8192 + 1];
104 assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
105 ret = testUtilBwipp(i, symbol, data[i].option_1, -1, -1, data[i].data, length, NULL, bwipp_buf, sizeof(bwipp_buf), NULL);
106 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
107
108 ret = testUtilBwippCmp(symbol, bwipp_msg, bwipp_buf, modules_dump);
109 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
110 i, testUtilBarcodeName(symbol->symbology), ret, bwipp_msg, bwipp_buf, modules_dump);
111 }
112 } else {
113 assert_zero(strcmp(symbol->errtxt, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected);
114 }
115
116 ZBarcode_Delete(symbol);
117 }
118
119 testFinish();
120 }
121
122 static void test_encode(const testCtx *const p_ctx) {
123 int debug = p_ctx->debug;
124
125 struct item {
126 int input_mode;
127 int option_1;
128 int option_2;
129 char *data;
130 int ret;
131
132 int expected_rows;
133 int expected_width;
134 int bwipp_cmp;
135 char *comment;
136 char *expected;
137 };
138 static const struct item data[] = {
139 /* 0*/ { UNICODE_MODE, -1, -1, "AQ45670", 0, 1, 102, 1, "SEMI T1-95 Figure 2, same",
140 "100101000100100100010101000100010101000101010000010101001010000101001001000101001000100101010100000101"
141 },
142 /* 1*/ { UNICODE_MODE, -1, -1, "A6BC1234", 0, 1, 114, 1, "https://www.grapecity.co.jp, same",
143 "100101000100100100100010100101001001000101000100010101000010100101010010000101010001000101010000100101010000010101"
144 },
145 };
146 const int data_size = ARRAY_SIZE(data);
147 int i, length, ret;
148 struct zint_symbol *symbol = NULL;
149
150 char escaped[1024];
151 char bwipp_buf[8192];
152 char bwipp_msg[1024];
153
154 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
155
156 testStartSymbol("test_encode", &symbol);
157
158 for (i = 0; i < data_size; i++) {
159
160 if (testContinue(p_ctx, i)) continue;
161
162 symbol = ZBarcode_Create();
163 assert_nonnull(symbol, "Symbol not created\n");
164
165 length = testUtilSetSymbol(symbol, BARCODE_BC412, data[i].input_mode, -1 /*eci*/, data[i].option_1, data[i].option_2, -1, -1 /*output_options*/, data[i].data, -1, debug);
166
167 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
168 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
169
170 if (p_ctx->generate) {
171 printf(" /*%3d*/ { %s, %d, %d, \"%s\", %s, %d, %d, %d, \"%s\",\n",
172 i, testUtilInputModeName(data[i].input_mode), data[i].option_1, data[i].option_2,
173 testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
174 testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].bwipp_cmp, data[i].comment);
175 testUtilModulesPrint(symbol, " ", "\n");
176 printf(" },\n");
177 } else {
178 if (ret < ZINT_ERROR) {
179 int width, row;
180
181 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);
182 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);
183
184 ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
185 assert_zero(ret, "i:%d testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, ret, width, row, data[i].data);
186
187 if (do_bwipp && testUtilCanBwipp(i, symbol, data[i].option_1, -1, -1, debug)) {
188 if (!data[i].bwipp_cmp) {
189 if (debug & ZINT_DEBUG_TEST_PRINT) printf("i:%d %s not BWIPP compatible (%s)\n", i, testUtilBarcodeName(symbol->symbology), data[i].comment);
190 } else {
191 ret = testUtilBwipp(i, symbol, data[i].option_1, -1, -1, data[i].data, length, NULL, bwipp_buf, sizeof(bwipp_buf), NULL);
192 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
193
194 ret = testUtilBwippCmp(symbol, bwipp_msg, bwipp_buf, data[i].expected);
195 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
196 i, testUtilBarcodeName(symbol->symbology), ret, bwipp_msg, bwipp_buf, data[i].expected);
197 }
198 }
199 }
200 }
201
202 ZBarcode_Delete(symbol);
203 }
204
205 testFinish();
206 }
207
208 int main(int argc, char *argv[]) {
209
210 testFunction funcs[] = { /* name, func */
211 { "test_input", test_input },
212 { "test_encode", test_encode },
213 };
214
215 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
216
217 testReport();
218
219 return 0;
220 }
221
222 /* vim: set ts=4 sw=4 et : */