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