Skip to content

Commit

Permalink
Add a test for Index::find_prefixes(). (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Sep 17, 2014
1 parent 0e6ce6d commit f500fa9
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/test_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,70 @@ void test_text_find_starts_with() {
}
}

void test_text_find_prefixes() {
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, "Text", grnxx::TEXT_DATA);
assert(column);

// Create an index.
auto index = column->create_index(&error, "Index", grnxx::TREE_INDEX);
assert(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]);
}

// 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 (int int_value = 0; int_value < 100; ++int_value) {
grnxx::Text value = bodies[int_value];
auto cursor = index->find_prefixes(&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(inclusive_starts_with(value, values[records.get_row_id(i)]));
}

grnxx::Int count = 0;
for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) {
if (inclusive_starts_with(value, values[i])) {
++count;
}
}
assert(count == records.size());
}
}

void test_reverse() {
constexpr grnxx::Int NUM_ROWS = 1 << 16;

Expand Down Expand Up @@ -910,6 +974,7 @@ int main() {
test_text_range();

test_text_find_starts_with();
test_text_find_prefixes();

test_reverse();
test_offset_and_limit();
Expand Down

0 comments on commit f500fa9

Please sign in to comment.