Kouhei Sutou
null+****@clear*****
Fri Apr 8 18:45:05 JST 2016
Kouhei Sutou 2016-04-08 18:45:05 +0900 (Fri, 08 Apr 2016) New Revision: 02db0924072b5b425baeff42911ba51e87e16d79 https://github.com/groonga/groonga/commit/02db0924072b5b425baeff42911ba51e87e16d79 Message: Add predicates for type ID Added files: test/unit/core/test-type.c Modified files: include/groonga/type.h lib/type.c test/unit/core/Makefile.am Modified: include/groonga/type.h (+3 -0) =================================================================== --- include/groonga/type.h 2016-04-08 18:07:12 +0900 (9262f20) +++ include/groonga/type.h 2016-04-08 18:45:05 +0900 (b514603) @@ -25,6 +25,9 @@ extern "C" { #define GRN_TYPE_IS_TEXT_FAMILY(type) \ (GRN_DB_SHORT_TEXT <= (type) && (type) <= GRN_DB_LONG_TEXT) +GRN_API grn_bool grn_type_id_is_builtin(grn_ctx *ctx, grn_id id); +GRN_API grn_bool grn_type_id_is_number_family(grn_ctx *ctx, grn_id id); + GRN_API grn_obj *grn_type_create(grn_ctx *ctx, const char *name, unsigned int name_size, grn_obj_flags flags, unsigned int size); GRN_API uint32_t grn_type_size(grn_ctx *ctx, grn_obj *type); Modified: lib/type.c (+12 -1) =================================================================== --- lib/type.c 2016-04-08 18:07:12 +0900 (af3edef) +++ lib/type.c 2016-04-08 18:45:05 +0900 (d21c23e) @@ -19,6 +19,18 @@ #include "grn_ctx_impl.h" #include "grn_db.h" +grn_bool +grn_type_id_is_builtin(grn_ctx *ctx, grn_id id) +{ + return id < GRN_N_RESERVED_TYPES; +} + +grn_bool +grn_type_id_is_number_family(grn_ctx *ctx, grn_id id) +{ + return GRN_DB_INT8 <= id && id <= GRN_DB_FLOAT; +} + grn_obj * grn_type_create(grn_ctx *ctx, const char *name, unsigned int name_size, grn_obj_flags flags, unsigned int size) @@ -67,4 +79,3 @@ grn_type_size(grn_ctx *ctx, grn_obj *type) size = GRN_TYPE_SIZE(DB_OBJ(type)); GRN_API_RETURN(size); } - Modified: test/unit/core/Makefile.am (+3 -1) =================================================================== --- test/unit/core/Makefile.am 2016-04-08 18:07:12 +0900 (71075b1) +++ test/unit/core/Makefile.am 2016-04-08 18:45:05 +0900 (ffa7853) @@ -66,7 +66,8 @@ noinst_LTLIBRARIES = \ test-proc.la \ test-uvector.la \ test-operator.la \ - test-config.la + test-config.la \ + test-type.la endif AM_CPPFLAGS = \ @@ -158,3 +159,4 @@ test_proc_la_SOURCES = test-proc.c test_uvector_la_SOURCES = test-uvector.c test_operator_la_SOURCES = test-operator.c test_config_la_SOURCES = test-config.c +test_type_la_SOURCES = test-type.c Added: test/unit/core/test-type.c (+159 -0) 100644 =================================================================== --- /dev/null +++ test/unit/core/test-type.c 2016-04-08 18:45:05 +0900 (ed6cc3c) @@ -0,0 +1,159 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "../../../config.h" + +#include <groonga.h> + +#include <gcutter.h> +#include <glib/gstdio.h> + +#include "../lib/grn-assertions.h" + +void data_id_is_builtin(void); +void test_id_is_builtin(gconstpointer data); +void data_id_is_number_family(void); +void test_id_is_number_family(gconstpointer data); + +static gchar *tmp_directory; +static const gchar *database_path; + +static grn_ctx *context; +static grn_obj *database; + +void +cut_startup(void) +{ + tmp_directory = g_build_filename(grn_test_get_tmp_dir(), + "type", + NULL); +} + +void +cut_shutdown(void) +{ + g_free(tmp_directory); +} + +static void +remove_tmp_directory(void) +{ + cut_remove_path(tmp_directory, NULL); +} + +void +cut_setup(void) +{ + remove_tmp_directory(); + g_mkdir_with_parents(tmp_directory, 0700); + + context = g_new0(grn_ctx, 1); + grn_ctx_init(context, 0); + + database_path = cut_build_path(tmp_directory, "database.groonga", NULL); + database = grn_db_create(context, database_path, NULL); +} + +void +cut_teardown(void) +{ + if (context) { + grn_obj_close(context, database); + grn_ctx_fin(context); + g_free(context); + } + + remove_tmp_directory(); +} + +void +data_id_is_builtin(void) +{ +#define ADD_DATUM(expected, name) \ + gcut_add_datum((expected ? "built-in - " name : "custom - " name), \ + "expected", G_TYPE_BOOLEAN, expected, \ + "name", G_TYPE_STRING, name, \ + NULL) + + ADD_DATUM(TRUE, "Bool"); + ADD_DATUM(TRUE, "WGS84GeoPoint"); + ADD_DATUM(FALSE, "Users"); + +#undef ADD_DATUM +} + +void +test_id_is_builtin(gconstpointer data) +{ + const gchar *name; + grn_obj *object; + grn_id id; + + assert_send_command("table_create Users TABLE_HASH_KEY ShortText"); + + name = gcut_data_get_string(data, "name"); + object = grn_ctx_get(context, name, strlen(name)); + id = grn_obj_id(context, object); + if (gcut_data_get_string(data, "expected")) { + cut_assert_true(grn_type_id_is_builtin(context, id)); + } else { + cut_assert_false(grn_type_id_is_builtin(context, id)); + } +} + +void +data_id_is_number_family(void) +{ +#define ADD_DATUM(expected, name) \ + gcut_add_datum((expected ? "number-family - " name : "column - " name), \ + "expected", G_TYPE_BOOLEAN, expected, \ + "name", G_TYPE_STRING, name, \ + NULL) + + ADD_DATUM(TRUE, "Int8"); + ADD_DATUM(TRUE, "UInt8"); + ADD_DATUM(TRUE, "Int16"); + ADD_DATUM(TRUE, "UInt16"); + ADD_DATUM(TRUE, "Int32"); + ADD_DATUM(TRUE, "UInt32"); + ADD_DATUM(TRUE, "Int64"); + ADD_DATUM(TRUE, "UInt64"); + ADD_DATUM(TRUE, "Float"); + ADD_DATUM(FALSE, "Time"); + +#undef ADD_DATUM +} + +void +test_id_is_number_family(gconstpointer data) +{ + const gchar *name; + grn_obj *object; + grn_id id; + + assert_send_command("table_create Users TABLE_HASH_KEY ShortText"); + + name = gcut_data_get_string(data, "name"); + object = grn_ctx_get(context, name, strlen(name)); + id = grn_obj_id(context, object); + if (gcut_data_get_string(data, "expected")) { + cut_assert_true(grn_type_id_is_number_family(context, id)); + } else { + cut_assert_false(grn_type_id_is_number_family(context, id)); + } +} -------------- next part -------------- HTML����������������������������...Download