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