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