Navigation Menu

Skip to content

Commit

Permalink
add tests for grn_table_sort with offset and limit options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo Onodera committed Oct 7, 2009
1 parent cde2920 commit fd84a48
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions test/unit/core/test-table.c
Expand Up @@ -170,6 +170,139 @@ test_temporary_table_add(gpointer data)
cut_assert_equal_int(1, grn_table_size(&context, table));
}

typedef struct _ArraySortTestData
{
int offset, limit;
gint32 expected_values[1000];
int n_expected_values;
} ArraySortTestData;


static ArraySortTestData sort_data_no_offset_no_limit = {
0, -1,
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99}, 100
};

static ArraySortTestData sort_data_offset = {
20, -1,
{20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99}, 80
};

static ArraySortTestData sort_data_limit = {
0, 20,
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 20
};

static ArraySortTestData sort_data_offset_limit = {
20, 20,
{20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39}, 20
};

void
data_array_sort_offset_and_limit(void)
{
#define ADD_DATA(label, p) \
cut_add_data(label, p, NULL, NULL)

ADD_DATA("no offset, no limit", &sort_data_no_offset_no_limit);
ADD_DATA("offset", &sort_data_offset);
ADD_DATA("limit", &sort_data_limit);
ADD_DATA("offset, limit", &sort_data_offset_limit);

#undef ADD_DATA
}

void
test_array_sort_offset_and_limit(ArraySortTestData *data)
{
const gint32 values[] = {
80, 37, 1, 95, 29, 21, 27, 11, 70, 0, 73, 79, 16, 17, 23, 43, 20, 55, 15,
72, 92, 69, 74, 82, 8, 41, 65, 57, 39, 75, 46, 83, 4, 67, 48, 32, 25, 62,
40, 30, 94, 99, 98, 91, 61, 5, 7, 31, 33, 13, 38, 36, 34, 24, 59, 10, 45,
63, 22, 93, 85, 58, 19, 44, 53, 6, 3, 18, 78, 49, 90, 47, 28, 54, 56, 51,
71, 66, 2, 52, 77, 64, 76, 35, 97, 68, 42, 84, 86, 14, 87, 96, 50, 26, 9,
60, 88, 12, 89, 81
};
const int n_values = sizeof(values)/sizeof(values[0]);

grn_obj *grn_type_int32 = grn_ctx_get(&context, "Int32", strlen("Int32"));
grn_obj *table, *column;
const gchar column_name[] = "sample_column";

grn_table_sort_key keys[1];
const int n_keys = 1;

int i;
grn_obj *result;
int n_results;
grn_table_cursor *cursor;

table = grn_table_create(&context, NULL, 0, NULL,
GRN_OBJ_TABLE_NO_KEY,
NULL,
NULL);
column = grn_column_create(&context,
table,
column_name,
strlen(column_name),
NULL, 0,
grn_type_int32);

keys[0].key = column;
keys[0].flags = GRN_TABLE_SORT_ASC;

for(i = 0; i < n_values; ++i) {
grn_obj record_value;
grn_id record_id;
record_id = grn_table_add(&context, table, NULL, 0, NULL);

GRN_INT32_INIT(&record_value, 0);
GRN_INT32_SET(&context, &record_value, values[i]);
grn_test_assert(grn_obj_set_value(&context, column, record_id,
&record_value, GRN_OBJ_SET));
GRN_OBJ_FIN(&context, &record_value);
}
cut_assert_equal_int(n_values, grn_table_size(&context, table));

result = grn_table_create(&context, NULL, 0, NULL, GRN_TABLE_NO_KEY,
NULL, table);
n_results = grn_table_sort(&context, table, data->offset, data->limit,
result, keys, n_keys);
cut_assert_equal_int(data->n_expected_values, n_results);
cut_assert_equal_int(data->n_expected_values, grn_table_size(&context, result));

i = 0;
cursor = grn_table_cursor_open(&context, result, NULL, 0, NULL, 0,
0, 0, GRN_CURSOR_ASCENDING);
while (grn_table_cursor_next(&context, cursor) != GRN_ID_NIL) {
void *value;
grn_id *id;
grn_obj record_value;

grn_table_cursor_get_value(&context, cursor, &value);
id = value;

GRN_INT32_INIT(&record_value, 0);
grn_obj_get_value(&context, column, *id, &record_value);
cut_assert_equal_int(data->expected_values[i++],
GRN_INT32_VALUE(&record_value));
GRN_OBJ_FIN(&context, &record_value);
}
cut_assert_equal_int(data->n_expected_values, i);
grn_table_cursor_close(&context, cursor);
grn_obj_close(&context, result);
}

void
test_nonexistent_column(void)
{
Expand Down

0 comments on commit fd84a48

Please sign in to comment.