1 | // |
---|---|
2 | // immer: immutable data structures for C++ |
3 | // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente |
4 | // |
5 | // This software is distributed under the Boost Software License, Version 1.0. |
6 | // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt |
7 | // |
8 | |
9 | #include <immer/refcount/refcount_policy.hpp> |
10 | #include <immer/refcount/unsafe_refcount_policy.hpp> |
11 | #include <immer/refcount/no_refcount_policy.hpp> |
12 | |
13 | #include <catch.hpp> |
14 | |
15 | TEST_CASE("no refcount has no data") |
16 | { |
17 | static_assert(std::is_empty<immer::no_refcount_policy>{}, ""); |
18 | } |
19 | |
20 | template <typename RefcountPolicy> |
21 | void test_refcount() |
22 | { |
23 | using refcount = RefcountPolicy; |
24 | |
25 | SECTION("starts at one") |
26 | { |
27 | refcount elem{}; |
28 | CHECK(elem.dec()); |
29 | } |
30 | |
31 | SECTION("disowned starts at zero") |
32 | { |
33 | refcount elem{immer::disowned{}}; |
34 | elem.inc(); |
35 | CHECK(elem.dec()); |
36 | } |
37 | |
38 | SECTION("inc dec") |
39 | { |
40 | refcount elem{}; |
41 | elem.inc(); |
42 | CHECK(!elem.dec()); |
43 | CHECK(elem.dec()); |
44 | } |
45 | |
46 | SECTION("inc dec unsafe") |
47 | { |
48 | refcount elem{}; |
49 | elem.inc(); |
50 | CHECK(!elem.dec()); |
51 | elem.inc(); |
52 | elem.dec_unsafe(); |
53 | CHECK(elem.dec()); |
54 | } |
55 | } |
56 | |
57 | TEST_CASE("basic refcount") |
58 | { |
59 | test_refcount<immer::refcount_policy>(); |
60 | } |
61 | |
62 | TEST_CASE("thread unsafe refcount") |
63 | { |
64 | test_refcount<immer::unsafe_refcount_policy>(); |
65 | } |
66 |