aboutsummaryrefslogtreecommitdiff
path: root/test/common/hash-str.h
blob: 0391e7167ff4f1d581992b01f8604736e2be194e (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
#include <cxxtest/TestSuite.h>
#include "common/hash-str.h"

/**
 * Test suite for common/hash-str.h
 * We test a number of case sensitive/insensitive hash and compare functions
 * using example strings and known hashes, trying to tackle
 * as much edge cases as possible.
 */
class HashStrTestSuite : public CxxTest::TestSuite {

	public:
	void test_case_sensitive_string_equal_to() {

		// Name says it all.
		// This verifies that the function returns true
		// for exactly the same string, false for the same
		// string in mixed case and false for some edge cases
		// with various spacings plus one character replaced
		// by itself+128 (if there's some processing done after
		// conversion to 7-bit ASCII this might yield funny results).

		const Common::String lower("test");
		const Common::String lower1("test");
		const Common::String mixed("tESt");
		const Common::String spaced("test ");
		const Common::String doublespaced("test  ");
		const Common::String tabbed("test\t");
		const Common::String plus128("t\345est");
		// 'e'+128 = 0xE5 = 0o345

		Common::CaseSensitiveString_EqualTo css_et;
		TS_ASSERT_EQUALS(css_et(lower, mixed), false);
		TS_ASSERT_EQUALS(css_et(lower, lower1), true);
		TS_ASSERT_EQUALS(css_et(lower, lower), true);

		// Different sorts of whitespace are to be treated differently.
		TS_ASSERT_EQUALS(css_et(lower, spaced), false);
		TS_ASSERT_EQUALS(css_et(lower, tabbed), false);
		TS_ASSERT_EQUALS(css_et(spaced, tabbed), false);
		TS_ASSERT_EQUALS(css_et(spaced, doublespaced), false);
		TS_ASSERT_EQUALS(css_et(lower, plus128), false);
	}

	void test_ignore_case_equal_to() {

		// This should be probably called case_insensitive_string_equal_to
		// or something,but it's basically the same thing as
		// test_case_sensitive_string_equal_to, only it's case
		// insensitive.

		const Common::String lower("test");
		const Common::String lower1("test");
		const Common::String mixed("tESt");
		const Common::String spaced("test ");
		const Common::String mixedspaced("tESt ");
		const Common::String doublespaced("test  ");
		const Common::String tabbed("test\t");
		const Common::String plus128("t\345est");

		Common::IgnoreCase_EqualTo ic_et;
		TS_ASSERT_EQUALS(ic_et(lower, mixed), true);
		TS_ASSERT_EQUALS(ic_et(lower, lower1), true);
		TS_ASSERT_EQUALS(ic_et(lower, lower), true);
		// Edge case:
		TS_ASSERT_EQUALS(ic_et(spaced, mixedspaced), true);

		// Different sorts of whitespace are to be treated differently.
		TS_ASSERT_EQUALS(ic_et(lower, spaced), false);
		TS_ASSERT_EQUALS(ic_et(lower, tabbed), false);
		TS_ASSERT_EQUALS(ic_et(spaced, tabbed), false);
		TS_ASSERT_EQUALS(ic_et(spaced, doublespaced), false);
		TS_ASSERT_EQUALS(ic_et(lower, plus128), false);
	}

	void test_case_sensitive_string_hash() {

		// Here we compute string hashes for different
		// strings and see that the functor is case sensitive
		// and does not ignore spaces.

		const Common::String lower("test");
		const Common::String lower1("test");
		const Common::String mixed("tESt");
		const Common::String spaced("test ");
		const Common::String mixedspaced("tESt ");
		const Common::String doublespaced("test  ");
		const Common::String tabbed("test\t");

		Common::CaseSensitiveString_Hash css_h;
		TS_ASSERT_EQUALS(css_h(lower), css_h(lower1));
		TS_ASSERT_DIFFERS(css_h(mixed), css_h(lower));
		TS_ASSERT_DIFFERS(css_h(spaced), css_h(lower));
		TS_ASSERT_DIFFERS(css_h(tabbed), css_h(spaced));
		TS_ASSERT_DIFFERS(css_h(spaced), css_h(doublespaced));
	}

	void test_ignore_case_hash() {
		// Same as test_case_sensitive_string_hash, but case insensitive.
		const Common::String lower("test");
		const Common::String lower1("test");
		const Common::String mixed("tESt");
		const Common::String spaced("test ");
		const Common::String mixedspaced("tESt ");
		const Common::String doublespaced("test  ");
		const Common::String tabbed("test\t");

		Common::IgnoreCase_Hash ic_h;
		TS_ASSERT_EQUALS(ic_h(lower), ic_h(lower1));
		TS_ASSERT_EQUALS(ic_h(mixed), ic_h(lower));
		TS_ASSERT_EQUALS(ic_h(spaced), ic_h(mixedspaced));
		TS_ASSERT_DIFFERS(ic_h(tabbed), ic_h(lower));
		TS_ASSERT_DIFFERS(ic_h(spaced), ic_h(doublespaced));
	}

	void test_cpp_string_hash()
	{
		// We run the same tests with Hash<String>,
		// a template specialization of Hash, also a functor.
		// It is supposed to be case sensitive.

		const Common::String lower("test");
		const Common::String lower1("test");
		const Common::String mixed("tESt");
		const Common::String spaced("test ");
		const Common::String mixedspaced("tESt ");
		const Common::String doublespaced("test  ");
		const Common::String tabbed("test\t");

		Common::Hash<Common::String> h;
		TS_ASSERT_EQUALS(h(lower), h(lower1));
		TS_ASSERT_DIFFERS(h(mixed), h(lower));
		TS_ASSERT_DIFFERS(h(spaced), h(lower));
		TS_ASSERT_DIFFERS(h(tabbed), h(spaced));
		TS_ASSERT_DIFFERS(h(spaced), h(doublespaced));
	}

	void test_c_style_string_hash()
	{
		// Same as test_cpp_string_hash but with Hash<const char*>,
		// a template specialization of Hash, also a functor,
		// that works with C-Style strings.
		// It is supposed to be case sensitive.

		char lower[] = "test";
		char lower1[] = "test";
		char mixed[] = "tESt";
		char spaced[] = "test ";
		char mixedspaced[] = "tESt ";
		char doublespaced[] = "test  ";
		char tabbed[] = "test\t";

		Common::Hash<const char *> h;
		TS_ASSERT_EQUALS(h(lower), h(lower1));
		TS_ASSERT_DIFFERS(h(mixed), h(lower));
		TS_ASSERT_DIFFERS(h(spaced), h(lower));
		TS_ASSERT_DIFFERS(h(spaced), h(mixedspaced));
		TS_ASSERT_DIFFERS(h(tabbed), h(spaced));
		TS_ASSERT_DIFFERS(h(spaced), h(doublespaced));

	}


};