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 (74 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download | duplicates (4)
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

#include <test.hpp>

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

    protozero::pbf_reader item{buffer};

    REQUIRE_FALSE(item.next());
}

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

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_int32() == 0L);
    REQUIRE_FALSE(item.next());
}

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

    protozero::pbf_reader item{buffer};

    REQUIRE(item.next());
    REQUIRE(item.get_int32() == 0L);

    REQUIRE(item.next());
    REQUIRE(item.get_int32() == 1L);

    REQUIRE(item.next());
    REQUIRE(item.get_int32() == -1L);

    REQUIRE(item.next());
    REQUIRE(item.get_int32() == std::numeric_limits<int32_t>::max());

    REQUIRE(item.next());
    REQUIRE(item.get_int32() == std::numeric_limits<int32_t>::min());

    REQUIRE_FALSE(item.next());
}

TEST_CASE("read repeated fields: end of buffer") {
    const std::string buffer = load_data("repeated/data-one");

    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_int32(), protozero::end_of_buffer_exception);
    }
}

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

    SECTION("one") {
        pw.add_int32(1, 0L);
        REQUIRE(buffer == load_data("repeated/data-one"));
    }

    SECTION("many") {
        pw.add_int32(1, 0L);
        pw.add_int32(1, 1L);
        pw.add_int32(1, -1L);
        pw.add_int32(1, std::numeric_limits<int32_t>::max());
        pw.add_int32(1, std::numeric_limits<int32_t>::min());

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