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

#include <vcpkg/base/strings.h>

#include <stdint.h>

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

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"));
}