1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17#include <platform/non_negative_counter.h>
18
19#include <folly/portability/GTest.h>
20
21TEST(NonNegativeCounterTest, Increment) {
22    cb::NonNegativeCounter<size_t> nnAtomic(1);
23    ASSERT_EQ(1u, nnAtomic);
24
25    EXPECT_EQ(2u, ++nnAtomic);
26    EXPECT_EQ(2u, nnAtomic++);
27    EXPECT_EQ(3u, nnAtomic);
28}
29
30TEST(NonNegativeCounterTest, Add) {
31    cb::NonNegativeCounter<size_t> nnAtomic(1);
32    ASSERT_EQ(1u, nnAtomic);
33
34    EXPECT_EQ(3u, nnAtomic += 2);
35    EXPECT_EQ(3u, nnAtomic.fetch_add(2));
36    EXPECT_EQ(5u, nnAtomic);
37
38    // Adding a negative should subtract from the value
39    EXPECT_EQ(5u, nnAtomic.fetch_add(-2));
40    EXPECT_EQ(3u, nnAtomic);
41
42    EXPECT_EQ(3u, nnAtomic.fetch_add(-3));
43    EXPECT_EQ(0u, nnAtomic);
44}
45
46TEST(NonNegativeCounterTest, Decrement) {
47    cb::NonNegativeCounter<size_t> nnAtomic(2);
48    ASSERT_EQ(2u, nnAtomic);
49
50    EXPECT_EQ(1u, --nnAtomic);
51    EXPECT_EQ(1u, nnAtomic--);
52    EXPECT_EQ(0u, nnAtomic);
53}
54
55TEST(NonNegativeCounterTest, Subtract) {
56    cb::NonNegativeCounter<size_t> nnAtomic(4);
57    ASSERT_EQ(4u, nnAtomic);
58
59    EXPECT_EQ(2u, nnAtomic -= 2);
60    EXPECT_EQ(2u, nnAtomic.fetch_sub(2));
61    EXPECT_EQ(0u, nnAtomic);
62
63    EXPECT_EQ(2u, nnAtomic -= -2);
64    EXPECT_EQ(2u, nnAtomic.fetch_sub(-2));
65    EXPECT_EQ(4u, nnAtomic);
66}
67
68// Test that a NonNegativeCounter will clamp to zero.
69TEST(NonNegativeCounterTest, ClampsToZero) {
70    cb::NonNegativeCounter<size_t, cb::ClampAtZeroUnderflowPolicy> nnAtomic(0);
71
72    EXPECT_EQ(0u, --nnAtomic);
73    EXPECT_EQ(0u, nnAtomic--);
74    EXPECT_EQ(0u, nnAtomic);
75
76    nnAtomic = 5;
77    EXPECT_EQ(5u, nnAtomic.fetch_sub(10)); // returns previous value
78    EXPECT_EQ(0u, nnAtomic); // has been clamped to zero
79
80    nnAtomic = 5;
81    EXPECT_EQ(5u, nnAtomic.fetch_add(-10)); // return previous value
82    EXPECT_EQ(0u, nnAtomic); // has been clamped to zero
83}
84
85// Test that attempting to construct or assign a negative value is rejected.
86TEST(NonNegativeCounterTest, ClampsToZeroAssignment) {
87    cb::NonNegativeCounter<size_t, cb::ClampAtZeroUnderflowPolicy> nnAtomic(-1);
88    EXPECT_EQ(0u, nnAtomic) << "Construction with of negative number should clamped to zero";
89
90    // Reset to different value before next test.
91    nnAtomic = 10;
92
93    nnAtomic = -2;
94    EXPECT_EQ(0u, nnAtomic) << "Assignment of negative number should have been clamped to zero";
95}
96
97// Test the ThrowException policy.
98TEST(NonNegativeCounterTest, ThrowExceptionPolicy) {
99    cb::NonNegativeCounter<size_t, cb::ThrowExceptionUnderflowPolicy> nnAtomic(0);
100
101    EXPECT_THROW(--nnAtomic, std::underflow_error);
102    EXPECT_EQ(0u, nnAtomic);
103    EXPECT_THROW(nnAtomic--, std::underflow_error);
104    EXPECT_EQ(0u, nnAtomic);
105
106    EXPECT_THROW(nnAtomic.fetch_add(-1), std::underflow_error);
107    EXPECT_EQ(0u, nnAtomic);
108
109    EXPECT_THROW(nnAtomic += -1, std::underflow_error);
110    EXPECT_EQ(0u, nnAtomic);
111
112    EXPECT_THROW(nnAtomic -= 2, std::underflow_error);
113    EXPECT_EQ(0u, nnAtomic);
114}
115
116// Test that attempting to construct or assign a negative value is rejected.
117TEST(NonNegativeCounterTest, ThrowExceptionPolicyAssignment) {
118    using ThrowingCounter = cb::NonNegativeCounter<size_t, cb::ThrowExceptionUnderflowPolicy>;
119    EXPECT_THROW(ThrowingCounter(-1), std::underflow_error) << "Construction with of negative number should throw";
120
121    ThrowingCounter nnAtomic(10);
122    EXPECT_THROW(nnAtomic = -2, std::underflow_error) << "Assignment of negative number should throw";
123}
124