Lower.c revision 1.1 1 /*
2 * Copyright (c) 2022, 2023, Oracle and/or its affiliates.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /* Test code for functions in src/Lower.c */
25 #include "config.h"
26 #include <X11/Xmu/CharSet.h>
27 #include <X11/Xmu/SysUtil.h>
28 #define XK_LATIN1
29 #include <X11/keysymdef.h>
30 #include <glib.h>
31 #include <string.h>
32
33 static const char upper[] = {
34 XK_space, XK_exclam, XK_quotedbl, XK_numbersign, XK_dollar, XK_percent,
35 XK_ampersand, XK_apostrophe, XK_quoteright, XK_parenleft, XK_parenright,
36 XK_asterisk, XK_plus, XK_comma, XK_minus, XK_period, XK_slash,
37 XK_0, XK_1, XK_2, XK_3, XK_4, XK_5, XK_6, XK_7, XK_8, XK_9,
38 XK_colon, XK_semicolon, XK_less, XK_equal, XK_greater, XK_question, XK_at,
39 XK_A, XK_B, XK_C, XK_D, XK_E, XK_F, XK_G, XK_H, XK_I, XK_J, XK_K, XK_L, XK_M,
40 XK_N, XK_O, XK_P, XK_Q, XK_R, XK_S, XK_T, XK_U, XK_V, XK_W, XK_X, XK_Y, XK_Z,
41 XK_bracketleft, XK_backslash, XK_bracketright, XK_asciicircum, XK_underscore,
42 XK_grave, XK_quoteleft, XK_braceleft, XK_bar, XK_braceright, XK_asciitilde,
43 XK_exclamdown, XK_cent, XK_sterling, XK_currency, XK_yen, XK_brokenbar,
44 XK_section, XK_diaeresis, XK_copyright, XK_ordfeminine, XK_guillemotleft,
45 XK_notsign, XK_hyphen, XK_registered, XK_macron, XK_degree, XK_plusminus,
46 XK_twosuperior, XK_threesuperior, XK_acute, XK_mu, XK_paragraph,
47 XK_periodcentered, XK_cedilla, XK_onesuperior, XK_masculine,
48 XK_guillemotright, XK_onequarter, XK_onehalf, XK_threequarters,
49 XK_questiondown, XK_multiply, XK_division,
50 XK_Agrave, XK_Aacute, XK_Acircumflex, XK_Atilde, XK_Adiaeresis, XK_Aring,
51 XK_AE, XK_Ccedilla, XK_Egrave, XK_Eacute, XK_Ecircumflex, XK_Ediaeresis,
52 XK_Igrave, XK_Iacute, XK_Icircumflex, XK_Idiaeresis, XK_ETH, XK_Ntilde,
53 XK_Ograve, XK_Oacute, XK_Ocircumflex, XK_Otilde, XK_Odiaeresis, XK_Oslash,
54 XK_Ooblique, XK_Ugrave, XK_Uacute, XK_Ucircumflex, XK_Udiaeresis, XK_Yacute,
55 XK_THORN, XK_ydiaeresis, '\0'
56 };
57
58 static const char lower[] = {
59 XK_space, XK_exclam, XK_quotedbl, XK_numbersign, XK_dollar, XK_percent,
60 XK_ampersand, XK_apostrophe, XK_quoteright, XK_parenleft, XK_parenright,
61 XK_asterisk, XK_plus, XK_comma, XK_minus, XK_period, XK_slash,
62 XK_0, XK_1, XK_2, XK_3, XK_4, XK_5, XK_6, XK_7, XK_8, XK_9,
63 XK_colon, XK_semicolon, XK_less, XK_equal, XK_greater, XK_question, XK_at,
64 XK_a, XK_b, XK_c, XK_d, XK_e, XK_f, XK_g, XK_h, XK_i, XK_j, XK_k, XK_l, XK_m,
65 XK_n, XK_o, XK_p, XK_q, XK_r, XK_s, XK_t, XK_u, XK_v, XK_w, XK_x, XK_y, XK_z,
66 XK_bracketleft, XK_backslash, XK_bracketright, XK_asciicircum, XK_underscore,
67 XK_grave, XK_quoteleft, XK_braceleft, XK_bar, XK_braceright, XK_asciitilde,
68 XK_exclamdown, XK_cent, XK_sterling, XK_currency, XK_yen, XK_brokenbar,
69 XK_section, XK_diaeresis, XK_copyright, XK_ordfeminine, XK_guillemotleft,
70 XK_notsign, XK_hyphen, XK_registered, XK_macron, XK_degree, XK_plusminus,
71 XK_twosuperior, XK_threesuperior, XK_acute, XK_mu, XK_paragraph,
72 XK_periodcentered, XK_cedilla, XK_onesuperior, XK_masculine,
73 XK_guillemotright, XK_onequarter, XK_onehalf, XK_threequarters,
74 XK_questiondown, XK_multiply, XK_division,
75 XK_agrave, XK_aacute, XK_acircumflex, XK_atilde, XK_adiaeresis, XK_aring,
76 XK_ae, XK_ccedilla, XK_egrave, XK_eacute, XK_ecircumflex, XK_ediaeresis,
77 XK_igrave, XK_iacute, XK_icircumflex, XK_idiaeresis, XK_eth, XK_ntilde,
78 XK_ograve, XK_oacute, XK_ocircumflex, XK_otilde, XK_odiaeresis, XK_oslash,
79 XK_ooblique, XK_ugrave, XK_uacute, XK_ucircumflex, XK_udiaeresis, XK_yacute,
80 XK_thorn, XK_ydiaeresis, '\0'
81 };
82
83 static const char mixed[] = {
84 XK_space, XK_exclam, XK_quotedbl, XK_numbersign, XK_dollar, XK_percent,
85 XK_ampersand, XK_apostrophe, XK_quoteright, XK_parenleft, XK_parenright,
86 XK_asterisk, XK_plus, XK_comma, XK_minus, XK_period, XK_slash,
87 XK_0, XK_1, XK_2, XK_3, XK_4, XK_5, XK_6, XK_7, XK_8, XK_9,
88 XK_colon, XK_semicolon, XK_less, XK_equal, XK_greater, XK_question, XK_at,
89 XK_a, XK_b, XK_c, XK_d, XK_e, XK_f, XK_g, XK_h, XK_i, XK_j, XK_k, XK_l, XK_m,
90 XK_N, XK_O, XK_P, XK_Q, XK_R, XK_S, XK_T, XK_U, XK_V, XK_W, XK_X, XK_Y, XK_Z,
91 XK_bracketleft, XK_backslash, XK_bracketright, XK_asciicircum, XK_underscore,
92 XK_grave, XK_quoteleft, XK_braceleft, XK_bar, XK_braceright, XK_asciitilde,
93 XK_exclamdown, XK_cent, XK_sterling, XK_currency, XK_yen, XK_brokenbar,
94 XK_section, XK_diaeresis, XK_copyright, XK_ordfeminine, XK_guillemotleft,
95 XK_notsign, XK_hyphen, XK_registered, XK_macron, XK_degree, XK_plusminus,
96 XK_twosuperior, XK_threesuperior, XK_acute, XK_mu, XK_paragraph,
97 XK_periodcentered, XK_cedilla, XK_onesuperior, XK_masculine,
98 XK_guillemotright, XK_onequarter, XK_onehalf, XK_threequarters,
99 XK_questiondown, XK_multiply, XK_division,
100 XK_agrave, XK_aacute, XK_acircumflex, XK_atilde, XK_adiaeresis, XK_aring,
101 XK_AE, XK_Ccedilla, XK_Egrave, XK_Eacute, XK_Ecircumflex, XK_Ediaeresis,
102 XK_igrave, XK_iacute, XK_icircumflex, XK_idiaeresis, XK_eth, XK_ntilde,
103 XK_Ograve, XK_Oacute, XK_Ocircumflex, XK_Otilde, XK_Odiaeresis, XK_Oslash,
104 XK_ooblique, XK_ugrave, XK_uacute, XK_ucircumflex, XK_udiaeresis, XK_yacute,
105 XK_THORN, XK_ydiaeresis, '\0'
106 };
107
108 #define DATA_LEN sizeof(upper)
109
110 static void
111 test_XmuCopyISOLatin1Lowered(void)
112 {
113 char buf[DATA_LEN];
114
115 XmuCopyISOLatin1Lowered(buf, upper);
116 g_assert_cmpstr(buf, ==, lower);
117
118 XmuCopyISOLatin1Lowered(buf, lower);
119 g_assert_cmpstr(buf, ==, lower);
120
121 XmuCopyISOLatin1Lowered(buf, mixed);
122 g_assert_cmpstr(buf, ==, lower);
123 }
124
125 static void
126 test_XmuCopyISOLatin1Uppered(void)
127 {
128 char buf[DATA_LEN];
129
130 XmuCopyISOLatin1Uppered(buf, upper);
131 g_assert_cmpstr(buf, ==, upper);
132
133 XmuCopyISOLatin1Uppered(buf, lower);
134 g_assert_cmpstr(buf, ==, upper);
135
136 XmuCopyISOLatin1Uppered(buf, mixed);
137 g_assert_cmpstr(buf, ==, upper);
138 }
139
140 static void
141 test_XmuNCopyISOLatin1Lowered(void)
142 {
143 char buf[DATA_LEN];
144
145 XmuNCopyISOLatin1Lowered(buf, upper, DATA_LEN);
146 g_assert_cmpstr(buf, ==, lower);
147
148 XmuNCopyISOLatin1Lowered(buf, lower, DATA_LEN);
149 g_assert_cmpstr(buf, ==, lower);
150
151 XmuNCopyISOLatin1Lowered(buf, mixed, DATA_LEN);
152 g_assert_cmpstr(buf, ==, lower);
153 }
154
155 static void
156 test_XmuNCopyISOLatin1Uppered(void)
157 {
158 char buf[DATA_LEN];
159
160 XmuNCopyISOLatin1Uppered(buf, upper, DATA_LEN);
161 g_assert_cmpstr(buf, ==, upper);
162
163 XmuNCopyISOLatin1Uppered(buf, lower, DATA_LEN);
164 g_assert_cmpstr(buf, ==, upper);
165
166 XmuNCopyISOLatin1Uppered(buf, mixed, DATA_LEN);
167 g_assert_cmpstr(buf, ==, upper);
168 }
169
170 static void
171 test_XmuCompareISOLatin1(void)
172 {
173 int cmp;
174
175 cmp = XmuCompareISOLatin1(upper, lower);
176 g_assert_cmpint(cmp, ==, 0);
177
178 cmp = XmuCompareISOLatin1(upper, mixed);
179 g_assert_cmpint(cmp, ==, 0);
180
181 cmp = XmuCompareISOLatin1(lower, mixed);
182 g_assert_cmpint(cmp, ==, 0);
183
184 cmp = XmuCompareISOLatin1(upper + 1, lower);
185 g_assert_cmpint(cmp, >, 0);
186
187 cmp = XmuCompareISOLatin1(mixed, lower + 1);
188 g_assert_cmpint(cmp, <, 0);
189 }
190
191 static void
192 test_XmuSnprintf(void)
193 {
194 char buf[DATA_LEN];
195 int ret;
196
197 g_assert_cmpint(DATA_LEN, >, 40);
198 ret = XmuSnprintf(buf, 40, "%s", upper);
199 g_assert_cmpint(ret, ==, sizeof(upper) - 1);
200 g_assert_cmpint(buf[39], ==, 0);
201 g_assert_cmpmem(buf, 39, upper, 39);
202
203 ret = XmuSnprintf(buf, sizeof(buf), "%s", upper);
204 g_assert_cmpint(ret, ==, sizeof(upper) - 1);
205 g_assert_cmpstr(buf, ==, upper);
206
207 ret = XmuSnprintf(buf, sizeof(buf), "%d", 12345678);
208 g_assert_cmpint(ret, ==, 8);
209 g_assert_cmpstr(buf, ==, "12345678");
210 }
211
212 int
213 main(int argc, char** argv)
214 {
215 g_test_init(&argc, &argv, NULL);
216 g_test_bug_base(PACKAGE_BUGREPORT);
217
218 g_assert_cmpuint(sizeof(upper), ==, sizeof(lower));
219 g_assert_cmpuint(sizeof(upper), ==, sizeof(mixed));
220
221 g_test_add_func("/Lower/XmuCopyISOLatin1Lowered",
222 test_XmuCopyISOLatin1Lowered);
223 g_test_add_func("/Lower/XmuCopyISOLatin1Uppered",
224 test_XmuCopyISOLatin1Uppered);
225
226 g_test_add_func("/Lower/XmuNCopyISOLatin1Lowered",
227 test_XmuNCopyISOLatin1Lowered);
228 g_test_add_func("/Lower/XmuNCopyISOLatin1Uppered",
229 test_XmuNCopyISOLatin1Uppered);
230
231 g_test_add_func("/Lower/XmuCompareISOLatin1",
232 test_XmuCompareISOLatin1);
233 g_test_add_func("/Lower/XmuSnprintf",
234 test_XmuSnprintf);
235
236
237 return g_test_run();
238 }
239