File: dictionary.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (129 lines) | stat: -rw-r--r-- 3,516 bytes parent folder | download
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
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE dictionary

#include "caf/dictionary.hpp"

#include "core-test.hpp"

using namespace caf;

namespace {

using int_dict = dictionary<int>;

} // namespace

CAF_TEST(construction and comparison) {
  int_dict xs;
  CHECK_EQ(xs.empty(), true);
  CHECK_EQ(xs.size(), 0u);
  int_dict ys{{"foo", 1}, {"bar", 2}};
  CHECK_EQ(ys.empty(), false);
  CHECK_EQ(ys.size(), 2u);
  CHECK_NE(xs, ys);
  int_dict zs{ys.begin(), ys.end()};
  CHECK_EQ(zs.empty(), false);
  CHECK_EQ(zs.size(), 2u);
  CHECK_EQ(ys, zs);
  zs.clear();
  CHECK_EQ(zs.empty(), true);
  CHECK_EQ(zs.size(), 0u);
  CHECK_EQ(xs, zs);
}

CAF_TEST(iterators) {
  using std::equal;
  using vector_type = std::vector<int_dict::value_type>;
  int_dict xs{{"a", 1}, {"b", 2}, {"c", 3}};
  vector_type ys{{"a", 1}, {"b", 2}, {"c", 3}};
  CHECK(equal(xs.begin(), xs.end(), ys.begin()));
  CHECK(equal(xs.cbegin(), xs.cend(), ys.cbegin()));
  CHECK(equal(xs.rbegin(), xs.rend(), ys.rbegin()));
  CHECK(equal(xs.crbegin(), xs.crend(), ys.crbegin()));
}

CAF_TEST(swapping) {
  int_dict xs{{"foo", 1}, {"bar", 2}};
  int_dict ys;
  int_dict zs{{"foo", 1}, {"bar", 2}};
  CHECK_NE(xs, ys);
  CHECK_NE(ys, zs);
  CHECK_EQ(xs, zs);
  xs.swap(ys);
  CHECK_NE(xs, ys);
  CHECK_EQ(ys, zs);
  CHECK_NE(xs, zs);
}

CAF_TEST(emplacing) {
  int_dict xs;
  CHECK_EQ(xs.emplace("x", 1).second, true);
  CHECK_EQ(xs.emplace("y", 2).second, true);
  CHECK_EQ(xs.emplace("y", 3).second, false);
}

CAF_TEST(insertion) {
  int_dict xs;
  CHECK_EQ(xs.insert("a", 1).second, true);
  CHECK_EQ(xs.insert("b", 2).second, true);
  CHECK_EQ(xs.insert("c", 3).second, true);
  CHECK_EQ(xs.insert("c", 4).second, false);
  int_dict ys;
  CHECK_EQ(ys.insert_or_assign("a", 1).second, true);
  CHECK_EQ(ys.insert_or_assign("b", 2).second, true);
  CHECK_EQ(ys.insert_or_assign("c", 0).second, true);
  CHECK_EQ(ys.insert_or_assign("c", 3).second, false);
  CHECK_EQ(xs, ys);
}

CAF_TEST(insertion with hint) {
  int_dict xs;
  auto xs_last = xs.end();
  auto xs_insert = [&](string_view key, int val) {
    xs_last = xs.insert(xs_last, key, val);
  };
  xs_insert("a", 1);
  xs_insert("c", 3);
  xs_insert("b", 2);
  xs_insert("c", 4);
  int_dict ys;
  auto ys_last = ys.end();
  auto ys_insert_or_assign = [&](string_view key, int val) {
    ys_last = ys.insert_or_assign(ys_last, key, val);
  };
  ys_insert_or_assign("a", 1);
  ys_insert_or_assign("c", 0);
  ys_insert_or_assign("b", 2);
  ys_insert_or_assign("c", 3);
  CHECK_EQ(xs, ys);
}

CAF_TEST(bounds) {
  int_dict xs{{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}};
  const int_dict& const_xs = xs;
  CHECK_EQ(xs.lower_bound("c")->first, "c");
  CHECK_EQ(xs.upper_bound("c")->first, "d");
  CHECK_EQ(const_xs.lower_bound("c")->first, "c");
  CHECK_EQ(const_xs.upper_bound("c")->first, "d");
}

CAF_TEST(find) {
  int_dict xs{{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}};
  const int_dict& const_xs = xs;
  CHECK_EQ(xs.find("e"), xs.end());
  CHECK_EQ(xs.find("a")->second, 1);
  CHECK_EQ(xs.find("c")->second, 3);
  CHECK_EQ(const_xs.find("e"), xs.end());
  CHECK_EQ(const_xs.find("a")->second, 1);
  CHECK_EQ(const_xs.find("c")->second, 3);
}

CAF_TEST(element access) {
  int_dict xs{{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}};
  CHECK_EQ(xs["a"], 1);
  CHECK_EQ(xs["b"], 2);
  CHECK_EQ(xs["e"], 0);
}