testing: parser
[awesomized/libmemcached] / test / fixtures / parser.hpp
1 #pragma once
2
3 #include <cstring>
4 #include <functional>
5 #include <utility>
6
7 using namespace std;
8
9 struct c_string {
10 const char *c_str;
11 size_t size;
12
13 c_string(const char *s, size_t l) noexcept
14 : c_str{s}
15 , size{l}
16 {}
17 bool operator ==(const c_string &cs) const noexcept {
18 return !strcmp(c_str, cs.c_str);
19 }
20 bool operator ==(const char *p) const noexcept {
21 return !strcmp(c_str, p);
22 }
23 };
24
25 static c_string null_c_string{nullptr, 0};
26
27 struct test_case {
28 using setting_key_t = variant<memcached_flag_t, memcached_behavior_t>;
29 using setting_t = pair<setting_key_t, uint64_t>;
30 using result_t = variant<c_string, setting_t>;
31 c_string option;
32 result_t result;
33
34 test_case(c_string o, c_string r) noexcept
35 : option{o}
36 , result{r}
37 {}
38 test_case(const char *os, size_t ol, const char *rs, size_t rl) noexcept
39 : option{os, ol}
40 , result{c_string{rs, rl}}
41 {}
42 test_case(const char *os, size_t ol, memcached_behavior_t b, uint64_t v) noexcept
43 : option{os, ol}
44 , result{setting_t{b, v}}
45 {}
46 test_case(const char *os, size_t ol, memcached_flag_t f, uint64_t v) noexcept
47 : option{os, ol}
48 , result{setting_t{f, v}}
49 {}
50 };
51
52 #define test_count(tca) (sizeof(tca)/sizeof(tca[0]))
53
54 struct test_group {
55 using check_func = function<void(memcached_st *, const test_case::result_t &)>;
56 const char *name;
57 check_func check;
58 test_case *tests;
59 size_t ntests;
60
61 test_group(const char *name_, check_func check_, test_case *tests_, size_t ntests_) noexcept
62 : name{name_}
63 , check{move(check_)}
64 , tests{tests_}
65 , ntests{ntests_}
66 {}
67 };
68
69 static test_case host_string_tests[] = {
70 {S("--server=localhost"), S("localhost")},
71 {S("--server=10.0.2.1"), S("10.0.2.1")},
72 {S("--server=example.com"), S("example.com")},
73 {S("--server=localhost:30"), S("localhost")},
74 {S("--server=10.0.2.1:20"), S("10.0.2.1")},
75 {S("--server=example.com:1024"), S("example.com")},
76 {S("--server=10.0.2.1:30/?40"), S("10.0.2.1")},
77 {S("--server=example.com:1024/?30"), S("example.com")},
78 {S("--server=10.0.2.1/?20"), S("10.0.2.1")},
79 {S("--server=example.com/?10"), S("example.com")},
80 };
81
82 static test_group test_host_strings{
83 "host strings",
84 [](memcached_st *memc, const test_case::result_t &result) {
85 auto instance = memcached_server_instance_by_position(memc, 0);
86 REQUIRE(instance);
87 REQUIRE(get<c_string>(result) == memcached_server_name(instance));
88 },
89 host_string_tests,
90 test_count(host_string_tests)
91 };
92
93 static test_case bad_host_string_tests[] = {
94 {{S("-servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225")}, null_c_string},
95 {{S("-- servers=a.example.com:81,localhost:82,b.example.com")}, null_c_string},
96 {{S("--servers=localhost:+80")}, null_c_string},
97 // all of the following should not fail IMO
98 {{S("--servers=localhost.com.")}, null_c_string},
99 {{S("--server=localhost.com.")}, null_c_string},
100 {{S("--server=localhost.com.:80")}, null_c_string},
101 };
102
103 static test_group test_bad_host_strings{
104 "bad host strings",
105 [](memcached_st *memc, const test_case::result_t &) {
106 REQUIRE_FALSE(memc);
107 },
108 bad_host_string_tests,
109 test_count(bad_host_string_tests)
110 };
111
112 static test_case behavior_tests[] = {
113 {S("--CONNECT-TIMEOUT=456"), MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 456},
114 {S("--IO-BYTES-WATERMARK=456"), MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK, 456},
115 {S("--IO-KEY-PREFETCH=456"), MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH, 456},
116 {S("--IO-MSG-WATERMARK=456"), MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 456},
117 {S("--NUMBER-OF-REPLICAS=456"), MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 456},
118 {S("--POLL-TIMEOUT=456"), MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 456},
119 {S("--RCV-TIMEOUT=456"), MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 456},
120 {S("--REMOVE-FAILED-SERVERS=3"), MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, 1},
121 {S("--RETRY-TIMEOUT=456"), MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 456},
122 {S("--SND-TIMEOUT=456"), MEMCACHED_BEHAVIOR_SND_TIMEOUT, 456},
123 {S("--SOCKET-RECV-SIZE=456"), MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, 456},
124 {S("--SOCKET-SEND-SIZE=456"), MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, 456},
125 {S("--BINARY-PROTOCOL"), MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1},
126 {S("--BUFFER-REQUESTS"), MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1},
127 {S("--NOREPLY"), MEMCACHED_BEHAVIOR_NOREPLY, 1},
128 {S("--RANDOMIZE-REPLICA-READ"), MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1},
129 {S("--SORT-HOSTS"), MEMCACHED_BEHAVIOR_SORT_HOSTS, 1},
130 {S("--SUPPORT-CAS"), MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1},
131 {S("--TCP-NODELAY"), MEMCACHED_BEHAVIOR_TCP_NODELAY, 1},
132 {S("--TCP-KEEPALIVE"), MEMCACHED_BEHAVIOR_TCP_KEEPALIVE, 1},
133 {S("--TCP-KEEPIDLE"), MEMCACHED_BEHAVIOR_TCP_KEEPIDLE, 1},
134 {S("--USE-UDP"), MEMCACHED_BEHAVIOR_USE_UDP, 1},
135 {S("--VERIFY-KEY"), MEMCACHED_BEHAVIOR_VERIFY_KEY, 1},
136 {S("--DISTRIBUTION=consistent"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT},
137 {S("--DISTRIBUTION=consistent,CRC"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT},
138 {S("--DISTRIBUTION=consistent,MD5"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT},
139 {S("--DISTRIBUTION=consistent,JENKINS"), MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_JENKINS},
140 {S("--DISTRIBUTION=random"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_RANDOM},
141 {S("--DISTRIBUTION=modula"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_MODULA},
142 {S("--HASH=CRC"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_CRC},
143 {S("--HASH=FNV1A_32"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_FNV1A_32},
144 {S("--HASH=FNV1_32"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_FNV1_32},
145 {S("--HASH=JENKINS"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_JENKINS},
146 {S("--HASH=MD5"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_MD5},
147
148 };
149
150 static test_group test_behaviors{
151 "behaviors",
152 [](memcached_st *memc, const test_case::result_t &result) {
153 auto setting = get<test_case::setting_t>(result);
154 REQUIRE(memc);
155 REQUIRE(setting.second ==
156 memcached_behavior_get(memc, get<memcached_behavior_t>(setting.first)));
157 },
158 behavior_tests,
159 test_count(behavior_tests)
160 };
161
162 static test_case flag_tests[] = {
163 {S("--FETCH-VERSION"), MEMCACHED_FLAG_IS_FETCHING_VERSION, 1},
164 {S("--HASH-WITH-NAMESPACE"), MEMCACHED_FLAG_HASH_WITH_NAMESPACE, 1},
165 };
166
167 static test_group test_flags{
168 "flags",
169 [](memcached_st *memc, const test_case::result_t &result) {
170 auto setting = get<test_case::setting_t>(result);
171 REQUIRE(memc);
172 REQUIRE(setting.second == memcached_flag(*memc, get<memcached_flag_t>(setting.first)));
173 },
174 flag_tests,
175 test_count(flag_tests)
176 };
177
178 static test_case namespace_tests[] = {
179 {S("--NAMESPACE=foo"), S("foo")},
180 {S("--NAMESPACE=\"foo\""), S("foo")},
181 {S("--NAMESPACE=\"The quick brown fox jumps over the lazy dog\""), S("The quick brown fox jumps over the lazy dog")},
182 };
183
184 static test_group test_namespace{
185 "namespace",
186 [](memcached_st *memc, const test_case::result_t &result) {
187 auto ns = get<c_string>(result);
188 REQUIRE(memc);
189 REQUIRE(ns == memcached_get_namespace(*memc));
190 },
191 namespace_tests,
192 test_count(namespace_tests)
193 };
194
195 static test_group tests[] = {
196 test_host_strings,
197 test_bad_host_strings,
198 test_behaviors,
199 test_flags,
200 };