1 /*
2     libzint - the open source barcode library
3     Copyright (C) 2021 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 /* vim: set ts=4 sw=4 et : */
31 
32 #include "testcommon.h"
33 #include "test_ksx1001_tab.h"
34 #include "../ksx1001.h"
35 
36 // 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
ksx1001_wctomb_zint2(unsigned int * r,unsigned int wc)37 static int ksx1001_wctomb_zint2(unsigned int *r, unsigned int wc) {
38     int tab_length, start_i, end_i;
39     int i;
40 
41     if (wc < 0x80) {
42         return 0;
43     }
44     if (wc == 0x20AC) { // Euro sign added KS X 1001:1998
45         *r = 0x2266;
46         return 2;
47     }
48     if (wc == 0xAE) { // Registered trademark added KS X 1001:1998
49         *r = 0x2267;
50         return 2;
51     }
52     if (wc == 0x327E) { // Korean postal code symbol added KS X 1001:2002
53         *r = 0x2268;
54         return 2;
55     }
56     tab_length = ARRAY_SIZE(test_ksx1001_tab);
57     start_i = test_ksx1001_tab_ind[wc >> 10];
58     end_i = start_i + 0x800 > tab_length ? tab_length : start_i + 0x800;
59     for (i = start_i; i < end_i; i += 2) {
60         if (test_ksx1001_tab[i + 1] == wc) {
61             *r = test_ksx1001_tab[i];
62             return *r > 0xFF ? 2 : 1;
63         }
64     }
65     return 0;
66 }
67 
test_ksx1001_wctomb_zint(void)68 static void test_ksx1001_wctomb_zint(void) {
69 
70     int ret, ret2;
71     unsigned int val, val2;
72     unsigned i;
73 
74     testStart("test_ksx1001_wctomb_zint");
75 
76     for (i = 0; i < 0xFFFE; i++) {
77         if (i >= 0xD800 && i <= 0xDFFF) { // UTF-16 surrogates
78             continue;
79         }
80         val = val2 = 0;
81         ret = ksx1001_wctomb_zint(&val, i);
82         ret2 = ksx1001_wctomb_zint2(&val2, i);
83         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);
84         if (ret2) {
85             assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2);
86         }
87     }
88 
89     testFinish();
90 }
91 
main(int argc,char * argv[])92 int main(int argc, char *argv[]) {
93 
94     testFunction funcs[] = { /* name, func, has_index, has_generate, has_debug */
95         { "test_ksx1001_wctomb_zint", test_ksx1001_wctomb_zint, 0, 0, 0 },
96     };
97 
98     testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
99 
100     testReport();
101 
102     return 0;
103 }
104