Merge in updates (including removal of some depcrated bits from the examples).
[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
16 #include <libtest/server.h>
17
18 #include <libtest/test.h>
19
20 #include <string>
21 #include <iostream>
22
23 using namespace std;
24 using namespace memcache;
25
26 extern "C" {
27 test_return_t basic_test(memcached_st *memc);
28 test_return_t increment_test(memcached_st *memc);
29 test_return_t basic_master_key_test(memcached_st *memc);
30 test_return_t mget_result_function(memcached_st *memc);
31 test_return_t basic_behavior(memcached_st *memc);
32 test_return_t mget_test(memcached_st *memc);
33 memcached_return_t callback_counter(const memcached_st *,
34 memcached_result_st *,
35 void *context);
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_t 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_t 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_t 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_t callback_counter(const 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_t 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_fn 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_t mget_test(memcached_st *memc)
209 {
210 Memcache mc(memc);
211 bool rc;
212 memcached_return_t 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_return_t basic_behavior(memcached_st *memc)
263 {
264 Memcache mc(memc);
265 bool rc;
266 uint64_t value = 1;
267 rc = mc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value);
268 assert(rc);
269 uint64_t behavior = mc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY);
270 assert(behavior == value);
271
272 return TEST_SUCCESS;
273 }
274
275 test_st tests[] ={
276 { "basic", 0,
277 reinterpret_cast<test_callback_fn>(basic_test) },
278 { "basic_master_key", 0,
279 reinterpret_cast<test_callback_fn>(basic_master_key_test) },
280 { "increment_test", 0,
281 reinterpret_cast<test_callback_fn>(increment_test) },
282 { "mget", 1,
283 reinterpret_cast<test_callback_fn>(mget_test) },
284 { "mget_result_function", 1,
285 reinterpret_cast<test_callback_fn>(mget_result_function) },
286 { "basic_behavior", 0,
287 reinterpret_cast<test_callback_fn>(basic_behavior) },
288 {0, 0, 0}
289 };
290
291 collection_st collection[] ={
292 {"block", 0, 0, tests},
293 {0, 0, 0, 0}
294 };
295
296 #define SERVERS_TO_CREATE 5
297
298 #include "libmemcached_world.h"
299
300 void get_world(world_st *world)
301 {
302 world->collections= collection;
303
304 world->create= reinterpret_cast<test_callback_create_fn>(world_create);
305 world->destroy= reinterpret_cast<test_callback_fn>(world_destroy);
306
307 world->test.startup= reinterpret_cast<test_callback_fn>(world_test_startup);
308 world->test.flush= reinterpret_cast<test_callback_fn>(world_flush);
309 world->test.pre_run= reinterpret_cast<test_callback_fn>(world_pre_run);
310 world->test.post_run= reinterpret_cast<test_callback_fn>(world_post_run);
311 world->test.on_error= reinterpret_cast<test_callback_error_fn>(world_on_error);
312
313 world->collection.startup= reinterpret_cast<test_callback_fn>(world_container_startup);
314 world->collection.shutdown= reinterpret_cast<test_callback_fn>(world_container_shutdown);
315
316 world->runner= &defualt_libmemcached_runner;
317 }