File: reader_test_cases.cpp

package info (click to toggle)
protozero 1.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,548 kB
  • sloc: cpp: 20,364; sh: 18; makefile: 14
file content (149 lines) | stat: -rw-r--r-- 4,082 bytes parent folder | download | duplicates (2)
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

#include <test.hpp>

namespace TestBoolean {

enum class Test : protozero::pbf_tag_type {
    required_bool_b = 1
};

} // end namespace TestBoolean

TEST_CASE("read bool field using pbf_reader: false") {
    const std::string buffer = load_data("bool/data-false");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE_FALSE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_reader: true") {
    const std::string buffer = load_data("bool/data-true");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_reader: also true") {
    const std::string buffer = load_data("bool/data-also-true");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next(1));
    REQUIRE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_reader: still true") {
    const std::string buffer = load_data("bool/data-still-true");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next(1));
    REQUIRE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_message: false") {
    const std::string buffer = load_data("bool/data-false");

    protozero::pbf_message<TestBoolean::Test> item{buffer};

    REQUIRE(item.next());
    REQUIRE_FALSE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_message: true") {
    const std::string buffer = load_data("bool/data-true");

    protozero::pbf_message<TestBoolean::Test> item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_message: also true") {
    const std::string buffer = load_data("bool/data-also-true");

    protozero::pbf_message<TestBoolean::Test> item{buffer};

    REQUIRE(item.next(TestBoolean::Test::required_bool_b));
    REQUIRE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bool field using pbf_message: still true") {
    const std::string buffer = load_data("bool/data-still-true");

    protozero::pbf_message<TestBoolean::Test> item{buffer};

    REQUIRE(item.next(TestBoolean::Test::required_bool_b));
    REQUIRE(item.get_bool());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("write bool field using pbf_writer") {
    std::string buffer;
    protozero::pbf_writer pw{buffer};

    SECTION("false") {
        pw.add_bool(1, false);
        REQUIRE(buffer == load_data("bool/data-false"));
    }

    SECTION("true") {
        pw.add_bool(1, true);
        REQUIRE(buffer == load_data("bool/data-true"));
    }
}

TEST_CASE("write bool field using pbf_builder") {
    std::string buffer;
    protozero::pbf_builder<TestBoolean::Test> pw{buffer};

    SECTION("false") {
        pw.add_bool(TestBoolean::Test::required_bool_b, false);
        REQUIRE(buffer == load_data("bool/data-false"));
    }

    SECTION("true") {
        pw.add_bool(TestBoolean::Test::required_bool_b, true);
        REQUIRE(buffer == load_data("bool/data-true"));
    }
}

TEST_CASE("write bool field using moved pbf_builder") {
    std::string buffer;
    protozero::pbf_builder<TestBoolean::Test> pw2{buffer};
    REQUIRE(pw2.valid());

    protozero::pbf_builder<TestBoolean::Test> pw{std::move(pw2)};
    REQUIRE(pw.valid());
    REQUIRE_FALSE(pw2.valid()); // NOLINT(hicpp-invalid-access-moved, bugprone-use-after-move, clang-analyzer-cplusplus.Move)

    SECTION("false") {
        pw.add_bool(TestBoolean::Test::required_bool_b, false);
        REQUIRE(buffer == load_data("bool/data-false"));
    }

    SECTION("true") {
        pw.add_bool(TestBoolean::Test::required_bool_b, true);
        REQUIRE(buffer == load_data("bool/data-true"));
    }
}

TEST_CASE("read bool from using pbf_reader: truncated message") {
    std::vector<char> buffer = { 0x08 };

    protozero::pbf_reader item{buffer.data(), buffer.size()};

    REQUIRE(item.next());
    REQUIRE_THROWS_AS(item.get_bool(), protozero::end_of_buffer_exception);
}