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