1 /*
2  * libdpkg - Debian packaging suite library routines
3  * t-c-ctype.c - test C locale ctype functions
4  *
5  * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2,
10  * or (at your option) any later version.
11  *
12  * This is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 #include <compat.h>
23 
24 #include <dpkg/test.h>
25 #include <dpkg/c-ctype.h>
26 
27 static void
test_ctype(void)28 test_ctype(void)
29 {
30 	int c;
31 
32 	for (c = -1; c < 256; c++) {
33 		/* Test blank. */
34 		if (c == '\t' || c == ' ')
35 			test_pass(c_isblank(c));
36 		else
37 			test_fail(c_isblank(c));
38 
39 		/* Test white. */
40 		if (c == '\t' || c == ' ' || c == '\n')
41 			test_pass(c_iswhite(c));
42 		else
43 			test_fail(c_iswhite(c));
44 
45 		/* Test space. */
46 		if (c == '\t' || c == '\v' || c == '\f' ||
47 		    c == '\r' || c == '\n' || c == ' ')
48 			test_pass(c_isspace(c));
49 		else
50 			test_fail(c_isspace(c));
51 
52 		/* Test digit. */
53 		if (c >= '0' && c <= '9')
54 			test_pass(c_isdigit(c));
55 		else
56 			test_fail(c_isdigit(c));
57 
58 		/* Test lower case. */
59 		if (c >= 'a' && c <= 'z')
60 			test_pass(c_islower(c));
61 		else
62 			test_fail(c_islower(c));
63 
64 		/* Test upper case. */
65 		if (c >= 'A' && c <= 'Z')
66 			test_pass(c_isupper(c));
67 		else
68 			test_fail(c_isupper(c));
69 
70 		/* Test alpha. */
71 		if (c_islower(c) || c_isupper(c))
72 			test_pass(c_isalpha(c));
73 		else
74 			test_fail(c_isalpha(c));
75 
76 		/* Test alphanumeric. */
77 		if (c_isdigit(c) || c_isalpha(c))
78 			test_pass(c_isalnum(c));
79 		else
80 			test_fail(c_isalnum(c));
81 	}
82 }
83 
84 static void
test_casing(void)85 test_casing(void)
86 {
87 	test_pass(c_tolower('A') == 'a');
88 	test_pass(c_tolower('Z') == 'z');
89 
90 	test_pass(c_tolower('a') == 'a');
91 	test_pass(c_tolower('z') == 'z');
92 
93 	test_pass(c_tolower('0') == '0');
94 	test_pass(c_tolower('9') == '9');
95 
96 	/* Test if we can handle the value for EOF. */
97 	test_pass(c_tolower(-1) == -1);
98 }
99 
TEST_ENTRY(test)100 TEST_ENTRY(test)
101 {
102 	test_plan(2063);
103 
104 	test_ctype();
105 	test_casing();
106 }
107