aboutsummaryrefslogtreecommitdiff
path: root/test/common/util.h
blob: e195f13a848d4425aade30638ca81ce1681b8bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <cxxtest/TestSuite.h>
#include "common/util.h"

/**
 * Test suite for the functions in common/util.h
 */
class UtilTestSuite : public CxxTest::TestSuite {
	public:
	void test_parsebool_yesno() {

		// First test for the parseBool function.
		// These are the mixed case yes/no cases that must work

		bool valasbool;
		bool success;

		Common::String string_1("Yes");
		success = Common::parseBool(string_1, valasbool);
		TS_ASSERT_EQUALS(success, 1);
		TS_ASSERT_EQUALS(valasbool, 1);

		Common::String string_2("nO");
		success = Common::parseBool(string_2, valasbool);
		TS_ASSERT_EQUALS(success, 1);
		TS_ASSERT_EQUALS(valasbool, 0);
	}

	void test_parsebool_truefalse() {

		// First test for the parseBool function.
		// These are the mixed case true/false cases that must work

		bool valasbool;
		bool success;

		Common::String string_3("tRuE");
		success = Common::parseBool(string_3, valasbool);
		TS_ASSERT_EQUALS(success, 1);
		TS_ASSERT_EQUALS(valasbool, 1);

		Common::String string_4("fAlSe");
		success = Common::parseBool(string_4, valasbool);
		TS_ASSERT_EQUALS(success, 1);
		TS_ASSERT_EQUALS(valasbool, 0);
	}

	void test_parsebool_onezero() {

		// Third test for the parseBool function.
		// These are the 1/0 cases that must work.
		// Note that while 'a-z'+0x20 must work just fine,
		// '0-1'+0x20 should NOT; this is addressed in
		// parsebool_bad

		bool valasbool;
		bool success;

		Common::String string_5("1");
		success = Common::parseBool(string_5, valasbool);
		TS_ASSERT_EQUALS(success, 1);
		TS_ASSERT_EQUALS(valasbool, 1);

		Common::String string_6("0");
		success = Common::parseBool(string_6, valasbool);
		TS_ASSERT_EQUALS(success, 1);
		TS_ASSERT_EQUALS(valasbool, 0);

	}

	void test_parsebool_bad() {

		bool valasbool;
		bool success;

		// Bad cases that should not return success #1:
		// Random string
		Common::String string_1("u_f1ght_l1k3_a_c0w");
		success = Common::parseBool(string_1, valasbool);
		TS_ASSERT_EQUALS(success, 0);

		// Bad cases that should not return success #2, #3:
		// The function should NOT accept trailing whitespaces:
		Common::String string_2(" yes");
		success = Common::parseBool(string_2, valasbool);
		TS_ASSERT_EQUALS(success, 0);

		Common::String string_3("yes ");
		success = Common::parseBool(string_3, valasbool);
		TS_ASSERT_EQUALS(success, 0);

		// While 'a-z'+0x20 must work just fine,
		// '0-1'+0x20 should NOT. '2' is not good either.

		Common::String string_4("\x50");
		success = Common::parseBool(string_4, valasbool);
		TS_ASSERT_EQUALS(success, 0);

		Common::String string_5("\x51");
		success = Common::parseBool(string_5, valasbool);
		TS_ASSERT_EQUALS(success, 0);

		Common::String string_6("2");
		success = Common::parseBool(string_6, valasbool);
		TS_ASSERT_EQUALS(success, 0);
	}

	void test_is_al_num() {

		// Test the isAlnum function
		// Should return true if and only if the input is an alphanumeric char

		TS_ASSERT_EQUALS(Common::isAlnum('a'), 1);
		TS_ASSERT_EQUALS(Common::isAlnum('A'), 1);
		TS_ASSERT_EQUALS(Common::isAlnum('z'), 1);
		TS_ASSERT_EQUALS(Common::isAlnum('Z'), 1);
		TS_ASSERT_EQUALS(Common::isAlnum('1'), 1);
		TS_ASSERT_EQUALS(Common::isAlnum('0'), 1);
		TS_ASSERT_EQUALS(Common::isAlnum('\xA7'), 0);
		TS_ASSERT_EQUALS(Common::isAlnum('$'), 0);
		TS_ASSERT_EQUALS(Common::isAlnum(' '), 0);
		TS_ASSERT_EQUALS(Common::isAlnum('\n'), 0);
		TS_ASSERT_EQUALS(Common::isAlnum('\b'), 0);
		TS_ASSERT_EQUALS(Common::isAlnum(0), 0);
		TS_ASSERT_EQUALS(Common::isAlnum(255), 0);

	}

	void test_is_alpha() {

		// Test the isAlpha function
		// Should return true if and only if the input is an alphanumeric char

		TS_ASSERT_EQUALS(Common::isAlpha('a'), 1);
		TS_ASSERT_EQUALS(Common::isAlpha('A'), 1);
		TS_ASSERT_EQUALS(Common::isAlpha('z'), 1);
		TS_ASSERT_EQUALS(Common::isAlpha('Z'), 1);
		TS_ASSERT_EQUALS(Common::isAlpha('1'), 0);
		TS_ASSERT_EQUALS(Common::isAlpha('0'), 0);
		TS_ASSERT_EQUALS(Common::isAlpha('\xA7'), 0);
		TS_ASSERT_EQUALS(Common::isAlpha('$'), 0);
		TS_ASSERT_EQUALS(Common::isAlpha(' '), 0);
		TS_ASSERT_EQUALS(Common::isAlpha('\n'), 0);
		TS_ASSERT_EQUALS(Common::isAlpha('\b'), 0);
		TS_ASSERT_EQUALS(Common::isAlpha(0), 0);
		TS_ASSERT_EQUALS(Common::isAlpha(255), 0);

	}

	void test_is_digit() {

		// Test the isDigit function
		// Should return true if and only if the input is a single digit

		TS_ASSERT_EQUALS(Common::isDigit('a'), 0);
		TS_ASSERT_EQUALS(Common::isDigit('A'), 0);
		TS_ASSERT_EQUALS(Common::isDigit('z'), 0);
		TS_ASSERT_EQUALS(Common::isDigit('Z'), 0);
		TS_ASSERT_EQUALS(Common::isDigit('1'), 1);
		TS_ASSERT_EQUALS(Common::isDigit('0'), 1);
		TS_ASSERT_EQUALS(Common::isDigit('\xA7'), 0);
		TS_ASSERT_EQUALS(Common::isDigit('$'), 0);
		TS_ASSERT_EQUALS(Common::isDigit(' '), 0);
		TS_ASSERT_EQUALS(Common::isDigit('\n'), 0);
		TS_ASSERT_EQUALS(Common::isDigit('\b'), 0);
		TS_ASSERT_EQUALS(Common::isDigit(0), 0);
		TS_ASSERT_EQUALS(Common::isDigit(255), 0);

	}

	void test_is_lower() {

		// Test the isLower function
		// Should return true if and only if the input is a lowercase char

		TS_ASSERT_EQUALS(Common::isLower('a'), 1);
		TS_ASSERT_EQUALS(Common::isLower('A'), 0);
		TS_ASSERT_EQUALS(Common::isLower('z'), 1);
		TS_ASSERT_EQUALS(Common::isLower('Z'), 0);
		TS_ASSERT_EQUALS(Common::isLower('1'), 0);
		TS_ASSERT_EQUALS(Common::isLower('0'), 0);
		TS_ASSERT_EQUALS(Common::isLower('\xA7'), 0);
		TS_ASSERT_EQUALS(Common::isLower('$'), 0);
		TS_ASSERT_EQUALS(Common::isLower(' '), 0);
		TS_ASSERT_EQUALS(Common::isLower('\n'), 0);
		TS_ASSERT_EQUALS(Common::isLower('\b'), 0);
		TS_ASSERT_EQUALS(Common::isLower(0), 0);
		TS_ASSERT_EQUALS(Common::isLower(255), 0);

	}


	void test_is_upper() {

		// Test the isUpper function
		// Should return true if and only if the input is an uppercase char

		TS_ASSERT_EQUALS(Common::isUpper('a'), 0);
		TS_ASSERT_EQUALS(Common::isUpper('A'), 1);
		TS_ASSERT_EQUALS(Common::isUpper('z'), 0);
		TS_ASSERT_EQUALS(Common::isUpper('Z'), 1);
		TS_ASSERT_EQUALS(Common::isUpper('1'), 0);
		TS_ASSERT_EQUALS(Common::isUpper('0'), 0);
		TS_ASSERT_EQUALS(Common::isUpper('\xA7'), 0);
		TS_ASSERT_EQUALS(Common::isUpper('$'), 0);
		TS_ASSERT_EQUALS(Common::isUpper(' '), 0);
		TS_ASSERT_EQUALS(Common::isUpper('\n'), 0);
		TS_ASSERT_EQUALS(Common::isUpper('\b'), 0);
		TS_ASSERT_EQUALS(Common::isUpper(0), 0);
		TS_ASSERT_EQUALS(Common::isUpper(255), 0);

	}
	void test_is_space() {
		// isSpace should return true iff the character is some kind of whitespace
		// or tab character
		for (int c = 0; c < 255; c++) {
			 if (c == ' '  || c == '\t' ||
				 c == '\r' || c == '\n' ||
				 c == '\v' || c == '\f') {
				 TS_ASSERT_EQUALS(Common::isSpace(c), 1);
			 } else {
				 TS_ASSERT_EQUALS(Common::isSpace(c), 0);
			 }
		}
	}

	void test_is_print() {
		// isPrint should return true iff the input is a printable ascii char.
		// That is to say, 0x20 to 0x7E.
		for (int c = 0; c < 255; c++) {
			 if (c >= 0x20 && c <= 0x7E) {
				 TS_ASSERT_EQUALS(Common::isPrint(c), 1);
			 } else {
				 TS_ASSERT_EQUALS(Common::isPrint(c), 0);
			 }
		}
	}
};