1b7be39d2b13a2d898720cc0f245866c9fa72516
[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 test_return basic_test(memcached_st *memc);
25 uint8_t increment_test(memcached_st *memc);
26 test_return basic_master_key_test(memcached_st *memc);
27 void *world_create(void);
28 void world_destroy(void *p);
29 }
30
31 test_return basic_test(memcached_st *memc)
32 {
33 Memcached foo(memc);
34 const char *value_set= "This is some data";
35 string value;
36 size_t value_length;
37
38 foo.set("mine", value_set, strlen(value_set));
39 value= foo.get("mine", &value_length);
40
41 assert((memcmp(value.c_str(), value_set, value_length) == 0));
42
43 return TEST_SUCCESS;
44 }
45
46 uint8_t increment_test(memcached_st *memc)
47 {
48 Memcached mcach(memc);
49 memcached_return rc;
50 const string key("inctest");
51 const char *inc_value= "1";
52 string ret_value;
53 uint64_t int_inc_value;
54 uint64_t int_ret_value;
55 size_t value_length;
56
57 mcach.set(key, inc_value, strlen(inc_value));
58 ret_value= mcach.get(key, &value_length);
59 printf("\nretvalue %s\n",ret_value.c_str());
60 int_inc_value= uint64_t(atol(inc_value));
61 int_ret_value= uint64_t(atol(ret_value.c_str()));
62 assert(int_ret_value == int_inc_value);
63
64 rc= mcach.increment(key, 1, &int_ret_value);
65 assert(rc == MEMCACHED_SUCCESS);
66 assert(int_ret_value == 2);
67
68 rc= mcach.increment(key, 1, &int_ret_value);
69 assert(rc == MEMCACHED_SUCCESS);
70 assert(int_ret_value == 3);
71
72 rc= mcach.increment(key, 5, &int_ret_value);
73 assert(rc == MEMCACHED_SUCCESS);
74 assert(int_ret_value == 8);
75
76 return 0;
77 }
78
79 test_return basic_master_key_test(memcached_st *memc)
80 {
81 Memcached foo(memc);
82 const char *value_set= "Data for server A";
83 const string master_key_a("server-a");
84 const string master_key_b("server-b");
85 const string key("xyz");
86 string value;
87 size_t value_length;
88
89 foo.set_by_key(master_key_a, key, value_set, strlen(value_set));
90 value= foo.get_by_key(master_key_a, key, &value_length);
91
92 assert((memcmp(value.c_str(), value_set, value_length) == 0));
93
94 value= foo.get_by_key(master_key_b, key, &value_length);
95 assert((memcmp(value.c_str(), value_set, value_length) == 0));
96
97 return TEST_SUCCESS;
98 }
99
100
101 test_st tests[] ={
102 {"basic", 0, basic_test },
103 {"basic_master_key", 0, basic_master_key_test },
104 {0, 0, 0}
105 };
106
107 collection_st collection[] ={
108 {"block", 0, 0, tests},
109 {0, 0, 0, 0}
110 };
111
112 #define SERVERS_TO_CREATE 1
113
114 extern "C" void *world_create(void)
115 {
116 server_startup_st *construct;
117
118 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
119 memset(construct, 0, sizeof(server_startup_st));
120
121 construct->count= SERVERS_TO_CREATE;
122 server_startup(construct);
123
124 return construct;
125 }
126
127 void world_destroy(void *p)
128 {
129 server_startup_st *construct= static_cast<server_startup_st *>(p);
130 memcached_server_st *servers=
131 static_cast<memcached_server_st *>(construct->servers);
132 memcached_server_list_free(servers);
133
134 server_shutdown(construct);
135 free(construct);
136 }
137
138 void get_world(world_st *world)
139 {
140 world->collections= collection;
141 world->create= world_create;
142 world->destroy= world_destroy;
143 }