460166f544fd9685ab7e1787db772f445ef1cee8
[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.hpp>
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 test_true(foo.setByKey(master_key_a, key, value, 0, 0));
118 test_true(foo.getByKey(master_key_a, key, test_value));
119
120 test_compare(value.size(), test_value.size());
121 test_memcmp(&value[0], &test_value[0], value.size());
122
123 test_value.clear();
124
125 test_false(foo.getByKey(master_key_b, key, test_value));
126 test_compare(0, test_value.size());
127
128 return TEST_SUCCESS;
129 }
130
131 /* Count the results */
132 memcached_return_t callback_counter(const memcached_st *,
133 memcached_result_st *,
134 void *context)
135 {
136 unsigned int *counter= static_cast<unsigned int *>(context);
137
138 *counter= *counter +1;
139
140 return MEMCACHED_SUCCESS;
141 }
142
143 test_return_t mget_test(memcached_st *original)
144 {
145 Memcache memc(original);
146 memcached_return_t mc_rc;
147 vector<string> keys;
148 vector< vector<char> *> values;
149 keys.reserve(3);
150 keys.push_back("fudge");
151 keys.push_back("son");
152 keys.push_back("food");
153 vector<char> val1;
154 vector<char> val2;
155 vector<char> val3;
156 populate_vector(val1, "fudge");
157 populate_vector(val2, "son");
158 populate_vector(val3, "food");
159 values.reserve(3);
160 values.push_back(&val1);
161 values.push_back(&val2);
162 values.push_back(&val3);
163
164 string return_key;
165 vector<char> return_value;
166
167 /* We need to empty the server before we continue the test */
168 test_true(memc.flush());
169
170 test_true(memc.mget(keys));
171
172 test_compare(MEMCACHED_NOTFOUND,
173 memc.fetch(return_key, return_value));
174
175 test_true(memc.setAll(keys, values, 50, 9));
176
177 test_true(memc.mget(keys));
178 size_t count= 0;
179 while (memcached_success(mc_rc= memc.fetch(return_key, return_value)))
180 {
181 test_compare(return_key.length(), return_value.size());
182 test_memcmp(&return_value[0], return_key.c_str(), return_value.size());
183 count++;
184 }
185 test_compare(values.size(), count);
186
187 return TEST_SUCCESS;
188 }
189
190 test_return_t basic_behavior(memcached_st *original)
191 {
192 Memcache memc(original);
193 uint64_t value= 1;
194 test_true(memc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value));
195 uint64_t behavior= memc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY);
196 test_compare(behavior, value);
197
198 return TEST_SUCCESS;
199 }
200
201 test_st tests[] ={
202 { "basic", 0,
203 reinterpret_cast<test_callback_fn*>(basic_test) },
204 { "basic_master_key", 0,
205 reinterpret_cast<test_callback_fn*>(basic_master_key_test) },
206 { "increment_test", 0,
207 reinterpret_cast<test_callback_fn*>(increment_test) },
208 { "mget", 1,
209 reinterpret_cast<test_callback_fn*>(mget_test) },
210 { "basic_behavior", 0,
211 reinterpret_cast<test_callback_fn*>(basic_behavior) },
212 {0, 0, 0}
213 };
214
215 collection_st collection[] ={
216 {"block", 0, 0, tests},
217 {0, 0, 0, 0}
218 };
219
220 #define SERVERS_TO_CREATE 5
221
222 #include "libmemcached_world.h"
223
224 void get_world(Framework *world)
225 {
226 world->collections= collection;
227
228 world->_create= reinterpret_cast<test_callback_create_fn*>(world_create);
229 world->_destroy= reinterpret_cast<test_callback_fn*>(world_destroy);
230
231 world->item._startup= reinterpret_cast<test_callback_fn*>(world_test_startup);
232 world->item._flush= reinterpret_cast<test_callback_fn*>(world_flush);
233 world->item.set_pre(reinterpret_cast<test_callback_fn*>(world_pre_run));
234 world->item.set_post(reinterpret_cast<test_callback_fn*>(world_post_run));
235 world->_on_error= reinterpret_cast<test_callback_error_fn*>(world_on_error);
236
237 world->collection_startup= reinterpret_cast<test_callback_fn*>(world_container_startup);
238 world->collection_shutdown= reinterpret_cast<test_callback_fn*>(world_container_shutdown);
239
240 world->runner= &defualt_libmemcached_runner;
241 }