Groonga 3.0.9 Source Code Document
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-context.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2008-2010 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ctx.h>
20 #include <db.h>
21 
22 #include <gcutter.h>
23 #include <glib/gstdio.h>
24 
25 #include "../lib/grn-assertions.h"
26 
27 void test_at_nonexistent(void);
29 void test_command_version(void);
30 void test_support_zlib(void);
31 void test_support_lzo(void);
32 
33 static grn_ctx *context;
34 static grn_obj *database;
35 static int default_flags;
36 static void *memory;
37 
38 void
39 cut_setup(void)
40 {
41  context = NULL;
42  database = NULL;
43  default_flags = GRN_CTX_USE_QL;
44  memory = NULL;
45 }
46 
47 void
49 {
50  if (context) {
51  if (memory) {
52  grn_ctx *ctx = context;
53  GRN_FREE(memory);
54  }
55  if (database) {
56  grn_db_close(context, database);
57  }
58  grn_ctx_fin(context);
59  g_free(context);
60  }
61 }
62 
63 #define cut_assert_ensure_context() do \
64 { \
65  if (!context) { \
66  context = g_new0(grn_ctx, 1); \
67  grn_test_assert(grn_ctx_init(context, default_flags)); \
68  } \
69 } while (0)
70 
71 #define cut_assert_ensure_database() do \
72 { \
73  cut_assert_ensure_context(); \
74  if (!database) { \
75  database = grn_db_create(context, NULL, NULL); \
76  } \
77 } while (0)
78 
79 void
81 {
83  cut_assert_null(grn_ctx_at(context, 99999));
84 }
85 
86 #ifdef USE_DYNAMIC_MALLOC_CHANGE
87 static void *
88 malloc_always_fail(grn_ctx *ctx, size_t size,
89  const char *file, int line, const char *func)
90 {
91  return NULL;
92 }
93 #endif
94 
95 void
97 {
98 #ifdef USE_DYNAMIC_MALLOC_CHANGE
99  cut_assert_ensue_context();
100  {
101  grn_ctx *ctx = context;
102 
103  memory = GRN_MALLOC(1);
104  cut_assert_not_null(memory);
105  GRN_FREE(memory);
106 
107  grn_ctx_set_malloc(ctx, malloc_always_fail);
108  memory = GRN_MALLOC(1);
109  cut_assert_null(memory);
110  }
111 #endif
112 }
113 
114 void
116 {
117  cut_assert_equal_int(GRN_COMMAND_VERSION_MAX - 1,
119 
121  cut_assert_equal_int(GRN_COMMAND_VERSION_MAX - 1,
122  grn_ctx_get_command_version(context));
123 }
124 
125 void
127 {
128  int support_p;
129  grn_obj grn_support_p;
130 
132  GRN_BOOL_INIT(&grn_support_p, 0);
133  grn_obj_get_info(context, NULL, GRN_INFO_SUPPORT_ZLIB, &grn_support_p);
134  support_p = GRN_BOOL_VALUE(&grn_support_p);
135  GRN_OBJ_FIN(context, &grn_support_p);
136 
137 #ifdef GRN_WITH_ZLIB
138  cut_assert_true(support_p);
139 #else
140  cut_assert_false(support_p);
141 #endif
142 }
143 
144 void
146 {
147  int support_p;
148  grn_obj grn_support_p;
149 
151  GRN_BOOL_INIT(&grn_support_p, 0);
152  grn_obj_get_info(context, NULL, GRN_INFO_SUPPORT_LZO, &grn_support_p);
153  support_p = GRN_BOOL_VALUE(&grn_support_p);
154  GRN_OBJ_FIN(context, &grn_support_p);
155 
156 #ifdef GRN_WITH_LZO
157  cut_assert_true(support_p);
158 #else
159  cut_assert_false(support_p);
160 #endif
161 }