Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-id-cursor.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2011-2012 Brazil
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <gcutter.h>
20 #include <cppcutter.h>
21 
22 #include <grn-assertions.h>
23 #include <dat/id-cursor.hpp>
24 #include <dat/trie.hpp>
25 
26 #include <cstring>
27 
28 namespace
29 {
30  void create_trie(grn::dat::Trie *trie)
31  {
32  trie->create();
33  trie->insert("みかん", std::strlen("みかん"));
34  trie->insert("オレンジ", std::strlen("オレンジ"));
35  trie->insert("グレープフルーツ", std::strlen("グレープフルーツ"));
36  trie->insert("柚子", std::strlen("柚子"));
37  trie->insert("スダチ", std::strlen("スダチ"));
38  trie->insert("伊予柑", std::strlen("伊予柑"));
39  trie->insert("八朔", std::strlen("八朔"));
40  trie->insert("文旦", std::strlen("文旦"));
41  }
42 }
43 
44 namespace test_dat_id_cursor
45 {
46  void test_null(void)
47  {
48  grn::dat::Trie trie;
49  create_trie(&trie);
50 
51  grn::dat::IdCursor cursor;
52  cursor.open(trie, grn::dat::String(), grn::dat::String());
53  for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) {
54  const grn::dat::Key &key = cursor.next();
55  cppcut_assert_equal(true, key.is_valid());
56  cppcut_assert_equal(i, key.id());
57  }
58  cppcut_assert_equal(false, cursor.next().is_valid());
59  }
60 
61  void test_min_by_str(void)
62  {
63  grn::dat::Trie trie;
64  create_trie(&trie);
65 
66  grn::dat::UInt32 key_pos;
67  cppcut_assert_equal(true,
68  trie.search("スダチ", std::strlen("スダチ"), &key_pos));
69  const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id();
70 
71  grn::dat::IdCursor cursor;
72  cursor.open(trie, grn::dat::String("スダチ"), grn::dat::String());
73  for (grn::dat::UInt32 i = min_key_id; i <= trie.max_key_id(); ++i) {
74  const grn::dat::Key &key = cursor.next();
75  cppcut_assert_equal(true, key.is_valid());
76  cppcut_assert_equal(i, key.id());
77  }
78  cppcut_assert_equal(false, cursor.next().is_valid());
79  }
80 
81  void test_max_by_str(void)
82  {
83  grn::dat::Trie trie;
84  create_trie(&trie);
85 
86  grn::dat::UInt32 key_pos;
87  cppcut_assert_equal(true,
88  trie.search("オレンジ", std::strlen("オレンジ"), &key_pos));
89  const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id();
90 
91  grn::dat::IdCursor cursor;
92  cursor.open(trie, grn::dat::String(), grn::dat::String("オレンジ"));
93  for (grn::dat::UInt32 i = trie.min_key_id(); i <= max_key_id; ++i) {
94  const grn::dat::Key &key = cursor.next();
95  cppcut_assert_equal(true, key.is_valid());
96  cppcut_assert_equal(i, key.id());
97  }
98  cppcut_assert_equal(false, cursor.next().is_valid());
99  }
100 
102  {
103  grn::dat::Trie trie;
104  create_trie(&trie);
105 
106  grn::dat::UInt32 key_pos;
107  cppcut_assert_equal(true,
108  trie.search("みかん", std::strlen("みかん"), &key_pos));
109  const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id();
110  cppcut_assert_equal(true,
111  trie.search("八朔", std::strlen("八朔"), &key_pos));
112  const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id();
113 
114  grn::dat::IdCursor cursor;
115  cursor.open(trie, grn::dat::String("みかん"), grn::dat::String("八朔"));
116  for (grn::dat::UInt32 i = min_key_id; i <= max_key_id; ++i) {
117  const grn::dat::Key &key = cursor.next();
118  cppcut_assert_equal(true, key.is_valid());
119  cppcut_assert_equal(i, key.id());
120  }
121  cppcut_assert_equal(false, cursor.next().is_valid());
122  }
123 
124  void test_invalid_id(void)
125  {
126  grn::dat::Trie trie;
127  create_trie(&trie);
128 
129  grn::dat::IdCursor cursor;
131  for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) {
132  const grn::dat::Key &key = cursor.next();
133  cppcut_assert_equal(true, key.is_valid());
134  cppcut_assert_equal(i, key.id());
135  }
136  cppcut_assert_equal(false, cursor.next().is_valid());
137  }
138 
139  void test_min_by_id(void)
140  {
141  grn::dat::Trie trie;
142  create_trie(&trie);
143 
144  grn::dat::UInt32 key_pos;
145  cppcut_assert_equal(true,
146  trie.search("伊予柑", std::strlen("伊予柑"), &key_pos));
147  const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id();
148 
149  grn::dat::IdCursor cursor;
150  cursor.open(trie, min_key_id, grn::dat::INVALID_KEY_ID);
151  for (grn::dat::UInt32 i = min_key_id; i <= trie.max_key_id(); ++i) {
152  const grn::dat::Key &key = cursor.next();
153  cppcut_assert_equal(true, key.is_valid());
154  cppcut_assert_equal(i, key.id());
155  }
156  cppcut_assert_equal(false, cursor.next().is_valid());
157  }
158 
159  void test_max_by_id(void)
160  {
161  grn::dat::Trie trie;
162  create_trie(&trie);
163 
164  grn::dat::UInt32 key_pos;
165  cppcut_assert_equal(true,
166  trie.search("柚子", std::strlen("柚子"), &key_pos));
167  const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id();
168 
169  grn::dat::IdCursor cursor;
170  cursor.open(trie, grn::dat::INVALID_KEY_ID, max_key_id);
171  for (grn::dat::UInt32 i = trie.min_key_id(); i <= max_key_id; ++i) {
172  const grn::dat::Key &key = cursor.next();
173  cppcut_assert_equal(true, key.is_valid());
174  cppcut_assert_equal(i, key.id());
175  }
176  cppcut_assert_equal(false, cursor.next().is_valid());
177  }
178 
180  {
181  grn::dat::Trie trie;
182  create_trie(&trie);
183 
184  grn::dat::UInt32 key_pos;
185  cppcut_assert_equal(true,
186  trie.search("グレープフルーツ", std::strlen("グレープフルーツ"), &key_pos));
187  const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id();
188  cppcut_assert_equal(true,
189  trie.search("文旦", std::strlen("文旦"), &key_pos));
190  const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id();
191 
192  grn::dat::IdCursor cursor;
193  cursor.open(trie, min_key_id, max_key_id);
194  for (grn::dat::UInt32 i = min_key_id; i <= max_key_id; ++i) {
195  const grn::dat::Key &key = cursor.next();
196  cppcut_assert_equal(true, key.is_valid());
197  cppcut_assert_equal(i, key.id());
198  }
199  cppcut_assert_equal(false, cursor.next().is_valid());
200  }
201 
202  void test_offset(void)
203  {
204  grn::dat::Trie trie;
205  create_trie(&trie);
206 
207  grn::dat::IdCursor cursor;
208 
209  cursor.open(trie, 2, 5, 0);
210  for (grn::dat::UInt32 i = 2; i <= 5; ++i) {
211  const grn::dat::Key &key = cursor.next();
212  cppcut_assert_equal(true, key.is_valid());
213  cppcut_assert_equal(i, key.id());
214  }
215  cppcut_assert_equal(false, cursor.next().is_valid());
216 
218  for (grn::dat::UInt32 i = trie.min_key_id() + 5;
219  i <= trie.max_key_id(); ++i) {
220  const grn::dat::Key &key = cursor.next();
221  cppcut_assert_equal(true, key.is_valid());
222  cppcut_assert_equal(i, key.id());
223  }
224  cppcut_assert_equal(false, cursor.next().is_valid());
225 
226  cursor.open(trie, 3, 7, 2);
227  for (grn::dat::UInt32 i = 3 + 2; i <= 7; ++i) {
228  const grn::dat::Key &key = cursor.next();
229  cppcut_assert_equal(true, key.is_valid());
230  cppcut_assert_equal(i, key.id());
231  }
232  cppcut_assert_equal(false, cursor.next().is_valid());
233 
234  cursor.open(trie, 2, 5, 100);
235  cppcut_assert_equal(false, cursor.next().is_valid());
236  }
237 
239  grn::dat::Trie trie;
240  create_trie(&trie);
241 
242  for (grn::dat::UInt32 i = 1; i <= trie.max_key_id(); i += 2) {
243  trie.remove(i);
244  }
245 
246  grn::dat::IdCursor cursor;
247 
248  cursor.open(trie, 0, 0, 1);
249  for (grn::dat::UInt32 i = 4; i <= trie.max_key_id(); i += 2) {
250  const grn::dat::Key &key = cursor.next();
251  cppcut_assert_equal(true, key.is_valid());
252  }
253  cppcut_assert_equal(false, cursor.next().is_valid());
254 
255  cursor.open(trie, 0, 0, 2);
256  for (grn::dat::UInt32 i = 6; i <= trie.max_key_id(); i += 2) {
257  const grn::dat::Key &key = cursor.next();
258  cppcut_assert_equal(true, key.is_valid());
259  }
260  cppcut_assert_equal(false, cursor.next().is_valid());
261 
262  cursor.open(trie, 0, 0, trie.max_key_id() / 2);
263  cppcut_assert_equal(false, cursor.next().is_valid());
264  }
265 
266  void test_limit(void)
267  {
268  grn::dat::Trie trie;
269  create_trie(&trie);
270 
271  grn::dat::IdCursor cursor;
272 
273  cursor.open(trie, 3, 6, 0, grn::dat::MAX_UINT32);
274  for (grn::dat::UInt32 i = 3; i <= 6; ++i) {
275  const grn::dat::Key &key = cursor.next();
276  cppcut_assert_equal(true, key.is_valid());
277  cppcut_assert_equal(i, key.id());
278  }
279  cppcut_assert_equal(false, cursor.next().is_valid());
280 
281  cursor.open(trie, grn::dat::INVALID_KEY_ID,
283  for (grn::dat::UInt32 i = trie.min_key_id();
284  i < (trie.min_key_id() + 3); ++i) {
285  const grn::dat::Key &key = cursor.next();
286  cppcut_assert_equal(true, key.is_valid());
287  cppcut_assert_equal(i, key.id());
288  }
289  cppcut_assert_equal(false, cursor.next().is_valid());
290 
291  cursor.open(trie, 3, 7, 1, 2);
292  for (grn::dat::UInt32 i = 3 + 1; i < (3 + 1 + 2); ++i) {
293  const grn::dat::Key &key = cursor.next();
294  cppcut_assert_equal(true, key.is_valid());
295  cppcut_assert_equal(i, key.id());
296  }
297  cppcut_assert_equal(false, cursor.next().is_valid());
298 
299  cursor.open(trie, 2, 5, 0, 0);
300  cppcut_assert_equal(false, cursor.next().is_valid());
301  }
302 
304  grn::dat::Trie trie;
305  create_trie(&trie);
306 
307  for (grn::dat::UInt32 i = 1; i <= trie.max_key_id(); i += 2) {
308  trie.remove(i);
309  }
310 
311  grn::dat::IdCursor cursor;
312 
313  cursor.open(trie, 0, 0, 0, trie.max_key_id() / 2);
314  for (grn::dat::UInt32 i = 2; i <= trie.max_key_id(); i += 2) {
315  const grn::dat::Key &key = cursor.next();
316  cppcut_assert_equal(true, key.is_valid());
317  }
318  cppcut_assert_equal(false, cursor.next().is_valid());
319 
320  cursor.open(trie, 0, 0, 0, 1);
321  for (grn::dat::UInt32 i = 2; i <= 2; i += 2) {
322  const grn::dat::Key &key = cursor.next();
323  cppcut_assert_equal(true, key.is_valid());
324  }
325  cppcut_assert_equal(false, cursor.next().is_valid());
326 
327  cursor.open(trie, 0, 0, 0, 3);
328  for (grn::dat::UInt32 i = 2; i <= (2 + 4); i += 2) {
329  const grn::dat::Key &key = cursor.next();
330  cppcut_assert_equal(true, key.is_valid());
331  }
332  cppcut_assert_equal(false, cursor.next().is_valid());
333  }
334 
336  {
337  grn::dat::Trie trie;
338  create_trie(&trie);
339 
340  grn::dat::IdCursor cursor;
341 
344  for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) {
345  const grn::dat::Key &key = cursor.next();
346  cppcut_assert_equal(true, key.is_valid());
347  cppcut_assert_equal(i, key.id());
348  }
349  cppcut_assert_equal(false, cursor.next().is_valid());
350 
351  cursor.open(trie, 2, 7, 0, grn::dat::MAX_UINT32,
353  for (grn::dat::UInt32 i = 2; i <= 7; ++i) {
354  const grn::dat::Key &key = cursor.next();
355  cppcut_assert_equal(true, key.is_valid());
356  cppcut_assert_equal(i, key.id());
357  }
358  cppcut_assert_equal(false, cursor.next().is_valid());
359 
360  cursor.open(trie, 3, 6, 1, 5, grn::dat::ASCENDING_CURSOR);
361  for (grn::dat::UInt32 i = 3 + 1; i <= 6; ++i) {
362  const grn::dat::Key &key = cursor.next();
363  cppcut_assert_equal(true, key.is_valid());
364  cppcut_assert_equal(i, key.id());
365  }
366  cppcut_assert_equal(false, cursor.next().is_valid());
367  }
368 
370  {
371  grn::dat::Trie trie;
372  create_trie(&trie);
373 
374  grn::dat::IdCursor cursor;
375 
378  for (grn::dat::UInt32 i = trie.max_key_id(); i >= trie.min_key_id(); --i) {
379  const grn::dat::Key &key = cursor.next();
380  cppcut_assert_equal(true, key.is_valid());
381  cppcut_assert_equal(i, key.id());
382  }
383  cppcut_assert_equal(false, cursor.next().is_valid());
384 
385  cursor.open(trie, 2, 7, 0, grn::dat::MAX_UINT32,
387  for (grn::dat::UInt32 i = 7; i >= 2; --i) {
388  const grn::dat::Key &key = cursor.next();
389  cppcut_assert_equal(true, key.is_valid());
390  cppcut_assert_equal(i, key.id());
391  }
392  cppcut_assert_equal(false, cursor.next().is_valid());
393 
394  cursor.open(trie, 2, 8, 2, 3, grn::dat::DESCENDING_CURSOR);
395  for (grn::dat::UInt32 i = 8 - 2; i > (8 - 2 - 3); --i) {
396  const grn::dat::Key &key = cursor.next();
397  cppcut_assert_equal(true, key.is_valid());
398  cppcut_assert_equal(i, key.id());
399  }
400  cppcut_assert_equal(false, cursor.next().is_valid());
401  }
402 
404  {
405  grn::dat::Trie trie;
406  create_trie(&trie);
407 
408  grn::dat::IdCursor cursor;
409 
413  for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) {
414  const grn::dat::Key &key = cursor.next();
415  cppcut_assert_equal(true, key.is_valid());
416  cppcut_assert_equal(i, key.id());
417  }
418  cppcut_assert_equal(false, cursor.next().is_valid());
419 
420  cursor.open(trie, trie.min_key_id(), trie.max_key_id(),
423  for (grn::dat::UInt32 i = trie.min_key_id() + 1;
424  i <= (trie.max_key_id() - 1); ++i) {
425  const grn::dat::Key &key = cursor.next();
426  cppcut_assert_equal(true, key.is_valid());
427  cppcut_assert_equal(i, key.id());
428  }
429  cppcut_assert_equal(false, cursor.next().is_valid());
430 
431  cursor.open(trie, 2, 7, 1, 100, grn::dat::EXCEPT_LOWER_BOUND);
432  for (grn::dat::UInt32 i = 2 + 1 + 1; i <= 7; ++i) {
433  const grn::dat::Key &key = cursor.next();
434  cppcut_assert_equal(true, key.is_valid());
435  cppcut_assert_equal(i, key.id());
436  }
437  cppcut_assert_equal(false, cursor.next().is_valid());
438 
439  cursor.open(trie, 2, 7, 1, 100, grn::dat::EXCEPT_UPPER_BOUND);
440  for (grn::dat::UInt32 i = 2 + 1; i <= (7 - 1); ++i) {
441  const grn::dat::Key &key = cursor.next();
442  cppcut_assert_equal(true, key.is_valid());
443  cppcut_assert_equal(i, key.id());
444  }
445  cppcut_assert_equal(false, cursor.next().is_valid());
446  }
447 }