-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_encoder.cpp
More file actions
95 lines (80 loc) · 2.7 KB
/
Copy pathtest_encoder.cpp
File metadata and controls
95 lines (80 loc) · 2.7 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
//
// Created by fbdtemme on 09/07/19.
//
#include <string_view>
#include <catch2/catch_all.hpp>
#include <bencode/traits/all.hpp>
#include <bencode/detail/encoder.hpp>
namespace bc = bencode;
TEST_CASE("test encoder", "[encoder]")
{
using namespace std::string_view_literals;
using namespace std::string_literals;
std::ostringstream os {};
auto emitter = bc::encoder(os);
SECTION("integral") {
emitter << true
<< std::int8_t{1}
<< std::int16_t{1}
<< std::int32_t{1}
<< std::int64_t{1}
<< std::uint8_t{1}
<< std::uint16_t{1}
<< std::uint32_t{1}
<< std::uint64_t{1};
CHECK(os.str() == "i1ei1ei1ei1ei1ei1ei1ei1ei1e");
}
SECTION("string") {
const char* const_char_ptr = "char ptr";
emitter << "string literal"
<< "std::string_view"sv
<< "std::string"s
<< const_char_ptr;
CHECK(os.str() == "14:string literal16:std::string_view11:std::string8:char ptr");
}
SECTION("list") {
emitter << bc::list_begin
<< "string literal"
<< 1
<< "string"s
<< "string_view"sv
<< bc::list_end;
CHECK(os.str() == "l14:string literali1e6:string11:string_viewe");
}
SECTION("dict") {
emitter << bc::dict_begin
<< "key1"sv << 1
<< "key2"sv << "two"
<< "key3"sv
<< bc::list_begin
<< 1 << 2 << 3
<< bc::list_end
<< bc::dict_end;
CHECK(os.str() == "d4:key1i1e4:key23:two4:key3li1ei2ei3eee");
}
SECTION("error - invalid dict key - integer") {
emitter << bc::dict_begin;
CHECK_THROWS_AS(emitter << 1, bc::encoding_error);
}
SECTION("error - invalid dict key - list") {
emitter << bc::dict_begin;
CHECK_THROWS_AS(emitter << bc::list_begin, bc::encoding_error);
CHECK_THROWS_AS(emitter << bc::list_end, bc::encoding_error);
}
SECTION("error - invalid dict key - dict") {
emitter << bc::dict_begin;
CHECK_THROWS_AS(emitter << bc::dict_begin, bc::encoding_error);
}
SECTION("error - invalid dict end") {
emitter << bc::dict_begin;
emitter << "key1";
emitter << bc::list_begin;
emitter << 1;
CHECK_THROWS_AS(emitter << bc::dict_end, bc::encoding_error);
}
SECTION("error - invalid list end") {
emitter << bc::list_begin;
emitter << bc::dict_begin << "key1";
CHECK_THROWS_AS(emitter << bc::list_end, bc::encoding_error);
}
}