File: test_string_table.cpp

package info (click to toggle)
libosmium 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,564 kB
  • sloc: cpp: 53,570; sh: 148; makefile: 19
file content (131 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (6)
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
#include "catch.hpp"

#include <osmium/io/detail/string_table.hpp>
#include <osmium/util/misc.hpp>

#include <iterator>
#include <string>

TEST_CASE("Empty StringStore") {
    const osmium::io::detail::StringStore ss{100};

    REQUIRE(ss.begin() == ss.end());
    REQUIRE(ss.get_chunk_size() == 100);
    REQUIRE(ss.get_chunk_count() == 1);
}

TEST_CASE("Add zero-length string to StringStore") {
    osmium::io::detail::StringStore ss{100};

    const char* s1 = ss.add("");
    REQUIRE(std::string{s1}.empty());

    auto it = ss.begin();
    REQUIRE(s1 == *it);
    REQUIRE(std::string{*it}.empty());
    REQUIRE(++it == ss.end());

    REQUIRE(ss.get_chunk_count() == 1);
}

TEST_CASE("Add strings to StringStore") {
    osmium::io::detail::StringStore ss{100};

    const char* s1 = ss.add("foo");
    const char* s2 = ss.add("bar");
    REQUIRE(s1 != s2);
    REQUIRE(std::string{s1} == "foo");
    REQUIRE(std::string{s2} == "bar");

    auto it = ss.begin();
    REQUIRE(s1 == *it++);
    REQUIRE(s2 == *it++);
    REQUIRE(it == ss.end());
}

TEST_CASE("Add zero-length string and longer strings to StringStore") {
    osmium::io::detail::StringStore ss{100};

    ss.add("");
    ss.add("xxx");
    ss.add("yyyyy");

    auto it = ss.begin();
    REQUIRE(std::string{*it++}.empty());
    REQUIRE(std::string{*it++} == "xxx");
    REQUIRE(std::string{*it++} == "yyyyy");
    REQUIRE(it == ss.end());
}

TEST_CASE("Add many strings to StringStore") {
    for (const char* teststring : {"", "a", "abc", "abcd", "abcde"}) {
        osmium::io::detail::StringStore ss{100};
        int i = 0;
        for (; i < 200; ++i) {
            ss.add(teststring);
        }

        for (const char* s : ss) {
            REQUIRE(std::string{s} == teststring);
            --i;
        }

        REQUIRE(i == 0);
        REQUIRE(ss.get_chunk_count() > 1);
    }
}

TEST_CASE("Empty StringTable") {
    const osmium::io::detail::StringTable st;

    REQUIRE(st.size() == 1);
    REQUIRE(std::next(st.begin()) == st.end());
}

TEST_CASE("Add strings to StringTable") {
    osmium::io::detail::StringTable st;

    REQUIRE(st.add("foo") == 1);
    REQUIRE(st.add("bar") == 2);
    REQUIRE(st.add("bar") == 2);
    REQUIRE(st.add("baz") == 3);
    REQUIRE(st.add("foo") == 1);
    REQUIRE(st.size() == 4);

    auto it = st.begin();
    REQUIRE(std::string{} == *it++);
    REQUIRE(std::string{"foo"} == *it++);
    REQUIRE(std::string{"bar"} == *it++);
    REQUIRE(std::string{"baz"} == *it++);
    REQUIRE(it == st.end());
}

TEST_CASE("Add empty string to StringTable") {
    osmium::io::detail::StringTable st;

    REQUIRE(st.add("") == 1);
    REQUIRE(st.size() == 2);
    REQUIRE(st.add("") == 1);
    REQUIRE(st.size() == 2);
}

TEST_CASE("Lots of strings in string table so chunk overflows") {
    osmium::io::detail::StringTable st{100};
    REQUIRE(st.size() == 1);

    const int n = 1000;
    for (int i = 0; i < n; ++i) {
        const auto s = std::to_string(i);
        st.add(s.c_str());
    }

    REQUIRE(st.size() == n + 1);

    auto it = st.begin();
    REQUIRE(std::string{} == *it++);
    for (int i = 0; i < n; ++i) {
        REQUIRE(osmium::detail::str_to_int<int>(*it++) == i);
    }
    REQUIRE(it == st.end());
}