comparison mupdf-source/thirdparty/zint/backend/tests/test_code16k.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 char *pattern;
39 int length;
40 int ret;
41 int expected_rows;
42 int expected_width;
43 char *expected_errtxt;
44 };
45 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
46 static const struct item data[] = {
47 /* 0*/ { "A", 77, 0, 16, 70, "" }, /* BS EN 12323:2005 4.1 (l) */
48 /* 1*/ { "A", 78, ZINT_ERROR_TOO_LONG, -1, -1, "Error 421: Input too long, requires 79 symbol characters (maximum 78)" },
49 /* 2*/ { "A", 257, ZINT_ERROR_TOO_LONG, -1, -1, "Error 420: Input length 257 too long (maximum 256)" },
50 /* 3*/ { "0", 154, 0, 16, 70, "" }, /* BS EN 12323:2005 4.1 (l) */
51 /* 4*/ { "0", 155, ZINT_ERROR_TOO_LONG, -1, -1, "Error 421: Input too long, requires 80 symbol characters (maximum 78)" },
52 /* 5*/ { "0", 153, ZINT_ERROR_TOO_LONG, -1, -1, "Error 421: Input too long, requires 79 symbol characters (maximum 78)" }, /* Fails due to odd length */
53 /* 6*/ { "0", 161, ZINT_ERROR_TOO_LONG, -1, -1, "Error 421: Input too long, requires 79 symbol characters (maximum 78)" },
54 /* 7*/ { "\001", 77, 0, 16, 70, "" },
55 /* 8*/ { "\001", 78, ZINT_ERROR_TOO_LONG, -1, -1, "Error 421: Input too long, requires 79 symbol characters (maximum 78)" },
56 /* 9*/ { "\377", 38, 0, 16, 70, "" }, /* FNC4 + char for each so half 77 as not using double latch */
57 /* 10*/ { "\377", 39, ZINT_ERROR_TOO_LONG, -1, -1, "Error 421: Input too long, requires 79 symbol characters (maximum 78)" },
58 };
59 const int data_size = ARRAY_SIZE(data);
60 int i, length, ret;
61 struct zint_symbol *symbol = NULL;
62
63 char data_buf[4096];
64
65 testStartSymbol("test_large", &symbol);
66
67 for (i = 0; i < data_size; i++) {
68
69 if (testContinue(p_ctx, i)) continue;
70
71 symbol = ZBarcode_Create();
72 assert_nonnull(symbol, "Symbol not created\n");
73
74 testUtilStrCpyRepeat(data_buf, data[i].pattern, data[i].length);
75 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));
76
77 length = testUtilSetSymbol(symbol, BARCODE_CODE16K, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, -1, -1, -1 /*output_options*/, data_buf, data[i].length, debug);
78
79 ret = ZBarcode_Encode(symbol, (unsigned char *) data_buf, length);
80 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
81 assert_equal(symbol->errtxt[0] == '\0', ret == 0, "i:%d symbol->errtxt not %s (%s)\n", i, ret ? "set" : "empty", symbol->errtxt);
82 assert_zero(strcmp(symbol->errtxt, data[i].expected_errtxt), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected_errtxt);
83
84 if (ret < ZINT_ERROR) {
85 assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d\n", i, symbol->rows, data[i].expected_rows);
86 assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d\n", i, symbol->width, data[i].expected_width);
87 }
88
89 ZBarcode_Delete(symbol);
90 }
91
92 testFinish();
93 }
94
95 static void test_reader_init(const testCtx *const p_ctx) {
96 int debug = p_ctx->debug;
97
98 struct item {
99 int input_mode;
100 int output_options;
101 char *data;
102 int ret;
103 int expected_rows;
104 int expected_width;
105 char *expected;
106 char *comment;
107 };
108 static const struct item data[] = {
109 /* 0*/ { UNICODE_MODE, READER_INIT, "A", 0, 2, 70, "(10) 1 96 33 103 103 103 103 103 68 35", "ModeB FNC3 A Pad (5)" },
110 /* 1*/ { UNICODE_MODE, READER_INIT, "12", 0, 2, 70, "(10) 5 96 12 103 103 103 103 103 99 41", "ModeC1SB FNC3 12 Pad (5)" },
111 /* 2*/ { UNICODE_MODE, READER_INIT, "A1234", 0, 2, 70, "(10) 6 96 33 12 34 103 103 103 65 53", "ModeC2SB FNC3 A 12 34 Pad (3)" },
112 /* 3*/ { GS1_MODE, READER_INIT, "[90]1", ZINT_ERROR_INVALID_OPTION, 0, 0, "Error 422: Cannot use Reader Initialisation in GS1 mode", "" },
113 };
114 const int data_size = ARRAY_SIZE(data);
115 int i, length, ret;
116 struct zint_symbol *symbol = NULL;
117
118 char escaped[1024];
119
120 testStartSymbol("test_reader_init", &symbol);
121
122 for (i = 0; i < data_size; i++) {
123
124 if (testContinue(p_ctx, i)) continue;
125
126 symbol = ZBarcode_Create();
127 assert_nonnull(symbol, "Symbol not created\n");
128
129 symbol->debug = ZINT_DEBUG_TEST; /* Needed to get codeword dump in errtxt */
130
131 length = testUtilSetSymbol(symbol, BARCODE_CODE16K, data[i].input_mode, -1 /*eci*/, -1 /*option_1*/, -1 /*option_2*/, -1, data[i].output_options, data[i].data, -1, debug);
132
133 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
134 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
135
136 if (p_ctx->generate) {
137 printf(" /*%3d*/ { %s, %s, \"%s\", %s, %d, %d, \"%s\", \"%s\" },\n",
138 i, testUtilInputModeName(data[i].input_mode), testUtilOutputOptionsName(data[i].output_options),
139 testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
140 testUtilErrorName(data[i].ret), symbol->rows, symbol->width, symbol->errtxt, data[i].comment);
141 } else {
142 assert_zero(strcmp(symbol->errtxt, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected);
143 if (ret < ZINT_ERROR) {
144 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);
145 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);
146 }
147 }
148
149 ZBarcode_Delete(symbol);
150 }
151
152 testFinish();
153 }
154
155 static void test_input(const testCtx *const p_ctx) {
156 int debug = p_ctx->debug;
157
158 struct item {
159 int input_mode;
160 int option_1;
161 char *data;
162 int length;
163 int ret;
164 int expected_rows;
165 int expected_width;
166 int bwipp_cmp;
167 char *expected;
168 char *comment;
169 };
170 /*
171 NUL U+0000, CodeA-only
172 US U+001F (\037, 31), CodeA-only
173 a U+0061 (\141, 97), CodeB-only
174 b U+0062 (\142, 98), CodeB-only
175 APC U+009F (\237, 159), UTF-8 C29F, CodeA-only extended ASCII, not in ISO 8859-1
176 ß U+00DF (\337, 223), UTF-8 C39F, CodeA and CodeB extended ASCII
177 é U+00E9 (\351, 233), UTF-8 C3A9, CodeB-only extended ASCII
178 */
179 static const struct item data[] = {
180 /* 0*/ { UNICODE_MODE, -1, "\037", -1, 0, 2, 70, 1, "(10) 0 95 103 103 103 103 103 103 22 42", "ModeA US Pad (6)" },
181 /* 1*/ { UNICODE_MODE, -1, "A", -1, 0, 2, 70, 1, "(10) 1 33 103 103 103 103 103 103 52 82", "ModeB A Pad (6)" },
182 /* 2*/ { UNICODE_MODE, -1, "12", -1, 0, 2, 70, 1, "(10) 2 12 103 103 103 103 103 103 98 27", "ModeC 12 Pad (6)" },
183 /* 3*/ { GS1_MODE, -1, "[90]A", -1, 0, 2, 70, 1, "(10) 3 25 16 33 103 103 103 103 83 20", "ModeBFNC1 9 0 A Pad (4)" },
184 /* 4*/ { GS1_MODE, -1, "[90]12", -1, 0, 2, 70, 1, "(10) 4 90 12 103 103 103 103 103 79 62", "ModeCFNC1 90 12 Pad (5)" },
185 /* 5*/ { GS1_MODE, -1, "[90]12[20]12", -1, 0, 2, 70, 1, "(10) 4 90 12 102 20 12 103 103 9 72", "ModeCFNC1 90 12 FNC1 20 12 Pad (2)" },
186 /* 6*/ { GS1_MODE, -1, "[90]123[20]12", -1, 0, 3, 70, 1, "(15) 11 90 12 100 19 99 102 20 12 103 103 103 103 102 11", "ModeCFNC1 90 CodeB 3 CodeC FNC1 20 12 Pad (4)" },
187 /* 7*/ { GS1_MODE, -1, "[90]123[91]1A3[20]12", -1, 0, 4, 70, 1, "(20) 18 90 12 100 19 102 25 99 11 100 33 19 99 102 20 12 103 103 0 3", "ModeCFNC1 90 12 CodeB 3 FNC1 9 CodeC 11 CodeB A 3 CodeC FNC1 20 12 Pad (2)" },
188 /* 8*/ { GS1_MODE, -1, "[90]123A[91]123", -1, 0, 3, 70, 1, "(15) 11 90 12 100 19 33 102 25 99 11 23 103 103 81 56", "ModeCFNC1 90 12 CodeB 3 A FNC1 9 CodeC 11 23 Pad (2)" },
189 /* 9*/ { GS1_MODE | GS1PARENS_MODE, -1, "(90)12", -1, 0, 2, 70, 1, "(10) 4 90 12 103 103 103 103 103 79 62", "ModeCFNC1 90 12 Pad (5)" },
190 /* 10*/ { UNICODE_MODE, -1, "a0123456789", -1, 0, 2, 70, 1, "(10) 5 65 1 23 45 67 89 103 27 86", "ModeC1SB a 01 23 45 67 89 Pad" },
191 /* 11*/ { UNICODE_MODE, -1, "ab0123456789", -1, 0, 2, 70, 1, "(10) 6 65 66 1 23 45 67 89 19 42", "ModeC2SB a b 01 23 45 67 89" },
192 /* 12*/ { UNICODE_MODE, -1, "1234\037a", -1, 0, 2, 70, 0, "(10) 2 12 34 101 95 98 65 103 67 53", "ModeC 12 34 CodeA US 1SB a Pad; BWIPP different encodation, CodeB instead of 1SB" },
193 /* 13*/ { UNICODE_MODE, -1, "\000\037ß", 4, 0, 2, 70, 1, "(10) 0 64 95 101 63 103 103 103 75 11", "ModeA NUL US FNC4 ß Pad (3)" },
194 /* 14*/ { UNICODE_MODE, -1, "\000\037é", 4, 0, 2, 70, 0, "(10) 0 64 95 101 98 73 103 103 75 6", "ModeA NUL US FNC4 1SB é Pad (2); BWIPP different encodation, CodeB instead of 1SB" },
195 /* 15*/ { UNICODE_MODE, -1, "\000\037éa", 5, 0, 2, 70, 0, "(10) 0 64 95 100 100 73 65 103 99 69", "ModeA NUL US CodeB FNC4 é a Pad; BWIPP different encodation, FNC4 before CodeB" },
196 /* 16*/ { UNICODE_MODE, -1, "abß", -1, 0, 2, 70, 1, "(10) 1 65 66 100 63 103 103 103 66 56", "ModeB a b FNC4 ß Pad (3)" },
197 /* 17*/ { DATA_MODE, -1, "\141\142\237", -1, 0, 2, 70, 0, "(10) 1 65 66 100 98 95 103 103 6 71", "ModeB a b FNC4 1SA APC Pad (2); BWIPP different encodation, CodeA instead of 1SA" },
198 /* 18*/ { DATA_MODE, -1, "\141\142\237\037", -1, 0, 2, 70, 0, "(10) 1 65 66 101 101 95 95 103 72 93", "ModeB a b CodeA FNC4 APC US Pad; BWIPP different encodation, FNC4 before CodeA" },
199 /* 19*/ { UNICODE_MODE, -1, "ééé", -1, 0, 2, 70, 0, "(10) 1 100 73 100 73 100 73 103 105 106", "ModeB FNC4 é FNC4 é FNC4 é Pad; BWIPP different encodation, uses double FNC4 latch" },
200 /* 20*/ { UNICODE_MODE, -1, "aééééb", -1, 0, 3, 70, 1, "(15) 8 65 100 73 100 73 100 73 100 73 66 103 103 39 83", "ModeB a FNC4 é (4) b Pad (2)" },
201 /* 21*/ { UNICODE_MODE, -1, "aéééééb", -1, 0, 3, 70, 0, "(15) 8 65 100 73 100 73 100 73 100 73 100 73 66 74 106", "ModeB a FNC4 é (5) b; BWIPP different encodation, uses double FNC4 latch" },
202 /* 22*/ { UNICODE_MODE, -1, "aééééébcdeé", -1, 0, 4, 70, 0, "(20) 15 65 100 73 100 73 100 73 100 73 100 73 66 67 68 69 100 73 14 69", "ModeB a FNC4 é (5) b c d e FNC4 é; BWIPP different encodation, uses double FNC4 latch then FNC4 escapes" },
203 /* 23*/ { UNICODE_MODE, -1, "123456789012345678901234", -1, 0, 3, 70, 1, "(15) 9 12 34 56 78 90 12 34 56 78 90 12 34 71 42", "3 rows" },
204 /* 24*/ { UNICODE_MODE, 2, "123456789012345678901234", -1, 0, 3, 70, 0, "(15) 9 12 34 56 78 90 12 34 56 78 90 12 34 71 42", "Min 2 rows (no change); BWIPP doesn't support min rows < required" },
205 /* 25*/ { UNICODE_MODE, 3, "123456789012345678901234", -1, 0, 3, 70, 1, "(15) 9 12 34 56 78 90 12 34 56 78 90 12 34 71 42", "Min 3 rows (no change)" },
206 /* 26*/ { UNICODE_MODE, 4, "123456789012345678901234", -1, 0, 4, 70, 1, "(20) 16 12 34 56 78 90 12 34 56 78 90 12 34 103 103 103 103 103 66 96", "Min 4 rows" },
207 /* 27*/ { UNICODE_MODE, 5, "123456789012345678901234", -1, 0, 5, 70, 1, "(25) 23 12 34 56 78 90 12 34 56 78 90 12 34 103 103 103 103 103 103 103 103 103 103 68 61", "Min 5 rows" },
208 /* 28*/ { UNICODE_MODE, 16, "123456789012345678901234", -1, 0, 16, 70, 1, "(80) 100 12 34 56 78 90 12 34 56 78 90 12 34 103 103 103 103 103 103 103 103 103 103 103", "Min 16 rows" },
209 /* 29*/ { UNICODE_MODE, 1, "123456789012345678901234", -1, ZINT_ERROR_INVALID_OPTION, -1, -1, 1, "Error 424: Minimum number of rows '1' out of range (2 to 16)", "" },
210 /* 30*/ { UNICODE_MODE, 17, "123456789012345678901234", -1, ZINT_ERROR_INVALID_OPTION, -1, -1, 1, "Error 424: Minimum number of rows '17' out of range (2 to 16)", "" },
211 /* 31*/ { UNICODE_MODE, -1, "ÁÁÁÁÁÁ99999999999999Á", -1, 0, 6, 70, 0, "(30) 29 100 33 100 33 100 33 100 33 100 33 100 33 99 99 99 99 99 99 99 99 100 100 33 103", "BWIPP different encodation, uses double FNC4 latch" },
212 /* 32*/ { UNICODE_MODE, -1, "ÿ\012àa\0121\012àAà", -1, 0, 4, 70, 0, "(20) 15 100 95 98 74 100 64 65 98 74 17 98 74 100 64 33 100 64 47 35", "BWIPP different encodation, uses CodeA instead of 1SA" },
213 /* 33*/ { UNICODE_MODE, -1, "ÿ\012àa\0121\012àAà\012à", -1, 0, 5, 70, 0, "(25) 22 100 95 98 74 100 64 65 98 74 17 98 74 100 64 33 100 64 98 74 100 64 103 89 18", "BWIPP different encodation, uses CodeA instead of 1SA" },
214 /* 34*/ { UNICODE_MODE, -1, "y1234\012àa\0121\0127890àAàDà\012à", -1, 0, 7, 70, 0, "(35) 40 89 12 34 101 74 100 100 64 65 98 74 17 98 74 99 78 90 100 100 64 33 100 64 36 100", "BWIPP different encodation, uses Sh2B + other differences" },
215 /* 35*/ { UNICODE_MODE, -1, "ÿ1234\012àa\0121\0127890àAàDà\012à", -1, 0, 7, 70, 0, "(35) 41 100 95 12 34 101 74 100 100 64 65 98 74 17 98 74 99 78 90 100 100 64 33 100 64 36", "BWIPP different encodation" },
216 /* 36*/ { UNICODE_MODE, -1, "yÿ1234\012àa\0121\0127890àAàDà\012à", -1, 0, 8, 70, 0, "(40) 43 89 100 95 99 12 34 101 74 100 100 64 65 98 74 17 98 74 99 78 90 100 100 64 33 100", "BWIPP different encodation (and fits in 7 rows)" },
217 /* 37*/ { UNICODE_MODE, -1, "ÿy1234\012àa\0121\0127890àAàDà\012à", -1, 0, 8, 70, 0, "(40) 43 100 95 89 99 12 34 101 74 100 100 64 65 98 74 17 98 74 99 78 90 100 100 64 33 100", "BWIPP different encodation (and fits in 7 rows)" },
218 /* 38*/ { UNICODE_MODE, -1, "ÿÿ1234\012àa\0121\0127890àAàDà\012à", -1, 0, 8, 70, 0, "(40) 43 100 95 100 95 99 12 34 101 74 100 100 64 65 98 74 17 98 74 99 78 90 100 100 64 33", "BWIPP different encodation (and fits in 7 rows)" },
219 /* 39*/ { UNICODE_MODE, -1, "ÿ1234\012àa\0121\01223456\0127890àAàDà\012à", -1, 0, 8, 70, 0, "(40) 48 100 95 12 34 101 74 100 100 64 65 98 74 17 98 74 18 99 34 56 101 74 99 78 90 100", "BWIPP different encodation, uses Sh2B + other differences" },
220 /* 40*/ { UNICODE_MODE, -1, "ÿ1234\012à1234a\0121\01223456\0127890àAàDà\012à", -1, 0, 9, 70, 0, "(45) 55 100 95 12 34 101 74 101 98 64 99 12 34 100 65 98 74 17 98 74 18 99 34 56 101 74 99", "BWIPP different encodation, uses Sh2C + other differences" },
221 /* 41*/ { UNICODE_MODE, -1, "ÿ1234\012à1234ab\0121\01223456\012\0127890àAàBCDà\012\012à", -1, 0, 10, 70, 0, "(50) 62 100 95 12 34 101 74 101 98 64 99 12 34 100 65 66 98 74 17 98 74 18 99 34 56 101 74", "BWIPP different encodation, uses Sh2C + other differences" },
222 /* 42*/ { UNICODE_MODE, -1, "ÿ123456\012à123456abcd\0121\01223456\012\0127890àAàBCDEà\012\012à", -1, 0, 11, 70, 0, "(55) 69 100 95 12 34 56 101 74 101 98 64 99 12 34 56 100 65 66 67 68 98 74 17 98 74 18 99", "BWIPP different encodation, uses Sh3C + other differences" },
223 /* 43*/ { UNICODE_MODE, -1, "ÿ12345678\012à12345678abcdef\0121\01223456\012\0127890àAàBCDEFà\012\012à", -1, 0, 12, 70, 0, "(60) 76 100 95 12 34 56 78 101 74 101 98 64 99 12 34 56 78 100 65 66 67 68 69 70 98 74 17", "BWIPP different encodation, uses Sh2C + other differences" },
224 };
225 const int data_size = ARRAY_SIZE(data);
226 int i, length, ret;
227 struct zint_symbol *symbol = NULL;
228
229 char escaped[1024];
230 char cmp_buf[8192];
231 char cmp_msg[1024];
232
233 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
234 int do_zxingcpp = (debug & ZINT_DEBUG_TEST_ZXINGCPP) && testUtilHaveZXingCPPDecoder(); /* Only do ZXing-C++ test if asked, too slow otherwise */
235
236 testStartSymbol("test_input", &symbol);
237
238 for (i = 0; i < data_size; i++) {
239
240 if (testContinue(p_ctx, i)) continue;
241
242 symbol = ZBarcode_Create();
243 assert_nonnull(symbol, "Symbol not created\n");
244
245 symbol->debug = ZINT_DEBUG_TEST; /* Needed to get codeword dump in errtxt */
246
247 length = testUtilSetSymbol(symbol, BARCODE_CODE16K, data[i].input_mode, -1 /*eci*/, data[i].option_1, -1, -1, -1 /*output_options*/, data[i].data, data[i].length, 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
252 if (p_ctx->generate) {
253 printf(" /*%3d*/ { %s, %d, \"%s\", %d, %s, %d, %d, %d, \"%s\", \"%s\" },\n",
254 i, testUtilInputModeName(data[i].input_mode), data[i].option_1,
255 testUtilEscape(data[i].data, length, escaped, sizeof(escaped)), data[i].length,
256 testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].bwipp_cmp, symbol->errtxt, data[i].comment);
257 } else {
258 assert_zero(strcmp(symbol->errtxt, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected);
259 if (ret < ZINT_ERROR) {
260 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);
261 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);
262
263 if (do_bwipp && testUtilCanBwipp(i, symbol, data[i].option_1, -1, -1, debug)) {
264 if (!data[i].bwipp_cmp) {
265 if (debug & ZINT_DEBUG_TEST_PRINT) printf("i:%d %s not BWIPP compatible (%s)\n", i, testUtilBarcodeName(symbol->symbology), data[i].comment);
266 } else {
267 char modules_dump[4096];
268 assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
269 ret = testUtilBwipp(i, symbol, data[i].option_1, -1, -1, data[i].data, length, NULL, cmp_buf, sizeof(cmp_buf), NULL);
270 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
271
272 ret = testUtilBwippCmp(symbol, cmp_msg, cmp_buf, modules_dump);
273 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
274 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_buf, modules_dump);
275 }
276 }
277 if (do_zxingcpp && testUtilCanZXingCPP(i, symbol, data[i].data, length, debug)) {
278 int cmp_len, ret_len;
279 char modules_dump[4096];
280 assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
281 ret = testUtilZXingCPP(i, symbol, data[i].data, length, modules_dump, cmp_buf, sizeof(cmp_buf), &cmp_len);
282 assert_zero(ret, "i:%d %s testUtilZXingCPP ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
283
284 ret = testUtilZXingCPPCmp(symbol, cmp_msg, cmp_buf, cmp_len, data[i].data, length, NULL /*primary*/, escaped, &ret_len);
285 assert_zero(ret, "i:%d %s testUtilZXingCPPCmp %d != 0 %s\n actual: %.*s\nexpected: %.*s\n",
286 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_len, cmp_buf, ret_len, escaped);
287 }
288 }
289 }
290
291 ZBarcode_Delete(symbol);
292 }
293
294 testFinish();
295 }
296
297 static void test_encode(const testCtx *const p_ctx) {
298 int debug = p_ctx->debug;
299
300 struct item {
301 int input_mode;
302 int option_1;
303 char *data;
304 int ret;
305
306 int expected_rows;
307 int expected_width;
308 char *comment;
309 char *expected;
310 };
311 static const struct item data[] = {
312 /* 0*/ { UNICODE_MODE, -1, "ab0123456789", 0, 2, 70, "BS EN 12323:2005 Figure 3",
313 "1110010101100110111011010011110110111100100110010011000100100010001101"
314 "1100110101000100111011110100110010010000100110100011010010001110011001"
315 },
316 /* 1*/ { UNICODE_MODE, -1, "www.wikipedia.de", 0, 4, 70, "https://commons.wikimedia.org/wiki/File:Code_16K_wikipedia.png",
317 "1110010101000110011000011010110000110101100001101011011001100010001101"
318 "1100110100001101011011110010110011110110101111001011010110000110011001"
319 "1101100101001101111011110110010111100101101101001111011001100010010011"
320 "1000010101111011001010011011110010111101101100001011010001001110111101"
321 },
322 /* 2*/ { UNICODE_MODE, -1, "12345678901234567890123456789012", 0, 4, 70, "",
323 "1110010101100010011010011000110111010011100011101001001111010110001101"
324 "1100110100100001001010011000110111010011100011101001001111010110011001"
325 "1101100100100001001010011000110111010011100011101001001111010110010011"
326 "1000010100100001001010011000110010111101100001011101000111010010111101"
327 },
328 /* 3*/ { UNICODE_MODE, 5, "12345678901234567890123456789012", 0, 5, 70, "Min 5 rows",
329 "1110010100010010001010011000110111010011100011101001001111010110001101"
330 "1100110100100001001010011000110111010011100011101001001111010110011001"
331 "1101100100100001001010011000110111010011100011101001001111010110010011"
332 "1000010100100001001010011000110010111101100101111011001011110110111101"
333 "1011100100101111011001011110110010111101101000010001011110011010100011"
334 },
335 /* 4*/ { UNICODE_MODE, 16, "12345678901234567890123456789012", 0, 16, 70, "Min 16 rows",
336 "1110010101000010001010011000110111010011100011101001001111010110001101"
337 "1100110100100001001010011000110111010011100011101001001111010110011001"
338 "1101100100100001001010011000110111010011100011101001001111010110010011"
339 "1000010100100001001010011000110010111101100101111011001011110110111101"
340 "1011100100101111011001011110110010111101100101111011001011110110100011"
341 "1001110100101111011001011110110010111101100101111011001011110110110001"
342 "1010000100101111011001011110110010111101100101111011001011110110101111"
343 "1110100100101111011001011110110010111101100101111011001011110110001011"
344 "1110010100101111011001011110110010111101100101111011001011110110100011"
345 "1100110100101111011001011110110010111101100101111011001011110110110001"
346 "1101100100101111011001011110110010111101100101111011001011110110101111"
347 "1000010100101111011001011110110010111101100101111011001011110110001011"
348 "1011100100101111011001011110110010111101100101111011001011110110001101"
349 "1001110100101111011001011110110010111101100101111011001011110110011001"
350 "1010000100101111011001011110110010111101100101111011001011110110010011"
351 "1110100100101111011001011110110010111101100101110001001110010010111101"
352 },
353 };
354 const int data_size = ARRAY_SIZE(data);
355 int i, length, ret;
356 struct zint_symbol *symbol = NULL;
357
358 char escaped[1024];
359 char cmp_buf[8192];
360 char cmp_msg[1024];
361
362 int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
363 int do_zxingcpp = (debug & ZINT_DEBUG_TEST_ZXINGCPP) && testUtilHaveZXingCPPDecoder(); /* Only do ZXing-C++ test if asked, too slow otherwise */
364
365 testStartSymbol("test_encode", &symbol);
366
367 for (i = 0; i < data_size; i++) {
368
369 if (testContinue(p_ctx, i)) continue;
370
371 symbol = ZBarcode_Create();
372 assert_nonnull(symbol, "Symbol not created\n");
373
374 length = testUtilSetSymbol(symbol, BARCODE_CODE16K, data[i].input_mode, -1 /*eci*/, data[i].option_1, -1, -1, -1 /*output_options*/, data[i].data, -1, debug);
375
376 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
377 assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
378
379 if (p_ctx->generate) {
380 printf(" /*%3d*/ { %s, %d, \"%s\", %s, %d, %d, \"%s\",\n",
381 i, testUtilInputModeName(data[i].input_mode), data[i].option_1,
382 testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
383 testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].comment);
384 testUtilModulesPrint(symbol, " ", "\n");
385 printf(" },\n");
386 } else {
387 if (ret < ZINT_ERROR) {
388 int width, row;
389
390 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);
391 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);
392
393 ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
394 assert_zero(ret, "i:%d testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, ret, width, row, data[i].data);
395
396 if (do_bwipp && testUtilCanBwipp(i, symbol, data[i].option_1, -1, -1, debug)) {
397 ret = testUtilBwipp(i, symbol, data[i].option_1, -1, -1, data[i].data, length, NULL, cmp_buf, sizeof(cmp_buf), NULL);
398 assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
399
400 ret = testUtilBwippCmp(symbol, cmp_msg, cmp_buf, data[i].expected);
401 assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
402 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_buf, data[i].expected);
403 }
404 if (do_zxingcpp && testUtilCanZXingCPP(i, symbol, data[i].data, length, debug)) {
405 int cmp_len, ret_len;
406 char modules_dump[4096];
407 assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
408 ret = testUtilZXingCPP(i, symbol, data[i].data, length, modules_dump, cmp_buf, sizeof(cmp_buf), &cmp_len);
409 assert_zero(ret, "i:%d %s testUtilZXingCPP ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
410
411 ret = testUtilZXingCPPCmp(symbol, cmp_msg, cmp_buf, cmp_len, data[i].data, length, NULL /*primary*/, escaped, &ret_len);
412 assert_zero(ret, "i:%d %s testUtilZXingCPPCmp %d != 0 %s\n actual: %.*s\nexpected: %.*s\n",
413 i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_len, cmp_buf, ret_len, escaped);
414 }
415 }
416 }
417
418 ZBarcode_Delete(symbol);
419 }
420
421 testFinish();
422 }
423
424 int main(int argc, char *argv[]) {
425
426 testFunction funcs[] = { /* name, func */
427 { "test_large", test_large },
428 { "test_reader_init", test_reader_init },
429 { "test_input", test_input },
430 { "test_encode", test_encode },
431 };
432
433 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
434
435 testReport();
436
437 return 0;
438 }
439
440 /* vim: set ts=4 sw=4 et : */