1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
4 TEST_CASE("memcached_inc_dec") {
5 pair
<string
, MemcachedCluster
> tests
[] = {
6 {"mixed", MemcachedCluster::mixed()},
9 LOOPED_SECTION(tests
) {
10 auto memc
= &test
.memc
;
11 uint64_t binary
= GENERATE(0, 1);
12 char *prefix
= GENERATE(as
<char *>{}, "", "namespace:");
14 REQUIRE_SUCCESS(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, binary
));
16 REQUIRE_SUCCESS(memcached_callback_set(memc
, MEMCACHED_CALLBACK_NAMESPACE
, prefix
));
19 DYNAMIC_SECTION("increment (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
22 REQUIRE_SUCCESS(memcached_set(memc
, S("number"), S("0"), 0, 0));
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
));
30 DYNAMIC_SECTION("increment by_key (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
33 REQUIRE_SUCCESS(memcached_set_by_key(memc
, S("key"), S("number"), S("0"), 0, 0));
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
));
41 DYNAMIC_SECTION("increment with initial (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
43 uint64_t initial
= GENERATE(0, 456);
46 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS
,
47 memcached_increment_with_initial(memc
, S("number"), 1, initial
, 0, &number
));
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);
56 DYNAMIC_SECTION("increment with initial by_key (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
58 uint64_t initial
= GENERATE(0, 456);
61 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS
,
62 memcached_increment_with_initial_by_key(memc
, S("key"), S("number"), 1, initial
, 0, &number
));
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);
71 DYNAMIC_SECTION("decrement (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
74 REQUIRE_SUCCESS(memcached_set(memc
, S("number"), S("10"), 0, 0));
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
));
81 DYNAMIC_SECTION("decrement by_key (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
84 REQUIRE_SUCCESS(memcached_set_by_key(memc
, S("key"), S("number"), S("10"), 0, 0));
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
));
91 DYNAMIC_SECTION("decrement with initial (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
93 uint64_t initial
= GENERATE(987, 456);
96 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS
,
97 memcached_decrement_with_initial(memc
, S("number"), 1, initial
, 0, &number
));
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);
105 DYNAMIC_SECTION("decrement with initial by_key (binary=" << binary
<< ", prefix=" << prefix
<< ")") {
107 uint64_t initial
= GENERATE(987, 456);
110 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS
,
111 memcached_decrement_with_initial_by_key(memc
, S("key"), S("number"), 1, initial
, 0, &number
));
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);