[Groonga-commit] groonga/grnxx at 5b319d7 [master] Add a test for TreeIndex<Text>::find_in_range(). (#124)

Back to archive index

susumu.yata null+****@clear*****
Tue Dec 2 13:33:30 JST 2014


susumu.yata	2014-12-02 13:33:30 +0900 (Tue, 02 Dec 2014)

  New Revision: 5b319d72c65b121244ec2ecf2e5fc88d92d906ac
  https://github.com/groonga/grnxx/commit/5b319d72c65b121244ec2ecf2e5fc88d92d906ac

  Message:
    Add a test for TreeIndex<Text>::find_in_range(). (#124)

  Modified files:
    test/test_index.cpp

  Modified: test/test_index.cpp (+54 -56)
===================================================================
--- test/test_index.cpp    2014-12-02 13:33:07 +0900 (43121de)
+++ test/test_index.cpp    2014-12-02 13:33:30 +0900 (5200a89)
@@ -650,70 +650,68 @@ void test_float_range() {
   assert(count == records.size());
 }
 
-//void test_text_range() {
-//  constexpr grnxx::Int NUM_ROWS = 1 << 16;
-
-//  grnxx::Error error;
+void test_text_range() {
+  constexpr size_t NUM_ROWS = 1 << 16;
 
-//  // Create a database with the default options.
-//  auto db = grnxx::open_db(&error, "");
-//  assert(db);
+  // Create a database with the default options.
+  auto db = grnxx::open_db("");
 
-//  // Create a table with the default options.
-//  auto table = db->create_table(&error, "Table");
-//  assert(table);
+  // Create a table with the default options.
+  auto table = db->create_table("Table");
 
-//  // Create a column.
-//  auto column = table->create_column(&error, "Text", grnxx::TEXT_DATA);
-//  assert(column);
+  // Create a column.
+  auto column = table->create_column("Text", grnxx::TEXT_DATA);
 
-//  // Create an index.
-//  auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX);
-//  assert(index);
+  // Create an index.
+  auto index = column->create_index("Index", grnxx::TREE_INDEX);
 
-//  // Generate random values.
-//  // Text: ["0", "99"].
-//  grnxx::Array<grnxx::Text> values;
-//  char bodies[100][3];
-//  assert(values.resize(&error, NUM_ROWS + 1));
-//  for (int i = 0; i < 100; ++i) {
-//    std::sprintf(bodies[i], "%d", i);
-//  }
-//  for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) {
-//    values.set(i, bodies[mersenne_twister() % 100]);
-//  }
+  // Generate random values.
+  // Text: ["0", "99"].
+  grnxx::Array<grnxx::Text> values;
+  char bodies[100][3];
+  values.resize(NUM_ROWS);
+  for (int i = 0; i < 100; ++i) {
+    std::sprintf(bodies[i], "%d", i);
+  }
+  for (size_t i = 0; i < NUM_ROWS; ++i) {
+    if ((mersenne_twister() % 100) == 0) {
+      values[i] = grnxx::Text::na();
+    } else {
+      values[i] = grnxx::Text(bodies[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]));
-//  }
+  // Store generated values into columns.
+  for (size_t i = 0; i < NUM_ROWS; ++i) {
+    grnxx::Int row_id = table->insert_row();
+    column->set(row_id, values[i]);
+  }
 
-//  // Create a cursor.
-//  grnxx::IndexRange range;
-//  range.set_lower_bound(grnxx::Text("25"), grnxx::EXCLUSIVE_END_POINT);
-//  range.set_upper_bound(grnxx::Text("75"), grnxx::INCLUSIVE_END_POINT);
-//  auto cursor = index->find_in_range(&error, range);
-//  assert(cursor);
+  // Create a cursor.
+  grnxx::IndexRange range;
+  range.set_lower_bound(grnxx::Text("25"), grnxx::EXCLUSIVE_END_POINT);
+  range.set_upper_bound(grnxx::Text("75"), grnxx::INCLUSIVE_END_POINT);
+  auto cursor = index->find_in_range(range);
 
-//  grnxx::Array<grnxx::Record> records;
-//  auto result = cursor->read_all(&error, &records);
-//  assert(result.is_ok);
-//  for (grnxx::Int i = 1; i < records.size(); ++i) {
-//    assert(values[records.get_row_id(i - 1)] <= values[records.get_row_id(i)]);
-//  }
+  grnxx::Array<grnxx::Record> records;
+  size_t count = cursor->read_all(&records);
+  for (size_t i = 1; i < records.size(); ++i) {
+    size_t lhs_row_id = records[i - 1].row_id.raw();
+    size_t rhs_row_id = records[i].row_id.raw();
+    assert(!values[lhs_row_id].is_na());
+    assert(!values[rhs_row_id].is_na());
+    assert((values[lhs_row_id] <= values[rhs_row_id]).is_true());
+  }
 
-//  grnxx::Int count = 0;
-//  for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) {
-//    if ((values[i] > "25") && (values[i] <= "75")) {
-//      ++count;
-//    }
-//  }
-//  assert(count == records.size());
-//}
+  count = 0;
+  for (size_t i = 0; i < NUM_ROWS; ++i) {
+    if ((values[i] > grnxx::Text("25")).is_true() &&
+        (values[i] <= grnxx::Text("75")).is_true()) {
+      ++count;
+    }
+  }
+  assert(count == records.size());
+}
 
 //bool inclusive_starts_with(const grnxx::Text &arg1, const grnxx::Text &arg2) {
 //  if (arg1.size() < arg2.size()) {
@@ -1057,7 +1055,7 @@ int main() {
 
   test_int_range();
   test_float_range();
-//  test_text_range();
+  test_text_range();
 
 //  test_text_find_starts_with();
 //  test_text_find_prefixes();
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index