File: ringbuffer.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (134 lines) | stat: -rw-r--r-- 4,514 bytes parent folder | download | duplicates (4)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#define CAF_SUITE detail.ringbuffer

#include "caf/detail/ringbuffer.hpp"

#include "caf/test/dsl.hpp"

#include <algorithm>

using namespace caf;

namespace {

static constexpr size_t buf_size = 64;

using int_ringbuffer = detail::ringbuffer<int, buf_size>;

std::vector<int> consumer(int_ringbuffer& buf, size_t num) {
  std::vector<int> result;
  for (size_t i = 0; i < num; ++i) {
    buf.wait_nonempty();
    result.emplace_back(buf.front());
    buf.pop_front();
  }
  return result;
}

void producer(int_ringbuffer& buf, int first, int last) {
  for (auto i = first; i != last; ++i)
    buf.push_back(std::move(i));
}

struct fixture {
  int_ringbuffer buf;
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(ringbuffer_tests, fixture)

CAF_TEST(construction) {
  CAF_CHECK_EQUAL(buf.empty(), true);
  CAF_CHECK_EQUAL(buf.full(), false);
  CAF_CHECK_EQUAL(buf.size(), 0u);
}

CAF_TEST(push_back) {
  CAF_MESSAGE("add one element");
  buf.push_back(42);
  CAF_CHECK_EQUAL(buf.empty(), false);
  CAF_CHECK_EQUAL(buf.full(), false);
  CAF_CHECK_EQUAL(buf.size(), 1u);
  CAF_CHECK_EQUAL(buf.front(), 42);
  CAF_MESSAGE("remove element");
  buf.pop_front();
  CAF_CHECK_EQUAL(buf.empty(), true);
  CAF_CHECK_EQUAL(buf.full(), false);
  CAF_CHECK_EQUAL(buf.size(), 0u);
  CAF_MESSAGE("fill buffer");
  for (int i = 0; i < static_cast<int>(buf_size - 1); ++i)
    buf.push_back(std::move(i));
  CAF_CHECK_EQUAL(buf.empty(), false);
  CAF_CHECK_EQUAL(buf.full(), true);
  CAF_CHECK_EQUAL(buf.size(), buf_size - 1);
  CAF_CHECK_EQUAL(buf.front(), 0);
}

CAF_TEST(get all) {
  using array_type = std::array<int, buf_size>;
  using vector_type = std::vector<int>;
  array_type tmp;
  auto fetch_all = [&] {
    auto i = tmp.begin();
    auto e = buf.get_all(i);
    return vector_type(i, e);
  };
  CAF_MESSAGE("add five element");
  for (int i = 0; i < 5; ++i)
    buf.push_back(std::move(i));
  CAF_CHECK_EQUAL(buf.empty(), false);
  CAF_CHECK_EQUAL(buf.full(), false);
  CAF_CHECK_EQUAL(buf.size(), 5u);
  CAF_CHECK_EQUAL(buf.front(), 0);
  CAF_MESSAGE("drain elements");
  CAF_CHECK_EQUAL(fetch_all(), vector_type({0, 1, 2, 3, 4}));
  CAF_CHECK_EQUAL(buf.empty(), true);
  CAF_CHECK_EQUAL(buf.full(), false);
  CAF_CHECK_EQUAL(buf.size(), 0u);
  CAF_MESSAGE("add 60 elements (wraps around)");
  vector_type expected;
  for (int i = 0; i < 60; ++i) {
    expected.push_back(i);
    buf.push_back(std::move(i));
  }
  CAF_CHECK_EQUAL(buf.size(), 60u);
  CAF_CHECK_EQUAL(fetch_all(), expected);
  CAF_CHECK_EQUAL(buf.empty(), true);
  CAF_CHECK_EQUAL(buf.full(), false);
  CAF_CHECK_EQUAL(buf.size(), 0u);
}

CAF_TEST(concurrent access) {
  std::vector<std::thread> producers;
  producers.emplace_back(producer, std::ref(buf), 0, 100);
  producers.emplace_back(producer, std::ref(buf), 100, 200);
  producers.emplace_back(producer, std::ref(buf), 200, 300);
  auto vec = consumer(buf, 300);
  std::sort(vec.begin(), vec.end());
  CAF_CHECK(std::is_sorted(vec.begin(), vec.end()));
  CAF_CHECK_EQUAL(vec.size(), 300u);
  CAF_CHECK_EQUAL(vec.front(), 0);
  CAF_CHECK_EQUAL(vec.back(), 299);
  for (auto& t : producers)
    t.join();
}

CAF_TEST_FIXTURE_SCOPE_END()