1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2015 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 "config.h"
19
20#include <relaxed_atomic.h>
21
22#include <gtest/gtest.h>
23
24// Test that we can use RelaxedAtomic<T> in STL containers.
25TEST(RelaxedAtomicTest, STLContainer) {
26    std::vector<Couchbase::RelaxedAtomic<uint64_t>> vec;
27
28    // Check we can populate a std::vector with assign()
29    vec.assign(3, 1);
30    EXPECT_EQ(1, vec[0]);
31    EXPECT_EQ(1, vec[1]);
32    EXPECT_EQ(1, vec[2]);
33
34    // Check we can change existing values
35    vec[2] = 2;
36    EXPECT_EQ(2, vec[2]);
37}
38
39TEST(RelaxedAtomicTest, setIfSmaller) {
40    Couchbase::RelaxedAtomic<uint8_t> val;
41    val.store(10);
42
43    // Check we don't store larger numbers
44    val.setIfSmaller(15);
45    EXPECT_EQ(val.load(), 10);
46
47    // Check we store smaller numbers
48    val.setIfSmaller(5);
49    EXPECT_EQ(val.load(), 5);
50
51    Couchbase::RelaxedAtomic<uint8_t> smaller;
52    smaller.store(3);
53
54    // Check we can correctly store from another Couchbase::RelaxedAtomic
55    val.setIfSmaller(smaller);
56    EXPECT_EQ(val.load(), 3);
57}
58