File: test-util.cpp

package info (click to toggle)
osm2pgsql 2.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,652 kB
  • sloc: cpp: 59,934; python: 1,039; ansic: 763; sh: 25; makefile: 14
file content (148 lines) | stat: -rw-r--r-- 4,233 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
/**
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This file is part of osm2pgsql (https://osm2pgsql.org/).
 *
 * Copyright (C) 2006-2025 by the osm2pgsql developer community.
 * For a full list of authors see the git log.
 */

#include <catch.hpp>

#include "util.hpp"

#include <cstring>
#include <limits>
#include <string>
#include <vector>

TEST_CASE("double_to_buffer 0", "[NoDB]")
{
    util::double_to_buffer const buffer{0.0};
    REQUIRE(std::strcmp(buffer.c_str(), "0") == 0);
}

TEST_CASE("double_to_buffer 3.141", "[NoDB]")
{
    util::double_to_buffer const buffer{3.141};
    REQUIRE(std::strcmp(buffer.c_str(), "3.141") == 0);
}

TEST_CASE("human readable time durations", "[NoDB]")
{
    REQUIRE(util::human_readable_duration(0) == "0s");
    REQUIRE(util::human_readable_duration(17) == "17s");
    REQUIRE(util::human_readable_duration(59) == "59s");
    REQUIRE(util::human_readable_duration(60) == "60s (1m 0s)");
    REQUIRE(util::human_readable_duration(66) == "66s (1m 6s)");
    REQUIRE(util::human_readable_duration(247) == "247s (4m 7s)");
    REQUIRE(util::human_readable_duration(3599) == "3599s (59m 59s)");
    REQUIRE(util::human_readable_duration(3600) == "3600s (1h 0m 0s)");
    REQUIRE(util::human_readable_duration(3723) == "3723s (1h 2m 3s)");
    REQUIRE(util::human_readable_duration(152592) == "152592s (42h 23m 12s)");
}

TEST_CASE("find_by_name()", "[NoDB]")
{
    class test_class
    {
    public:
        explicit test_class(std::string n) : m_name(std::move(n)) {}
        std::string name() const noexcept { return m_name; }

    private:
        std::string m_name;
    };

    std::vector<test_class> t;

    REQUIRE(util::find_by_name(t, "") == nullptr);
    REQUIRE(util::find_by_name(t, "foo") == nullptr);
    REQUIRE(util::find_by_name(t, "nothing") == nullptr);

    t.emplace_back("foo");
    t.emplace_back("bar");
    t.emplace_back("baz");

    REQUIRE(util::find_by_name(t, "") == nullptr);
    REQUIRE(util::find_by_name(t, "foo") == t.data());
    REQUIRE(util::find_by_name(t, "bar") == &t[1]);
    REQUIRE(util::find_by_name(t, "baz") == &t[2]);
    REQUIRE(util::find_by_name(t, "nothing") == nullptr);
}

TEST_CASE("Use string_joiner_t with delim only without items", "[NoDB]")
{
    util::string_joiner_t joiner{','};
    REQUIRE(joiner.empty());
    REQUIRE(joiner().empty());
}

TEST_CASE("Use string_joiner_t with all params without items", "[NoDB]")
{
    util::string_joiner_t joiner{',', '"', '(', ')'};
    REQUIRE(joiner.empty());
    REQUIRE(joiner().empty());
}

TEST_CASE("Use string_joiner_t without quote char", "[NoDB]")
{
    util::string_joiner_t joiner{',', '\0', '(', ')'};
    REQUIRE(joiner.empty());
    joiner.add("foo");
    REQUIRE_FALSE(joiner.empty());
    joiner.add("bar");
    REQUIRE_FALSE(joiner.empty());
    REQUIRE(joiner() == "(foo,bar)");
}

TEST_CASE("Use string_joiner_t with quote char", "[NoDB]")
{
    util::string_joiner_t joiner{',', '-', '(', ')'};
    REQUIRE(joiner.empty());
    joiner.add("foo");
    REQUIRE_FALSE(joiner.empty());
    joiner.add("bar");
    REQUIRE_FALSE(joiner.empty());
    REQUIRE(joiner() == "(-foo-,-bar-)");
}

TEST_CASE("string_joiner_t without before/after", "[NoDB]")
{
    util::string_joiner_t joiner{','};
    joiner.add("xxx");
    joiner.add("yyy");
    REQUIRE_FALSE(joiner.empty());
    REQUIRE(joiner() == "xxx,yyy");
}

TEST_CASE("string_joiner_t with single single-char item", "[NoDB]")
{
    util::string_joiner_t joiner{','};
    joiner.add("x");
    REQUIRE(joiner() == "x");
}

TEST_CASE("string_joiner_t with single single-char item and wrapper", "[NoDB]")
{
    util::string_joiner_t joiner{',', '\0', '(', ')'};
    joiner.add("x");
    REQUIRE(joiner() == "(x)");
}

TEST_CASE("join strings", "[NoDB]")
{
    std::vector<std::string> const t{"abc", "def", "", "ghi"};

    REQUIRE(util::join(t, ',') == "abc,def,,ghi");
    REQUIRE(util::join(t, '-', '#', '[', ']') == "[#abc#-#def#-##-#ghi#]");
    REQUIRE(util::join(t, '-', '#', '[', ']') == "[#abc#-#def#-##-#ghi#]");
}

TEST_CASE("join strings with empty list", "[NoDB]")
{
    std::vector<std::string> const t{};

    REQUIRE(util::join(t, ',').empty());
    REQUIRE(util::join(t, '-', '#', '[', ']').empty());
}