Merge in all of libtest updates.
[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/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 extern "C" {
63 test_return_t basic_test(memcached_st *memc);
64 test_return_t increment_test(memcached_st *memc);
65 test_return_t basic_master_key_test(memcached_st *memc);
66 test_return_t mget_result_function(memcached_st *memc);
67 test_return_t basic_behavior(memcached_st *memc);
68 test_return_t mget_test(memcached_st *memc);
69 memcached_return_t callback_counter(const memcached_st *,
70 memcached_result_st *,
71 void *context);
72 }
73
74 static void populate_vector(vector<char> &vec, const string &str)
75 {
76 vec.reserve(str.length());
77 vec.assign(str.begin(), str.end());
78 }
79
80 static void copy_vec_to_string(vector<char> &vec, string &str)
81 {
82 str.clear();
83 if (not vec.empty())
84 {
85 str.assign(vec.begin(), vec.end());
86 }
87 }
88
89 test_return_t basic_test(memcached_st *memc)
90 {
91 Memcache foo(memc);
92 const string value_set("This is some data");
93 std::vector<char> value;
94 std::vector<char> test_value;
95
96 populate_vector(value, value_set);
97
98 test_true(foo.set("mine", value, 0, 0));
99 test_true(foo.get("mine", test_value));
100
101 test_memcmp(&test_value[0], &value[0], test_value.size());
102 test_false(foo.set("", value, 0, 0));
103
104 return TEST_SUCCESS;
105 }
106
107 test_return_t increment_test(memcached_st *original)
108 {
109 Memcache mcach(original);
110 const string key("blah");
111 const string inc_value("1");
112 std::vector<char> inc_val;
113 vector<char> ret_value;
114 string ret_string;
115 uint64_t int_inc_value;
116 uint64_t int_ret_value;
117
118 populate_vector(inc_val, inc_value);
119
120 test_true(mcach.set(key, inc_val, 0, 0));
121
122 test_true(mcach.get(key, ret_value));
123 test_false(ret_value.empty());
124 copy_vec_to_string(ret_value, ret_string);
125
126 int_inc_value= uint64_t(atol(inc_value.c_str()));
127 int_ret_value= uint64_t(atol(ret_string.c_str()));
128 test_compare(int_inc_value, int_ret_value);
129
130 test_true(mcach.increment(key, 1, &int_ret_value));
131 test_compare(2UL, int_ret_value);
132
133 test_true(mcach.increment(key, 1, &int_ret_value));
134 test_compare(3UL, int_ret_value);
135
136 test_true(mcach.increment(key, 5, &int_ret_value));
137 test_compare(8UL, int_ret_value);
138
139 return TEST_SUCCESS;
140 }
141
142 test_return_t basic_master_key_test(memcached_st *original)
143 {
144 Memcache foo(original);
145 const string value_set("Data for server A");
146 vector<char> value;
147 vector<char> test_value;
148 const string master_key_a("server-a");
149 const string master_key_b("server-b");
150 const string key("xyz");
151
152 populate_vector(value, value_set);
153
154 test_true(foo.setByKey(master_key_a, key, value, 0, 0));
155 test_true(foo.getByKey(master_key_a, key, test_value));
156
157 test_compare(value.size(), test_value.size());
158 test_memcmp(&value[0], &test_value[0], value.size());
159
160 test_value.clear();
161
162 #if 0
163 test_false(foo.getByKey(master_key_b, key, test_value));
164 test_zero(test_value.size());
165 #endif
166
167 return TEST_SUCCESS;
168 }
169
170 /* Count the results */
171 memcached_return_t callback_counter(const memcached_st *,
172 memcached_result_st *,
173 void *context)
174 {
175 unsigned int *counter= static_cast<unsigned int *>(context);
176
177 *counter= *counter +1;
178
179 return MEMCACHED_SUCCESS;
180 }
181
182 test_return_t mget_test(memcached_st *original)
183 {
184 Memcache memc(original);
185 memcached_return_t mc_rc;
186 vector<string> keys;
187 vector< vector<char> *> values;
188 keys.reserve(3);
189 keys.push_back("fudge");
190 keys.push_back("son");
191 keys.push_back("food");
192 vector<char> val1;
193 vector<char> val2;
194 vector<char> val3;
195 populate_vector(val1, "fudge");
196 populate_vector(val2, "son");
197 populate_vector(val3, "food");
198 values.reserve(3);
199 values.push_back(&val1);
200 values.push_back(&val2);
201 values.push_back(&val3);
202
203 string return_key;
204 vector<char> return_value;
205
206 /* We need to empty the server before we continue the test */
207 test_true(memc.flush());
208
209 test_true(memc.mget(keys));
210
211 test_compare(MEMCACHED_NOTFOUND,
212 memc.fetch(return_key, return_value));
213
214 test_true(memc.setAll(keys, values, 50, 9));
215
216 test_true(memc.mget(keys));
217 size_t count= 0;
218 while (memcached_success(mc_rc= memc.fetch(return_key, return_value)))
219 {
220 test_compare(return_key.length(), return_value.size());
221 test_memcmp(&return_value[0], return_key.c_str(), return_value.size());
222 count++;
223 }
224 test_compare(values.size(), count);
225
226 return TEST_SUCCESS;
227 }
228
229 test_return_t basic_behavior(memcached_st *original)
230 {
231 Memcache memc(original);
232 uint64_t value= 1;
233 test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value));
234 uint64_t behavior= memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY);
235 test_compare(behavior, value);
236
237 return TEST_SUCCESS;
238 }
239
240 test_st tests[] ={
241 { "basic", 0,
242 reinterpret_cast<test_callback_fn*>(basic_test) },
243 { "basic_master_key", 0,
244 reinterpret_cast<test_callback_fn*>(basic_master_key_test) },
245 { "increment_test", 0,
246 reinterpret_cast<test_callback_fn*>(increment_test) },
247 { "mget", 1,
248 reinterpret_cast<test_callback_fn*>(mget_test) },
249 { "basic_behavior", 0,
250 reinterpret_cast<test_callback_fn*>(basic_behavior) },
251 {0, 0, 0}
252 };
253
254 collection_st collection[] ={
255 {"block", 0, 0, tests},
256 {0, 0, 0, 0}
257 };
258
259 #define SERVERS_TO_CREATE 5
260
261 #include "libmemcached_world.h"
262
263 void get_world(Framework *world)
264 {
265 world->collections= collection;
266
267 world->_create= world_create;
268 world->_destroy= world_destroy;
269
270 world->item._startup= reinterpret_cast<test_callback_fn*>(world_test_startup);
271 world->item._flush= reinterpret_cast<test_callback_fn*>(world_flush);
272 world->item.set_pre(reinterpret_cast<test_callback_fn*>(world_pre_run));
273 world->item.set_post(reinterpret_cast<test_callback_fn*>(world_post_run));
274 world->_on_error= reinterpret_cast<test_callback_error_fn*>(world_on_error);
275
276 world->collection_startup= reinterpret_cast<test_callback_fn*>(world_container_startup);
277 world->collection_shutdown= reinterpret_cast<test_callback_fn*>(world_container_shutdown);
278
279 world->set_runner(&defualt_libmemcached_runner);
280 }