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 the ThrowException policy.
86TEST(NonNegativeCounterTest, ThrowExceptionPolicy) {
87    cb::NonNegativeCounter<size_t, cb::ThrowExceptionUnderflowPolicy> nnAtomic(0);
88
89    EXPECT_THROW(--nnAtomic, std::underflow_error);
90    EXPECT_EQ(0u, nnAtomic);
91    EXPECT_THROW(nnAtomic--, std::underflow_error);
92    EXPECT_EQ(0u, nnAtomic);
93
94    EXPECT_THROW(nnAtomic.fetch_add(-1), std::underflow_error);
95    EXPECT_EQ(0u, nnAtomic);
96
97    EXPECT_THROW(nnAtomic += -1, std::underflow_error);
98    EXPECT_EQ(0u, nnAtomic);
99
100    EXPECT_THROW(nnAtomic -= 2, std::underflow_error);
101    EXPECT_EQ(0u, nnAtomic);
102}
103
104