Create version 1.0 directory structure.
[m6w6/libmemcached] / tests / plus.cpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <config.h>
39
40 /*
41 C++ interface test
42 */
43 #include <libmemcached-1.0/memcached.hpp>
44 #include <libtest/test.hpp>
45
46 #include <cstdio>
47 #include <cstdlib>
48 #include <cstring>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <ctime>
54
55 #include <string>
56 #include <iostream>
57
58 using namespace std;
59 using namespace memcache;
60 using namespace libtest;
61
62 static void populate_vector(vector<char> &vec, const string &str)
63 {
64 vec.reserve(str.length());
65 vec.assign(str.begin(), str.end());
66 }
67
68 static void copy_vec_to_string(vector<char> &vec, string &str)
69 {
70 str.clear();
71 if (not vec.empty())
72 {
73 str.assign(vec.begin(), vec.end());
74 }
75 }
76
77 static test_return_t basic_test(memcached_st *memc)
78 {
79 Memcache foo(memc);
80 const string value_set("This is some data");
81 std::vector<char> value;
82 std::vector<char> test_value;
83
84 populate_vector(value, value_set);
85
86 test_true(foo.set("mine", value, 0, 0));
87 test_true(foo.get("mine", test_value));
88
89 test_memcmp(&test_value[0], &value[0], test_value.size());
90 test_false(foo.set("", value, 0, 0));
91
92 return TEST_SUCCESS;
93 }
94
95 static test_return_t increment_test(memcached_st *original)
96 {
97 Memcache mcach(original);
98 const string key("blah");
99 const string inc_value("1");
100 std::vector<char> inc_val;
101 vector<char> ret_value;
102 string ret_string;
103 uint64_t int_inc_value;
104 uint64_t int_ret_value;
105
106 populate_vector(inc_val, inc_value);
107
108 test_true(mcach.set(key, inc_val, 0, 0));
109
110 test_true(mcach.get(key, ret_value));
111 test_false(ret_value.empty());
112 copy_vec_to_string(ret_value, ret_string);
113
114 int_inc_value= uint64_t(atol(inc_value.c_str()));
115 int_ret_value= uint64_t(atol(ret_string.c_str()));
116 test_compare(int_inc_value, int_ret_value);
117
118 test_true(mcach.increment(key, 1, &int_ret_value));
119 test_compare(uint64_t(2), int_ret_value);
120
121 test_true(mcach.increment(key, 1, &int_ret_value));
122 test_compare(uint64_t(3), int_ret_value);
123
124 test_true(mcach.increment(key, 5, &int_ret_value));
125 test_compare(uint64_t(8), int_ret_value);
126
127 return TEST_SUCCESS;
128 }
129
130 static test_return_t basic_master_key_test(memcached_st *original)
131 {
132 Memcache foo(original);
133 const string value_set("Data for server A");
134 vector<char> value;
135 vector<char> test_value;
136 const string master_key_a("server-a");
137 const string master_key_b("server-b");
138 const string key("xyz");
139
140 populate_vector(value, value_set);
141
142 test_true(foo.setByKey(master_key_a, key, value, 0, 0));
143 test_true(foo.getByKey(master_key_a, key, test_value));
144
145 test_compare(value.size(), test_value.size());
146 test_memcmp(&value[0], &test_value[0], value.size());
147
148 test_value.clear();
149
150 #if 0
151 test_false(foo.getByKey(master_key_b, key, test_value));
152 test_zero(test_value.size());
153 #endif
154
155 return TEST_SUCCESS;
156 }
157
158 static test_return_t mget_test(memcached_st *original)
159 {
160 Memcache memc(original);
161 memcached_return_t mc_rc;
162 vector<string> keys;
163 vector< vector<char> *> values;
164 keys.reserve(3);
165 keys.push_back("fudge");
166 keys.push_back("son");
167 keys.push_back("food");
168 vector<char> val1;
169 vector<char> val2;
170 vector<char> val3;
171 populate_vector(val1, "fudge");
172 populate_vector(val2, "son");
173 populate_vector(val3, "food");
174 values.reserve(3);
175 values.push_back(&val1);
176 values.push_back(&val2);
177 values.push_back(&val3);
178
179 string return_key;
180 vector<char> return_value;
181
182 /* We need to empty the server before we continue the test */
183 test_true(memc.flush());
184
185 test_true(memc.mget(keys));
186
187 test_compare(MEMCACHED_NOTFOUND,
188 memc.fetch(return_key, return_value));
189
190 test_true(memc.setAll(keys, values, 50, 9));
191
192 test_true(memc.mget(keys));
193 size_t count= 0;
194 while (memcached_success(mc_rc= memc.fetch(return_key, return_value)))
195 {
196 test_compare(return_key.length(), return_value.size());
197 test_memcmp(&return_value[0], return_key.c_str(), return_value.size());
198 count++;
199 }
200 test_compare(values.size(), count);
201
202 return TEST_SUCCESS;
203 }
204
205 static test_return_t basic_behavior(memcached_st *original)
206 {
207 Memcache memc(original);
208 uint64_t value= 1;
209 test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value));
210 uint64_t behavior= memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY);
211 test_compare(behavior, value);
212
213 return TEST_SUCCESS;
214 }
215
216 static test_return_t error_test(memcached_st *)
217 {
218 Memcache memc("--server=localhost:0");
219 std::vector<char> value;
220
221 test_false(memc.set("key", value, time_t(0), uint32_t(0)));
222
223 test_true(memc.error());
224
225 return TEST_SUCCESS;
226 }
227
228 static test_return_t error_std_string_test(memcached_st *)
229 {
230 Memcache memc("--server=localhost:0");
231 std::vector<char> value;
232
233 test_false(memc.set("key", value, time_t(0), uint32_t(0)));
234
235 std::string error_message;
236 test_true(memc.error(error_message));
237 test_false(error_message.empty());
238
239 return TEST_SUCCESS;
240 }
241
242 static test_return_t error_memcached_return_t_test(memcached_st *)
243 {
244 Memcache memc("--server=localhost:0");
245 std::vector<char> value;
246
247 test_false(memc.set("key", value, time_t(0), uint32_t(0)));
248
249 memcached_return_t rc;
250 test_true(memc.error(rc));
251 test_compare(MEMCACHED_CONNECTION_FAILURE, rc);
252
253 return TEST_SUCCESS;
254 }
255
256 test_st error_tests[] ={
257 { "error()", false, reinterpret_cast<test_callback_fn*>(error_test) },
258 { "error(std::string&)", false, reinterpret_cast<test_callback_fn*>(error_std_string_test) },
259 { "error(memcached_return_t&)", false, reinterpret_cast<test_callback_fn*>(error_memcached_return_t_test) },
260 {0, 0, 0}
261 };
262
263 test_st tests[] ={
264 { "basic", false,
265 reinterpret_cast<test_callback_fn*>(basic_test) },
266 { "basic_master_key", false,
267 reinterpret_cast<test_callback_fn*>(basic_master_key_test) },
268 { "increment_test", false,
269 reinterpret_cast<test_callback_fn*>(increment_test) },
270 { "mget", true,
271 reinterpret_cast<test_callback_fn*>(mget_test) },
272 { "basic_behavior", false,
273 reinterpret_cast<test_callback_fn*>(basic_behavior) },
274 {0, 0, 0}
275 };
276
277 collection_st collection[] ={
278 {"block", 0, 0, tests},
279 {"error()", 0, 0, error_tests},
280 {0, 0, 0, 0}
281 };
282
283 #define SERVERS_TO_CREATE 5
284
285 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
286 #include "libmemcached_world.h"
287
288 void get_world(Framework *world)
289 {
290 world->collections= collection;
291
292 world->_create= world_create;
293 world->_destroy= world_destroy;
294
295 world->item._startup= reinterpret_cast<test_callback_fn*>(world_test_startup);
296 world->item._flush= reinterpret_cast<test_callback_fn*>(world_flush);
297 world->item.set_pre(reinterpret_cast<test_callback_fn*>(world_pre_run));
298 world->item.set_post(reinterpret_cast<test_callback_fn*>(world_post_run));
299 world->_on_error= reinterpret_cast<test_callback_error_fn*>(world_on_error);
300
301 world->collection_startup= reinterpret_cast<test_callback_fn*>(world_container_startup);
302 world->collection_shutdown= reinterpret_cast<test_callback_fn*>(world_container_shutdown);
303
304 world->set_runner(&defualt_libmemcached_runner);
305 }