Navigation Menu

Skip to content

Commit

Permalink
make test_array_sort() use gcut_add_datum()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo Onodera committed Oct 15, 2009
1 parent a71bab5 commit ff3ec6e
Showing 1 changed file with 26 additions and 46 deletions.
72 changes: 26 additions & 46 deletions test/unit/core/test-table.c
Expand Up @@ -32,6 +32,8 @@ void data_temporary_table_default_tokenizer(void);
void test_temporary_table_default_tokenizer(gpointer data);
void data_temporary_table_add(void);
void test_temporary_table_add(gpointer data);
void data_array_sort(void);
void test_array_sort(gconstpointer data);
void test_nonexistent_column(void);

static grn_logger_info *logger;
Expand Down Expand Up @@ -170,12 +172,6 @@ test_temporary_table_add(gpointer data)
cut_assert_equal_int(1, grn_table_size(&context, table));
}

typedef struct _grn_sort_test_data
{
int offset, limit;
GList *expected_values;
} grn_sort_test_data;

static GList *make_glist_from_array(const gint *value, guint length)
{
GList *list = NULL;
Expand All @@ -186,51 +182,33 @@ static GList *make_glist_from_array(const gint *value, guint length)
return g_list_reverse(list);
}

static grn_sort_test_data *
sort_test_data_new(int offset, int limit,
gint32 expected_values[], int n_expected_values)
{
grn_sort_test_data *test_data;

test_data = g_new0(grn_sort_test_data, 1);
test_data->offset = offset;
test_data->limit = limit;
test_data->expected_values = make_glist_from_array(expected_values,
n_expected_values);

return test_data;
}

static void
sort_test_data_free(grn_sort_test_data *test_data)
{
if (test_data->expected_values)
g_list_free(test_data->expected_values);
g_free(test_data);
}

void
data_array_sort(void)
{
#define ADD_DATA(label, test_data) \
cut_add_data(label, test_data, sort_test_data_free, NULL)

gint32 sorted_values[] = {
#define ADD_DATUM(label, offset, limit, expected_values, n_expected_values) \
gcut_add_datum(label, \
"offset", G_TYPE_INT, offset, \
"limit", G_TYPE_INT, limit, \
"expected_values", G_TYPE_POINTER, \
make_glist_from_array(expected_values, n_expected_values),\
g_list_free, \
NULL)

const gint32 sorted_values[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
};

ADD_DATA("no offset, no limit", sort_test_data_new(0, -1, &sorted_values[0], 20));
ADD_DATA("offset", sort_test_data_new(10, -1, &sorted_values[10], 10));
ADD_DATA("limit", sort_test_data_new(0, 10, &sorted_values[0], 10));
ADD_DATA("offset, limit", sort_test_data_new(5, 10, &sorted_values[5], 10));
ADD_DATUM("no offset, no limit", 0, -1, &sorted_values[0], 20);
ADD_DATUM("offset", 10, -1, &sorted_values[10], 10);
ADD_DATUM("limit", 0, 10, &sorted_values[0], 10);
ADD_DATUM("offset, limit", 5, 10, &sorted_values[5], 10);

#undef ADD_DATA
#undef ADD_DATUM
}

void
test_array_sort(gconstpointer data)
{
const grn_sort_test_data *test_data = data;
const gint32 values[] = {
5, 6, 18, 9, 0, 4, 13, 12, 8, 14, 19, 11, 7, 3, 1, 10, 15, 2, 17, 16
};
Expand All @@ -239,14 +217,13 @@ test_array_sort(gconstpointer data)
const int n_keys = 1;
grn_table_sort_key keys[n_keys];

grn_obj *grn_type_int32 = grn_ctx_get(&context, "Int32", strlen("Int32"));
grn_obj *table, *column, *result;
grn_table_cursor *cursor;
int n_results;
guint i;

guint n_expected_values;
GList *sorted_values = NULL;
GList *expected_values, *sorted_values = NULL;

table = grn_table_create(&context, NULL, 0, NULL,
GRN_OBJ_TABLE_NO_KEY,
Expand All @@ -257,7 +234,7 @@ test_array_sort(gconstpointer data)
column_name,
strlen(column_name),
NULL, 0,
grn_type_int32);
grn_ctx_get(&context, "Int32", strlen("Int32")));

keys[0].key = column;
keys[0].flags = GRN_TABLE_SORT_ASC;
Expand All @@ -277,9 +254,12 @@ test_array_sort(gconstpointer data)

result = grn_table_create(&context, NULL, 0, NULL, GRN_TABLE_NO_KEY,
NULL, table);
n_results = grn_table_sort(&context, table, test_data->offset, test_data->limit,
n_results = grn_table_sort(&context, table,
gcut_data_get_int(data, "offset"),
gcut_data_get_int(data, "limit"),
result, keys, n_keys);
n_expected_values = g_list_length(test_data->expected_values);
expected_values = (GList *)gcut_data_get_pointer(data, "expected_values");
n_expected_values = g_list_length(expected_values);
cut_assert_equal_int(n_expected_values, n_results);
cut_assert_equal_int(n_expected_values, grn_table_size(&context, result));

Expand All @@ -299,8 +279,8 @@ test_array_sort(gconstpointer data)
GINT_TO_POINTER(GRN_INT32_VALUE(&record_value)));
GRN_OBJ_FIN(&context, &record_value);
}
gcut_assert_equal_list_int(test_data->expected_values, sorted_values);
g_list_free(sorted_values);
gcut_take_list(sorted_values, NULL);
gcut_assert_equal_list_int(expected_values, sorted_values);

grn_table_cursor_close(&context, cursor);
grn_obj_close(&context, result);
Expand Down

0 comments on commit ff3ec6e

Please sign in to comment.