File: packed_access.hpp

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 (305 lines) | stat: -rw-r--r-- 9,541 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// NOLINT(llvm-header-guard)

#include <array>
#include <sstream>

#define PBF_TYPE_NAME PROTOZERO_TEST_STRING(PBF_TYPE)
#define GET_TYPE PROTOZERO_TEST_CONCAT(get_packed_, PBF_TYPE)
#define ADD_TYPE PROTOZERO_TEST_CONCAT(add_packed_, PBF_TYPE)

using packed_field_type = PROTOZERO_TEST_CONCAT(protozero::packed_field_, PBF_TYPE);

TEST_CASE("read repeated packed field: " PBF_TYPE_NAME) {
    // Run these tests twice, the second time we basically move the data
    // one byte down in the buffer. It doesn't matter how the data or buffer
    // is aligned before that, in at least one of these cases the ints will
    // not be aligned properly. So we test that even in that case the ints
    // will be extracted properly.

    for (std::string::size_type n = 0; n < 2; ++n) {
        std::string abuffer;
        abuffer.reserve(1000);
        abuffer.append(n, '\0');

        SECTION("empty") {
            abuffer.append(load_data("repeated_packed_" PBF_TYPE_NAME "/data-empty"));

            protozero::pbf_reader item{abuffer.data() + n, abuffer.size() - n};

            REQUIRE_FALSE(item.next());
        }

        SECTION("one") {
            abuffer.append(load_data("repeated_packed_" PBF_TYPE_NAME "/data-one"));

            protozero::pbf_reader item{abuffer.data() + n, abuffer.size() - n};

            REQUIRE(item.next());
            const auto it_range = item.GET_TYPE();
            REQUIRE_FALSE(item.next());

            REQUIRE(it_range.begin() != it_range.end());
            REQUIRE(*it_range.begin() == 17);
            REQUIRE(std::next(it_range.begin()) == it_range.end());
        }

        SECTION("many") {
            abuffer.append(load_data("repeated_packed_" PBF_TYPE_NAME "/data-many"));

            protozero::pbf_reader item{abuffer.data() + n, abuffer.size() - n};

            REQUIRE(item.next());
            const auto it_range = item.GET_TYPE();
            REQUIRE_FALSE(item.next());

            auto it = it_range.begin();
            REQUIRE(it != it_range.end());
            REQUIRE(*it++ ==   17);
            REQUIRE(*it++ ==  200);
            REQUIRE(*it++ ==    0);
            REQUIRE(*it++ ==    1);
            REQUIRE(*it++ == std::numeric_limits<cpp_type>::max());
#if PBF_TYPE_IS_SIGNED
            REQUIRE(*it++ == -200);
            REQUIRE(*it++ ==   -1);
            REQUIRE(*it++ == std::numeric_limits<cpp_type>::min());
#endif
            REQUIRE(it == it_range.end());
        }

        SECTION("swap iterator range") {
            abuffer.append(load_data("repeated_packed_" PBF_TYPE_NAME "/data-many"));

            protozero::pbf_reader item{abuffer.data() + n, abuffer.size() - n};

            REQUIRE(item.next());
            auto it_range1 = item.GET_TYPE();
            REQUIRE_FALSE(item.next());

            decltype(it_range1) it_range;
            using std::swap;
            swap(it_range, it_range1);

            auto it = it_range.begin();
            REQUIRE(it != it_range.end());
            REQUIRE(*it++ ==   17);
            REQUIRE(*it++ ==  200);
            REQUIRE(*it++ ==    0);
            REQUIRE(*it++ ==    1);
            REQUIRE(*it++ == std::numeric_limits<cpp_type>::max());
        }

        SECTION("end_of_buffer") {
            abuffer.append(load_data("repeated_packed_" PBF_TYPE_NAME "/data-many"));

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

    }

}

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

    SECTION("empty") {
        std::array<cpp_type, 1> data = {{ 17 }};
        pw.ADD_TYPE(1, std::begin(data), std::begin(data) /* !!!! */);

        REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-empty"));
    }

    SECTION("one") {
        std::array<cpp_type, 1> data = {{ 17 }};
        pw.ADD_TYPE(1, std::begin(data), std::end(data));

        REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-one"));
    }

    SECTION("many") {
        std::array<cpp_type,
#if PBF_TYPE_IS_SIGNED
                   8
#else
                   5
#endif
        > data = {{
               17
            , 200
            ,   0
            ,   1
            ,std::numeric_limits<cpp_type>::max()
#if PBF_TYPE_IS_SIGNED
            ,-200
            ,  -1
            ,std::numeric_limits<cpp_type>::min()
#endif
        }};
        pw.ADD_TYPE(1, std::begin(data), std::end(data));

        REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-many"));
    }

}

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

    SECTION("empty - should do rollback") {
        {
            const packed_field_type field{pw, 1};
        }

        REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-empty"));
    }

    SECTION("one") {
        {
            packed_field_type field{pw, 1};
            field.add_element(static_cast<cpp_type>(17));
        }

        REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-one"));
    }

    SECTION("many") {
        {
            packed_field_type field{pw, 1};
            field.add_element(static_cast<cpp_type>(  17));
            field.add_element(static_cast<cpp_type>( 200));
            field.add_element(static_cast<cpp_type>(   0));
            field.add_element(static_cast<cpp_type>(   1));
            field.add_element(std::numeric_limits<cpp_type>::max());
#if PBF_TYPE_IS_SIGNED
            field.add_element(static_cast<cpp_type>(-200));
            field.add_element(static_cast<cpp_type>(  -1));
            field.add_element(std::numeric_limits<cpp_type>::min());
#endif
            REQUIRE(field.valid());
            SECTION("with commit") {
                field.commit();
                REQUIRE_FALSE(field.valid());
            }
        }

        REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-many"));
    }
}

TEST_CASE("move repeated packed field: " PBF_TYPE_NAME) {
    std::string buffer;
    protozero::pbf_writer pw{buffer};

    SECTION("move rvalue") {
        packed_field_type field;
        REQUIRE_FALSE(field.valid());
        field = packed_field_type{pw, 1};
        REQUIRE(field.valid());
        field.add_element(static_cast<cpp_type>(17));
    }

    SECTION("explicit move") {
        packed_field_type field2{pw, 1};
        packed_field_type field;

        REQUIRE(field2.valid());
        REQUIRE_FALSE(field.valid());

        field = std::move(field2);

        REQUIRE_FALSE(field2.valid()); // NOLINT(hicpp-invalid-access-moved, bugprone-use-after-move)
        REQUIRE(field.valid());

        field.add_element(static_cast<cpp_type>(17));
    }

    SECTION("move constructor") {
        packed_field_type field2{pw, 1};
        REQUIRE(field2.valid());

        packed_field_type field{std::move(field2)};
        REQUIRE(field.valid());
        REQUIRE_FALSE(field2.valid()); // NOLINT(hicpp-invalid-access-moved, bugprone-use-after-move)

        field.add_element(static_cast<cpp_type>(17));
    }

    SECTION("swap") {
        packed_field_type field;
        packed_field_type field2{pw, 1};

        REQUIRE_FALSE(field.valid());
        REQUIRE(field2.valid());

        using std::swap;
        swap(field, field2);

        REQUIRE(field.valid());
        REQUIRE_FALSE(field2.valid());

        field.add_element(static_cast<cpp_type>(17));
    }

    REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-one"));
}

TEST_CASE("write from different types of iterators: " PBF_TYPE_NAME) {
    std::string buffer;
    protozero::pbf_writer pw{buffer};

    SECTION("from uint16_t") {
#if PBF_TYPE_IS_SIGNED
        const std::array< int16_t, 5> data = {{ 1, 4, 9, 16, 25 }};
#else
        const std::array<uint16_t, 5> data = {{ 1, 4, 9, 16, 25 }};
#endif

        pw.ADD_TYPE(1, std::begin(data), std::end(data));
    }

    SECTION("from string") {
        const std::string data{"1 4 9 16 25"};
        std::stringstream sdata{data};

#if PBF_TYPE_IS_SIGNED
        using test_type =  int32_t;
#else
        using test_type = uint32_t;
#endif

        const std::istream_iterator<test_type> eod;
        const std::istream_iterator<test_type> it(sdata);

        pw.ADD_TYPE(1, it, eod);
    }

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    auto it_range = item.GET_TYPE();
    REQUIRE_FALSE(item.next());
    REQUIRE_FALSE(it_range.empty());
    REQUIRE(std::distance(it_range.begin(), it_range.end()) == 5);
    REQUIRE(it_range.size() == 5);

    REQUIRE(it_range.front() ==  1); it_range.drop_front();
    REQUIRE(it_range.front() ==  4); it_range.drop_front();
    REQUIRE(std::distance(it_range.begin(), it_range.end()) == 3);
    REQUIRE(it_range.size() == 3);
    REQUIRE(it_range.front() ==  9); it_range.drop_front();
    REQUIRE(it_range.front() == 16); it_range.drop_front();
    REQUIRE(it_range.front() == 25); it_range.drop_front();
    REQUIRE(it_range.empty());
    REQUIRE(std::distance(it_range.begin(), it_range.end()) == 0);
    REQUIRE(it_range.size() == 0); // NOLINT(readability-container-size-empty)

    REQUIRE_THROWS_AS(it_range.front(), assert_error);
    REQUIRE_THROWS_AS(it_range.drop_front(), assert_error);
}