-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_basic_bvalue.cpp
More file actions
156 lines (130 loc) · 4.56 KB
/
Copy pathtest_basic_bvalue.cpp
File metadata and controls
156 lines (130 loc) · 4.56 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Created by fbdtemme on 8/14/20.
//
#include <catch2/catch_all.hpp>
#include <string_view>
#include "bencode/traits/all.hpp"
#include "bencode/bvalue.hpp"
using namespace std::string_view_literals;
namespace bc = bencode;
TEST_CASE("test basic_bvalue members", "[bvalue]")
{
const bencode::bvalue const_integer(1);
const bencode::bvalue const_string("test");
const bencode::bvalue const_list(bc::btype::list, {1, 2, 3});
const bencode::bvalue const_dict(bc::btype::dict, {{"test1", 1}, {"test2", "2"}});
bencode::bvalue integer(1);
bencode::bvalue string("test");
bencode::bvalue list(bc::btype::list, {1, 2, 3});
bencode::bvalue dict(bc::btype::dict, {{"test1", 1}, {"test2", "2"}});
SECTION("operator bool()") {
CHECK(const_integer);
CHECK(const_string);
CHECK(const_list);
CHECK(const_dict);
CHECK_FALSE(bc::bvalue{});
}
SECTION("at()") {
SECTION("at(int)") {
CHECK(const_list.at(0) == 1);
CHECK(list.at(0) == 1);
CHECK_THROWS_AS(const_string.at(0), bc::bad_bvalue_access);
CHECK_THROWS_AS(string.at(0), bc::bad_bvalue_access);
}
SECTION("at(string") {
CHECK(const_dict.at("test1") == 1);
CHECK(dict.at("test1") == 1);
CHECK_THROWS_AS(const_string.at("test1"), bc::bad_bvalue_access);
CHECK_THROWS_AS(string.at("test1"), bc::bad_bvalue_access);
}
}
SECTION("operator[]") {
SECTION("operator[](int)") {
CHECK(const_list[0] == 1);
CHECK(list[0] == 1);
CHECK_THROWS_AS(const_integer[0], bc::bad_bvalue_access);
CHECK_THROWS_AS(integer[0], bc::bad_bvalue_access);
}
const auto s = std::string("test5");
SECTION("operator[](int)") {
CHECK(dict["test1"] == 1);
dict["test4"] = 4;
dict[s] = 5;
CHECK(dict["test4"] == 4);
CHECK(dict[s] == 5);
CHECK_THROWS_AS(integer["test1"], bc::bad_bvalue_access);
CHECK_THROWS_AS(integer[s], bc::bad_bvalue_access);
}
}
SECTION("front") {
CHECK(const_list.front() == 1);
list.front() = 2;
CHECK(list.front() == 2);
SECTION("error - not list type") {
CHECK_THROWS_AS(integer.front(), bc::bad_bvalue_access);
CHECK_THROWS_AS(const_integer.front(), bc::bad_bvalue_access);
}
}
SECTION("back") {
CHECK(const_list.back() == 3);
list.back() = 2;
CHECK(list.back() == 2);
SECTION("error - not list type") {
CHECK_THROWS_AS(integer.back(), bc::bad_bvalue_access);
CHECK_THROWS_AS(const_integer.back(), bc::bad_bvalue_access);
}
}
SECTION("push_back") {
const bc::bvalue s_const = "long string to disable sso optimisation 1";
std::string s = "long string to disable sso optimisation 2";
SECTION("copy") {
list.push_back(s_const);
CHECK(list[3] == s_const);
CHECK(s_const == "long string to disable sso optimisation 1");
}
SECTION("move") {
list.push_back(std::move(s));
CHECK(s.empty());
}
SECTION("error - not list type") {
CHECK_THROWS_AS(integer.push_back("test1"), bc::bad_bvalue_access);
CHECK_THROWS_AS(integer.push_back(s_const), bc::bad_bvalue_access);
}
}
SECTION("emplace back") {
auto l = bc::bvalue(bc::btype::list, {1, 2, 3});
SECTION("copy") {
list.emplace_back(l);
CHECK(list[3]==l);
}
SECTION("move") {
list.emplace_back(std::move(l));
CHECK(get_list(l).empty());
}
SECTION("error - not list type") {
CHECK_THROWS_AS(integer.emplace_back(l), bc::bad_bvalue_access);
}
}
SECTION("contains") {
const std::string s = "test1";
CHECK(dict.contains(s));
CHECK(dict.contains("test1"));
SECTION("error - not dict type") {
CHECK_THROWS_AS(integer.contains("test1"), bc::bad_bvalue_access);
CHECK_THROWS_AS(integer.contains(s), bc::bad_bvalue_access);
}
}
SECTION("clear") {
auto b = bc::bvalue();
b.clear();
CHECK_FALSE(b);
integer.clear();
CHECK(get_integer(integer) == 0);
string.clear();
CHECK(get_string(string).empty());
list.clear();
CHECK(get_list(list).empty());
dict.clear();
CHECK(get_dict(dict).empty());
}
}