susumu.yata
null+****@clear*****
Mon Sep 8 16:57:41 JST 2014
susumu.yata 2014-09-08 16:57:41 +0900 (Mon, 08 Sep 2014) New Revision: 2214b1a3ae2de40cfbe0c39e8862a3aa7cdf9193 https://github.com/groonga/grnxx/commit/2214b1a3ae2de40cfbe0c39e8862a3aa7cdf9193 Message: Add tests for Index. Added files: test/test_index.cpp Modified files: .gitignore test/Makefile.am Modified: .gitignore (+1 -0) =================================================================== --- .gitignore 2014-09-08 16:57:15 +0900 (e1895f7) +++ .gitignore 2014-09-08 16:57:41 +0900 (38e351f) @@ -33,6 +33,7 @@ test/test_array test/test_db test/test_table test/test_column +test/test_index test/test_expression test/test_sorter test/test_pipeline Modified: test/Makefile.am (+4 -0) =================================================================== --- test/Makefile.am 2014-09-08 16:57:15 +0900 (462e5ca) +++ test/Makefile.am 2014-09-08 16:57:41 +0900 (3550995) @@ -3,6 +3,7 @@ TESTS = \ test_db \ test_table \ test_column \ + test_index \ test_expression \ test_sorter \ test_pipeline \ @@ -22,6 +23,9 @@ test_table_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la test_column_SOURCES = test_column.cpp test_column_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la +test_index_SOURCES = test_index.cpp +test_index_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la + test_expression_SOURCES = test_expression.cpp test_expression_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la Added: test/test_index.cpp (+221 -0) 100644 =================================================================== --- /dev/null +++ test/test_index.cpp 2014-09-08 16:57:41 +0900 (fe0fd79) @@ -0,0 +1,221 @@ +/* + Copyright (C) 2012-2014 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include <cassert> +#include <iostream> +#include <random> + +#include "grnxx/column.hpp" +#include "grnxx/cursor.hpp" +#include "grnxx/datum.hpp" +#include "grnxx/db.hpp" +#include "grnxx/error.hpp" +#include "grnxx/index.hpp" +#include "grnxx/table.hpp" + +std::mt19937_64 mersenne_twister; + +void test_index() { + grnxx::Error error; + + // Create a database with the default options. + auto db = grnxx::open_db(&error, ""); + assert(db); + + // Create a table with the default options. + auto table = db->create_table(&error, "Table"); + assert(table); + + // Append the first row. + grnxx::Int row_id; + assert(table->insert_row(&error, grnxx::NULL_ROW_ID, + grnxx::Datum(), &row_id)); + + // Create a column named "Column". + auto column = table->create_column(&error, "Column", grnxx::INT_DATA); + assert(column); + + // Create an index named "Index". + auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX); + assert(index); + assert(index->column() == column); + assert(index->name() == "Index"); + assert(index->type() == grnxx::TREE_INDEX); +} + +void test_set_and_index() { + constexpr grnxx::Int NUM_ROWS = 1 << 16; + + grnxx::Error error; + + // Create a database with the default options. + auto db = grnxx::open_db(&error, ""); + assert(db); + + // Create a table with the default options. + auto table = db->create_table(&error, "Table"); + assert(table); + + // Create a column. + auto column = table->create_column(&error, "Int", grnxx::INT_DATA); + assert(column); + + // Generate random values. + // Int: [0, 100). + grnxx::Array<grnxx::Int> values; + assert(values.resize(&error, NUM_ROWS + 1)); + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + values.set(i, mersenne_twister() % 100); + } + + // Store generated values into columns. + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + grnxx::Int row_id; + assert(table->insert_row(&error, grnxx::NULL_ROW_ID, + grnxx::Datum(), &row_id)); + assert(row_id == i); + assert(column->set(&error, row_id, values[i])); + } + + // Create an index. + auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX); + assert(index); + + // Create a cursor. + auto cursor = index->create_cursor(&error); + assert(cursor); + + grnxx::Array<grnxx::Record> records; + assert(cursor->read_all(&error, &records) == NUM_ROWS); + for (grnxx::Int i = 1; i < NUM_ROWS; ++i) { + assert(values[records.get_row_id(i)] <= values[records.get_row_id(i)]); + } +} + +void test_index_and_set() { + constexpr grnxx::Int NUM_ROWS = 1 << 16; + + grnxx::Error error; + + // Create a database with the default options. + auto db = grnxx::open_db(&error, ""); + assert(db); + + // Create a table with the default options. + auto table = db->create_table(&error, "Table"); + assert(table); + + // Create a column. + auto column = table->create_column(&error, "Int", grnxx::INT_DATA); + assert(column); + + // Create an index. + auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX); + assert(index); + + // Generate random values. + // Int: [0, 100). + grnxx::Array<grnxx::Int> values; + assert(values.resize(&error, NUM_ROWS + 1)); + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + values.set(i, mersenne_twister() % 100); + } + + // Store generated values into columns. + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + grnxx::Int row_id; + assert(table->insert_row(&error, grnxx::NULL_ROW_ID, + grnxx::Datum(), &row_id)); + assert(row_id == i); + assert(column->set(&error, row_id, values[i])); + } + + // Create a cursor. + auto cursor = index->create_cursor(&error); + assert(cursor); + + grnxx::Array<grnxx::Record> records; + assert(cursor->read_all(&error, &records) == NUM_ROWS); + for (grnxx::Int i = 1; i < NUM_ROWS; ++i) { + assert(values[records.get_row_id(i)] <= values[records.get_row_id(i)]); + } +} + +void test_remove() { + constexpr grnxx::Int NUM_ROWS = 1 << 16; + + grnxx::Error error; + + // Create a database with the default options. + auto db = grnxx::open_db(&error, ""); + assert(db); + + // Create a table with the default options. + auto table = db->create_table(&error, "Table"); + assert(table); + + // Create a column. + auto column = table->create_column(&error, "Int", grnxx::INT_DATA); + assert(column); + + // Generate random values. + // Int: [0, 100). + grnxx::Array<grnxx::Int> values; + assert(values.resize(&error, NUM_ROWS + 1)); + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + values.set(i, mersenne_twister() % 100); + } + + // Store generated values into columns. + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + grnxx::Int row_id; + assert(table->insert_row(&error, grnxx::NULL_ROW_ID, + grnxx::Datum(), &row_id)); + assert(row_id == i); + assert(column->set(&error, row_id, values[i])); + } + + // Create an index. + auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX); + assert(index); + + // Remove odd rows. + for (grnxx::Int i = 1; i <= NUM_ROWS; i += 2) { + assert(table->remove_row(&error, i)); + assert(!table->test_row(&error, i)); + } + + // Create a cursor. + auto cursor = index->create_cursor(&error); + assert(cursor); + + grnxx::Array<grnxx::Record> records; + assert(cursor->read_all(&error, &records) == (NUM_ROWS / 2)); + for (grnxx::Int i = 1; i < (NUM_ROWS / 2); ++i) { + assert(values[records.get_row_id(i)] <= values[records.get_row_id(i)]); + } +} + +int main() { + test_index(); + + test_set_and_index(); + test_index_and_set(); + test_remove(); + + return 0; +} -------------- next part -------------- HTML����������������������������... 다운로드