File: scalar_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 (129 lines) | stat: -rw-r--r-- 3,541 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
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
// NOLINT(llvm-header-guard)

#define PBF_TYPE_NAME PROTOZERO_TEST_STRING(PBF_TYPE)
#define GET_TYPE PROTOZERO_TEST_CONCAT(get_, PBF_TYPE)
#define ADD_TYPE PROTOZERO_TEST_CONCAT(add_, PBF_TYPE)

TEST_CASE("read field: " PBF_TYPE_NAME) {

    SECTION("zero") {
        const std::string buffer = load_data(PBF_TYPE_NAME "/data-zero");

        protozero::pbf_reader item{buffer};

        REQUIRE(item.next());
        REQUIRE(item.GET_TYPE() == 0);
        REQUIRE_FALSE(item.next());
    }

    SECTION("positive") {
        const std::string buffer = load_data(PBF_TYPE_NAME "/data-pos");

        protozero::pbf_reader item{buffer};

        REQUIRE(item.next());
        REQUIRE(item.GET_TYPE() == 1);
        REQUIRE_FALSE(item.next());
    }

    SECTION("pos200") {
        const std::string buffer = load_data(PBF_TYPE_NAME "/data-pos200");

        protozero::pbf_reader item{buffer};

        REQUIRE(item.next());
        REQUIRE(item.GET_TYPE() == 200);
        REQUIRE_FALSE(item.next());
    }

    SECTION("max") {
        const std::string buffer = load_data(PBF_TYPE_NAME "/data-max");

        protozero::pbf_reader item{buffer};

        REQUIRE(item.next());
        REQUIRE(item.GET_TYPE() == std::numeric_limits<cpp_type>::max());
        REQUIRE_FALSE(item.next());
    }

#if PBF_TYPE_IS_SIGNED
    SECTION("negative") {
        if (std::is_signed<cpp_type>::value) {
            const std::string buffer = load_data(PBF_TYPE_NAME "/data-neg");

            protozero::pbf_reader item{buffer};

            REQUIRE(item.next());
            REQUIRE(item.GET_TYPE() == -1);
            REQUIRE_FALSE(item.next());
        }
    }

    SECTION("neg200") {
        const std::string buffer = load_data(PBF_TYPE_NAME "/data-neg200");

        protozero::pbf_reader item{buffer};

        REQUIRE(item.next());
        REQUIRE(item.GET_TYPE() == -200);
        REQUIRE_FALSE(item.next());
    }

    SECTION("min") {
        if (std::is_signed<cpp_type>::value) {
            const std::string buffer = load_data(PBF_TYPE_NAME "/data-min");

            protozero::pbf_reader item{buffer};

            REQUIRE(item.next());
            REQUIRE(item.GET_TYPE() == std::numeric_limits<cpp_type>::min());
            REQUIRE_FALSE(item.next());
        }
    }
#endif

    SECTION("end_of_buffer") {
        const std::string buffer = load_data(PBF_TYPE_NAME "/data-max");

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

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

    SECTION("zero") {
        pw.ADD_TYPE(1, 0);
        REQUIRE(buffer == load_data(PBF_TYPE_NAME "/data-zero"));
    }

    SECTION("positive") {
        pw.ADD_TYPE(1, 1);
        REQUIRE(buffer == load_data(PBF_TYPE_NAME "/data-pos"));
    }

    SECTION("max") {
        pw.ADD_TYPE(1, std::numeric_limits<cpp_type>::max());
        REQUIRE(buffer == load_data(PBF_TYPE_NAME "/data-max"));
    }

#if PBF_TYPE_IS_SIGNED
    SECTION("negative") {
        pw.ADD_TYPE(1, -1);
        REQUIRE(buffer == load_data(PBF_TYPE_NAME "/data-neg"));
    }

    SECTION("min") {
        if (std::is_signed<cpp_type>::value) {
            pw.ADD_TYPE(1, std::numeric_limits<cpp_type>::min());
            REQUIRE(buffer == load_data(PBF_TYPE_NAME "/data-min"));
        }
    }
#endif
}