aboutsummaryrefslogtreecommitdiff
path: root/test/ring.cc
blob: 657fdfa5d5aa1eabb746b3e0759767955045b179 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <ring.h>

#include <cstdio>

#define LOG    \
  if (false) { \
  } else       \
    puts(__PRETTY_FUNCTION__)

/// A helper sruct to count instances, copy- and move constructions.
struct m {
  m() {
    LOG;
    ++cnt;
  }
  ~m() {
    LOG;
    --cnt;
  }
  m(const m&) {
    LOG;
    ++cnt;
    ++cpy;
  }
  m(m&&) noexcept {
    LOG;
    ++cnt;
    ++mov;
  }
  m& operator=(const m&) = delete;
  m& operator=(m&&) = delete;

  static std::size_t cnt;
  static std::size_t mov;
  static std::size_t cpy;
};

std::size_t m::cnt = 0;
std::size_t m::mov = 0;
std::size_t m::cpy = 0;

int main() {
  {
    ring<m, 8> r;

    assert(r.size() == 0);
    assert(r.is_empty());
    assert(!r.is_full());

    for (int i = 0; i < 10; ++i) {
      bool ok = r.emplace();
      i < 8 ? assert(ok) : assert(!ok);
    }

    assert(r.size() == 8);
    assert(!r.is_empty());
    assert(r.is_full());

    auto v = r.take();
#if __cplusplus >= 201703L
    assert(v.has_value());
#endif

    assert(r.size() == 7);
    assert(!r.is_empty());
    assert(!r.is_full());

    assert(m::cnt == 8);
  }

  assert(m::cnt == 0);

  m::mov = 0;
  m::cpy = 0;
  {
    ring<m, 8> r;

    // Move construct m from an m rvalue reference.
    assert(m::mov == 0);
    r.push(m{});
    assert(m::mov == 1);

    // Copy construct m from an m lvalue reference.
    m m;
    assert(m::cpy == 0);
    r.push(m);
    assert(m::cpy == 1);

    assert(m::cnt == 3);
  }

  assert(m::cnt == 0);
}