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