null+****@clear*****
null+****@clear*****
2011年 11月 10日 (木) 18:30:51 JST
Susumu Yata 2011-11-10 09:30:51 +0000 (Thu, 10 Nov 2011) New Revision: 2e70305668d4dce455bab2bb6571677a0fe84351 Log: add a test of grn::dat::IdCursor and skeltons of other cursors' tests. Added files: test/unit/core/dat/test-id-cursor.cpp test/unit/core/dat/test-key-cursor.cpp test/unit/core/dat/test-predictive-cursor.cpp test/unit/core/dat/test-prefix-cursor.cpp Modified files: test/unit/core/dat/Makefile.am Modified: test/unit/core/dat/Makefile.am (+8 -10) =================================================================== --- test/unit/core/dat/Makefile.am 2011-11-10 06:31:34 +0000 (22dae9e) +++ test/unit/core/dat/Makefile.am 2011-11-10 09:30:51 +0000 (aad3902) @@ -8,17 +8,15 @@ noinst_LTLIBRARIES = \ test-entry.la \ test-file.la \ test-header.la \ + test-id-cursor.la \ + test-key-cursor.la \ test-key.la \ test-node.la \ + test-predictive-cursor.la \ + test-prefix-cursor.la \ test-string.la \ test-trie.la \ test-vector.la - -# test-id-cursor.la \ -# test-key-cursor.la \ -# test-predictive-cursor.la \ -# test-prefix-cursor.la - endif INCLUDES = \ @@ -49,12 +47,12 @@ test_cursor_factory_la_SOURCES = test-cursor-factory.cpp test_entry_la_SOURCES = test-entry.cpp test_file_la_SOURCES = test-file.cpp test_header_la_SOURCES = test-header.cpp -#test_id_cursor_la_SOURCES = test-id-cursor.cpp -#test_key_cursor_la_SOURCES = test-key-cursor.cpp +test_id_cursor_la_SOURCES = test-id-cursor.cpp +test_key_cursor_la_SOURCES = test-key-cursor.cpp test_key_la_SOURCES = test-key.cpp test_node_la_SOURCES = test-node.cpp -#test_predictive_cursor_la_SOURCES = test-predictive-cursor.cpp -#test_prefix_cursor_la_SOURCES = test-prefix-cursor.cpp +test_predictive_cursor_la_SOURCES = test-predictive-cursor.cpp +test_prefix_cursor_la_SOURCES = test-prefix-cursor.cpp test_string_la_SOURCES = test-string.cpp test_trie_la_SOURCES = test-trie.cpp test_vector_la_SOURCES = test-vector.cpp Added: test/unit/core/dat/test-id-cursor.cpp (+375 -0) 100644 =================================================================== --- /dev/null +++ test/unit/core/dat/test-id-cursor.cpp 2011-11-10 09:30:51 +0000 (c184e22) @@ -0,0 +1,375 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2011 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <gcutter.h> +#include <cppcutter.h> + +#include <grn-assertions.h> +#include <dat/id-cursor.hpp> +#include <dat/trie.hpp> + +#include <cstring> + +namespace +{ + void create_trie(grn::dat::Trie *trie) + { + trie->create(); + trie->insert("みかん", std::strlen("みかん")); + trie->insert("オレンジ", std::strlen("オレンジ")); + trie->insert("グレープフルーツ", std::strlen("グレープフルーツ")); + trie->insert("柚子", std::strlen("柚子")); + trie->insert("スダチ", std::strlen("スダチ")); + trie->insert("伊予柑", std::strlen("伊予柑")); + trie->insert("八朔", std::strlen("八朔")); + trie->insert("文旦", std::strlen("文旦")); + } +} + +namespace test_dat_id_cursor +{ + void test_null(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + cursor.open(trie, grn::dat::String(), grn::dat::String()); + for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_min_by_str(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::UInt32 key_pos; + cut_assert(trie.search("スダチ", std::strlen("スダチ"), &key_pos)); + const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id(); + + grn::dat::IdCursor cursor; + cursor.open(trie, grn::dat::String("スダチ"), grn::dat::String()); + for (grn::dat::UInt32 i = min_key_id; i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_max_by_str(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::UInt32 key_pos; + cut_assert(trie.search("オレンジ", std::strlen("オレンジ"), &key_pos)); + const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id(); + + grn::dat::IdCursor cursor; + cursor.open(trie, grn::dat::String(), grn::dat::String("オレンジ")); + for (grn::dat::UInt32 i = trie.min_key_id(); i <= max_key_id; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_min_max_by_str(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::UInt32 key_pos; + cut_assert(trie.search("みかん", std::strlen("みかん"), &key_pos)); + const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id(); + cut_assert(trie.search("八朔", std::strlen("八朔"), &key_pos)); + const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id(); + + grn::dat::IdCursor cursor; + cursor.open(trie, grn::dat::String("みかん"), grn::dat::String("八朔")); + for (grn::dat::UInt32 i = min_key_id; i <= max_key_id; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_invalid_id(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + cursor.open(trie, grn::dat::INVALID_KEY_ID, grn::dat::INVALID_KEY_ID); + for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_min_by_id(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::UInt32 key_pos; + cut_assert(trie.search("伊予柑", std::strlen("伊予柑"), &key_pos)); + const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id(); + + grn::dat::IdCursor cursor; + cursor.open(trie, min_key_id, grn::dat::INVALID_KEY_ID); + for (grn::dat::UInt32 i = min_key_id; i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_max_by_id(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::UInt32 key_pos; + cut_assert(trie.search("柚子", std::strlen("柚子"), &key_pos)); + const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id(); + + grn::dat::IdCursor cursor; + cursor.open(trie, grn::dat::INVALID_KEY_ID, max_key_id); + for (grn::dat::UInt32 i = trie.min_key_id(); i <= max_key_id; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_min_max_by_id(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::UInt32 key_pos; + cut_assert(trie.search("グレープフルーツ", std::strlen("グレープフルーツ"), &key_pos)); + const grn::dat::UInt32 min_key_id = trie.get_key(key_pos).id(); + cut_assert(trie.search("文旦", std::strlen("文旦"), &key_pos)); + const grn::dat::UInt32 max_key_id = trie.get_key(key_pos).id(); + + grn::dat::IdCursor cursor; + cursor.open(trie, min_key_id, max_key_id); + for (grn::dat::UInt32 i = min_key_id; i <= max_key_id; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_offset(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + + cursor.open(trie, 2, 5, 0); + for (grn::dat::UInt32 i = 2; i <= 5; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, grn::dat::INVALID_KEY_ID, grn::dat::INVALID_KEY_ID, 5); + for (grn::dat::UInt32 i = trie.min_key_id() + 5; i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 3, 7, 2); + for (grn::dat::UInt32 i = 3 + 2; i <= 7; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 5, 100); + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_limit(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + + cursor.open(trie, 3, 6, 0, grn::dat::UINT32_MAX); + for (grn::dat::UInt32 i = 3; i <= 6; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, grn::dat::INVALID_KEY_ID, grn::dat::INVALID_KEY_ID, 0, 3); + for (grn::dat::UInt32 i = trie.min_key_id(); i < (trie.min_key_id() + 3); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 3, 7, 1, 2); + for (grn::dat::UInt32 i = 3 + 1; i < (3 + 1 + 2); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 5, 0, 0); + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_ascending_cursor(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + + cursor.open(trie, grn::dat::INVALID_KEY_ID, grn::dat::INVALID_KEY_ID, + 0, grn::dat::UINT32_MAX, grn::dat::ASCENDING_CURSOR); + for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 7, 0, grn::dat::UINT32_MAX, + grn::dat::ASCENDING_CURSOR); + for (grn::dat::UInt32 i = 2; i <= 7; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 3, 6, 1, 5, grn::dat::ASCENDING_CURSOR); + for (grn::dat::UInt32 i = 3 + 1; i <= 6; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_descending_cursor(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + + cursor.open(trie, grn::dat::INVALID_KEY_ID, grn::dat::INVALID_KEY_ID, + 0, grn::dat::UINT32_MAX, grn::dat::DESCENDING_CURSOR); + for (grn::dat::UInt32 i = trie.max_key_id(); i >= trie.min_key_id(); --i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 7, 0, grn::dat::UINT32_MAX, + grn::dat::DESCENDING_CURSOR); + for (grn::dat::UInt32 i = 7; i >= 2; --i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 8, 2, 3, grn::dat::DESCENDING_CURSOR); + for (grn::dat::UInt32 i = 8 - 2; i > (8 - 2 - 3); --i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } + + void test_except_boundary(void) + { + grn::dat::Trie trie; + create_trie(&trie); + + grn::dat::IdCursor cursor; + + cursor.open(trie, grn::dat::INVALID_KEY_ID, grn::dat::INVALID_KEY_ID, + 0, grn::dat::UINT32_MAX, + grn::dat::EXCEPT_LOWER_BOUND | grn::dat::EXCEPT_UPPER_BOUND); + for (grn::dat::UInt32 i = trie.min_key_id(); i <= trie.max_key_id(); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, trie.min_key_id(), trie.max_key_id(), + 0, grn::dat::UINT32_MAX, + grn::dat::EXCEPT_LOWER_BOUND | grn::dat::EXCEPT_UPPER_BOUND); + for (grn::dat::UInt32 i = trie.min_key_id() + 1; i <= (trie.max_key_id() - 1); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 7, 1, 100, grn::dat::EXCEPT_LOWER_BOUND); + for (grn::dat::UInt32 i = 2 + 1 + 1; i <= 7; ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + + cursor.open(trie, 2, 7, 1, 100, grn::dat::EXCEPT_UPPER_BOUND); + for (grn::dat::UInt32 i = 2 + 1; i <= (7 - 1); ++i) { + const grn::dat::Key &key = cursor.next(); + cppcut_assert_equal(key.is_valid(), true); + cppcut_assert_equal(key.id(), i); + } + cppcut_assert_equal(cursor.next().is_valid(), false); + } +} Added: test/unit/core/dat/test-key-cursor.cpp (+28 -0) 100644 =================================================================== --- /dev/null +++ test/unit/core/dat/test-key-cursor.cpp 2011-11-10 09:30:51 +0000 (f99d9fd) @@ -0,0 +1,28 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2011 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <gcutter.h> +#include <cppcutter.h> + +#include <grn-assertions.h> +#include <dat/key-cursor.hpp> +#include <dat/trie.hpp> + +namespace test_dat_key_cursor +{ +} Added: test/unit/core/dat/test-predictive-cursor.cpp (+28 -0) 100644 =================================================================== --- /dev/null +++ test/unit/core/dat/test-predictive-cursor.cpp 2011-11-10 09:30:51 +0000 (69759ce) @@ -0,0 +1,28 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2011 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <gcutter.h> +#include <cppcutter.h> + +#include <grn-assertions.h> +#include <dat/predictive-cursor.hpp> +#include <dat/trie.hpp> + +namespace test_dat_predictive_cursor +{ +} Added: test/unit/core/dat/test-prefix-cursor.cpp (+28 -0) 100644 =================================================================== --- /dev/null +++ test/unit/core/dat/test-prefix-cursor.cpp 2011-11-10 09:30:51 +0000 (837bf00) @@ -0,0 +1,28 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2011 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <gcutter.h> +#include <cppcutter.h> + +#include <grn-assertions.h> +#include <dat/prefix-cursor.hpp> +#include <dat/trie.hpp> + +namespace test_dat_prefix_cursor +{ +}