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