1 /*
2 libzint - the open source barcode library
3 Copyright (C) 2020 - 2021 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 /* vim: set ts=4 sw=4 et : */
31
32 #include "testcommon.h"
33 #include <sys/stat.h>
34
35 INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf);
36
test_pixel_plot(int index,int debug)37 static void test_pixel_plot(int index, int debug) {
38
39 struct item {
40 int width;
41 int height;
42 char *pattern;
43 int repeat;
44 int ret;
45 };
46 // s/\/\*[ 0-9]*\*\//\=printf("\/*%3d*\/", line(".") - line("'<"))
47 struct item data[] = {
48 /* 0*/ { 1, 1, "1", 0, 0 },
49 /* 1*/ { 2, 1, "11", 0, 0 },
50 /* 2*/ { 2, 2, "10", 1, 0 },
51 /* 3*/ { 3, 1, "101", 0, 0 },
52 /* 4*/ { 3, 2, "101010", 0, 0 },
53 /* 5*/ { 3, 3, "101010101", 0, 0 },
54 /* 6*/ { 4, 1, "1001", 0, 0 },
55 /* 7*/ { 4, 3, "1001", 1, 0 },
56 /* 8*/ { 5, 1, "10101", 0, 0 },
57 /* 9*/ { 8, 2, "CBMWKRYGGYRKWMBC", 0, 0 },
58 };
59 int data_size = ARRAY_SIZE(data);
60 int i, ret;
61 struct zint_symbol *symbol;
62
63 char *bmp = "out.bmp";
64
65 char data_buf[8 * 2 + 1];
66
67 int have_identify = testUtilHaveIdentify();
68
69 testStart("test_pixel_plot");
70
71 for (i = 0; i < data_size; i++) {
72 int size;
73
74 if (index != -1 && i != index) continue;
75
76 symbol = ZBarcode_Create();
77 assert_nonnull(symbol, "Symbol not created\n");
78
79 strcpy(symbol->outfile, bmp);
80
81 symbol->bitmap_width = data[i].width;
82 symbol->bitmap_height = data[i].height;
83 symbol->debug |= debug;
84
85 size = data[i].width * data[i].height;
86 assert_nonzero(size < (int) sizeof(data_buf), "i:%d bmp_pixel_plot size %d < sizeof(data_buf) %d\n", i, size, (int) sizeof(data_buf));
87
88 if (data[i].repeat) {
89 testUtilStrCpyRepeat(data_buf, data[i].pattern, size);
90 } else {
91 strcpy(data_buf, data[i].pattern);
92 }
93 assert_equal(size, (int) strlen(data_buf), "i:%d bmp_pixel_plot size %d != strlen(data_buf) %d\n", i, size, (int) strlen(data_buf));
94
95 if (*data_buf > '9') {
96 symbol->symbology = BARCODE_ULTRA;
97 }
98
99 symbol->bitmap = (unsigned char *) data_buf;
100
101 ret = bmp_pixel_plot(symbol, (unsigned char *) data_buf);
102 assert_equal(ret, data[i].ret, "i:%d bmp_pixel_plot ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
103
104 if (ret < ZINT_ERROR) {
105 if (have_identify) {
106 ret = testUtilVerifyIdentify(symbol->outfile, debug);
107 assert_zero(ret, "i:%d identify %s ret %d != 0\n", i, symbol->outfile, ret);
108 }
109 if (!(debug & ZINT_DEBUG_TEST_KEEP_OUTFILE)) {
110 assert_zero(remove(symbol->outfile), "i:%d remove(%s) != 0\n", i, symbol->outfile);
111 }
112 } else {
113 if (!(debug & ZINT_DEBUG_TEST_KEEP_OUTFILE)) {
114 (void) remove(symbol->outfile);
115 }
116 }
117
118 symbol->bitmap = NULL;
119
120 ZBarcode_Delete(symbol);
121 }
122
123 testFinish();
124 }
125
test_print(int index,int generate,int debug)126 static void test_print(int index, int generate, int debug) {
127
128 struct item {
129 int symbology;
130 int border_width;
131 int output_options;
132 int whitespace_width;
133 int whitespace_height;
134 int option_1;
135 int option_2;
136 char *fgcolour;
137 char *bgcolour;
138 char *data;
139 char *expected_file;
140 };
141 struct item data[] = {
142 /* 0*/ { BARCODE_PDF417, -1, -1, 5, -1, -1, -1, "147AD0", "FC9630", "123", "pdf417_fg_bg.bmp" },
143 /* 1*/ { BARCODE_ULTRA, -1, -1, 5, -1, -1, -1, "147AD0", "FC9630", "123", "ultracode_fg_bg.bmp" },
144 /* 2*/ { BARCODE_ULTRA, 1, BARCODE_BOX, 1, 1, -1, -1, "147AD0", "FC9630", "123", "ultracode_fg_bg_hvwsp1_box1.bmp" },
145 /* 3*/ { BARCODE_PDF417COMP, -1, -1, 2, 2, -1, -1, "", "", "123", "pdf417comp_hvwsp2.bmp" },
146 };
147 int data_size = ARRAY_SIZE(data);
148 int i, length, ret;
149 struct zint_symbol *symbol;
150
151 const char *data_dir = "/backend/tests/data/bmp";
152 const char *bmp = "out.bmp";
153 char expected_file[4096];
154 char escaped[1024];
155 int escaped_size = 1024;
156
157 int have_identify = testUtilHaveIdentify();
158
159 testStart("test_print");
160
161 if (generate) {
162 char data_dir_path[1024];
163 assert_nonzero(testUtilDataPath(data_dir_path, sizeof(data_dir_path), data_dir, NULL), "testUtilDataPath(%s) == 0\n", data_dir);
164 if (!testUtilDirExists(data_dir_path)) {
165 ret = testUtilMkDir(data_dir_path);
166 assert_zero(ret, "testUtilMkDir(%s) ret %d != 0 (%d: %s)\n", data_dir_path, ret, errno, strerror(errno));
167 }
168 }
169
170 for (i = 0; i < data_size; i++) {
171
172 if (index != -1 && i != index) continue;
173
174 symbol = ZBarcode_Create();
175 assert_nonnull(symbol, "Symbol not created\n");
176
177 length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, data[i].option_1, data[i].option_2, -1, data[i].output_options, data[i].data, -1, debug);
178 if (data[i].border_width != -1) {
179 symbol->border_width = data[i].border_width;
180 }
181 if (data[i].whitespace_width != -1) {
182 symbol->whitespace_width = data[i].whitespace_width;
183 }
184 if (data[i].whitespace_height != -1) {
185 symbol->whitespace_height = data[i].whitespace_height;
186 }
187 if (*data[i].fgcolour) {
188 strcpy(symbol->fgcolour, data[i].fgcolour);
189 }
190 if (*data[i].bgcolour) {
191 strcpy(symbol->bgcolour, data[i].bgcolour);
192 }
193
194 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
195 assert_zero(ret, "i:%d %s ZBarcode_Encode ret %d != 0 %s\n", i, testUtilBarcodeName(data[i].symbology), ret, symbol->errtxt);
196
197 strcpy(symbol->outfile, bmp);
198 ret = ZBarcode_Print(symbol, 0);
199 assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret);
200
201 assert_nonzero(testUtilDataPath(expected_file, sizeof(expected_file), data_dir, data[i].expected_file), "i:%d testUtilDataPath == 0\n", i);
202
203 if (generate) {
204 printf(" /*%3d*/ { %s, %d, %s, %d, %d, %d, %d, \"%s\", \"%s\", \"%s\", \"%s\"},\n",
205 i, testUtilBarcodeName(data[i].symbology), data[i].border_width, testUtilOutputOptionsName(data[i].output_options),
206 data[i].whitespace_width, data[i].whitespace_height,
207 data[i].option_1, data[i].option_2, data[i].fgcolour, data[i].bgcolour,
208 testUtilEscape(data[i].data, length, escaped, escaped_size), data[i].expected_file);
209 ret = testUtilRename(symbol->outfile, expected_file);
210 assert_zero(ret, "i:%d testUtilRename(%s, %s) ret %d != 0 (%d: %s)\n", i, symbol->outfile, expected_file, ret, errno, strerror(errno));
211 if (have_identify) {
212 ret = testUtilVerifyIdentify(expected_file, debug);
213 assert_zero(ret, "i:%d %s identify %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), expected_file, ret);
214 }
215 } else {
216 assert_nonzero(testUtilExists(symbol->outfile), "i:%d testUtilExists(%s) == 0\n", i, symbol->outfile);
217 assert_nonzero(testUtilExists(expected_file), "i:%d testUtilExists(%s) == 0\n", i, expected_file);
218
219 ret = testUtilCmpBins(symbol->outfile, expected_file);
220 assert_zero(ret, "i:%d %s testUtilCmpBins(%s, %s) %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, expected_file, ret);
221 assert_zero(remove(symbol->outfile), "i:%d remove(%s) != 0\n", i, symbol->outfile);
222 }
223
224 ZBarcode_Delete(symbol);
225 }
226
227 testFinish();
228 }
229
test_outfile(void)230 static void test_outfile(void) {
231 int ret;
232 struct zint_symbol symbol = {0};
233 unsigned char data[] = { "1" };
234
235 testStart("test_outfile");
236
237 symbol.symbology = BARCODE_CODE128;
238 symbol.bitmap = data;
239 symbol.bitmap_width = symbol.bitmap_height = 1;
240
241 strcpy(symbol.outfile, "nosuch_dir/out.bmp");
242
243 ret = bmp_pixel_plot(&symbol, data);
244 assert_equal(ret, ZINT_ERROR_FILE_ACCESS, "bmp_pixel_plot ret %d != ZINT_ERROR_FILE_ACCESS (%d) (%s)\n", ret, ZINT_ERROR_FILE_ACCESS, symbol.errtxt);
245
246 symbol.output_options |= BARCODE_STDOUT;
247
248 ret = bmp_pixel_plot(&symbol, data);
249 printf(" - ignore (BMP to stdout)\n"); fflush(stdout);
250 assert_zero(ret, "bmp_pixel_plot ret %d != 0 (%s)\n", ret, symbol.errtxt);
251
252 testFinish();
253 }
254
main(int argc,char * argv[])255 int main(int argc, char *argv[]) {
256
257 testFunction funcs[] = { /* name, func, has_index, has_generate, has_debug */
258 { "test_pixel_plot", test_pixel_plot, 1, 0, 1 },
259 { "test_print", test_print, 1, 1, 1 },
260 { "test_outfile", test_outfile, 0, 0, 0 },
261 };
262
263 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
264
265 testReport();
266
267 return 0;
268 }
269