test_dxfilmedge.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. libzint - the open source barcode library
  3. Copyright (C) 2024 Antoine Merino <antoine.merino.dev@gmail.com>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. Neither the name of the project nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. SUCH DAMAGE.
  26. */
  27. /* SPDX-License-Identifier: BSD-3-Clause */
  28. #include "testcommon.h"
  29. static void test_encode(const testCtx *const p_ctx) {
  30. int debug = p_ctx->debug;
  31. struct item {
  32. int symbology;
  33. int option_2;
  34. char *data;
  35. int ret;
  36. int expected_rows;
  37. int expected_width;
  38. char *comment;
  39. char *expected;
  40. };
  41. static const struct item data[] = {
  42. /* 0*/ { BARCODE_DXFILMEDGE, -1, "79-7", 0, 2, 23, "DX code 1: 79, DX code 2: 7. DX Extract = 1271. DX Full: X1271X (X is any digit)",
  43. "1111101010101010101011110101010011110011100101"
  44. },
  45. /* 1*/ { BARCODE_DXFILMEDGE, -1, "1271", 0, 2, 23, "",
  46. "1111101010101010101011110101010011110011100101"
  47. },
  48. /* 2*/ { BARCODE_DXFILMEDGE, -1, "012710", 0, 2, 23, "",
  49. "1111101010101010101011110101010011110011100101"
  50. },
  51. /* 3*/ { BARCODE_DXFILMEDGE, -1, "112712", 0, 2, 23, "",
  52. "1111101010101010101011110101010011110011100101"
  53. },
  54. /* 4*/ { BARCODE_DXFILMEDGE, -1, "1-0", 0, 2, 23, "Lower limit: DX part 1 = 1, DX part 2 = 0",
  55. "1111101010101010101011110101000000010000010101"
  56. },
  57. /* 5*/ { BARCODE_DXFILMEDGE, -1, "000160", 0, 2, 23, "",
  58. "1111101010101010101011110101000000010000010101"
  59. },
  60. /* 6*/ { BARCODE_DXFILMEDGE, -1, "16", 0, 2, 23, "",
  61. "1111101010101010101011110101000000010000010101"
  62. },
  63. /* 7*/ { BARCODE_DXFILMEDGE, -1, "920479/63A", 0, 2, 31, "Upper limit: DX part 1 = 127, DX part 2 = 15",
  64. "11111010101010101010101010101111010101111111011111111111000101"
  65. },
  66. /* 8*/ { BARCODE_DXFILMEDGE, -1, "127-15/00A", 0, 2, 31, "",
  67. "11111010101010101010101010101111010101111111011111111111000101"
  68. },
  69. /* 9*/ { BARCODE_DXFILMEDGE, -1, "79-7/1", 0, 2, 31, "",
  70. "11111010101010101010101010101111010101001111001110000010010101"
  71. },
  72. /* 10*/ { BARCODE_DXFILMEDGE, -1, "0079-7/001", 0, 2, 31, "Optional behaviour: leading zeros are accepted",
  73. "11111010101010101010101010101111010101001111001110000010010101"
  74. },
  75. /* 11*/ { BARCODE_DXFILMEDGE, -1, "79-7/1", 0, 2, 31, "Frame number",
  76. "11111010101010101010101010101111010101001111001110000010010101"
  77. },
  78. /* 12*/ { BARCODE_DXFILMEDGE, -1, "1271/1", 0, 2, 31, "",
  79. "11111010101010101010101010101111010101001111001110000010010101"
  80. },
  81. /* 13*/ { BARCODE_DXFILMEDGE, -1, "912718/1", 0, 2, 31, "",
  82. "11111010101010101010101010101111010101001111001110000010010101"
  83. },
  84. /* 14*/ { BARCODE_DXFILMEDGE, -1, "79-7/1A", 0, 2, 31, "",
  85. "11111010101010101010101010101111010101001111001110000011000101"
  86. },
  87. /* 15*/ { BARCODE_DXFILMEDGE, -1, "1271/1a", 0, 2, 31, "",
  88. "11111010101010101010101010101111010101001111001110000011000101"
  89. },
  90. /* 16*/ { BARCODE_DXFILMEDGE, -1, "212715/1A", 0, 2, 31, "",
  91. "11111010101010101010101010101111010101001111001110000011000101"
  92. },
  93. /* 17*/ { BARCODE_DXFILMEDGE, -1, "79-7/62", 0, 2, 31, "Special frame numbers",
  94. "11111010101010101010101010101111010101001111001111111100010101"
  95. },
  96. /* 18*/ { BARCODE_DXFILMEDGE, -1, "79-7/S", 0, 2, 31, "",
  97. "11111010101010101010101010101111010101001111001111111100010101"
  98. },
  99. /* 19*/ { BARCODE_DXFILMEDGE, -1, "79-7/x", 0, 2, 31, "",
  100. "11111010101010101010101010101111010101001111001111111100010101"
  101. },
  102. /* 20*/ { BARCODE_DXFILMEDGE, -1, "79-7/62a", 0, 2, 31, "",
  103. "11111010101010101010101010101111010101001111001111111101000101"
  104. },
  105. /* 21*/ { BARCODE_DXFILMEDGE, -1, "79-7/sA", 0, 2, 31, "",
  106. "11111010101010101010101010101111010101001111001111111101000101"
  107. },
  108. /* 22*/ { BARCODE_DXFILMEDGE, -1, "79-7/Xa", 0, 2, 31, "",
  109. "11111010101010101010101010101111010101001111001111111101000101"
  110. },
  111. /* 23*/ { BARCODE_DXFILMEDGE, -1, "79-7/63", 0, 2, 31, "",
  112. "11111010101010101010101010101111010101001111001111111110000101"
  113. },
  114. /* 24*/ { BARCODE_DXFILMEDGE, -1, "79-7/k", 0, 2, 31, "",
  115. "11111010101010101010101010101111010101001111001111111110000101"
  116. },
  117. /* 25*/ { BARCODE_DXFILMEDGE, -1, "79-7/00", 0, 2, 31, "",
  118. "11111010101010101010101010101111010101001111001111111110000101"
  119. },
  120. /* 26*/ { BARCODE_DXFILMEDGE, -1, "79-7/63a", 0, 2, 31, "",
  121. "11111010101010101010101010101111010101001111001111111111010101"
  122. },
  123. /* 27*/ { BARCODE_DXFILMEDGE, -1, "79-7/kA", 0, 2, 31, "",
  124. "11111010101010101010101010101111010101001111001111111111010101"
  125. },
  126. /* 28*/ { BARCODE_DXFILMEDGE, -1, "79-7/00a", 0, 2, 31, "",
  127. "11111010101010101010101010101111010101001111001111111111010101"
  128. },
  129. /* 29*/ { BARCODE_DXFILMEDGE, -1, "79-7/0", 0, 2, 31, "",
  130. "11111010101010101010101010101111010101001111001110000000000101"
  131. },
  132. /* 30*/ { BARCODE_DXFILMEDGE, -1, "79-7/F", 0, 2, 31, "",
  133. "11111010101010101010101010101111010101001111001110000000000101"
  134. },
  135. /* 31*/ { BARCODE_DXFILMEDGE, -1, "79-7/0a", 0, 2, 31, "",
  136. "11111010101010101010101010101111010101001111001110000001010101"
  137. },
  138. /* 32*/ { BARCODE_DXFILMEDGE, -1, "79-7/fA", 0, 2, 31, "",
  139. "11111010101010101010101010101111010101001111001110000001010101"
  140. },
  141. };
  142. const int data_size = ARRAY_SIZE(data);
  143. int i, length, ret;
  144. struct zint_symbol *symbol = NULL;
  145. char escaped[1024];
  146. char cmp_buf[4096];
  147. char cmp_msg[1024];
  148. int do_zxingcpp = (debug & ZINT_DEBUG_TEST_ZXINGCPP) && testUtilHaveZXingCPPDecoder(); /* Only do ZXing-C++ test if asked, too slow otherwise */
  149. testStartSymbol("test_encode", &symbol);
  150. for (i = 0; i < data_size; i++) {
  151. if (testContinue(p_ctx, i)) continue;
  152. symbol = ZBarcode_Create();
  153. assert_nonnull(symbol, "Symbol not created\n");
  154. length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, -1 /*option_1*/, data[i].option_2, -1, -1 /*output_options*/, data[i].data, -1, debug);
  155. ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
  156. assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
  157. if (p_ctx->generate) {
  158. printf(" /*%3d*/ { %s, %d, \"%s\", %s, %d, %d, \"%s\",\n",
  159. i, testUtilBarcodeName(data[i].symbology), data[i].option_2, testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
  160. testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].comment);
  161. testUtilModulesPrint(symbol, " ", "\n");
  162. printf(" },\n");
  163. } else {
  164. if (ret < ZINT_ERROR) {
  165. int width, row;
  166. 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);
  167. 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);
  168. ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row);
  169. assert_zero(ret, "i:%d testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, ret, width, row, data[i].data);
  170. if (do_zxingcpp && testUtilCanZXingCPP(i, symbol, data[i].data, length, debug)) {
  171. int cmp_len, ret_len;
  172. char modules_dump[8192 + 1];
  173. assert_notequal(testUtilModulesDump(symbol, modules_dump, sizeof(modules_dump)), -1, "i:%d testUtilModulesDump == -1\n", i);
  174. ret = testUtilZXingCPP(i, symbol, data[i].data, length, modules_dump, cmp_buf, sizeof(cmp_buf), &cmp_len);
  175. assert_zero(ret, "i:%d %s testUtilZXingCPP ret %d != 0\n", i, testUtilBarcodeName(symbol->symbology), ret);
  176. ret = testUtilZXingCPPCmp(symbol, cmp_msg, cmp_buf, cmp_len, data[i].data, length, NULL /*primary*/, escaped, &ret_len);
  177. assert_zero(ret, "i:%d %s testUtilZXingCPPCmp %d != 0 %s\n actual: %.*s\nexpected: %.*s\n",
  178. i, testUtilBarcodeName(symbol->symbology), ret, cmp_msg, cmp_len, cmp_buf, ret_len, escaped);
  179. }
  180. }
  181. }
  182. ZBarcode_Delete(symbol);
  183. }
  184. testFinish();
  185. }
  186. static void test_input(const testCtx *const p_ctx) {
  187. int debug = p_ctx->debug;
  188. struct item {
  189. int symbology;
  190. int input_mode;
  191. char *data;
  192. int ret;
  193. int expected_rows;
  194. int expected_width;
  195. char *expected_errtxt;
  196. };
  197. /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
  198. static const struct item data[] = {
  199. /* 0*/ { BARCODE_DXFILMEDGE, -1, "79-1/123A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 972: Frame number part length 4 too long (maximum 3)" },
  200. /* 1*/ { BARCODE_DXFILMEDGE, -1, "79-1/1@A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 973: Frame number \"1@A\" is invalid (expected digits, optionally followed by a single \"A\")" },
  201. /* 2*/ { BARCODE_DXFILMEDGE, -1, "012312365", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 974: DX information length 9 too long (maximum 6)" },
  202. /* 3*/ { BARCODE_DXFILMEDGE, -1, "12-", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 977: Wrong format for DX parts 1 and 2 (expected format: XXX-XX, digits)" },
  203. /* 4*/ { BARCODE_DXFILMEDGE, -1, "01234/00A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 980: DX number \"01234\" is incorrect; expected 4 digits (DX extract) or 6 digits (DX full)" },
  204. /* 5*/ { BARCODE_DXFILMEDGE, -1, "0123/0AA", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 983: Frame number \"0AA\" is invalid (expected digits, optionally followed by a single \"A\")" },
  205. /* 6*/ { BARCODE_DXFILMEDGE, -1, "128-0/24", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 978: DX part 1 \"128\" out of range (1 to 127)" },
  206. /* 7*/ { BARCODE_DXFILMEDGE, -1, "127-16", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 979: DX part 2 \"16\" out of range (0 to 15)" },
  207. /* 8*/ { BARCODE_DXFILMEDGE, -1, "79-2/A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 983: Frame number \"A\" is invalid (expected digits, optionally followed by a single \"A\")" },
  208. /* 9*/ { BARCODE_DXFILMEDGE, -1, "79-2/-1", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 984: Frame number \"-1\" out of range (0 to 63)" },
  209. /* 10*/ { BARCODE_DXFILMEDGE, -1, "79-2/64", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 984: Frame number \"64\" out of range (0 to 63)" },
  210. /* 11*/ { BARCODE_DXFILMEDGE, -1, "79-2-1", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 976: The \"-\" is used to separate DX parts 1 and 2, and should be used no more than once" },
  211. /* 12*/ { BARCODE_DXFILMEDGE, -1, "110-2/2B", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 985: Frame number \"2B\" is invalid (expected digits, optionally followed by a single \"A\")" },
  212. /* 13*/ { BARCODE_DXFILMEDGE, -1, "099990/123A", ZINT_ERROR_TOO_LONG, -1, -1, "Error 986: Input length 11 too long (maximum 10)" },
  213. /* 14*/ { BARCODE_DXFILMEDGE, -1, "0123123/1", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 971: DX information length 7 too long (maximum 6)" },
  214. /* 15*/ { BARCODE_DXFILMEDGE, -1, "120481", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 981: DX extract \"2048\" out of range (16 to 2047)" },
  215. /* 16*/ { BARCODE_DXFILMEDGE, -1, "100151", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 981: DX extract \"15\" out of range (16 to 2047)" },
  216. /* 17*/ { BARCODE_DXFILMEDGE, -1, "15", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 981: DX extract \"15\" out of range (16 to 2047)" },
  217. /* 18*/ { BARCODE_DXFILMEDGE, -1, "12-12A", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 975: Invalid character at position 6 in DX info (digits and \"-\" character only)" },
  218. /* 19*/ { BARCODE_DXFILMEDGE, -1, "012X", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 975: Invalid character at position 4 in DX info (digits and \"-\" character only)" },
  219. /* 20*/ { BARCODE_DXFILMEDGE, -1, "110-2/", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 982: Frame number indicator \"/\" at position 6, but frame number is empty" },
  220. /* 21*/ { BARCODE_DXFILMEDGE, -1, "/", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 970: Invalid first character \"/\", DX code should start with a number" },
  221. /* 22*/ { BARCODE_DXFILMEDGE, -1, "-12", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 970: Invalid first character \"-\", DX code should start with a number" },
  222. /* 23*/ { BARCODE_DXFILMEDGE, -1, "X1234X", ZINT_ERROR_INVALID_DATA, -1, -1, "Error 970: Invalid first character \"X\", DX code should start with a number" },
  223. };
  224. const int data_size = ARRAY_SIZE(data);
  225. int i, length, ret;
  226. struct zint_symbol *symbol = NULL;
  227. testStartSymbol("test_input", &symbol);
  228. for (i = 0; i < data_size; i++) {
  229. if (testContinue(p_ctx, i)) continue;
  230. symbol = ZBarcode_Create();
  231. assert_nonnull(symbol, "Symbol not created\n");
  232. length = testUtilSetSymbol(symbol, data[i].symbology, data[i].input_mode, -1 /*eci*/, -1 /*option_1*/, -1 /*option_2*/, -1, -1 /*output_options*/, data[i].data, -1, debug);
  233. ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
  234. assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
  235. assert_equal(symbol->errtxt[0] == '\0', ret == 0, "i:%d symbol->errtxt not %s (%s)\n", i, ret ? "set" : "empty", symbol->errtxt);
  236. assert_zero(strcmp(symbol->errtxt, data[i].expected_errtxt), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected_errtxt);
  237. if (ret < ZINT_ERROR) {
  238. assert_equal(symbol->rows, data[i].expected_rows, "i:%d symbol->rows %d != %d\n", i, symbol->rows, data[i].expected_rows);
  239. assert_equal(symbol->width, data[i].expected_width, "i:%d symbol->width %d != %d\n", i, symbol->width, data[i].expected_width);
  240. }
  241. ZBarcode_Delete(symbol);
  242. }
  243. testFinish();
  244. }
  245. int main(int argc, char *argv[]) {
  246. testFunction funcs[] = { /* name, func */
  247. { "test_input", test_input },
  248. { "test_encode", test_encode },
  249. };
  250. testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
  251. testReport();
  252. return 0;
  253. }
  254. /* vim: set ts=4 sw=4 et : */