2021-01-12 07:11:41 +13:00
|
|
|
/*
|
|
|
|
libzint - the open source barcode library
|
2022-06-03 07:32:25 +12:00
|
|
|
Copyright (C) 2021-2022 Robin Stuart <rstuart114@gmail.com>
|
2021-01-12 07:11:41 +13:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of the project nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software
|
|
|
|
without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
SUCH DAMAGE.
|
|
|
|
*/
|
2022-09-13 06:26:04 +12:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause */
|
2021-01-12 07:11:41 +13:00
|
|
|
|
|
|
|
#include "testcommon.h"
|
|
|
|
#include "test_ksx1001_tab.h"
|
|
|
|
#include "../ksx1001.h"
|
2022-06-03 07:32:25 +12:00
|
|
|
/* For local "private" testing using previous libiconv adaptation, not included for licensing reasons */
|
2022-12-05 23:11:36 +13:00
|
|
|
#if 0
|
|
|
|
#define TEST_JUST_SAY_GNO
|
|
|
|
#endif
|
2022-06-03 07:32:25 +12:00
|
|
|
#ifdef TEST_JUST_SAY_GNO
|
|
|
|
#include "../just_say_gno/ksx1001_gnu.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
INTERNAL int u_ksx1001_test(const unsigned int u, unsigned char *dest);
|
|
|
|
|
2022-09-13 06:26:04 +12:00
|
|
|
/* Version of `u_ksx1001()` taking unsigned int destination for backward-compatible testing */
|
2022-06-03 07:32:25 +12:00
|
|
|
static int u_ksx1001_int(const unsigned int u, unsigned int *d) {
|
|
|
|
unsigned char dest[2];
|
|
|
|
int ret = u_ksx1001_test(u, dest);
|
|
|
|
if (ret) {
|
|
|
|
*d = ret == 1 ? dest[0] : ((dest[0] << 8) | dest[1]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2021-01-12 07:11:41 +13:00
|
|
|
|
2022-09-13 06:26:04 +12:00
|
|
|
/* As control convert to KS X 1001 using simple table generated from
|
|
|
|
https://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/KSC/KSX1001.TXT plus simple processing
|
|
|
|
*/
|
2022-06-03 07:32:25 +12:00
|
|
|
static int u_ksx1001_int2(unsigned int u, unsigned int *dest) {
|
2021-06-24 02:00:49 +12:00
|
|
|
int tab_length, start_i, end_i;
|
|
|
|
int i;
|
|
|
|
|
2022-06-03 07:32:25 +12:00
|
|
|
if (u < 0x80) {
|
|
|
|
*dest = u;
|
|
|
|
return 1;
|
2021-01-12 07:11:41 +13:00
|
|
|
}
|
2022-09-13 06:26:04 +12:00
|
|
|
if (u == 0x20AC) { /* Euro sign added KS X 1001:1998 */
|
2022-06-03 07:32:25 +12:00
|
|
|
*dest = 0x2266 + 0x8080;
|
2021-01-12 07:11:41 +13:00
|
|
|
return 2;
|
|
|
|
}
|
2022-09-13 06:26:04 +12:00
|
|
|
if (u == 0xAE) { /* Registered trademark added KS X 1001:1998 */
|
2022-06-03 07:32:25 +12:00
|
|
|
*dest = 0x2267 + 0x8080;
|
2021-01-12 07:11:41 +13:00
|
|
|
return 2;
|
|
|
|
}
|
2022-09-13 06:26:04 +12:00
|
|
|
if (u == 0x327E) { /* Korean postal code symbol added KS X 1001:2002 */
|
2022-06-03 07:32:25 +12:00
|
|
|
*dest = 0x2268 + 0x8080;
|
2021-01-12 07:11:41 +13:00
|
|
|
return 2;
|
|
|
|
}
|
2021-06-24 02:00:49 +12:00
|
|
|
tab_length = ARRAY_SIZE(test_ksx1001_tab);
|
2022-06-03 07:32:25 +12:00
|
|
|
start_i = test_ksx1001_tab_ind[u >> 10];
|
2021-06-24 02:00:49 +12:00
|
|
|
end_i = start_i + 0x800 > tab_length ? tab_length : start_i + 0x800;
|
|
|
|
for (i = start_i; i < end_i; i += 2) {
|
2022-06-03 07:32:25 +12:00
|
|
|
if (test_ksx1001_tab[i + 1] == u) {
|
|
|
|
*dest = test_ksx1001_tab[i] + 0x8080;
|
|
|
|
return 2;
|
2021-01-12 07:11:41 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-03 07:32:25 +12:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define TEST_PERF_TIME(arg) (((arg) * 1000.0) / CLOCKS_PER_SEC)
|
|
|
|
#define TEST_PERF_RATIO(a1, a2) (a2 ? TEST_PERF_TIME(a1) / TEST_PERF_TIME(a2) : 0)
|
|
|
|
|
|
|
|
#ifdef TEST_JUST_SAY_GNO
|
|
|
|
#define TEST_INT_PERF_ITERATIONS 100
|
|
|
|
#endif
|
|
|
|
|
2022-09-13 06:26:04 +12:00
|
|
|
static void test_u_ksx1001_int(const testCtx *const p_ctx) {
|
|
|
|
int debug = p_ctx->debug;
|
2021-01-12 07:11:41 +13:00
|
|
|
|
|
|
|
int ret, ret2;
|
|
|
|
unsigned int val, val2;
|
2021-06-24 02:00:49 +12:00
|
|
|
unsigned i;
|
|
|
|
|
2022-06-03 07:32:25 +12:00
|
|
|
#ifdef TEST_JUST_SAY_GNO
|
|
|
|
int j;
|
|
|
|
clock_t start;
|
|
|
|
clock_t total = 0, total_gno = 0;
|
|
|
|
#else
|
|
|
|
(void)debug;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
testStart("test_u_ksx1001_int");
|
|
|
|
|
|
|
|
#ifdef TEST_JUST_SAY_GNO
|
|
|
|
if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
|
|
|
|
printf("test_u_ksx1001_int perf iterations: %d\n", TEST_INT_PERF_ITERATIONS);
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-12 07:11:41 +13:00
|
|
|
|
2021-06-24 02:00:49 +12:00
|
|
|
for (i = 0; i < 0xFFFE; i++) {
|
2022-09-13 06:26:04 +12:00
|
|
|
if (i >= 0xD800 && i <= 0xDFFF) { /* UTF-16 surrogates */
|
2021-01-12 07:11:41 +13:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-05 23:11:36 +13:00
|
|
|
if (testContinue(p_ctx, i)) continue;
|
2021-01-12 07:11:41 +13:00
|
|
|
val = val2 = 0;
|
2022-06-03 07:32:25 +12:00
|
|
|
ret = u_ksx1001_int(i, &val);
|
|
|
|
ret2 = u_ksx1001_int2(i, &val2);
|
2021-07-08 00:58:04 +12:00
|
|
|
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);
|
2021-01-12 07:11:41 +13:00
|
|
|
if (ret2) {
|
2021-07-08 00:58:04 +12:00
|
|
|
assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
|
2021-01-12 07:11:41 +13:00
|
|
|
}
|
2022-06-03 07:32:25 +12:00
|
|
|
#ifdef TEST_JUST_SAY_GNO
|
2022-09-13 06:26:04 +12:00
|
|
|
if (i >= 0x80) { /* `ksx1001_wctomb_zint()` doesn't handle ASCII */
|
2022-06-03 07:32:25 +12:00
|
|
|
if (!(debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
|
|
|
|
val2 = 0;
|
|
|
|
ret2 = ksx1001_wctomb_zint(&val2, i);
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < TEST_INT_PERF_ITERATIONS; j++) {
|
|
|
|
val = val2 = 0;
|
|
|
|
|
|
|
|
start = clock();
|
|
|
|
ret = u_ksx1001_int(i, &val);
|
|
|
|
total += clock() - start;
|
|
|
|
|
|
|
|
start = clock();
|
|
|
|
ret2 = ksx1001_wctomb_zint(&val2, i);
|
|
|
|
total_gno += clock() - start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (ret2) {
|
2022-09-13 06:26:04 +12:00
|
|
|
val2 += 0x8080; /* `ksx1001_wctomb_zint()` returns pure KS X 1001 values, convert to EUC-KR */
|
2022-06-03 07:32:25 +12:00
|
|
|
assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-12 07:11:41 +13:00
|
|
|
}
|
|
|
|
|
2022-06-03 07:32:25 +12:00
|
|
|
#ifdef TEST_JUST_SAY_GNO
|
|
|
|
if ((debug & ZINT_DEBUG_TEST_PERFORMANCE)) { /* -d 256 */
|
|
|
|
printf("test_u_ksx1001_int perf totals: new % 8gms, gno % 8gms ratio %g\n",
|
|
|
|
TEST_PERF_TIME(total), TEST_PERF_TIME(total_gno), TEST_PERF_RATIO(total, total_gno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-12 07:11:41 +13:00
|
|
|
testFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
2022-09-13 06:26:04 +12:00
|
|
|
testFunction funcs[] = { /* name, func */
|
|
|
|
{ "test_u_ksx1001_int", test_u_ksx1001_int },
|
2021-01-12 07:11:41 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
|
|
|
|
|
|
|
|
testReport();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-06-03 07:32:25 +12:00
|
|
|
|
|
|
|
/* vim: set ts=4 sw=4 et : */
|