1 #include <cstdlib>
2 #include <cstring>
3 #include <ctime>
4 #include <string>
5 #include <vector>
6
7 #include <marisa.h>
8
9 #include "marisa-assert.h"
10
11 namespace {
12
TestTypes()13 void TestTypes() {
14 TEST_START();
15
16 ASSERT(sizeof(marisa_uint8) == 1);
17 ASSERT(sizeof(marisa_uint16) == 2);
18 ASSERT(sizeof(marisa_uint32) == 4);
19 ASSERT(sizeof(marisa_uint64) == 8);
20
21 ASSERT(MARISA_WORD_SIZE == (sizeof(std::size_t) * 8));
22
23 ASSERT(MARISA_UINT8_MAX == 0xFFU);
24 ASSERT(MARISA_UINT16_MAX == 0xFFFFU);
25 ASSERT(MARISA_UINT32_MAX == 0xFFFFFFFFU);
26 ASSERT(MARISA_UINT64_MAX == 0xFFFFFFFFFFFFFFFFULL);
27
28 ASSERT(sizeof(marisa::UInt8) == 1);
29 ASSERT(sizeof(marisa::UInt16) == 2);
30 ASSERT(sizeof(marisa::UInt32) == 4);
31 ASSERT(sizeof(marisa::UInt64) == 8);
32
33 TEST_END();
34 }
35
TestSwap()36 void TestSwap() {
37 TEST_START();
38
39 int x = 100, y = 200;
40 marisa::swap(x, y);
41 ASSERT(x == 200);
42 ASSERT(y == 100);
43
44 double a = 1.23, b = 2.34;
45 marisa::swap(a, b);
46 ASSERT(a == 2.34);
47 ASSERT(b == 1.23);
48
49 TEST_END();
50 }
51
TestException()52 void TestException() {
53 TEST_START();
54
55 try {
56 MARISA_THROW(MARISA_OK, "Message");
57 } catch (const marisa::Exception &ex) {
58 ASSERT(std::strcmp(ex.filename(), __FILE__) == 0);
59 ASSERT(ex.line() == (__LINE__ - 3));
60 ASSERT(ex.error_code() == MARISA_OK);
61 ASSERT(std::strstr(ex.error_message(), "Message") != NULL);
62 }
63
64 EXCEPT(MARISA_THROW(MARISA_OK, "OK"), MARISA_OK);
65 EXCEPT(MARISA_THROW(MARISA_NULL_ERROR, "NULL"), MARISA_NULL_ERROR);
66
67 TEST_END();
68 }
69
TestKey()70 void TestKey() {
71 TEST_START();
72
73 const char * const str = "apple";
74
75 marisa::Key key;
76
77 ASSERT(key.ptr() == NULL);
78 ASSERT(key.length() == 0);
79
80 key.set_str(str);
81
82 ASSERT(key.ptr() == str);
83 ASSERT(key.length() == std::strlen(str));
84
85 key.set_str(str, 4);
86
87 ASSERT(key.ptr() == str);
88 ASSERT(key.length() == 4);
89
90 key.set_weight(1.0);
91
92 ASSERT(key.weight() == 1.0);
93
94 key.set_id(100);
95
96 ASSERT(key.id() == 100);
97
98 TEST_END();
99 }
100
TestKeyset()101 void TestKeyset() {
102 TEST_START();
103
104 marisa::Keyset keyset;
105
106 ASSERT(keyset.size() == 0);
107 ASSERT(keyset.empty());
108 ASSERT(keyset.total_length() == 0);
109
110 std::vector<std::string> keys;
111 keys.push_back("apple");
112 keys.push_back("orange");
113 keys.push_back("banana");
114
115 std::size_t total_length = 0;
116 for (std::size_t i = 0; i < keys.size(); ++i) {
117 keyset.push_back(keys[i].c_str());
118 ASSERT(keyset.size() == (i + 1));
119 ASSERT(!keyset.empty());
120
121 total_length += keys[i].length();
122 ASSERT(keyset.total_length() == total_length);
123
124 ASSERT(keyset[i].length() == keys[i].length());
125 ASSERT(std::memcmp(keyset[i].ptr(), keys[i].c_str(),
126 keyset[i].length()) == 0);
127 ASSERT(keyset[i].weight() == 1.0);
128 }
129
130 keyset.clear();
131
132 marisa::Key key;
133
134 key.set_str("123");
135 keyset.push_back(key);
136 ASSERT(keyset[0].length() == 3);
137 ASSERT(std::memcmp(keyset[0].ptr(), "123", 3) == 0);
138
139 key.set_str("456");
140 keyset.push_back(key, '\0');
141 ASSERT(keyset[1].length() == 3);
142 ASSERT(std::memcmp(keyset[1].ptr(), "456", 3) == 0);
143 ASSERT(std::strcmp(keyset[1].ptr(), "456") == 0);
144
145 key.set_str("789");
146 keyset.push_back(key, '0');
147 ASSERT(keyset[2].length() == 3);
148 ASSERT(std::memcmp(keyset[2].ptr(), "789", 3) == 0);
149 ASSERT(std::memcmp(keyset[2].ptr(), "7890", 4) == 0);
150
151 ASSERT(keyset.size() == 3);
152
153 keyset.clear();
154
155 ASSERT(keyset.size() == 0);
156 ASSERT(keyset.total_length() == 0);
157
158 keys.resize(1000);
159 std::vector<float> weights(keys.size());
160
161 total_length = 0;
162 for (std::size_t i = 0; i < keys.size(); ++i) {
163 keys[i].resize(std::rand() % (marisa::Keyset::EXTRA_BLOCK_SIZE * 2));
164 for (std::size_t j = 0; j < keys[i].length(); ++j) {
165 keys[i][j] = (char)(std::rand() & 0xFF);
166 }
167 double weight = 100.0 * static_cast<double>(std::rand()) /
168 static_cast<double>(RAND_MAX);
169 weights[i] = static_cast<float>(weight);
170
171 keyset.push_back(keys[i].c_str(), keys[i].length(), weights[i]);
172 total_length += keys[i].length();
173 ASSERT(keyset.total_length() == total_length);
174 }
175
176 ASSERT(keyset.size() == keys.size());
177 for (std::size_t i = 0; i < keys.size(); ++i) {
178 ASSERT(keyset[i].length() == keys[i].length());
179 ASSERT(std::memcmp(keyset[i].ptr(), keys[i].c_str(),
180 keyset[i].length()) == 0);
181 ASSERT(keyset[i].weight() == weights[i]);
182 }
183
184 keyset.reset();
185
186 ASSERT(keyset.size() == 0);
187 ASSERT(keyset.total_length() == 0);
188
189 total_length = 0;
190 for (std::size_t i = 0; i < keys.size(); ++i) {
191 keys[i].resize(std::rand() % (marisa::Keyset::EXTRA_BLOCK_SIZE * 2));
192 for (std::size_t j = 0; j < keys[i].length(); ++j) {
193 keys[i][j] = (char)(std::rand() & 0xFF);
194 }
195 double weight = 100.0 * static_cast<double>(std::rand()) /
196 static_cast<double>(RAND_MAX);
197 weights[i] = static_cast<float>(weight);
198
199 keyset.push_back(keys[i].c_str(), keys[i].length(), weights[i]);
200 total_length += keys[i].length();
201 ASSERT(keyset.total_length() == total_length);
202 }
203
204 ASSERT(keyset.size() == keys.size());
205 for (std::size_t i = 0; i < keys.size(); ++i) {
206 ASSERT(keyset[i].length() == keys[i].length());
207 ASSERT(std::memcmp(keyset[i].ptr(), keys[i].c_str(),
208 keyset[i].length()) == 0);
209 ASSERT(keyset[i].weight() == weights[i]);
210 }
211
212 TEST_END();
213 }
214
TestQuery()215 void TestQuery() {
216 TEST_START();
217
218 marisa::Query query;
219
220 ASSERT(query.ptr() == NULL);
221 ASSERT(query.length() == 0);
222 ASSERT(query.id() == 0);
223
224 const char *str = "apple";
225 query.set_str(str);
226
227 ASSERT(query.ptr() == str);
228 ASSERT(query.length() == std::strlen(str));
229
230 query.set_str(str, 3);
231
232 ASSERT(query.ptr() == str);
233 ASSERT(query.length() == 3);
234
235 query.set_id(100);
236
237 ASSERT(query.id() == 100);
238
239 query.clear();
240
241 ASSERT(query.ptr() == NULL);
242 ASSERT(query.length() == 0);
243 ASSERT(query.id() == 0);
244
245 TEST_END();
246 }
247
TestAgent()248 void TestAgent() {
249 TEST_START();
250
251 marisa::Agent agent;
252
253 ASSERT(agent.query().ptr() == NULL);
254 ASSERT(agent.query().length() == 0);
255 ASSERT(agent.query().id() == 0);
256
257 ASSERT(agent.key().ptr() == NULL);
258 ASSERT(agent.key().length() == 0);
259
260 ASSERT(!agent.has_state());
261
262 const char *query_str = "query";
263 const char *key_str = "key";
264
265 agent.set_query(query_str);
266 agent.set_query(123);
267 agent.set_key(key_str);
268 agent.set_key(234);
269
270 ASSERT(agent.query().ptr() == query_str);
271 ASSERT(agent.query().length() == std::strlen(query_str));
272 ASSERT(agent.query().id() == 123);
273
274 ASSERT(agent.key().ptr() == key_str);
275 ASSERT(agent.key().length() == std::strlen(key_str));
276 ASSERT(agent.key().id() == 234);
277
278 agent.init_state();
279
280 ASSERT(agent.has_state());
281
282 EXCEPT(agent.init_state(), MARISA_STATE_ERROR);
283
284 agent.clear();
285
286 ASSERT(agent.query().ptr() == NULL);
287 ASSERT(agent.query().length() == 0);
288 ASSERT(agent.query().id() == 0);
289
290 ASSERT(agent.key().ptr() == NULL);
291 ASSERT(agent.key().length() == 0);
292
293 ASSERT(!agent.has_state());
294
295 TEST_END();
296 }
297
298 } // namespace
299
main()300 int main() try {
301 TestTypes();
302 TestSwap();
303 TestException();
304 TestKey();
305 TestKeyset();
306 TestQuery();
307 TestAgent();
308
309 return 0;
310 } catch (const marisa::Exception &ex) {
311 std::cerr << ex.what() << std::endl;
312 throw;
313 }
314