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