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