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 (139 lines) | stat: -rw-r--r-- 3,543 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

#include <test.hpp>

TEST_CASE("read bytes field: empty") {
    const std::string buffer = load_data("bytes/data-empty");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_bytes().empty());
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bytes field: one") {
    const std::string buffer = load_data("bytes/data-one");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_bytes() == "x");
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bytes field: string") {
    const std::string buffer = load_data("bytes/data-string");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_bytes() == "foobar");
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bytes field: binary") {
    const std::string buffer = load_data("bytes/data-binary");

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    const std::string data = item.get_bytes();
    REQUIRE(data.size() == 3);
    REQUIRE(data[0] == char(1));
    REQUIRE(data[1] == char(2));
    REQUIRE(data[2] == char(3));
    REQUIRE_FALSE(item.next());
}

TEST_CASE("read bytes field: end of buffer") {
    const std::string buffer = load_data("bytes/data-binary");

    for (std::string::size_type i = 1; i < buffer.size(); ++i) {
        protozero::pbf_reader item{buffer.data(), i};
        REQUIRE(item.next());
        REQUIRE_THROWS_AS(item.get_bytes(), protozero::end_of_buffer_exception);
    }
}

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

    SECTION("empty") {
        pw.add_string(1, "");
        REQUIRE(buffer == load_data("bytes/data-empty"));
    }

    SECTION("one") {
        pw.add_string(1, "x");
        REQUIRE(buffer == load_data("bytes/data-one"));
    }

    SECTION("string") {
        pw.add_string(1, "foobar");
        REQUIRE(buffer == load_data("bytes/data-string"));
    }

    SECTION("binary") {
        std::string data;
        data.append(1, static_cast<char>(1));
        data.append(1, static_cast<char>(2));
        data.append(1, static_cast<char>(3));

        pw.add_string(1, data);

        REQUIRE(buffer == load_data("bytes/data-binary"));
    }
}

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

    SECTION("using two strings") {
        const std::string d1{"foo"};
        const std::string d2{"bar"};

        pw.add_bytes_vectored(1, d1, d2);
    }

    SECTION("using a string and a dataview") {
        const std::string d1{"foo"};
        const std::string d2{"bar"};
        const protozero::data_view dv{d2};

        pw.add_bytes_vectored(1, d1, dv);
    }

    SECTION("using three strings") {
        const std::string d1{"foo"};
        const std::string d2{"ba"};
        const std::string d3{"r"};

        pw.add_bytes_vectored(1, d1, d2, d3);
    }

    SECTION("with empty string") {
        const std::string d1{"foo"};
        const std::string d2{};
        const std::string d3{"bar"};

        pw.add_bytes_vectored(1, d1, d2, d3);
    }

    REQUIRE(buffer == load_data("bytes/data-string"));
}

TEST_CASE("write bytes field using vectored approach with builder") {
    enum class foo : protozero::pbf_tag_type { bar = 1 };
    std::string buffer;
    protozero::pbf_builder<foo> pw{buffer};

    const std::string d1{"foo"};
    const std::string d2{"bar"};

    pw.add_bytes_vectored(foo::bar, d1, d2);

    REQUIRE(buffer == load_data("bytes/data-string"));
}