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
18#include <platform/atomic_duration.h>
19
20#include <folly/portability/GTest.h>
21
22TEST(AtomicDurationTest, Constructors) {
23    // Test the constructors. Implicitly tests load() and store() functions.
24    cb::AtomicDuration<> atomicDurationDefault;
25    cb::AtomicDuration<> atomicDurationValue(
26            std::chrono::steady_clock::duration(20));
27    cb::AtomicDuration<> atomicDurationCopy(atomicDurationValue);
28
29    ASSERT_EQ(std::chrono::steady_clock::duration::zero(),
30              atomicDurationDefault.load());
31    ASSERT_EQ(std::chrono::steady_clock::duration(20),
32              atomicDurationValue.load());
33    ASSERT_EQ(atomicDurationValue.load(), atomicDurationCopy.load());
34}
35
36TEST(AtomicDurationTest, FetchAdd) {
37    cb::AtomicDuration<> atomicDuration(
38            std::chrono::steady_clock::duration(10));
39    EXPECT_EQ(std::chrono::steady_clock::duration(10),
40              atomicDuration.fetch_add(std::chrono::steady_clock::duration(
41                      5))); // returns previous value
42    EXPECT_EQ(std::chrono::steady_clock::duration(15), atomicDuration.load());
43}
44
45TEST(AtomicDurationTest, FetchSub) {
46    cb::AtomicDuration<> atomicDuration(
47            std::chrono::steady_clock::duration(10));
48    EXPECT_EQ(std::chrono::steady_clock::duration(10),
49              atomicDuration.fetch_sub(std::chrono::steady_clock::duration(
50                      5))); // returns previous value
51    EXPECT_EQ(std::chrono::steady_clock::duration(5), atomicDuration.load());
52}
53
54TEST(AtomicDurationTest, TypeCastOperator) {
55    cb::AtomicDuration<> atomicDuration(
56            std::chrono::steady_clock::duration(10));
57    EXPECT_EQ(std::chrono::steady_clock::duration(10),
58              static_cast<std::chrono::steady_clock::duration>(atomicDuration));
59}
60
61TEST(AtomicDurationTest, AssignOperator) {
62    cb::AtomicDuration<> atomicDuration;
63    atomicDuration = std::chrono::steady_clock::duration(10);
64    EXPECT_EQ(std::chrono::steady_clock::duration(10), atomicDuration.load());
65}
66
67TEST(AtomicDurationTest, AddAssignOperator) {
68    cb::AtomicDuration<> atomicDuration(
69            std::chrono::steady_clock::duration(10));
70    atomicDuration += std::chrono::steady_clock::duration(5);
71    EXPECT_EQ(std::chrono::steady_clock::duration(15), atomicDuration.load());
72}
73
74TEST(AtomicDurationTest, SubtractAssignOperator) {
75    cb::AtomicDuration<> atomicDuration(
76            std::chrono::steady_clock::duration(10));
77    atomicDuration -= std::chrono::steady_clock::duration(5);
78    EXPECT_EQ(std::chrono::steady_clock::duration(5), atomicDuration.load());
79}
80
81TEST(AtomicDurationTest, PreIncrement) {
82    cb::AtomicDuration<> atomicDuration(
83            std::chrono::steady_clock::duration(10));
84    EXPECT_EQ(std::chrono::steady_clock::duration(11), ++atomicDuration);
85}
86
87TEST(AtomicDurationTest, PostIncrement) {
88    cb::AtomicDuration<> atomicDuration(
89            std::chrono::steady_clock::duration(10));
90    EXPECT_EQ(std::chrono::steady_clock::duration(10),
91              atomicDuration++); // returns previous value
92    EXPECT_EQ(std::chrono::steady_clock::duration(11), atomicDuration.load());
93}
94
95TEST(AtomicDurationTest, PreDecrement) {
96    cb::AtomicDuration<> atomicDuration(
97            std::chrono::steady_clock::duration(10));
98    EXPECT_EQ(std::chrono::steady_clock::duration(9), --atomicDuration);
99}
100
101TEST(AtomicDurationTest, PostDecrement) {
102    cb::AtomicDuration<> atomicDuration(
103            std::chrono::steady_clock::duration(10));
104    EXPECT_EQ(std::chrono::steady_clock::duration(10),
105              atomicDuration--); // returns previous value
106    EXPECT_EQ(std::chrono::steady_clock::duration(9), atomicDuration.load());
107}
108