bin: consolidate clients
[awesomized/libmemcached] / test / tests / memcached / inc_dec.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3
4 TEST_CASE("memcached_inc_dec") {
5 pair<string, MemcachedCluster> tests[] = {
6 {"mixed", MemcachedCluster::mixed()},
7 };
8
9 LOOPED_SECTION(tests) {
10 auto memc = &test.memc;
11 uint64_t binary = GENERATE(0, 1);
12 char *prefix = GENERATE(as<char *>{}, "", "namespace:");
13
14 REQUIRE_SUCCESS(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, binary));
15 if (*prefix) {
16 REQUIRE_SUCCESS(memcached_callback_set(memc, MEMCACHED_CALLBACK_NAMESPACE, prefix));
17 }
18
19 DYNAMIC_SECTION("increment (binary=" << binary << ", prefix=" << prefix << ")") {
20 uint64_t number;
21
22 REQUIRE_SUCCESS(memcached_set(memc, S("number"), S("0"), 0, 0));
23
24 for (auto i = 1; i <= 10; ++i) {
25 REQUIRE_SUCCESS(memcached_increment(memc, S("number"), 1, &number));
26 REQUIRE(number == static_cast<uint64_t>(i));
27 }
28 }
29
30 DYNAMIC_SECTION("increment by_key (binary=" << binary << ", prefix=" << prefix << ")") {
31 uint64_t number;
32
33 REQUIRE_SUCCESS(memcached_set_by_key(memc, S("key"), S("number"), S("0"), 0, 0));
34
35 for (auto i = 1; i <= 10; ++i) {
36 REQUIRE_SUCCESS(memcached_increment_by_key(memc, S("key"), S("number"), 1, &number));
37 REQUIRE(number == static_cast<uint64_t>(i));
38 }
39 }
40
41 DYNAMIC_SECTION("increment with initial (binary=" << binary << ", prefix=" << prefix << ")") {
42 uint64_t number;
43 uint64_t initial = GENERATE(0, 456);
44
45 if (!binary) {
46 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS,
47 memcached_increment_with_initial(memc, S("number"), 1, initial, 0, &number));
48 } else {
49 REQUIRE_SUCCESS(memcached_increment_with_initial(memc, S("number"), 123, initial, 0, &number));
50 REQUIRE(number == initial);
51 REQUIRE_SUCCESS(memcached_increment_with_initial(memc, S("number"), 123, initial, 0, &number));
52 REQUIRE(number == initial + 123);
53 }
54 }
55
56 DYNAMIC_SECTION("increment with initial by_key (binary=" << binary << ", prefix=" << prefix << ")") {
57 uint64_t number;
58 uint64_t initial = GENERATE(0, 456);
59
60 if (!binary) {
61 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS,
62 memcached_increment_with_initial_by_key(memc, S("key"), S("number"), 1, initial, 0, &number));
63 } else {
64 REQUIRE_SUCCESS(memcached_increment_with_initial_by_key(memc, S("key"), S("number"), 123, initial, 0, &number));
65 REQUIRE(number == initial);
66 REQUIRE_SUCCESS(memcached_increment_with_initial_by_key(memc, S("key"), S("number"), 123, initial, 0, &number));
67 REQUIRE(number == initial + 123);
68 }
69 }
70
71 DYNAMIC_SECTION("decrement (binary=" << binary << ", prefix=" << prefix << ")") {
72 uint64_t number;
73
74 REQUIRE_SUCCESS(memcached_set(memc, S("number"), S("10"), 0, 0));
75
76 for (auto i = 9; i >= 0; --i) {
77 REQUIRE_SUCCESS(memcached_decrement(memc, S("number"), 1, &number));
78 REQUIRE(number == static_cast<uint64_t>(i));
79 }
80 }
81 DYNAMIC_SECTION("decrement by_key (binary=" << binary << ", prefix=" << prefix << ")") {
82 uint64_t number;
83
84 REQUIRE_SUCCESS(memcached_set_by_key(memc, S("key"), S("number"), S("10"), 0, 0));
85
86 for (auto i = 9; i >= 0; --i) {
87 REQUIRE_SUCCESS(memcached_decrement_by_key(memc, S("key"), S("number"), 1, &number));
88 REQUIRE(number == static_cast<uint64_t>(i));
89 }
90 }
91 DYNAMIC_SECTION("decrement with initial (binary=" << binary << ", prefix=" << prefix << ")") {
92 uint64_t number;
93 uint64_t initial = GENERATE(987, 456);
94
95 if (!binary) {
96 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS,
97 memcached_decrement_with_initial(memc, S("number"), 1, initial, 0, &number));
98 } else {
99 REQUIRE_SUCCESS(memcached_decrement_with_initial(memc, S("number"), 123, initial, 0, &number));
100 REQUIRE(number == initial);
101 REQUIRE_SUCCESS(memcached_decrement_with_initial(memc, S("number"), 123, initial, 0, &number));
102 REQUIRE(number == initial - 123);
103 }
104 }
105 DYNAMIC_SECTION("decrement with initial by_key (binary=" << binary << ", prefix=" << prefix << ")") {
106 uint64_t number;
107 uint64_t initial = GENERATE(987, 456);
108
109 if (!binary) {
110 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS,
111 memcached_decrement_with_initial_by_key(memc, S("key"), S("number"), 1, initial, 0, &number));
112 } else {
113 REQUIRE_SUCCESS(memcached_decrement_with_initial_by_key(memc, S("key"), S("number"), 123, initial, 0, &number));
114 REQUIRE(number == initial);
115 REQUIRE_SUCCESS(memcached_decrement_with_initial_by_key(memc, S("key"), S("number"), 123, initial, 0, &number));
116 REQUIRE(number == initial - 123);
117 }
118 }
119 }
120 }