susumu.yata
null+****@clear*****
Tue Sep 16 11:32:48 JST 2014
susumu.yata 2014-09-16 11:32:48 +0900 (Tue, 16 Sep 2014) New Revision: b1a77f64343bbe0ac41b4e7ab33fbaa298cbc38e https://github.com/groonga/grnxx/commit/b1a77f64343bbe0ac41b4e7ab33fbaa298cbc38e Message: Add tests for TreeIndex<Float>. (#56) Modified files: test/test_index.cpp Modified: test/test_index.cpp (+128 -4) =================================================================== --- test/test_index.cpp 2014-09-16 11:30:04 +0900 (0024070) +++ test/test_index.cpp 2014-09-16 11:32:48 +0900 (39ccfb0) @@ -208,7 +208,7 @@ void test_remove() { } } -void test_exact_match() { +void test_int_exact_match() { constexpr grnxx::Int NUM_ROWS = 1 << 16; grnxx::Error error; @@ -267,7 +267,68 @@ void test_exact_match() { } } -void test_range() { +void test_float_exact_match() { + 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, "Float", grnxx::FLOAT_DATA); + assert(column); + + // Create an index. + auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX); + assert(index); + + // Generate random values. + // Float: [0.0, 1.0). + grnxx::Array<grnxx::Float> values; + assert(values.resize(&error, NUM_ROWS + 1)); + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + values.set(i, (mersenne_twister() % 256) / 256.0); + } + + // 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])); + } + + // Test cursors for each value. + for (grnxx::Int int_value = 0; int_value < 256; ++int_value) { + grnxx::Float value = int_value / 256.0; + + auto cursor = index->create_cursor(&error, value); + assert(cursor); + + grnxx::Array<grnxx::Record> records; + assert(cursor->read_all(&error, &records) != -1); + for (grnxx::Int i = 1; i < records.size(); ++i) { + assert(values[records.get_row_id(i)] == value); + } + + grnxx::Int count = 0; + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + if (values[i] == value) { + ++count; + } + } + assert(count == records.size()); + } +} + +void test_int_range() { constexpr grnxx::Int NUM_ROWS = 1 << 16; grnxx::Error error; @@ -327,6 +388,66 @@ void test_range() { assert(count == records.size()); } +void test_float_range() { + 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, "Float", grnxx::FLOAT_DATA); + assert(column); + + // Create an index. + auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX); + assert(index); + + // Generate random values. + // Float: [0.0, 1.0). + grnxx::Array<grnxx::Float> values; + assert(values.resize(&error, NUM_ROWS + 1)); + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + values.set(i, (mersenne_twister() % 256) / 256.0); + } + + // 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. + grnxx::IndexRange range; + range.set_lower_bound(grnxx::Float(64 / 256.0), grnxx::INCLUSIVE_END_POINT); + range.set_upper_bound(grnxx::Float(192 / 256.0), grnxx::EXCLUSIVE_END_POINT); + auto cursor = index->create_cursor(&error, range); + assert(cursor); + + grnxx::Array<grnxx::Record> records; + assert(cursor->read_all(&error, &records) != -1); + for (grnxx::Int i = 1; i < records.size(); ++i) { + assert(values[records.get_row_id(i - 1)] <= values[records.get_row_id(i)]); + } + + grnxx::Int count = 0; + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + if ((values[i] >= (64 / 256.0)) && (values[i] < (192 / 256.0))) { + ++count; + } + } + assert(count == records.size()); +} + void test_reverse() { constexpr grnxx::Int NUM_ROWS = 1 << 16; @@ -471,9 +592,12 @@ int main() { test_index_and_set(); test_remove(); - test_exact_match(); + test_int_exact_match(); + test_float_exact_match(); + + test_int_range(); + test_float_range(); - test_range(); test_reverse(); test_offset_and_limit(); -------------- next part -------------- HTML����������������������������...下載