Merge in fix so that we don't have to do weight on the const root
[m6w6/libmemcached] / tests / plus.cpp
1 /*
2 C++ interface test
3 */
4 #include <libmemcached/memcached.hpp>
5
6 #include <cstdio>
7 #include <cstdlib>
8 #include <cstring>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <ctime>
14
15 #include <libtest/server.h>
16
17 #include <libtest/test.h>
18
19 #include <string>
20 #include <iostream>
21
22 using namespace std;
23 using namespace memcache;
24
25 extern "C" {
26 test_return_t basic_test(memcached_st *memc);
27 test_return_t increment_test(memcached_st *memc);
28 test_return_t basic_master_key_test(memcached_st *memc);
29 test_return_t mget_result_function(memcached_st *memc);
30 test_return_t basic_behavior(memcached_st *memc);
31 test_return_t mget_test(memcached_st *memc);
32 memcached_return_t callback_counter(const memcached_st *,
33 memcached_result_st *,
34 void *context);
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 (not vec.empty())
47 {
48 str.assign(vec.begin(), vec.end());
49 }
50 }
51
52 test_return_t 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 test_true(foo.set("mine", value, 0, 0));
62 test_true(foo.get("mine", test_value));
63
64 test_memcmp(&test_value[0], &value[0], test_value.size());
65 test_false(foo.set("", value, 0, 0));
66
67 return TEST_SUCCESS;
68 }
69
70 test_return_t increment_test(memcached_st *original)
71 {
72 Memcache mcach(original);
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 test_true(mcach.set(key, inc_val, 0, 0));
84
85 test_true(mcach.get(key, ret_value));
86 test_false(ret_value.empty());
87 copy_vec_to_string(ret_value, ret_string);
88
89 int_inc_value= uint64_t(atol(inc_value.c_str()));
90 int_ret_value= uint64_t(atol(ret_string.c_str()));
91 test_compare(int_inc_value, int_ret_value);
92
93 test_true(mcach.increment(key, 1, &int_ret_value));
94 test_compare(2, int_ret_value);
95
96 test_true(mcach.increment(key, 1, &int_ret_value));
97 test_compare(3, int_ret_value);
98
99 test_true(mcach.increment(key, 5, &int_ret_value));
100 test_compare(8, int_ret_value);
101
102 return TEST_SUCCESS;
103 }
104
105 test_return_t basic_master_key_test(memcached_st *original)
106 {
107 Memcache foo(original);
108 const string value_set("Data for server A");
109 vector<char> value;
110 vector<char> test_value;
111 const string master_key_a("server-a");
112 const string master_key_b("server-b");
113 const string key("xyz");
114
115 populate_vector(value, value_set);
116
117 foo.setByKey(master_key_a, key, value, 0, 0);
118 foo.getByKey(master_key_a, key, test_value);
119
120 test_true((memcmp(&value[0], &test_value[0], value.size()) == 0));
121
122 test_value.clear();
123
124 foo.getByKey(master_key_b, key, test_value);
125 test_true((memcmp(&value[0], &test_value[0], value.size()) == 0));
126
127 return TEST_SUCCESS;
128 }
129
130 /* Count the results */
131 memcached_return_t callback_counter(const memcached_st *,
132 memcached_result_st *,
133 void *context)
134 {
135 unsigned int *counter= static_cast<unsigned int *>(context);
136
137 *counter= *counter + 1;
138
139 return MEMCACHED_SUCCESS;
140 }
141
142 test_return_t mget_test(memcached_st *original)
143 {
144 Memcache memc(original);
145 memcached_return_t mc_rc;
146 vector<string> keys;
147 vector< vector<char> *> values;
148 keys.reserve(3);
149 keys.push_back("fudge");
150 keys.push_back("son");
151 keys.push_back("food");
152 vector<char> val1;
153 vector<char> val2;
154 vector<char> val3;
155 populate_vector(val1, "fudge");
156 populate_vector(val2, "son");
157 populate_vector(val3, "food");
158 values.reserve(3);
159 values.push_back(&val1);
160 values.push_back(&val2);
161 values.push_back(&val3);
162
163 string return_key;
164 vector<char> return_value;
165
166 /* We need to empty the server before we continue the test */
167 test_true(memc.flush(0));
168
169 test_true(memc.mget(keys));
170
171 while ((mc_rc= memc.fetch(return_key, return_value)) != MEMCACHED_END)
172 {
173 test_true(return_value.size());
174 return_value.clear();
175 }
176 test_compare(mc_rc, MEMCACHED_END);
177
178 test_true(memc.setAll(keys, values, 50, 9));
179
180 test_true(memc.mget(keys));
181
182 while ((mc_rc= memc.fetch(return_key, return_value)) != MEMCACHED_END)
183 {
184 test_compare(return_key.length(), return_value.size());
185 test_memcmp(&return_value[0], return_key.c_str(), return_value.size());
186 }
187
188 return TEST_SUCCESS;
189 }
190
191 test_return_t basic_behavior(memcached_st *original)
192 {
193 Memcache memc(original);
194 uint64_t value= 1;
195 test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value));
196 uint64_t behavior= memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY);
197 test_compare(behavior, value);
198
199 return TEST_SUCCESS;
200 }
201
202 test_st tests[] ={
203 { "basic", 0,
204 reinterpret_cast<test_callback_fn>(basic_test) },
205 { "basic_master_key", 0,
206 reinterpret_cast<test_callback_fn>(basic_master_key_test) },
207 { "increment_test", 0,
208 reinterpret_cast<test_callback_fn>(increment_test) },
209 { "mget", 1,
210 reinterpret_cast<test_callback_fn>(mget_test) },
211 { "basic_behavior", 0,
212 reinterpret_cast<test_callback_fn>(basic_behavior) },
213 {0, 0, 0}
214 };
215
216 collection_st collection[] ={
217 {"block", 0, 0, tests},
218 {0, 0, 0, 0}
219 };
220
221 #define SERVERS_TO_CREATE 5
222
223 #include "libmemcached_world.h"
224
225 void get_world(world_st *world)
226 {
227 world->collections= collection;
228
229 world->create= reinterpret_cast<test_callback_create_fn>(world_create);
230 world->destroy= reinterpret_cast<test_callback_fn>(world_destroy);
231
232 world->test.startup= reinterpret_cast<test_callback_fn>(world_test_startup);
233 world->test.flush= reinterpret_cast<test_callback_fn>(world_flush);
234 world->test.pre_run= reinterpret_cast<test_callback_fn>(world_pre_run);
235 world->test.post_run= reinterpret_cast<test_callback_fn>(world_post_run);
236 world->test.on_error= reinterpret_cast<test_callback_error_fn>(world_on_error);
237
238 world->collection.startup= reinterpret_cast<test_callback_fn>(world_container_startup);
239 world->collection.shutdown= reinterpret_cast<test_callback_fn>(world_container_shutdown);
240
241 world->runner= &defualt_libmemcached_runner;
242 }