Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zint/backend/tests/test_ksx1001.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* | |
| 2 libzint - the open source barcode library | |
| 3 Copyright (C) 2021-2022 Robin Stuart <rstuart114@gmail.com> | |
| 4 | |
| 5 Redistribution and use in source and binary forms, with or without | |
| 6 modification, are permitted provided that the following conditions | |
| 7 are met: | |
| 8 | |
| 9 1. Redistributions of source code must retain the above copyright | |
| 10 notice, this list of conditions and the following disclaimer. | |
| 11 2. Redistributions in binary form must reproduce the above copyright | |
| 12 notice, this list of conditions and the following disclaimer in the | |
| 13 documentation and/or other materials provided with the distribution. | |
| 14 3. Neither the name of the project nor the names of its contributors | |
| 15 may be used to endorse or promote products derived from this software | |
| 16 without specific prior written permission. | |
| 17 | |
| 18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | |
| 22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 28 SUCH DAMAGE. | |
| 29 */ | |
| 30 /* SPDX-License-Identifier: BSD-3-Clause */ | |
| 31 | |
| 32 #include "testcommon.h" | |
| 33 #include "test_ksx1001_tab.h" | |
| 34 #include "../ksx1001.h" | |
| 35 /* For local "private" testing using previous libiconv adaptation, not included for licensing reasons */ | |
| 36 #if 0 | |
| 37 #define TEST_JUST_SAY_GNO | |
| 38 #endif | |
| 39 #ifdef TEST_JUST_SAY_GNO | |
| 40 #include "../just_say_gno/ksx1001_gnu.h" | |
| 41 #endif | |
| 42 | |
| 43 INTERNAL int u_ksx1001_test(const unsigned int u, unsigned char *dest); | |
| 44 | |
| 45 /* Version of `u_ksx1001()` taking unsigned int destination for backward-compatible testing */ | |
| 46 static int u_ksx1001_int(const unsigned int u, unsigned int *d) { | |
| 47 unsigned char dest[2]; | |
| 48 int ret = u_ksx1001_test(u, dest); | |
| 49 if (ret) { | |
| 50 *d = ret == 1 ? dest[0] : ((dest[0] << 8) | dest[1]); | |
| 51 } | |
| 52 return ret; | |
| 53 } | |
| 54 | |
| 55 /* As control convert to KS X 1001 using simple table generated from | |
| 56 https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/KSC/KSX1001.TXT plus simple processing | |
| 57 */ | |
| 58 static int u_ksx1001_int2(unsigned int u, unsigned int *dest) { | |
| 59 int tab_length, start_i, end_i; | |
| 60 int i; | |
| 61 | |
| 62 if (u < 0x80) { | |
| 63 *dest = u; | |
| 64 return 1; | |
| 65 } | |
| 66 if (u == 0x20AC) { /* Euro sign added KS X 1001:1998 */ | |
| 67 *dest = 0x2266 + 0x8080; | |
| 68 return 2; | |
| 69 } | |
| 70 if (u == 0xAE) { /* Registered trademark added KS X 1001:1998 */ | |
| 71 *dest = 0x2267 + 0x8080; | |
| 72 return 2; | |
| 73 } | |
| 74 if (u == 0x327E) { /* Korean postal code symbol added KS X 1001:2002 */ | |
| 75 *dest = 0x2268 + 0x8080; | |
| 76 return 2; | |
| 77 } | |
| 78 tab_length = ARRAY_SIZE(test_ksx1001_tab); | |
| 79 start_i = test_ksx1001_tab_ind[u >> 10]; | |
| 80 end_i = start_i + 0x800 > tab_length ? tab_length : start_i + 0x800; | |
| 81 for (i = start_i; i < end_i; i += 2) { | |
| 82 if (test_ksx1001_tab[i + 1] == u) { | |
| 83 *dest = test_ksx1001_tab[i] + 0x8080; | |
| 84 return 2; | |
| 85 } | |
| 86 } | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 #include <time.h> | |
| 91 | |
| 92 #define TEST_PERF_TIME(arg) (((arg) * 1000.0) / CLOCKS_PER_SEC) | |
| 93 #define TEST_PERF_RATIO(a1, a2) (a2 ? TEST_PERF_TIME(a1) / TEST_PERF_TIME(a2) : 0) | |
| 94 | |
| 95 #ifdef TEST_JUST_SAY_GNO | |
| 96 #define TEST_INT_PERF_ITERATIONS 100 | |
| 97 #endif | |
| 98 | |
| 99 static void test_u_ksx1001_int(const testCtx *const p_ctx) { | |
| 100 int debug = p_ctx->debug; | |
| 101 | |
| 102 int ret, ret2; | |
| 103 unsigned int val, val2; | |
| 104 unsigned i; | |
| 105 | |
| 106 #ifdef TEST_JUST_SAY_GNO | |
| 107 int j; | |
| 108 clock_t start; | |
| 109 clock_t total = 0, total_gno = 0; | |
| 110 #else | |
| 111 (void)debug; | |
| 112 #endif | |
| 113 | |
| 114 testStart("test_u_ksx1001_int"); | |
| 115 | |
| 116 #ifdef TEST_JUST_SAY_GNO | |
| 117 if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */ | |
| 118 printf("test_u_ksx1001_int perf iterations: %d\n", TEST_INT_PERF_ITERATIONS); | |
| 119 } | |
| 120 #endif | |
| 121 | |
| 122 for (i = 0; i < 0xFFFE; i++) { | |
| 123 if (i >= 0xD800 && i <= 0xDFFF) { /* UTF-16 surrogates */ | |
| 124 continue; | |
| 125 } | |
| 126 if (testContinue(p_ctx, i)) continue; | |
| 127 val = val2 = 0; | |
| 128 ret = u_ksx1001_int(i, &val); | |
| 129 ret2 = u_ksx1001_int2(i, &val2); | |
| 130 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); | |
| 131 if (ret2) { | |
| 132 assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2); | |
| 133 } | |
| 134 #ifdef TEST_JUST_SAY_GNO | |
| 135 if (i >= 0x80) { /* `ksx1001_wctomb_zint()` doesn't handle ASCII */ | |
| 136 if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */ | |
| 137 val2 = 0; | |
| 138 ret2 = ksx1001_wctomb_zint(&val2, i); | |
| 139 } else { | |
| 140 for (j = 0; j < TEST_INT_PERF_ITERATIONS; j++) { | |
| 141 val = val2 = 0; | |
| 142 | |
| 143 start = clock(); | |
| 144 ret = u_ksx1001_int(i, &val); | |
| 145 total += clock() - start; | |
| 146 | |
| 147 start = clock(); | |
| 148 ret2 = ksx1001_wctomb_zint(&val2, i); | |
| 149 total_gno += clock() - start; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 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); | |
| 154 if (ret2) { | |
| 155 val2 += 0x8080; /* `ksx1001_wctomb_zint()` returns pure KS X 1001 values, convert to EUC-KR */ | |
| 156 assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2); | |
| 157 } | |
| 158 } | |
| 159 #endif | |
| 160 } | |
| 161 | |
| 162 #ifdef TEST_JUST_SAY_GNO | |
| 163 if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */ | |
| 164 printf("test_u_ksx1001_int perf totals: new % 8gms, gno % 8gms ratio %g\n", | |
| 165 TEST_PERF_TIME(total), TEST_PERF_TIME(total_gno), TEST_PERF_RATIO(total, total_gno)); | |
| 166 } | |
| 167 #endif | |
| 168 | |
| 169 testFinish(); | |
| 170 } | |
| 171 | |
| 172 int main(int argc, char *argv[]) { | |
| 173 | |
| 174 testFunction funcs[] = { /* name, func */ | |
| 175 { "test_u_ksx1001_int", test_u_ksx1001_int }, | |
| 176 }; | |
| 177 | |
| 178 testRun(argc, argv, funcs, ARRAY_SIZE(funcs)); | |
| 179 | |
| 180 testReport(); | |
| 181 | |
| 182 return 0; | |
| 183 } | |
| 184 | |
| 185 /* vim: set ts=4 sw=4 et : */ |
