fdc7e916948e07e5c20f7f2445e1cae1f1ae8c64
[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 test_return 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 string value_set("This is some data");
35 string value;
36 size_t value_length;
37
38 foo.set("mine", value_set);
39 value= foo.get("mine", &value_length);
40
41 assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
42
43 return TEST_SUCCESS;
44 }
45
46 test_return increment_test(memcached_st *memc)
47 {
48 Memcached mcach(memc);
49 bool rc;
50 const string key("inctest");
51 const string 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);
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.c_str()));
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 == true);
66 assert(int_ret_value == 2);
67
68 rc= mcach.increment(key, 1, &int_ret_value);
69 assert(rc == true);
70 assert(int_ret_value == 3);
71
72 rc= mcach.increment(key, 5, &int_ret_value);
73 assert(rc == true);
74 assert(int_ret_value == 8);
75
76 return TEST_SUCCESS;
77 }
78
79 test_return basic_master_key_test(memcached_st *memc)
80 {
81 Memcached foo(memc);
82 const string 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);
90 value= foo.get_by_key(master_key_a, key, &value_length);
91
92 assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
93
94 value= foo.get_by_key(master_key_b, key, &value_length);
95 assert((memcmp(value.c_str(), value_set.c_str(), 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 { "increment_test", 0, increment_test },
105 {0, 0, 0}
106 };
107
108 collection_st collection[] ={
109 {"block", 0, 0, tests},
110 {0, 0, 0, 0}
111 };
112
113 #define SERVERS_TO_CREATE 1
114
115 extern "C" void *world_create(void)
116 {
117 server_startup_st *construct;
118
119 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
120 memset(construct, 0, sizeof(server_startup_st));
121
122 construct->count= SERVERS_TO_CREATE;
123 server_startup(construct);
124
125 return construct;
126 }
127
128 void world_destroy(void *p)
129 {
130 server_startup_st *construct= static_cast<server_startup_st *>(p);
131 memcached_server_st *servers=
132 static_cast<memcached_server_st *>(construct->servers);
133 memcached_server_list_free(servers);
134
135 server_shutdown(construct);
136 free(construct);
137 }
138
139 void get_world(world_st *world)
140 {
141 world->collections= collection;
142 world->create= world_create;
143 world->destroy= world_destroy;
144 }