aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg-test/strings.cpp
blob: 32fe5b3afac85634da354926cfd95309e5a728e2 (plain)
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
#include <catch2/catch.hpp>

#include <vcpkg/base/strings.h>

#include <stdint.h>

#include <string>
#include <utility>
#include <vector>

#if defined(_MSC_VER)
#pragma warning(disable : 6237)
#endif

TEST_CASE ("b32 encoding", "[strings]")
{
    using u64 = uint64_t;

    std::vector<std::pair<u64, std::string>> map;

    map.emplace_back(0, "AAAAAAAAAAAAA");
    map.emplace_back(1, "BAAAAAAAAAAAA");

    map.emplace_back(u64(1) << 32, "AAAAAAEAAAAAA");
    map.emplace_back((u64(1) << 32) + 1, "BAAAAAEAAAAAA");

    map.emplace_back(0xE4D0'1065'D11E'0229, "JRA4RIXMQAUJO");
    map.emplace_back(0xA626'FE45'B135'07FF, "77BKTYWI6XJMK");
    map.emplace_back(0xEE36'D228'0C31'D405, "FAVDDGAFSWN4O");
    map.emplace_back(0x1405'64E7'FE7E'A88C, "MEK5H774ELBIB");
    map.emplace_back(0xFFFF'FFFF'FFFF'FFFF, "777777777777P");

    for (const auto& pr : map)
    {
        REQUIRE(vcpkg::Strings::b32_encode(pr.first) == pr.second);
    }
}

TEST_CASE ("split by char", "[strings]")
{
    using vcpkg::Strings::split;
    using result_t = std::vector<std::string>;
    REQUIRE(split(",,,,,,", ',').empty());
    REQUIRE(split(",,a,,b,,", ',') == result_t{"a", "b"});
    REQUIRE(split("hello world", ' ') == result_t{"hello", "world"});
    REQUIRE(split("    hello  world    ", ' ') == result_t{"hello", "world"});
    REQUIRE(split("no delimiters", ',') == result_t{"no delimiters"});
}

TEST_CASE ("find_first_of", "[strings]")
{
    using vcpkg::Strings::find_first_of;
    REQUIRE(find_first_of("abcdefg", "hij") == std::string());
    REQUIRE(find_first_of("abcdefg", "a") == std::string("abcdefg"));
    REQUIRE(find_first_of("abcdefg", "g") == std::string("g"));
    REQUIRE(find_first_of("abcdefg", "bg") == std::string("bcdefg"));
    REQUIRE(find_first_of("abcdefg", "gb") == std::string("bcdefg"));
}

TEST_CASE ("edit distance", "[strings]")
{
    using vcpkg::Strings::byte_edit_distance;
    REQUIRE(byte_edit_distance("", "") == 0);
    REQUIRE(byte_edit_distance("a", "a") == 0);
    REQUIRE(byte_edit_distance("abcd", "abcd") == 0);
    REQUIRE(byte_edit_distance("aaa", "aa") == 1);
    REQUIRE(byte_edit_distance("aa", "aaa") == 1);
    REQUIRE(byte_edit_distance("abcdef", "bcdefa") == 2);
    REQUIRE(byte_edit_distance("hello", "world") == 4);
    REQUIRE(byte_edit_distance("CAPITAL", "capital") == 7);
    REQUIRE(byte_edit_distance("", "hello") == 5);
    REQUIRE(byte_edit_distance("world", "") == 5);
}

TEST_CASE ("replace_all", "[strings]")
{
    REQUIRE(vcpkg::Strings::replace_all("literal", "ter", "x") == "lixal");
}

TEST_CASE ("inplace_replace_all", "[strings]")
{
    using vcpkg::Strings::inplace_replace_all;
    std::string target;
    inplace_replace_all(target, "", "content");
    REQUIRE(target.empty());
    target = "aa";
    inplace_replace_all(target, "a", "content");
    REQUIRE(target == "contentcontent");
    inplace_replace_all(target, "content", "");
    REQUIRE(target.empty());
    target = "ababababa";
    inplace_replace_all(target, "aba", "X");
    REQUIRE(target == "XbXba");
    target = "ababababa";
    inplace_replace_all(target, "aba", "aba");
    REQUIRE(target == "ababababa");
}

TEST_CASE ("inplace_replace_all(char)", "[strings]")
{
    using vcpkg::Strings::inplace_replace_all;
    static_assert(noexcept(inplace_replace_all(std::declval<std::string&>(), 'a', 'a')));

    std::string target;
    inplace_replace_all(target, ' ', '?');
    REQUIRE(target.empty());
    target = "hello";
    inplace_replace_all(target, 'l', 'w');
    REQUIRE(target == "hewwo");
    inplace_replace_all(target, 'w', 'w');
    REQUIRE(target == "hewwo");
    inplace_replace_all(target, 'x', '?');
    REQUIRE(target == "hewwo");
}