Modified the fetch() function in the C++ interface to return a bool instead
[awesomized/libmemcached] / tests / plus.cpp
1 /*
2 C++ interface test
3 */
4 #include "libmemcached/memcached.hh"
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
23 extern "C" {
24 void *world_create(void);
25 void world_destroy(void *p);
26 }
27
28 static test_return basic_test(memcached_st *memc)
29 {
30 Memcached foo(memc);
31 const string value_set("This is some data");
32 string value;
33 size_t value_length;
34
35 foo.set("mine", value_set, 0, 0);
36 value= foo.get("mine", &value_length);
37
38 assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
39
40 return TEST_SUCCESS;
41 }
42
43 static test_return increment_test(memcached_st *memc)
44 {
45 Memcached mcach(memc);
46 bool rc;
47 const string key("inctest");
48 const string inc_value("1");
49 string ret_value;
50 uint64_t int_inc_value;
51 uint64_t int_ret_value;
52 size_t value_length;
53
54 mcach.set(key, inc_value, 0, 0);
55 ret_value= mcach.get(key, &value_length);
56 printf("\nretvalue %s\n",ret_value.c_str());
57 int_inc_value= uint64_t(atol(inc_value.c_str()));
58 int_ret_value= uint64_t(atol(ret_value.c_str()));
59 assert(int_ret_value == int_inc_value);
60
61 rc= mcach.increment(key, 1, &int_ret_value);
62 assert(rc == true);
63 assert(int_ret_value == 2);
64
65 rc= mcach.increment(key, 1, &int_ret_value);
66 assert(rc == true);
67 assert(int_ret_value == 3);
68
69 rc= mcach.increment(key, 5, &int_ret_value);
70 assert(rc == true);
71 assert(int_ret_value == 8);
72
73 return TEST_SUCCESS;
74 }
75
76 static test_return basic_master_key_test(memcached_st *memc)
77 {
78 Memcached foo(memc);
79 const string value_set("Data for server A");
80 const string master_key_a("server-a");
81 const string master_key_b("server-b");
82 const string key("xyz");
83 string value;
84 size_t value_length;
85
86 foo.set_by_key(master_key_a, key, value_set, 0, 0);
87 value= foo.get_by_key(master_key_a, key, &value_length);
88
89 assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
90
91 value= foo.get_by_key(master_key_b, key, &value_length);
92 assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
93
94 return TEST_SUCCESS;
95 }
96
97 /* Count the results */
98 static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
99 memcached_result_st *result __attribute__((unused)),
100 void *context)
101 {
102 unsigned int *counter= static_cast<unsigned int *>(context);
103
104 *counter= *counter + 1;
105
106 return MEMCACHED_SUCCESS;
107 }
108
109 static test_return mget_result_function(memcached_st *memc)
110 {
111 Memcached mc(memc);
112 bool rc;
113 string key1("fudge");
114 string key2("son");
115 string key3("food");
116 vector<string> keys;
117 keys.reserve(3);
118 keys.push_back(key1);
119 keys.push_back(key2);
120 keys.push_back(key3);
121 unsigned int counter;
122 memcached_execute_function callbacks[1];
123
124 /* We need to empty the server before we continue the test */
125 rc= mc.flush(0);
126 rc= mc.set_all(keys, keys, 50, 9);
127 assert(rc == true);
128
129 rc= mc.mget(keys);
130 assert(rc == true);
131
132 callbacks[0]= &callback_counter;
133 counter= 0;
134 rc= mc.fetch_execute(callbacks, static_cast<void *>(&counter), 1);
135
136 assert(counter == 3);
137
138 return TEST_SUCCESS;
139 }
140
141 static test_return mget_test(memcached_st *memc)
142 {
143 Memcached mc(memc);
144 bool rc;
145 memcached_return mc_rc;
146 vector<string> keys;
147 keys.reserve(3);
148 keys.push_back("fudge");
149 keys.push_back("son");
150 keys.push_back("food");
151 uint32_t flags;
152
153 string return_key;
154 size_t return_key_length;
155 string return_value;
156 size_t return_value_length;
157
158 /* We need to empty the server before we continue the test */
159 rc= mc.flush(0);
160 assert(rc == true);
161
162 rc= mc.mget(keys);
163 assert(rc == true);
164
165 while (mc.fetch(return_key, return_value, &return_key_length,
166 &return_value_length, &flags, &mc_rc))
167 {
168 assert(return_value.length() != 0);
169 }
170 assert(return_value_length == 0);
171 assert(mc_rc == MEMCACHED_END);
172
173 rc= mc.set_all(keys, keys, 50, 9);
174 assert(rc == true);
175
176 rc= mc.mget(keys);
177 assert(rc == true);
178
179 while ((mc.fetch(return_key, return_value, &return_key_length,
180 &return_value_length, &flags, &mc_rc)))
181 {
182 assert(return_value.length() != 0);
183 assert(mc_rc == MEMCACHED_SUCCESS);
184 assert(return_key_length == return_value_length);
185 assert(!memcmp(return_value.c_str(), return_key.c_str(), return_value_length));
186 }
187
188 return TEST_SUCCESS;
189 }
190
191 test_st tests[] ={
192 { "basic", 0, basic_test },
193 { "basic_master_key", 0, basic_master_key_test },
194 { "increment_test", 0, increment_test },
195 { "mget", 1, mget_test },
196 { "mget_result_function", 1, mget_result_function },
197 {0, 0, 0}
198 };
199
200 collection_st collection[] ={
201 {"block", 0, 0, tests},
202 {0, 0, 0, 0}
203 };
204
205 #define SERVERS_TO_CREATE 1
206
207 extern "C" void *world_create(void)
208 {
209 server_startup_st *construct;
210
211 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
212 memset(construct, 0, sizeof(server_startup_st));
213
214 construct->count= SERVERS_TO_CREATE;
215 server_startup(construct);
216
217 return construct;
218 }
219
220 void world_destroy(void *p)
221 {
222 server_startup_st *construct= static_cast<server_startup_st *>(p);
223 memcached_server_st *servers=
224 static_cast<memcached_server_st *>(construct->servers);
225 memcached_server_list_free(servers);
226
227 server_shutdown(construct);
228 free(construct);
229 }
230
231 void get_world(world_st *world)
232 {
233 world->collections= collection;
234 world->create= world_create;
235 world->destroy= world_destroy;
236 }