kblib 0.2.3
General utilities library for modern C++
convert.cpp
Go to the documentation of this file.
1#include "kblib/convert.h"
2#include "catch.hpp"
3
4using namespace std::literals;
5
6TEST_CASE("bases") {
7 CHECK(kblib::to_string<62>(0) == "0");
8 CHECK(kblib::to_string<62>(1) == "1");
9 CHECK(kblib::to_string<62>(-1) == "-1");
10 CHECK(kblib::to_string<62>(10) == "A");
11 CHECK(kblib::to_string<62>(26) == "Q");
12 CHECK(kblib::to_string<62>(35) == "Z");
13 CHECK(kblib::to_string<62>(36) == "a");
14 CHECK(kblib::to_string<62>(61) == "z");
15 CHECK(kblib::to_string<62>(62) == "10");
16 CHECK(kblib::to_string<62>(63) == "11");
17 CHECK(kblib::to_string<62>(630) == "AA");
18
19 CHECK(kblib::to_string<2>(65536) == "10000000000000000");
20
21 using namespace kblib::literals;
22 auto one = 1_c;
23 static_assert(decltype(one)::value == 1, "");
24 static_assert(one == 1, "");
25}
26
27TEST_CASE("parse_integer") {
28 using namespace Catch::Matchers;
29
30 CHECK_THROWS_MATCHES(kblib::parse_integer<long>(""), std::invalid_argument,
31 Message("\"\" is not an integer"));
32 CHECK_THROWS_MATCHES(kblib::parse_integer<long>(""s), std::invalid_argument,
33 Message("\"\" is not an integer"));
34 CHECK(kblib::parse_integer<long>("0") == 0);
35 CHECK(kblib::parse_integer<long>("1") == 1);
36 CHECK(kblib::parse_integer<long>("-1") == -1);
37 CHECK(kblib::parse_integer<long>("10") == 10);
38 CHECK(kblib::parse_integer<long>("010") == 010);
39 CHECK(kblib::parse_integer<long>("0x10") == 0x10);
40 CHECK(kblib::parse_integer<long>("0b10") == 0b10);
41 CHECK(kblib::parse_integer<long>("0b1'0000'0000'0000'0000") == 65536);
42
43 CHECK_THROWS_AS(kblib::parse_integer<long>("", 1), std::invalid_argument);
44 CHECK_THROWS_MATCHES(
45 kblib::parse_integer<long>("1", 1), std::invalid_argument,
46 Message("base must be either 0 or a positive number between 2 and 62"));
47 CHECK_THROWS_AS(kblib::parse_integer<long>("1", -1), std::invalid_argument);
48 CHECK_THROWS_AS(kblib::parse_integer<long>("1", 63), std::invalid_argument);
49 CHECK_THROWS_AS(kblib::parse_integer<long>("1", 100), std::invalid_argument);
50 CHECK(kblib::parse_integer<long>("0", 10) == 0);
51 CHECK(kblib::parse_integer<long>("1'0000'0000'0000'0000", 2) == 65536);
52
53 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("2135agfd"),
54 std::invalid_argument,
55 Message("invalid character in integer"));
56 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0-1"),
57 std::invalid_argument,
58 Message("unexpected - in integer"));
59 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0x-1"),
60 std::invalid_argument,
61 Message("unexpected - in integer"));
62 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0b-1"),
63 std::invalid_argument,
64 Message("unexpected - in integer"));
65}
66
67TEST_CASE("fromStr") {
68 using namespace std::literals;
69 const std::string str = "100";
70 CHECK(kblib::fromStr<std::string>(str) == str);
71 CHECK(kblib::fromStr<int>(str) == 100);
72 CHECK(kblib::fromStr<bool>("1") == true);
73 CHECK(kblib::fromStr<bool>("true") == true);
74 CHECK(kblib::fromStr<bool>("0") == false);
75 CHECK(kblib::fromStr<bool>("false") == false);
76
77#if KBLIB_USE_STRING_VIEW
78 const std::string_view strv = str;
79 CHECK(kblib::fromStr<std::string>(strv) == strv);
80 CHECK(kblib::fromStr<int>(strv) == 100);
81 CHECK(kblib::fromStr<bool>("1"sv) == true);
82 CHECK(kblib::fromStr<bool>("true"sv) == true);
83 CHECK(kblib::fromStr<bool>("0"sv) == false);
84 CHECK(kblib::fromStr<bool>("false"sv) == false);
85#endif
86}
TEST_CASE("bases")
Definition: convert.cpp:6
Provides facilities to convert between various kinds of representations.