Added simple exception test to the C++ test case file.
[m6w6/libmemcached] / tests / plus.cpp
1 /*
2 C++ interface test
3 */
4 #include "libmemcached/memcached.hpp"
5
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <time.h>
15 #include "server.h"
16
17 #include "test.h"
18
19 #include <string>
20 #include <iostream>
21
22 using namespace std;
23 using namespace memcache;
24
25 extern "C" {
26 test_return basic_test(memcached_st *memc);
27 test_return increment_test(memcached_st *memc);
28 test_return basic_master_key_test(memcached_st *memc);
29 test_return mget_result_function(memcached_st *memc);
30 test_return mget_test(memcached_st *memc);
31 memcached_return callback_counter(memcached_st *,
32 memcached_result_st *,
33 void *context);
34 void *world_create(void);
35 void world_destroy(void *p);
36 }
37
38 static void populate_vector(vector<char> &vec, const string &str)
39 {
40 vec.reserve(str.length());
41 vec.assign(str.begin(), str.end());
42 }
43
44 static void copy_vec_to_string(vector<char> &vec, string &str)
45 {
46 str.clear();
47 if (! vec.empty())
48 {
49 str.assign(vec.begin(), vec.end());
50 }
51 }
52
53 test_return basic_test(memcached_st *memc)
54 {
55 Memcache foo(memc);
56 const string value_set("This is some data");
57 std::vector<char> value;
58 std::vector<char> test_value;
59
60 populate_vector(value, value_set);
61
62 foo.set("mine", value, 0, 0);
63 foo.get("mine", test_value);
64
65 assert((memcmp(&test_value[0], &value[0], test_value.size()) == 0));
66
67 /*
68 * Simple test of the exceptions here...this should throw an exception
69 * saying that the key is empty.
70 */
71 try
72 {
73 foo.set("", value, 0, 0);
74 }
75 catch (Error &err)
76 {
77 return TEST_SUCCESS;
78 }
79
80 return TEST_FAILURE;
81 }
82
83 test_return increment_test(memcached_st *memc)
84 {
85 Memcache mcach(memc);
86 bool rc;
87 const string key("blah");
88 const string inc_value("1");
89 std::vector<char> inc_val;
90 vector<char> ret_value;
91 string ret_string;
92 uint64_t int_inc_value;
93 uint64_t int_ret_value;
94
95 populate_vector(inc_val, inc_value);
96
97 rc= mcach.set(key, inc_val, 0, 0);
98 if (rc == false)
99 {
100 return TEST_FAILURE;
101 }
102 mcach.get(key, ret_value);
103 if (ret_value.empty())
104 {
105 return TEST_FAILURE;
106 }
107 copy_vec_to_string(ret_value, ret_string);
108
109 int_inc_value= uint64_t(atol(inc_value.c_str()));
110 int_ret_value= uint64_t(atol(ret_string.c_str()));
111 assert(int_ret_value == int_inc_value);
112
113 rc= mcach.increment(key, 1, &int_ret_value);
114 assert(rc == true);
115 assert(int_ret_value == 2);
116
117 rc= mcach.increment(key, 1, &int_ret_value);
118 assert(rc == true);
119 assert(int_ret_value == 3);
120
121 rc= mcach.increment(key, 5, &int_ret_value);
122 assert(rc == true);
123 assert(int_ret_value == 8);
124
125 return TEST_SUCCESS;
126 }
127
128 test_return basic_master_key_test(memcached_st *memc)
129 {
130 Memcache foo(memc);
131 const string value_set("Data for server A");
132 vector<char> value;
133 vector<char> test_value;
134 const string master_key_a("server-a");
135 const string master_key_b("server-b");
136 const string key("xyz");
137
138 populate_vector(value, value_set);
139
140 foo.setByKey(master_key_a, key, value, 0, 0);
141 foo.getByKey(master_key_a, key, test_value);
142
143 assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
144
145 test_value.clear();
146
147 foo.getByKey(master_key_b, key, test_value);
148 assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
149
150 return TEST_SUCCESS;
151 }
152
153 /* Count the results */
154 memcached_return callback_counter(memcached_st *,
155 memcached_result_st *,
156 void *context)
157 {
158 unsigned int *counter= static_cast<unsigned int *>(context);
159
160 *counter= *counter + 1;
161
162 return MEMCACHED_SUCCESS;
163 }
164
165 test_return mget_result_function(memcached_st *memc)
166 {
167 Memcache mc(memc);
168 bool rc;
169 string key1("fudge");
170 string key2("son");
171 string key3("food");
172 vector<string> keys;
173 vector< vector<char> *> values;
174 vector<char> val1;
175 vector<char> val2;
176 vector<char> val3;
177 populate_vector(val1, key1);
178 populate_vector(val2, key2);
179 populate_vector(val3, key3);
180 keys.reserve(3);
181 keys.push_back(key1);
182 keys.push_back(key2);
183 keys.push_back(key3);
184 values.reserve(3);
185 values.push_back(&val1);
186 values.push_back(&val2);
187 values.push_back(&val3);
188 unsigned int counter;
189 memcached_execute_function callbacks[1];
190
191 /* We need to empty the server before we continue the test */
192 rc= mc.flush(0);
193 rc= mc.setAll(keys, values, 50, 9);
194 assert(rc == true);
195
196 rc= mc.mget(keys);
197 assert(rc == true);
198
199 callbacks[0]= &callback_counter;
200 counter= 0;
201 rc= mc.fetchExecute(callbacks, static_cast<void *>(&counter), 1);
202
203 assert(counter == 3);
204
205 return TEST_SUCCESS;
206 }
207
208 test_return mget_test(memcached_st *memc)
209 {
210 Memcache mc(memc);
211 bool rc;
212 memcached_return mc_rc;
213 vector<string> keys;
214 vector< vector<char> *> values;
215 keys.reserve(3);
216 keys.push_back("fudge");
217 keys.push_back("son");
218 keys.push_back("food");
219 vector<char> val1;
220 vector<char> val2;
221 vector<char> val3;
222 populate_vector(val1, "fudge");
223 populate_vector(val2, "son");
224 populate_vector(val3, "food");
225 values.reserve(3);
226 values.push_back(&val1);
227 values.push_back(&val2);
228 values.push_back(&val3);
229
230 string return_key;
231 vector<char> return_value;
232
233 /* We need to empty the server before we continue the test */
234 rc= mc.flush(0);
235 assert(rc == true);
236
237 rc= mc.mget(keys);
238 assert(rc == true);
239
240 while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END)
241 {
242 assert(return_value.size() != 0);
243 return_value.clear();
244 }
245 assert(mc_rc == MEMCACHED_END);
246
247 rc= mc.setAll(keys, values, 50, 9);
248 assert(rc == true);
249
250 rc= mc.mget(keys);
251 assert(rc == true);
252
253 while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END)
254 {
255 assert(return_key.length() == return_value.size());
256 assert(!memcmp(&return_value[0], return_key.c_str(), return_value.size()));
257 }
258
259 return TEST_SUCCESS;
260 }
261
262 test_st tests[] ={
263 { "basic", 0, basic_test },
264 { "basic_master_key", 0, basic_master_key_test },
265 { "increment_test", 0, increment_test },
266 { "mget", 1, mget_test },
267 { "mget_result_function", 1, mget_result_function },
268 {0, 0, 0}
269 };
270
271 collection_st collection[] ={
272 {"block", 0, 0, tests},
273 {0, 0, 0, 0}
274 };
275
276 #define SERVERS_TO_CREATE 1
277
278 extern "C" void *world_create(void)
279 {
280 server_startup_st *construct;
281
282 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
283 memset(construct, 0, sizeof(server_startup_st));
284
285 construct->count= SERVERS_TO_CREATE;
286 server_startup(construct);
287
288 return construct;
289 }
290
291 void world_destroy(void *p)
292 {
293 server_startup_st *construct= static_cast<server_startup_st *>(p);
294 memcached_server_st *servers=
295 static_cast<memcached_server_st *>(construct->servers);
296 memcached_server_list_free(servers);
297
298 server_shutdown(construct);
299 free(construct);
300 }
301
302 void get_world(world_st *world)
303 {
304 world->collections= collection;
305 world->create= world_create;
306 world->destroy= world_destroy;
307 }