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 (161 lines) | stat: -rw-r--r-- 3,970 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
150
151
152
153
154
155
156
157
158
159
160
161

#include <test.hpp>

namespace {

void check_subsub(protozero::pbf_reader message) {
    while (message.next()) {
        switch (message.tag()) {
            case 1: {
                REQUIRE("foobar" == message.get_string());
                break;
            }
            case 2: {
                REQUIRE(99 == message.get_int32());
                break;
            }
            default: {
                REQUIRE(false); // should never be here
                break;
            }
        }
    }
}

void check_sub(protozero::pbf_reader message) {
    while (message.next()) {
        switch (message.tag()) {
            case 1: {
                check_subsub(message.get_message());
                break;
            }
            case 2: {
                REQUIRE(88 == message.get_int32());
                break;
            }
            default: {
                REQUIRE(false); // should never be here
                break;
            }
        }
    }
}

void check(protozero::pbf_reader message) {
    while (message.next()) {
        switch (message.tag()) {
            case 1: {
                check_sub(message.get_message());
                break;
            }
            case 2: {
                REQUIRE(77 == message.get_int32());
                break;
            }
            default: {
                REQUIRE(false); // should never be here
                break;
            }
        }
    }
}

void check_empty(protozero::pbf_reader message) {
    while (message.next()) {
        switch (message.tag()) {
            case 1: {
                REQUIRE_FALSE(message.get_message().next());
                break;
            }
            case 2: {
                REQUIRE(77 == message.get_int32());
                break;
            }
            default: {
                REQUIRE(false); // should never be here
                break;
            }
        }
    }
}

} // anonymous namespace

TEST_CASE("read nested message fields: string") {
    const std::string buffer = load_data("nested/data-message");

    const protozero::pbf_reader message{buffer};
    check(message);
}

TEST_CASE("read nested message fields: no submessage") {
    const std::string buffer = load_data("nested/data-no-message");

    const protozero::pbf_reader message{buffer};
    check_empty(message);
}

TEST_CASE("write nested message fields") {
    std::string buffer_test;
    protozero::pbf_writer pbf_test{buffer_test};

    SECTION("string") {
        std::string buffer_subsub;
        protozero::pbf_writer pbf_subsub{buffer_subsub};
        pbf_subsub.add_string(1, "foobar");
        pbf_subsub.add_int32(2, 99);

        std::string buffer_sub;
        protozero::pbf_writer pbf_sub{buffer_sub};
        pbf_sub.add_string(1, buffer_subsub);
        pbf_sub.add_int32(2, 88);

        pbf_test.add_message(1, buffer_sub);
    }

    SECTION("string with subwriter") {
        protozero::pbf_writer pbf_sub{pbf_test, 1};
        {
            protozero::pbf_writer pbf_subsub{pbf_sub, 1};
            pbf_subsub.add_string(1, "foobar");
            pbf_subsub.add_int32(2, 99);
        }
        pbf_sub.add_int32(2, 88);
    }

    pbf_test.add_int32(2, 77);

    const protozero::pbf_reader message{buffer_test};
    check(message);
}

TEST_CASE("write nested message fields - no message") {
    std::string buffer_test;
    protozero::pbf_writer pbf_test{buffer_test};

    SECTION("nothing") {
    }

    SECTION("empty string") {
        const std::string buffer_sub;

        pbf_test.add_message(1, buffer_sub);
    }

    SECTION("string with pbf_writer") {
        std::string buffer_sub;
        const protozero::pbf_writer pbf_sub{buffer_sub};

        pbf_test.add_message(1, buffer_sub);
    }

    SECTION("string with subwriter") {
        const protozero::pbf_writer pbf_sub{pbf_test, 1};
    }

    pbf_test.add_int32(2, 77);

    const protozero::pbf_reader message{buffer_test};
    check_empty(message);
}