test_ksx1001.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_ksx1001_tab.h"
  30. #include "../ksx1001.h"
  31. /* For local "private" testing using previous libiconv adaptation, not included for licensing reasons */
  32. #if 0
  33. #define TEST_JUST_SAY_GNO
  34. #endif
  35. #ifdef TEST_JUST_SAY_GNO
  36. #include "../just_say_gno/ksx1001_gnu.h"
  37. #endif
  38. INTERNAL int u_ksx1001_test(const unsigned int u, unsigned char *dest);
  39. /* Version of `u_ksx1001()` taking unsigned int destination for backward-compatible testing */
  40. static int u_ksx1001_int(const unsigned int u, unsigned int *d) {
  41. unsigned char dest[2];
  42. int ret = u_ksx1001_test(u, dest);
  43. if (ret) {
  44. *d = ret == 1 ? dest[0] : ((dest[0] << 8) | dest[1]);
  45. }
  46. return ret;
  47. }
  48. /* As control convert to KS X 1001 using simple table generated from
  49. https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/KSC/KSX1001.TXT plus simple processing
  50. */
  51. static int u_ksx1001_int2(unsigned int u, unsigned int *dest) {
  52. int tab_length, start_i, end_i;
  53. int i;
  54. if (u < 0x80) {
  55. *dest = u;
  56. return 1;
  57. }
  58. if (u == 0x20AC) { /* Euro sign added KS X 1001:1998 */
  59. *dest = 0x2266 + 0x8080;
  60. return 2;
  61. }
  62. if (u == 0xAE) { /* Registered trademark added KS X 1001:1998 */
  63. *dest = 0x2267 + 0x8080;
  64. return 2;
  65. }
  66. if (u == 0x327E) { /* Korean postal code symbol added KS X 1001:2002 */
  67. *dest = 0x2268 + 0x8080;
  68. return 2;
  69. }
  70. tab_length = ARRAY_SIZE(test_ksx1001_tab);
  71. start_i = test_ksx1001_tab_ind[u >> 10];
  72. end_i = start_i + 0x800 > tab_length ? tab_length : start_i + 0x800;
  73. for (i = start_i; i < end_i; i += 2) {
  74. if (test_ksx1001_tab[i + 1] == u) {
  75. *dest = test_ksx1001_tab[i] + 0x8080;
  76. return 2;
  77. }
  78. }
  79. return 0;
  80. }
  81. #include <time.h>
  82. #define TEST_PERF_TIME(arg) (((arg) * 1000.0) / CLOCKS_PER_SEC)
  83. #define TEST_PERF_RATIO(a1, a2) (a2 ? TEST_PERF_TIME(a1) / TEST_PERF_TIME(a2) : 0)
  84. #ifdef TEST_JUST_SAY_GNO
  85. #define TEST_INT_PERF_ITERATIONS 100
  86. #endif
  87. static void test_u_ksx1001_int(const testCtx *const p_ctx) {
  88. int debug = p_ctx->debug;
  89. int ret, ret2;
  90. unsigned int val, val2;
  91. unsigned i;
  92. #ifdef TEST_JUST_SAY_GNO
  93. int j;
  94. clock_t start;
  95. clock_t total = 0, total_gno = 0;
  96. #else
  97. (void)debug;
  98. #endif
  99. testStart("test_u_ksx1001_int");
  100. #ifdef TEST_JUST_SAY_GNO
  101. if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  102. printf("test_u_ksx1001_int perf iterations: %d\n", TEST_INT_PERF_ITERATIONS);
  103. }
  104. #endif
  105. for (i = 0; i < 0xFFFE; i++) {
  106. if (i >= 0xD800 && i <= 0xDFFF) { /* UTF-16 surrogates */
  107. continue;
  108. }
  109. if (testContinue(p_ctx, i)) continue;
  110. val = val2 = 0;
  111. ret = u_ksx1001_int(i, &val);
  112. ret2 = u_ksx1001_int2(i, &val2);
  113. 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);
  114. if (ret2) {
  115. assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
  116. }
  117. #ifdef TEST_JUST_SAY_GNO
  118. if (i >= 0x80) { /* `ksx1001_wctomb_zint()` doesn't handle ASCII */
  119. if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  120. val2 = 0;
  121. ret2 = ksx1001_wctomb_zint(&val2, i);
  122. } else {
  123. for (j = 0; j < TEST_INT_PERF_ITERATIONS; j++) {
  124. val = val2 = 0;
  125. start = clock();
  126. ret = u_ksx1001_int(i, &val);
  127. total += clock() - start;
  128. start = clock();
  129. ret2 = ksx1001_wctomb_zint(&val2, i);
  130. total_gno += clock() - start;
  131. }
  132. }
  133. 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);
  134. if (ret2) {
  135. val2 += 0x8080; /* `ksx1001_wctomb_zint()` returns pure KS X 1001 values, convert to EUC-KR */
  136. assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
  137. }
  138. }
  139. #endif
  140. }
  141. #ifdef TEST_JUST_SAY_GNO
  142. if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  143. printf("test_u_ksx1001_int perf totals: new % 8gms, gno % 8gms ratio %g\n",
  144. TEST_PERF_TIME(total), TEST_PERF_TIME(total_gno), TEST_PERF_RATIO(total, total_gno));
  145. }
  146. #endif
  147. testFinish();
  148. }
  149. int main(int argc, char *argv[]) {
  150. testFunction funcs[] = { /* name, func */
  151. { "test_u_ksx1001_int", test_u_ksx1001_int },
  152. };
  153. testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
  154. testReport();
  155. return 0;
  156. }
  157. /* vim: set ts=4 sw=4 et : */