File: test-hex.cpp

package info (click to toggle)
osm2pgsql 2.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,772 kB
  • sloc: cpp: 60,940; python: 1,115; ansic: 763; sh: 25; makefile: 14
file content (86 lines) | stat: -rw-r--r-- 2,458 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
/**
 * 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 "hex.hpp"

#include <string>

TEST_CASE("hex encode a string", "[NoDB]")
{
    std::string result;
    util::encode_hex("ab~", &result);
    REQUIRE(result.size() == 6);
    REQUIRE(result == "61627E");
}

TEST_CASE("hex encode a string adding to existing string", "[NoDB]")
{
    std::string result{"0x"};
    util::encode_hex("\xca\xfe", &result);
    REQUIRE(result.size() == 6);
    REQUIRE(result == "0xCAFE");
}

TEST_CASE("hex encoding an empty string doesn't change output string", "[NoDB]")
{
    std::string result{"foo"};
    util::encode_hex("", &result);
    REQUIRE(result == "foo");
}

TEST_CASE("wkb hex decode of valid and invalid hex characters")
{
    REQUIRE(util::decode_hex_char('0') == 0);
    REQUIRE(util::decode_hex_char('9') == 9);
    REQUIRE(util::decode_hex_char('a') == 0x0a);
    REQUIRE(util::decode_hex_char('f') == 0x0f);
    REQUIRE(util::decode_hex_char('A') == 0x0a);
    REQUIRE(util::decode_hex_char('F') == 0x0f);
    REQUIRE(util::decode_hex_char('#') == 0);
    REQUIRE(util::decode_hex_char('@') == 0);
    REQUIRE(util::decode_hex_char('g') == 0);
    REQUIRE(util::decode_hex_char('G') == 0);
    REQUIRE(util::decode_hex_char(0x7f) == 0);
}

TEST_CASE("wkb hex decode of valid hex string")
{
    std::string const hex{"0001020F1099FF"};
    std::string const data = {0x00,
                              0x01,
                              0x02,
                              0x0f,
                              0x10,
                              static_cast<char>(0x99),
                              static_cast<char>(0xff)};

    auto const result = util::decode_hex(hex);
    REQUIRE(result.size() == hex.size() / 2);
    REQUIRE(result == data);
}

TEST_CASE("wkb hex decode of empty string is okay")
{
    std::string const hex{};
    REQUIRE(util::decode_hex(hex).empty());
}

TEST_CASE("wkb hex decode of string with odd number of characters fails")
{
    REQUIRE_THROWS(util::decode_hex("a"));
    REQUIRE_THROWS(util::decode_hex("abc"));
    REQUIRE_THROWS(util::decode_hex("00000"));
}

TEST_CASE("hex encode and decode") {
    std::string const str{"something somewhere"};
    REQUIRE(util::decode_hex(util::encode_hex(str)) == str);
}