zint/backend/tests/test_gs1.c

278 lines
17 KiB
C
Raw Normal View History

/*
libzint - the open source barcode library
Copyright (C) 2008-2019 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.
*/
/* vim: set ts=4 sw=4 et : */
#include "testcommon.h"
//#define TEST_GS1_REDUCE_GENERATE_EXPECTED
/*
* Check that EAN128 and RSS_EXP based symbologies reduce GS1 data
*/
static void test_gs1_reduce(void)
{
testStart("");
int ret;
struct item {
int symbology;
int input_mode;
unsigned char* data;
unsigned char* primary;
int ret;
char* comment;
unsigned char* expected;
};
struct item data[] = {
/* 0*/ { BARCODE_EAN128, -1, "12345678901234", "", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" },
/* 1*/ { BARCODE_EAN128, -1, "[01]12345678901234", "", 0, "Input mode ignored",
"11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110010001011000111010111101100011101011"
},
/* 2*/ { BARCODE_EAN128, GS1_MODE, "[01]12345678901234", "", 0, "Input mode ignored",
"11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110010001011000111010111101100011101011"
},
/* 3*/ { BARCODE_EAN128, UNICODE_MODE, "[01]12345678901234", "", 0, "Input mode ignored",
"11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110010001011000111010111101100011101011"
},
/* 4*/ { BARCODE_EAN128_CC, -1, "[01]12345678901234", "[21]1234", 0, "Input mode ignored",
"1101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010000000000000000000000000000000000000000000000"
"1101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010000000000000000000000000000000000000000000000"
"1101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010000000000000000000000000000000000000000000000"
"0010110001100001010001001100100110100110001101110100111000111010010011110101100100001001010011000110111010011100010100001011010000110011100010100"
"1101001110011110101110110011011001011001110010001011000111000101101100001010011011110110101100111001000101100011101011110100101111001100011101011"
},
/* 5*/ { BARCODE_EAN128_CC, GS1_MODE, "[01]12345678901234", "[21]1234", 0, "Input mode ignored",
"1101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010000000000000000000000000000000000000000000000"
"1101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010000000000000000000000000000000000000000000000"
"1101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010000000000000000000000000000000000000000000000"
"0010110001100001010001001100100110100110001101110100111000111010010011110101100100001001010011000110111010011100010100001011010000110011100010100"
"1101001110011110101110110011011001011001110010001011000111000101101100001010011011110110101100111001000101100011101011110100101111001100011101011"
},
/* 6*/ { BARCODE_EAN128_CC, UNICODE_MODE, "[01]12345678901234", "[21]1234", 0, "Input mode ignored",
"1101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010000000000000000000000000000000000000000000000"
"1101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010000000000000000000000000000000000000000000000"
"1101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010000000000000000000000000000000000000000000000"
"0010110001100001010001001100100110100110001101110100111000111010010011110101100100001001010011000110111010011100010100001011010000110011100010100"
"1101001110011110101110110011011001011001110010001011000111000101101100001010011011110110101100111001000101100011101011110100101111001100011101011"
},
/* 7*/ { BARCODE_EAN14, -1, "1234567890123", "", 0, "Input mode ignored",
"11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110011011000110100001100101100011101011"
},
/* 8*/ { BARCODE_EAN14, GS1_MODE, "1234567890123", "", 0, "Input mode ignored",
"11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110011011000110100001100101100011101011"
},
/* 9*/ { BARCODE_EAN14, UNICODE_MODE, "1234567890123", "", 0, "Input mode ignored",
"11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110011011000110100001100101100011101011"
},
/*10*/ { BARCODE_NVE18, -1, "12345678901234567", "", 0, "Input mode ignored",
"110100111001111010111011011001100101100111001000101100011100010110110000101001101111011010110011100100010110001110001011011000010010101101110001100011101011"
},
/*11*/ { BARCODE_NVE18, GS1_MODE, "12345678901234567", "", 0, "Input mode ignored",
"110100111001111010111011011001100101100111001000101100011100010110110000101001101111011010110011100100010110001110001011011000010010101101110001100011101011"
},
/*12*/ { BARCODE_NVE18, UNICODE_MODE, "12345678901234567", "", 0, "Input mode ignored",
"110100111001111010111011011001100101100111001000101100011100010110110000101001101111011010110011100100010110001110001011011000010010101101110001100011101011"
},
/*13*/ { BARCODE_RSS_EXP, -1, "2012", "", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" },
/*14*/ { BARCODE_RSS_EXP, -1, "[20]12", "", 0, "Input mode ignored",
"0101010100000000011011111111000010101011000000010001011111001011100010111100000000101"
},
/*15*/ { BARCODE_RSS_EXP, GS1_MODE, "[20]12", "", 0, "Input mode ignored",
"0101010100000000011011111111000010101011000000010001011111001011100010111100000000101"
},
/*16*/ { BARCODE_RSS_EXP, UNICODE_MODE, "[20]12", "", 0, "Input mode ignored",
"0101010100000000011011111111000010101011000000010001011111001011100010111100000000101"
},
/*17*/ { BARCODE_RSS_EXP_CC, -1, "[20]12", "[21]1234", 0, "Input mode ignored",
"00110110111011010000100000110100110011101100001001110100110000101000110001001001101100000011011000101"
"00110110110011111010001001100110101110010000001001100100100111100101111001110101100100000011001000101"
"00110110100010111110011000010100111101000000101001100110101110111001111001001111011000011011101000101"
"00001010111111111001000000001010010001100100011101101000001101000111010000111111100000000000000000000"
"01010101000000000110111111110000101110011011100010010111110010111000101111000000001010000000000000000"
},
/*18*/ { BARCODE_RSS_EXP_CC, GS1_MODE, "[20]12", "[21]1234", 0, "Input mode ignored",
"00110110111011010000100000110100110011101100001001110100110000101000110001001001101100000011011000101"
"00110110110011111010001001100110101110010000001001100100100111100101111001110101100100000011001000101"
"00110110100010111110011000010100111101000000101001100110101110111001111001001111011000011011101000101"
"00001010111111111001000000001010010001100100011101101000001101000111010000111111100000000000000000000"
"01010101000000000110111111110000101110011011100010010111110010111000101111000000001010000000000000000"
},
/*19*/ { BARCODE_RSS_EXP_CC, UNICODE_MODE, "[20]12", "[21]1234", 0, "Input mode ignored",
"00110110111011010000100000110100110011101100001001110100110000101000110001001001101100000011011000101"
"00110110110011111010001001100110101110010000001001100100100111100101111001110101100100000011001000101"
"00110110100010111110011000010100111101000000101001100110101110111001111001001111011000011011101000101"
"00001010111111111001000000001010010001100100011101101000001101000111010000111111100000000000000000000"
"01010101000000000110111111110000101110011011100010010111110010111000101111000000001010000000000000000"
},
/*20*/ { BARCODE_RSS_EXPSTACK, -1, "12", "", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" },
/*21*/ { BARCODE_RSS_EXPSTACK, -1, "[20]12", "", 0, "Input mode ignored",
"010010000010000101101111111100001010000010000110010101111100101110001011110000000010101111100001011101"
},
/*22*/ { BARCODE_RSS_EXPSTACK, GS1_MODE, "[20]12", "", 0, "Input mode ignored",
"010010000010000101101111111100001010000010000110010101111100101110001011110000000010101111100001011101"
},
/*23*/ { BARCODE_RSS_EXPSTACK, UNICODE_MODE, "[20]12", "", 0, "Input mode ignored",
"010010000010000101101111111100001010000010000110010101111100101110001011110000000010101111100001011101"
},
/*24*/ { BARCODE_RSS_EXPSTACK_CC, -1, "12", "[21]1234", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" },
/*25*/ { BARCODE_RSS_EXPSTACK_CC, -1, "[20]12", "[21]1234", 0, "Input mode ignored",
"001101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010"
"001101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010"
"001101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010"
"000001111111010110010000000010100100111001100001011010000011010001100100001010101001010000011110100000"
"010010000000101001101111111100001011000110011110100101111100101110001011110000000010101111100001011101"
},
/*26*/ { BARCODE_RSS_EXPSTACK_CC, GS1_MODE, "[20]12", "[21]1234", 0, "Input mode ignored",
"001101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010"
"001101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010"
"001101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010"
"000001111111010110010000000010100100111001100001011010000011010001100100001010101001010000011110100000"
"010010000000101001101111111100001011000110011110100101111100101110001011110000000010101111100001011101"
},
/*27*/ { BARCODE_RSS_EXPSTACK_CC, UNICODE_MODE, "[20]12", "[21]1234", 0, "Input mode ignored",
"001101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010"
"001101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010"
"001101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010"
"000001111111010110010000000010100100111001100001011010000011010001100100001010101001010000011110100000"
"010010000000101001101111111100001011000110011110100101111100101110001011110000000010101111100001011101"
},
};
int data_size = sizeof(data) / sizeof(struct item);
char* text;
for (int i = 0; i < data_size; i++) {
struct zint_symbol* symbol = ZBarcode_Create();
assert_nonnull(symbol, "Symbol not created\n");
symbol->symbology = data[i].symbology;
if (data[i].input_mode != -1) {
symbol->input_mode = data[i].input_mode;
}
if (strlen(data[i].primary)) {
text = data[i].primary;
strcpy(symbol->primary, data[i].data);
} else {
text = data[i].data;
}
int length = strlen(text);
ret = ZBarcode_Encode(symbol, text, length);
#ifdef TEST_GS1_REDUCE_GENERATE_EXPECTED
if (data[i].ret == 0) {
printf(" /*%2d*/ { %s, %s, \"%s\", \"%s\", %d, \"%s\",\n",
i, testUtilBarcodeName(data[i].symbology), testUtilInputModeName(data[i].input_mode), data[i].data, data[i].primary, data[i].ret, data[i].comment);
testUtilModulesDump(symbol, " ", "\n");
printf(" },\n");
} else {
printf(" /*%2d*/ { %s, %s, \"%s\", \"%s\", %s, \"%s\", \"\" },\n",
i, testUtilBarcodeName(data[i].symbology), testUtilInputModeName(data[i].input_mode), data[i].data, data[i].primary, testUtilErrorName(data[i].ret), data[i].comment);
}
#else
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d %s\n", i, ret, data[i].ret, symbol->errtxt);
if (ret == 0) {
int width, row;
ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
assert_zero(ret, "i:%d %s testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, testUtilBarcodeName(data[i].symbology), ret, width, row, data[i].data);
}
#endif
ZBarcode_Delete(symbol);
}
testFinish();
}
static void test_hrt(void)
{
testStart("");
int ret;
struct item {
int symbology;
unsigned char* data;
unsigned char* primary;
unsigned char* expected;
};
// s/\/\*[ 0-9]*\*\//\=printf("\/*%2d*\/", line(".") - line("'<"))
struct item data[] = {
/* 0*/ { BARCODE_EAN128, "[01]12345678901234[20]12", "", "(01)12345678901234(20)12" },
/* 1*/ { BARCODE_EAN128_CC, "[01]12345678901234[20]12", "[21]12345", "(01)12345678901234(20)12" },
/* 2*/ { BARCODE_RSS_EXP, "[01]12345678901234[20]12", "", "(01)12345678901234(20)12" },
/* 3*/ { BARCODE_RSS_EXP_CC, "[01]12345678901234", "[21]12345", "(01)12345678901234" },
/* 4*/ { BARCODE_RSS_EXPSTACK, "[20]12", "", "(20)12" },
/* 5*/ { BARCODE_RSS_EXPSTACK_CC, "[20]12", "[21]12345", "(20)12" },
};
int data_size = sizeof(data) / sizeof(struct item);
char* text;
for (int i = 0; i < data_size; i++) {
struct zint_symbol* symbol = ZBarcode_Create();
assert_nonnull(symbol, "Symbol not created\n");
symbol->symbology = data[i].symbology;
if (strlen(data[i].primary)) {
text = data[i].primary;
strcpy(symbol->primary, data[i].data);
} else {
text = data[i].data;
}
int length = strlen(text);
ret = ZBarcode_Encode(symbol, text, length);
assert_zero(ret, "i:%d ZBarcode_Encode ret %d != 0 %s\n", i, ret, symbol->errtxt);
assert_zero(strcmp(symbol->text, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->text, data[i].expected);
ZBarcode_Delete(symbol);
}
testFinish();
}
int main()
{
test_gs1_reduce();
test_hrt();
testReport();
return 0;
}