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