File: span.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 (117 lines) | stat: -rw-r--r-- 4,268 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
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 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 span

#include "caf/span.hpp"

#include "caf/test/dsl.hpp"

#include <algorithm>

using namespace caf;

namespace {

using i8_list = std::vector<int8_t>;

using i16_list = std::vector<int16_t>;

template <class T, class U>
bool equal(const T& xs, const U& ys) {
  return xs.size() == ys.size() && std::equal(xs.begin(), xs.end(), ys.begin());
}

struct fixture {
  i8_list chars{'a', 'b', 'c', 'd', 'e', 'f'};

  i8_list rchars{'f', 'e', 'd', 'c', 'b', 'a'};

  i16_list shorts{1, 2, 4, 8, 16, 32, 64};

  i16_list rshorts{64, 32, 16, 8, 4, 2, 1};
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(span_tests, fixture)

CAF_TEST(default construction) {
  span<int> xs;
  CAF_CHECK_EQUAL(xs.size(), 0u);
  CAF_CHECK_EQUAL(xs.empty(), true);
  CAF_CHECK_EQUAL(xs.data(), nullptr);
  CAF_CHECK_EQUAL(xs.size_bytes(), 0u);
  CAF_CHECK_EQUAL(xs.begin(), xs.end());
  CAF_CHECK_EQUAL(xs.cbegin(), xs.cend());
  CAF_CHECK_EQUAL(xs.rbegin(), xs.rend());
  CAF_CHECK_EQUAL(xs.crbegin(), xs.crend());
  CAF_CHECK_EQUAL(as_bytes(xs).size_bytes(), 0u);
  CAF_CHECK_EQUAL(as_writable_bytes(xs).size_bytes(), 0u);
}

CAF_TEST(iterators) {
  auto xs = make_span(chars);
  CAF_CHECK(std::equal(xs.begin(), xs.end(), chars.begin()));
  CAF_CHECK(std::equal(xs.rbegin(), xs.rend(), rchars.begin()));
  auto ys = make_span(shorts);
  CAF_CHECK(std::equal(ys.begin(), ys.end(), shorts.begin()));
  CAF_CHECK(std::equal(ys.rbegin(), ys.rend(), rshorts.begin()));
}

CAF_TEST(subspans) {
  auto xs = make_span(chars);
  CAF_CHECK(equal(xs.first(6), xs));
  CAF_CHECK(equal(xs.last(6), xs));
  CAF_CHECK(equal(xs.subspan(0, 6), xs));
  CAF_CHECK(equal(xs.first(3), i8_list({'a', 'b', 'c'})));
  CAF_CHECK(equal(xs.last(3), i8_list({'d', 'e', 'f'})));
  CAF_CHECK(equal(xs.subspan(2, 2), i8_list({'c', 'd'})));
}

CAF_TEST(free iterator functions) {
  auto xs = make_span(chars);
  CAF_CHECK_EQUAL(xs.begin(), begin(xs));
  CAF_CHECK_EQUAL(xs.cbegin(), cbegin(xs));
  CAF_CHECK_EQUAL(xs.end(), end(xs));
  CAF_CHECK_EQUAL(xs.cend(), cend(xs));
}

CAF_TEST(as bytes) {
  auto xs = make_span(chars);
  auto ys = make_span(shorts);
  CAF_CHECK_EQUAL(as_bytes(xs).size(), chars.size());
  CAF_CHECK_EQUAL(as_bytes(ys).size(), shorts.size() * 2);
  CAF_CHECK_EQUAL(as_writable_bytes(xs).size(), chars.size());
  CAF_CHECK_EQUAL(as_writable_bytes(ys).size(), shorts.size() * 2);
}

CAF_TEST(make_span) {
  auto xs = make_span(chars);
  auto ys = make_span(chars.data(), chars.size());
  auto zs = make_span(chars.data(), chars.data() + chars.size());
  CAF_CHECK(std::equal(xs.begin(), xs.end(), chars.begin()));
  CAF_CHECK(std::equal(ys.begin(), ys.end(), chars.begin()));
  CAF_CHECK(std::equal(zs.begin(), zs.end(), chars.begin()));
  CAF_CHECK_EQUAL(end(xs), end(ys));
  CAF_CHECK_EQUAL(end(ys), end(zs));
  CAF_CHECK_EQUAL(begin(xs), begin(ys));
  CAF_CHECK_EQUAL(begin(ys), begin(zs));
}

CAF_TEST_FIXTURE_SCOPE_END()