mirror of
https://github.com/zint/zint
synced 2024-11-16 20:57:25 +13:00
189 lines
8.0 KiB
C
189 lines
8.0 KiB
C
|
/*
|
||
|
libzint - the open source barcode library
|
||
|
Copyright (C) 2022 Robin Stuart <rstuart114@gmail.com>
|
||
|
|
||
|
Redistribution and use in source and binary forms, with or without
|
||
|
modification, are permitted provided that the following conditions
|
||
|
are met:
|
||
|
|
||
|
1. Redistributions of source code must retain the above copyright
|
||
|
notice, this list of conditions and the following disclaimer.
|
||
|
2. Redistributions in binary form must reproduce the above copyright
|
||
|
notice, this list of conditions and the following disclaimer in the
|
||
|
documentation and/or other materials provided with the distribution.
|
||
|
3. Neither the name of the project nor the names of its contributors
|
||
|
may be used to endorse or promote products derived from this software
|
||
|
without specific prior written permission.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
|
SUCH DAMAGE.
|
||
|
*/
|
||
|
/* SPDX-License-Identifier: BSD-3-Clause */
|
||
|
|
||
|
#include "testcommon.h"
|
||
|
|
||
|
static void test_input(int index, int generate, int debug) {
|
||
|
|
||
|
struct item {
|
||
|
int input_mode;
|
||
|
int option_1;
|
||
|
int option_2;
|
||
|
char *data;
|
||
|
int length;
|
||
|
int ret;
|
||
|
int expected_rows;
|
||
|
int expected_width;
|
||
|
char *expected;
|
||
|
char *comment;
|
||
|
};
|
||
|
struct item data[] = {
|
||
|
/* 0*/ { UNICODE_MODE, -1, -1, "123456", -1, ZINT_ERROR_TOO_LONG, 0, 0, "Error nan: Input wrong length (should be between 7 and 18 characters)", "" },
|
||
|
};
|
||
|
int data_size = ARRAY_SIZE(data);
|
||
|
int i, length, ret;
|
||
|
struct zint_symbol *symbol;
|
||
|
|
||
|
char escaped[1024];
|
||
|
|
||
|
testStart("test_input");
|
||
|
|
||
|
for (i = 0; i < data_size; i++) {
|
||
|
|
||
|
if (index != -1 && i != index) continue;
|
||
|
|
||
|
symbol = ZBarcode_Create();
|
||
|
assert_nonnull(symbol, "Symbol not created\n");
|
||
|
|
||
|
//symbol->debug = ZINT_DEBUG_TEST; // Needed to get codeword dump in errtxt
|
||
|
|
||
|
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);
|
||
|
|
||
|
ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
|
||
|
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
|
||
|
|
||
|
if (generate) {
|
||
|
printf(" /*%3d*/ { %s, %d, %d, \"%s\", %d, %s, %d, %d, \"%s\", \"%s\" },\n",
|
||
|
i, testUtilInputModeName(data[i].input_mode), data[i].option_1, data[i].option_2,
|
||
|
testUtilEscape(data[i].data, length, escaped, sizeof(escaped)), data[i].length,
|
||
|
testUtilErrorName(data[i].ret), symbol->rows, symbol->width, symbol->errtxt, data[i].comment);
|
||
|
} else {
|
||
|
if (ret < ZINT_ERROR) {
|
||
|
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);
|
||
|
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);
|
||
|
assert_zero(strcmp((char *) symbol->errtxt, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ZBarcode_Delete(symbol);
|
||
|
}
|
||
|
|
||
|
testFinish();
|
||
|
}
|
||
|
|
||
|
static void test_encode(int index, int generate, int debug) {
|
||
|
|
||
|
struct item {
|
||
|
int input_mode;
|
||
|
int option_1;
|
||
|
int option_2;
|
||
|
char *data;
|
||
|
int ret;
|
||
|
|
||
|
int expected_rows;
|
||
|
int expected_width;
|
||
|
int bwipp_cmp;
|
||
|
char *comment;
|
||
|
char *expected;
|
||
|
};
|
||
|
struct item data[] = {
|
||
|
/* 0*/ { UNICODE_MODE, -1, -1, "AQ45670", 0, 1, 102, 0, "SEMI T1-95 SPECIFICATION FOR BACK SURFACE BAR CODE; BWIPP TODO",
|
||
|
"100101000100100100010101000100010101000101010000010101001010000101001001000101001000100101010100000101"
|
||
|
},
|
||
|
};
|
||
|
int data_size = ARRAY_SIZE(data);
|
||
|
int i, length, ret;
|
||
|
struct zint_symbol *symbol;
|
||
|
|
||
|
char escaped[1024];
|
||
|
char bwipp_buf[8192];
|
||
|
char bwipp_msg[1024];
|
||
|
|
||
|
int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); // Only do BWIPP test if asked, too slow otherwise
|
||
|
|
||
|
testStart("test_encode");
|
||
|
|
||
|
for (i = 0; i < data_size; i++) {
|
||
|
|
||
|
if (index != -1 && i != index) continue;
|
||
|
|
||
|
symbol = ZBarcode_Create();
|
||
|
assert_nonnull(symbol, "Symbol not created\n");
|
||
|
|
||
|
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);
|
||
|
|
||
|
ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
|
||
|
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
|
||
|
|
||
|
if (generate) {
|
||
|
printf(" /*%3d*/ { %s, %d, %d, \"%s\", %s, %d, %d, %d, \"%s\",\n",
|
||
|
i, testUtilInputModeName(data[i].input_mode), data[i].option_1, data[i].option_2,
|
||
|
testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
|
||
|
testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].bwipp_cmp, data[i].comment);
|
||
|
testUtilModulesPrint(symbol, " ", "\n");
|
||
|
printf(" },\n");
|
||
|
} else {
|
||
|
if (ret < ZINT_ERROR) {
|
||
|
int width, row;
|
||
|
|
||
|
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);
|
||
|
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);
|
||
|
|
||
|
ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
|
||
|
assert_zero(ret, "i:%d testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, ret, width, row, data[i].data);
|
||
|
|
||
|
if (do_bwipp && testUtilCanBwipp(i, symbol, data[i].option_1, -1, -1, debug)) {
|
||
|
if (!data[i].bwipp_cmp) {
|
||
|
if (debug & ZINT_DEBUG_TEST_PRINT) printf("i:%d %s not BWIPP compatible (%s)\n", i, testUtilBarcodeName(symbol->symbology), data[i].comment);
|
||
|
} else {
|
||
|
ret = testUtilBwipp(i, symbol, data[i].option_1, -1, -1, data[i].data, length, NULL, bwipp_buf, sizeof(bwipp_buf), NULL);
|
||
|
assert_zero(ret, "i:%d %s testUtilBwipp ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
|
||
|
|
||
|
ret = testUtilBwippCmp(symbol, bwipp_msg, bwipp_buf, data[i].expected);
|
||
|
assert_zero(ret, "i:%d %s testUtilBwippCmp %d != 0 %s\n actual: %s\nexpected: %s\n",
|
||
|
i, testUtilBarcodeName(symbol->symbology), ret, bwipp_msg, bwipp_buf, data[i].expected);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ZBarcode_Delete(symbol);
|
||
|
}
|
||
|
|
||
|
testFinish();
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
|
||
|
testFunction funcs[] = { /* name, func, has_index, has_generate, has_debug */
|
||
|
{ "test_input", test_input, 1, 1, 1 },
|
||
|
{ "test_encode", test_encode, 1, 1, 1 },
|
||
|
};
|
||
|
|
||
|
testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
|
||
|
|
||
|
testReport();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* vim: set ts=4 sw=4 et : */
|