test_big5.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. libzint - the open source barcode library
  3. Copyright (C) 2021-2022 Robin Stuart <rstuart114@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. #include "test_big5_tab.h"
  30. /* For local "private" testing using previous libiconv adaptation, not included for licensing reasons */
  31. #if 0
  32. #define TEST_JUST_SAY_GNO
  33. #endif
  34. #ifdef TEST_JUST_SAY_GNO
  35. #include "../just_say_gno/big5_gnu.h"
  36. #endif
  37. INTERNAL int u_big5_test(const unsigned int u, unsigned char *dest);
  38. /* Version of `u_big5()` taking unsigned int destination for backward-compatible testing */
  39. static int u_big5_int(unsigned int u, unsigned int *d) {
  40. unsigned char dest[2];
  41. int ret = u_big5_test(u, dest);
  42. if (ret) {
  43. *d = ret == 1 ? dest[0] : ((dest[0] << 8) | dest[1]);
  44. }
  45. return ret;
  46. }
  47. /* As control convert to Big5 using simple table generated from
  48. https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/OTHER/BIG5.TXT plus simple processing */
  49. static int u_big5_int2(unsigned int u, unsigned int *dest) {
  50. int tab_length = ARRAY_SIZE(test_big5_tab);
  51. int start_i = test_big5_tab_ind[u >> 10];
  52. int end_i = start_i + 0x800 > tab_length ? tab_length : start_i + 0x800;
  53. int i;
  54. if (u < 0x80) {
  55. *dest = u;
  56. return 1;
  57. }
  58. for (i = start_i; i < end_i; i += 2) {
  59. if (test_big5_tab[i + 1] == u) {
  60. *dest = test_big5_tab[i];
  61. return *dest > 0xFF ? 2 : 1;
  62. }
  63. }
  64. return 0;
  65. }
  66. #include <time.h>
  67. #define TEST_PERF_TIME(arg) (((arg) * 1000.0) / CLOCKS_PER_SEC)
  68. #define TEST_PERF_RATIO(a1, a2) (a2 ? TEST_PERF_TIME(a1) / TEST_PERF_TIME(a2) : 0)
  69. #ifdef TEST_JUST_SAY_GNO
  70. #define TEST_INT_PERF_ITERATIONS 100
  71. #endif
  72. static void test_u_big5_int(const testCtx *const p_ctx) {
  73. int debug = p_ctx->debug;
  74. unsigned int i;
  75. int ret, ret2;
  76. unsigned int val, val2;
  77. #ifdef TEST_JUST_SAY_GNO
  78. int j;
  79. clock_t start;
  80. clock_t total = 0, total_gno = 0;
  81. #else
  82. (void)debug;
  83. #endif
  84. testStart("test_u_big5_int");
  85. #ifdef TEST_JUST_SAY_GNO
  86. if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  87. printf("test_u_big5_int perf iterations: %d\n", TEST_INT_PERF_ITERATIONS);
  88. }
  89. #endif
  90. for (i = 0; i < 0xFFFE; i++) {
  91. if (i >= 0xD800 && i < 0xE000) { /* UTF-16 surrogates */
  92. continue;
  93. }
  94. if (testContinue(p_ctx, i)) continue;
  95. val = val2 = 0;
  96. ret = u_big5_int(i, &val);
  97. ret2 = u_big5_int2(i, &val2);
  98. assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", (int) i, i, ret, ret2, val, val2);
  99. if (ret2) {
  100. assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
  101. }
  102. #ifdef TEST_JUST_SAY_GNO
  103. if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  104. val2 = 0;
  105. ret2 = big5_wctomb_zint(&val2, i);
  106. } else {
  107. for (j = 0; j < TEST_INT_PERF_ITERATIONS; j++) {
  108. val = val2 = 0;
  109. start = clock();
  110. ret = u_big5_int(i, &val);
  111. total += clock() - start;
  112. start = clock();
  113. ret2 = big5_wctomb_zint(&val2, i);
  114. total_gno += clock() - start;
  115. }
  116. }
  117. assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", (int) i, i, ret, ret2, val, val2);
  118. if (ret2) {
  119. assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
  120. }
  121. #endif
  122. }
  123. #ifdef TEST_JUST_SAY_GNO
  124. if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  125. printf("test_u_big5_int perf totals: new % 8gms, gno % 8gms ratio %g\n",
  126. TEST_PERF_TIME(total), TEST_PERF_TIME(total_gno), TEST_PERF_RATIO(total, total_gno));
  127. }
  128. #endif
  129. testFinish();
  130. }
  131. /* Convert UTF-8 string to Big5 and place in array of ints */
  132. static int big5_utf8(struct zint_symbol *symbol, const unsigned char source[], int *p_length,
  133. unsigned int *b5data) {
  134. int error_number;
  135. unsigned int i, length;
  136. unsigned int *utfdata = (unsigned int *) z_alloca(sizeof(unsigned int) * (*p_length + 1));
  137. error_number = utf8_to_unicode(symbol, source, utfdata, p_length, 0 /*disallow_4byte*/);
  138. if (error_number != 0) {
  139. return error_number;
  140. }
  141. for (i = 0, length = *p_length; i < length; i++) {
  142. if (!u_big5_int(utfdata[i], b5data + i)) {
  143. strcpy(symbol->errtxt, "800: Invalid character in input data");
  144. return ZINT_ERROR_INVALID_DATA;
  145. }
  146. }
  147. return 0;
  148. }
  149. static void test_big5_utf8(const testCtx *const p_ctx) {
  150. struct item {
  151. char *data;
  152. int length;
  153. int ret;
  154. int ret_length;
  155. unsigned int expected_b5data[20];
  156. char *comment;
  157. };
  158. /*
  159. _ U+FF3F fullwidth low line, not in ISO/Win, in Big5 0xA1C4, UTF-8 EFBCBF
  160. ╴ U+2574 drawings box light left, not in ISO/Win, not in original Big5 but in "Big5-2003" as 0xA15A, UTF-8 E295B4
  161. */
  162. /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
  163. struct item data[] = {
  164. /* 0*/ { "_", -1, 0, 1, { 0xA1C4 }, "" },
  165. /* 1*/ { "╴", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "" },
  166. };
  167. int data_size = ARRAY_SIZE(data);
  168. int i, length, ret;
  169. struct zint_symbol symbol = {0};
  170. unsigned int b5data[20];
  171. testStart("test_big5_utf8");
  172. for (i = 0; i < data_size; i++) {
  173. int ret_length;
  174. if (testContinue(p_ctx, i)) continue;
  175. length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
  176. ret_length = length;
  177. ret = big5_utf8(&symbol, (unsigned char *) data[i].data, &ret_length, b5data);
  178. assert_equal(ret, data[i].ret, "i:%d ret %d != %d (%s)\n", i, ret, data[i].ret, symbol.errtxt);
  179. if (ret == 0) {
  180. int j;
  181. assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %d != %d\n", i, ret_length, data[i].ret_length);
  182. for (j = 0; j < ret_length; j++) {
  183. assert_equal(b5data[j], data[i].expected_b5data[j], "i:%d b5data[%d] %04X != %04X\n", i, j, b5data[j], data[i].expected_b5data[j]);
  184. }
  185. }
  186. }
  187. testFinish();
  188. }
  189. int main(int argc, char *argv[]) {
  190. testFunction funcs[] = { /* name, func */
  191. { "test_u_big5_int", test_u_big5_int },
  192. { "test_big5_utf8", test_big5_utf8 },
  193. };
  194. testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
  195. testReport();
  196. return 0;
  197. }
  198. /* vim: set ts=4 sw=4 et : */