test_sjis.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. libzint - the open source barcode library
  3. Copyright (C) 2019-2023 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_sjis_tab.h"
  30. #include "../eci.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/sjis_gnu.c"
  37. #endif
  38. INTERNAL int u_sjis_int_test(const unsigned int u, unsigned int *dest);
  39. /* As control convert to Shift JIS using simple table generated from
  40. https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT plus simple processing
  41. */
  42. static int u_sjis_int2(unsigned int u, unsigned int *dest) {
  43. int tab_length, start_i, end_i;
  44. int i;
  45. if (u < 0x20 || u == 0x7F) {
  46. *dest = u;
  47. return 1;
  48. }
  49. /* Shortcut */
  50. if ((u > 0x00F7 && u < 0x0391) || (u > 0x0451 && u < 0x2010) || (u > 0x9FA0 && u < 0xE000) || (u > 0xE757 && u < 0xFF01) || u > 0xFFE5) {
  51. return 0;
  52. }
  53. if (u >= 0xE000 && u <= 0xE757) { /* PUA mappings, not in SHIFTJIS.TXT */
  54. if (u <= 0xE0BB) {
  55. *dest = u - 0xE000 + 0xF040 + (u >= 0xE000 + 0x3F);
  56. } else if (u <= 0xE177) {
  57. *dest = u - 0xE0BC + 0xF140 + (u >= 0xE0BC + 0x3F);
  58. } else if (u <= 0xE233) {
  59. *dest = u - 0xE178 + 0xF240 + (u >= 0xE178 + 0x3F);
  60. } else if (u <= 0xE2EF) {
  61. *dest = u - 0xE234 + 0xF340 + (u >= 0xE234 + 0x3F);
  62. } else if (u <= 0xE3AB) {
  63. *dest = u - 0xE2F0 + 0xF440 + (u >= 0xE2F0 + 0x3F);
  64. } else if (u <= 0xE467) {
  65. *dest = u - 0xE3AC + 0xF540 + (u >= 0xE3AC + 0x3F);
  66. } else if (u <= 0xE523) {
  67. *dest = u - 0xE468 + 0xF640 + (u >= 0xE468 + 0x3F);
  68. } else if (u <= 0xE5DF) {
  69. *dest = u - 0xE524 + 0xF740 + (u >= 0xE524 + 0x3F);
  70. } else if (u <= 0xE69B) {
  71. *dest = u - 0xE5E0 + 0xF840 + (u >= 0xE5E0 + 0x3F);
  72. } else {
  73. *dest = u - 0xE69C + 0xF940 + (u >= 0xE69C + 0x3F);
  74. }
  75. return 2;
  76. }
  77. tab_length = sizeof(test_sjis_tab) / sizeof(unsigned int);
  78. start_i = test_sjis_tab_ind[u >> 10];
  79. end_i = start_i + 0x800 > tab_length ? tab_length : start_i + 0x800;
  80. for (i = start_i; i < end_i; i += 2) {
  81. if (test_sjis_tab[i + 1] == u) {
  82. *dest = test_sjis_tab[i];
  83. return *dest > 0xFF ? 2 : 1;
  84. }
  85. }
  86. return 0;
  87. }
  88. #include <time.h>
  89. #define TEST_PERF_TIME(arg) (((arg) * 1000.0) / CLOCKS_PER_SEC)
  90. #define TEST_PERF_RATIO(a1, a2) (a2 ? TEST_PERF_TIME(a1) / TEST_PERF_TIME(a2) : 0)
  91. #ifdef TEST_JUST_SAY_GNO
  92. #define TEST_INT_PERF_ITERATIONS 100
  93. #endif
  94. static void test_u_sjis_int(const testCtx *const p_ctx) {
  95. int debug = p_ctx->debug;
  96. int ret, ret2;
  97. unsigned int val, val2;
  98. unsigned int i;
  99. #ifdef TEST_JUST_SAY_GNO
  100. int j;
  101. clock_t start;
  102. clock_t total = 0, total_gno = 0;
  103. #else
  104. (void)debug;
  105. #endif
  106. testStart("test_u_sjis_int");
  107. #ifdef TEST_JUST_SAY_GNO
  108. if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  109. printf("test_u_sjis_int perf iterations: %d\n", TEST_INT_PERF_ITERATIONS);
  110. }
  111. #endif
  112. for (i = 0; i < 0xFFFE; i++) {
  113. if (i >= 0xD800 && i <= 0xDFFF) { /* UTF-16 surrogates */
  114. continue;
  115. }
  116. if (testContinue(p_ctx, i)) continue;
  117. val = val2 = 0;
  118. ret = u_sjis_int_test(i, &val);
  119. ret2 = u_sjis_int2(i, &val2);
  120. 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);
  121. if (ret2) {
  122. assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
  123. }
  124. #ifdef TEST_JUST_SAY_GNO
  125. if (i != 0xFF3C) { /* Full-width reverse solidus duplicate no longer mapped to ignore */
  126. if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  127. val2 = 0;
  128. ret2 = sjis_wctomb_zint(&val2, i);
  129. } else {
  130. for (j = 0; j < TEST_INT_PERF_ITERATIONS; j++) {
  131. val = val2 = 0;
  132. start = clock();
  133. ret = u_sjis_int_test(i, &val);
  134. total += clock() - start;
  135. start = clock();
  136. ret2 = sjis_wctomb_zint(&val2, i);
  137. total_gno += clock() - start;
  138. }
  139. }
  140. 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);
  141. if (ret2) {
  142. assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
  143. }
  144. }
  145. #endif
  146. }
  147. #ifdef TEST_JUST_SAY_GNO
  148. if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  149. printf("test_u_sjis_int perf totals: new % 8gms, gno % 8gms ratio %g\n",
  150. TEST_PERF_TIME(total), TEST_PERF_TIME(total_gno), TEST_PERF_RATIO(total, total_gno));
  151. }
  152. #endif
  153. testFinish();
  154. }
  155. static void test_sjis_utf8(const testCtx *const p_ctx) {
  156. struct item {
  157. char *data;
  158. int length;
  159. int ret;
  160. int ret_length;
  161. unsigned int expected_jisdata[20];
  162. char *comment;
  163. };
  164. /*
  165. é U+00E9 in ISO 8859-1 plus other ISO 8859 (but not in ISO 8859-7 or ISO 8859-11), Win 1250 plus other Win, not in Shift JIS, UTF-8 C3A9
  166. β U+03B2 in ISO 8859-7 Greek (but not other ISO 8859 or Win page), in Shift JIS 0x83C0, UTF-8 CEB2
  167. Ж U+0416 in ISO 8859-5 Cyrillic (but not other ISO 8859), Win 1251, in Shift JIS 0x8447, UTF-8 D096
  168. ¥ U+00A5 in ISO 8859-1 0xA5 (\245), in Shift JIS single-byte 0x5C (\134) (backslash); 0xA5 same codepoint as single-byte half-width katakana ・ (U+FF65) in Shift JIS (below), UTF-8 C2A5
  169. ・ U+FF65 half-width katakana, not in ISO/Win, in Shift JIS single-byte 0xA5 (\245), UTF-8 EFBDA5
  170. ソ U+FF7F half-width katakana, not in ISO/Win, in Shift JIS single-byte 0xBF (\277), UTF-8 EFBDBF
  171. ‾ U+203E overline, not in ISO/Win, in Shift JIS single-byte 0x7E (\176) (tilde), UTF-8 E280BE
  172. \ U+FF3C full-width reverse solidus, in Shift JIS 0x815F, was duplicate of mapping of U+005C, UTF-8 EFBCBC
  173. 点 U+70B9 kanji, in Shift JIS 0x935F (\223\137), UTF-8 E782B9
  174. 茗 U+8317 kanji, in Shift JIS 0xE4AA (\344\252), UTF-8 E88C97
  175. テ U+30C6 katakana, in Shift JIS 0x8365 (\203\145), UTF-8 E38386
  176. */
  177. /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
  178. struct item data[] = {
  179. /* 0*/ { "é", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "" },
  180. /* 1*/ { "~", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "" },
  181. /* 2*/ { "β", -1, 0, 1, { 0x83C0 }, "" },
  182. /* 3*/ { "¥", -1, 0, 1, { 0x5C }, "" },
  183. /* 4*/ { "aβcЖ¥・ソ‾\\点茗テ", -1, 0, 12, { 'a', 0x83C0, 'c', 0x8447, 0x5C, 0xA5, 0xBF, 0x7E, 0x815F, 0x935F, 0xE4AA, 0x8365 }, "" },
  184. /* 5*/ { "\", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "U+FF3C full-width reverse solidus no longer duplicate mapping of U+005C" },
  185. /* 6*/ { "\200", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "Invalid UTF-8" },
  186. };
  187. int data_size = ARRAY_SIZE(data);
  188. int i, length, ret;
  189. struct zint_symbol symbol = {0};
  190. unsigned int jisdata[20];
  191. testStart("test_sjis_utf8");
  192. for (i = 0; i < data_size; i++) {
  193. int ret_length;
  194. if (testContinue(p_ctx, i)) continue;
  195. length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
  196. ret_length = length;
  197. ret = sjis_utf8(&symbol, (unsigned char *) data[i].data, &ret_length, jisdata);
  198. assert_equal(ret, data[i].ret, "i:%d ret %d != %d (%s)\n", i, ret, data[i].ret, symbol.errtxt);
  199. if (ret == 0) {
  200. int j;
  201. assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %d != %d\n", i, ret_length, data[i].ret_length);
  202. for (j = 0; j < ret_length; j++) {
  203. assert_equal(jisdata[j], data[i].expected_jisdata[j], "i:%d jisdata[%d] %04X != %04X\n", i, j, jisdata[j], data[i].expected_jisdata[j]);
  204. }
  205. }
  206. }
  207. testFinish();
  208. }
  209. static void test_sjis_utf8_to_eci(const testCtx *const p_ctx) {
  210. struct item {
  211. int eci;
  212. int full_multibyte;
  213. char *data;
  214. int length;
  215. int ret;
  216. int ret_length;
  217. unsigned int expected_jisdata[20];
  218. char *comment;
  219. };
  220. /*
  221. é U+00E9 in ISO 8859-1 0xE9, Win 1250 plus other Win, in QR Kanji mode first byte range 0x81..9F, 0xE0..EB
  222. β U+03B2 in ISO 8859-7 Greek 0xE2 (but not other ISO 8859 or Win page)
  223. ¥ U+00A5 in ISO 8859-1 0xA5, outside first byte range 0x81..9F, 0xE0..EB
  224. ú U+00FA in ISO 8859-1 0xFA, outside first byte range
  225. à U+00EO in ISO 8859-1 0xE0, in first byte range
  226. ë U+00EB in ISO 8859-1 0xEB, in first byte range
  227. ì U+00EC in ISO 8859-1 0xEC, outside first byte range
  228. µ U+00B5 in ISO 8859-1 0xB5, outside first byte range
  229. À U+00C0 in ISO 8859-1 0xC0, outside first byte range and 0xEBxx second byte range
  230. */
  231. /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
  232. struct item data[] = {
  233. /* 0*/ { 3, 0, "é", -1, 0, 1, { 0xE9 }, "" },
  234. /* 1*/ { 3, 1, "é", -1, 0, 1, { 0xE9 }, "" },
  235. /* 2*/ { 3, 0, "β", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "" },
  236. /* 3*/ { 3, 1, "β", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "" },
  237. /* 4*/ { 9, 0, "β", -1, 0, 1, { 0xE2 }, "" },
  238. /* 5*/ { 9, 1, "β", -1, 0, 1, { 0xE2 }, "" },
  239. /* 6*/ { 3, 0, "¥", -1, 0, 1, { 0xA5 }, "" },
  240. /* 7*/ { 3, 1, "¥", -1, 0, 1, { 0xA5 }, "" },
  241. /* 8*/ { 3, 0, "éa", -1, 0, 2, { 0xE9, 0x61 }, "Not full multibyte" },
  242. /* 9*/ { 3, 1, "éa", -1, 0, 1, { 0xE961 }, "In QR Kanji mode range" },
  243. /* 10*/ { 3, 0, "éaúbàcëdìeµ", -1, 0, 11, { 0xE9, 0x61, 0xFA, 0x62, 0xE0, 0x63, 0xEB, 0x64, 0xEC, 0x65, 0xB5 }, "" },
  244. /* 11*/ { 3, 1, "éaúbàcëdìeµ", -1, 0, 8, { 0xE961, 0xFA, 0x62, 0xE063, 0xEB64, 0xEC, 0x65, 0xB5 }, "" },
  245. /* 12*/ { 3, 0, "ëÀ", -1, 0, 2, { 0xEB, 0xC0 }, "Not full multibyte" },
  246. /* 13*/ { 3, 1, "ëÀ", -1, 0, 2, { 0xEB, 0xC0 }, "Outside QR Kanji mode range" },
  247. /* 14*/ { 20, 0, "\\\\", -1, 0, 2, { 0x815F, 0x815F }, "Shift JIS reverse solidus (backslash) mapping from ASCII to double byte" },
  248. /* 15*/ { 20, 1, "\\\\", -1, 0, 2, { 0x815F, 0x815F }, "Shift JIS reverse solidus (backslash) mapping from ASCII to double byte" },
  249. /* 16*/ { 20, 0, "爍", -1, 0, 1, { 0xE0A1 }, "Shift JIS U+720D" },
  250. /* 17*/ { 20, 1, "爍", -1, 0, 1, { 0xE0A1 }, "Shift JIS" },
  251. /* 18*/ { 20, 0, "~", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "ASCII tilde not in Shift JIS" },
  252. /* 19*/ { 25, 0, "12", -1, 0, 4, { 0x00, 0x31, 0x00, 0x32 }, "UCS-2BE ASCII" },
  253. /* 20*/ { 25, 0, "", -1, 0, 4, { 0x00, 0x81, 0x00, 0x81 }, "UCS-2BE U+0081" },
  254. /* 21*/ { 25, 1, "", -1, 0, 4, { 0x00, 0x81, 0x00, 0x81 }, "UCS-2BE outside QR Kanji mode range" },
  255. /* 22*/ { 25, 0, "腀", -1, 0, 2, { 0x81, 0x40 }, "UCS-2BE U+8140" },
  256. /* 23*/ { 25, 1, "腀", -1, 0, 1, { 0x8140 }, "UCS-2BE in QR Kanji mode range" },
  257. /* 24*/ { 28, 0, "¢¢", -1, 0, 4, { 0xA2, 0x46, 0xA2, 0x46 }, "Big5 U+00A2" },
  258. /* 25*/ { 28, 1, "¢¢", -1, 0, 4, { 0xA2, 0x46, 0xA2, 0x46 }, "Big5 outside QR Kanji mode range" },
  259. /* 26*/ { 28, 0, "觡", -1, 0, 2, { 0xE0, 0x40 }, "Big5 U+89E1" },
  260. /* 27*/ { 28, 1, "觡", -1, 0, 1, { 0xE040 }, "Big5 in QR Kanji mode range" },
  261. /* 28*/ { 29, 0, "¨¨", -1, 0, 4, { 0xA1, 0xA7, 0xA1, 0xA7 }, "GB 2312 U+00A8" },
  262. /* 29*/ { 29, 1, "¨¨", -1, 0, 4, { 0xA1, 0xA7, 0xA1, 0xA7 }, "GB 2312 outside QR Kanji mode range" },
  263. /* 30*/ { 29, 0, "崂", -1, 0, 2, { 0xE1, 0xC0 }, "GB 2312 U+5D02" },
  264. /* 31*/ { 29, 0, "釦", -1, ZINT_ERROR_INVALID_DATA, -1, {0}, "GB 18030 U+91E6 not in GB 2312" },
  265. /* 32*/ { 29, 1, "崂", -1, 0, 1, { 0xE1C0 }, "GB 2312 in QR Kanji mode range" },
  266. /* 33*/ { 30, 0, "¡¡", -1, 0, 4, { 0x22 + 0x80, 0x2E + 0x80, 0x22 + 0x80, 0x2E + 0x80 }, "EUC-KR U+00A1 (0xA2AE)" },
  267. /* 34*/ { 30, 1, "¡¡", -1, 0, 4, { 0x22 + 0x80, 0x2E + 0x80, 0x22 + 0x80, 0x2E + 0x80 }, "EUC-KR 0xA2AE outside QR Kanji mode range" },
  268. /* 35*/ { 30, 0, "詰", -1, 0, 2, { 0x7D + 0x80, 0x7E + 0x80 }, "EUC-KR U+8A70 (0xFDFE)" },
  269. /* 36*/ { 30, 1, "詰", -1, 0, 2, { 0x7D + 0x80, 0x7E + 0x80 }, "EUC-KR > 0xEBBF so not in QR Kanji mode range" },
  270. };
  271. int data_size = ARRAY_SIZE(data);
  272. int i, length, ret;
  273. unsigned int jisdata[20];
  274. testStart("test_sjis_utf8_to_eci");
  275. for (i = 0; i < data_size; i++) {
  276. int ret_length;
  277. if (testContinue(p_ctx, i)) continue;
  278. length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
  279. ret_length = length;
  280. ret = sjis_utf8_to_eci(data[i].eci, (unsigned char *) data[i].data, &ret_length, jisdata, data[i].full_multibyte);
  281. assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
  282. if (ret == 0) {
  283. int j;
  284. assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %d != %d\n", i, ret_length, data[i].ret_length);
  285. for (j = 0; j < ret_length; j++) {
  286. assert_equal(jisdata[j], data[i].expected_jisdata[j], "i:%d jisdata[%d] 0x%04X != 0x%04X\n", i, j, jisdata[j], data[i].expected_jisdata[j]);
  287. }
  288. }
  289. }
  290. testFinish();
  291. }
  292. static void test_sjis_cpy(const testCtx *const p_ctx) {
  293. struct item {
  294. int full_multibyte;
  295. char *data;
  296. int length;
  297. int ret;
  298. int ret_length;
  299. unsigned int expected_jisdata[20];
  300. char *comment;
  301. };
  302. /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
  303. struct item data[] = {
  304. /* 0*/ { 0, "\351", -1, 0, 1, { 0xE9 }, "Not full multibyte" },
  305. /* 1*/ { 1, "\351", -1, 0, 1, { 0xE9 }, "In QR Kanji mode first-byte range but only one byte" },
  306. /* 2*/ { 0, "\351\141", -1, 0, 2, { 0xE9, 0x61 }, "Not full multibyte" },
  307. /* 3*/ { 1, "\351\141", -1, 0, 1, { 0xE961 }, "In QR Kanji mode range" },
  308. /* 4*/ { 1, "\201", -1, 0, 1, { 0x81 }, "In QR Kanji mode first-byte range but only one byte" },
  309. /* 5*/ { 0, "\201\141", -1, 0, 2, { 0x81, 0x61 }, "Not full multibyte" },
  310. /* 6*/ { 1, "\201\141", -1, 0, 1, { 0x8161 }, "In QR Kanji mode range" },
  311. /* 7*/ { 0, "\201\077\201\100\237\374\237\375\340\077\340\100\353\277\353\300", -1, 0, 16, { 0x81, 0x3F, 0x81, 0x40, 0x9F, 0xFC, 0x9F, 0xFD, 0xE0, 0x3F, 0xE0, 0x40, 0xEB, 0xBF, 0xEB, 0xC0 }, "" },
  312. /* 8*/ { 1, "\201\077\201\100\237\374\237\375\340\077\340\100\353\277\353\300", -1, 0, 12, { 0x81, 0x3F, 0x8140, 0x9FFC, 0x9F, 0xFD, 0xE0, 0x3F, 0xE040, 0xEBBF, 0xEB, 0xC0 }, "" },
  313. };
  314. int data_size = ARRAY_SIZE(data);
  315. int i, length;
  316. unsigned int jisdata[20];
  317. testStart("test_sjis_cpy");
  318. for (i = 0; i < data_size; i++) {
  319. int ret_length;
  320. int j;
  321. if (testContinue(p_ctx, i)) continue;
  322. length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
  323. ret_length = length;
  324. sjis_cpy((unsigned char *) data[i].data, &ret_length, jisdata, data[i].full_multibyte);
  325. assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %d != %d\n", i, ret_length, data[i].ret_length);
  326. for (j = 0; j < ret_length; j++) {
  327. assert_equal(jisdata[j], data[i].expected_jisdata[j], "i:%d jisdata[%d] %04X != %04X\n", i, j, jisdata[j], data[i].expected_jisdata[j]);
  328. }
  329. }
  330. testFinish();
  331. }
  332. #define TEST_PERF_ITER_MILLES 100
  333. #define TEST_PERF_ITERATIONS (TEST_PERF_ITER_MILLES * 1000)
  334. /* Not a real test, just performance indicator */
  335. static void test_perf(const testCtx *const p_ctx) {
  336. int debug = p_ctx->debug;
  337. struct item {
  338. char *data;
  339. int ret;
  340. char *comment;
  341. };
  342. struct item data[] = {
  343. /* 0*/ { "1234567890", 0, "10 numerics" },
  344. /* 1*/ { "貫やぐ識禁ぱい再2間変字全ノレ没無8裁", 0, "Small mixed" },
  345. /* 2*/ { "貫やぐ識禁ぱい再2間変字全ノレ没無8裁花ほゃ過法ひなご札17能つーびれ投覧マ勝動エヨ額界よみ作皇ナヲニ打題ヌルヲ掲布益フが。入35能ト権話しこを断兆モヘ細情おじ名4減エヘイハ側機はょが意見想ハ業独案ユヲウ患職ヲ平美さ毎放どぽたけ家没べお化富べ町大シ情魚ッでれ一冬すぼめり。", 0, "Bigger mixed" },
  346. /* 3*/ { "貫やぐ識禁ぱい再2間変字全ノレ没無8裁花ほゃ過法ひなご札17能つーびれ投覧マ勝動エヨ額界よみ作皇ナヲニ打題ヌルヲ掲布益フが。入35能ト権話しこを断兆モヘ細情おじ名4減エヘイハ側機はょが意見想ハ業独案ユヲウ患職ヲ平美さ毎放どぽたけ家没べお化富べ町大シ情魚ッでれ一冬すぼめり。社ト可化モマ試音ばじご育青康演ぴぎ権型固スで能麩ぜらもほ河都しちほラ収90作の年要とだむ部動ま者断チ第41一1米索焦茂げむしれ。測フ物使だて目月国スリカハ夏検にいへ児72告物ゆは載核ロアメヱ登輸どべゃ催行アフエハ議歌ワ河倫剖だ。記タケウ因載ヒイホヤ禁3輩彦関トえび肝区勝ワリロ成禁ぼよ界白ウヒキレ中島べせぜい各安うしぽリ覧生テ基一でむしゃ中新トヒキソ声碁スしび起田ア信大未ゅもばち。", 0, "Bigger mixed" },
  347. /* 4*/ { "点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点点", 0, "784 kanji" },
  348. };
  349. int data_size = ARRAY_SIZE(data);
  350. int i, length, ret;
  351. struct zint_symbol symbol = {0};
  352. int ret_length;
  353. #ifdef TEST_JUST_SAY_GNO
  354. int ret_length2;
  355. #endif
  356. unsigned int ddata[8192];
  357. int ret2 = 0;
  358. #ifdef TEST_JUST_SAY_GNO
  359. unsigned int ddata2[8192];
  360. #endif
  361. clock_t start;
  362. clock_t total = 0, total_gno = 0;
  363. clock_t diff, diff_gno;
  364. int comment_max = 0;
  365. if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
  366. return;
  367. }
  368. for (i = 0; i < data_size; i++) if ((int) strlen(data[i].comment) > comment_max) comment_max = (int) strlen(data[i].comment);
  369. printf("Iterations %d\n", TEST_PERF_ITERATIONS);
  370. for (i = 0; i < data_size; i++) {
  371. int j;
  372. if (testContinue(p_ctx, i)) continue;
  373. length = (int) strlen(data[i].data);
  374. diff = diff_gno = 0;
  375. for (j = 0; j < TEST_PERF_ITERATIONS; j++) {
  376. ret_length = length;
  377. start = clock();
  378. ret = sjis_utf8(&symbol, (unsigned char *) data[i].data, &ret_length, ddata);
  379. diff += clock() - start;
  380. #ifdef TEST_JUST_SAY_GNO
  381. ret_length2 = length;
  382. start = clock();
  383. ret2 = sjis_utf8_wctomb(&symbol, (unsigned char *) data[i].data, &ret_length2, ddata2);
  384. diff_gno += clock() - start;
  385. #endif
  386. }
  387. assert_equal(ret, ret2, "i:%d ret %d != ret2 %d\n", (int) i, ret, ret2);
  388. printf("%*s: new % 8gms, gno % 8gms ratio %g\n", comment_max, data[i].comment,
  389. TEST_PERF_TIME(diff), TEST_PERF_TIME(diff_gno), TEST_PERF_RATIO(diff, diff_gno));
  390. total += diff;
  391. total_gno += diff_gno;
  392. }
  393. if (p_ctx->index == -1) {
  394. printf("%*s: new % 8gms, gno % 8gms ratio %g\n", comment_max, "totals",
  395. TEST_PERF_TIME(total), TEST_PERF_TIME(total_gno), TEST_PERF_RATIO(total, total_gno));
  396. }
  397. }
  398. int main(int argc, char *argv[]) {
  399. testFunction funcs[] = { /* name, func */
  400. { "test_u_sjis_int", test_u_sjis_int },
  401. { "test_sjis_utf8", test_sjis_utf8 },
  402. { "test_sjis_utf8_to_eci", test_sjis_utf8_to_eci },
  403. { "test_sjis_cpy", test_sjis_cpy },
  404. { "test_perf", test_perf },
  405. };
  406. testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
  407. testReport();
  408. return 0;
  409. }
  410. /* vim: set ts=4 sw=4 et : */