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/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>("-1") == -1);
38 CHECK(kblib::parse_integer<long>("10") == 10);
39 CHECK(kblib::parse_integer<long>("010") == 010);
40 CHECK(kblib::parse_integer<long>("0x10") == 0x10);
41 CHECK(kblib::parse_integer<long>("0b10") == 0b10);
42 CHECK(kblib::parse_integer<long>("0b1'0000'0000'0000'0000")
43 == 0b1'0000'0000'0000'0000);
44 CHECK(kblib::parse_integer<long>("-10") == -10);
45 CHECK(kblib::parse_integer<long>("+10") == 10);
46 // octal literals
47 CHECK(kblib::parse_integer<long>("-010") == -010);
48 CHECK(kblib::parse_integer<long>("+010") == 010);
49 CHECK(kblib::parse_integer<long>("-0x10") == -0x10);
50 CHECK(kblib::parse_integer<long>("+0x10") == 0x10);
51 CHECK(kblib::parse_integer<long>("-0b10") == -0b10);
52 CHECK(kblib::parse_integer<long>("+0b10") == 0b10);
53 CHECK(kblib::parse_integer<long>("-0b1'0000'0000'0000'0000")
54 == -0b1'0000'0000'0000'0000);
55 CHECK(kblib::parse_integer<long>("+0b1'0000'0000'0000'0000")
56 == 0b1'0000'0000'0000'0000);
57
58 CHECK_THROWS_AS(kblib::parse_integer<long>("", 1), std::invalid_argument);
60 kblib::parse_integer<long>("1", 1), std::invalid_argument,
61 Message("base must be either 0 or a positive number between 2 and 62"));
62 CHECK_THROWS_AS(kblib::parse_integer<long>("1", -1), std::invalid_argument);
63 CHECK_THROWS_AS(kblib::parse_integer<long>("1", 63), std::invalid_argument);
64 CHECK_THROWS_AS(kblib::parse_integer<long>("1", 100), std::invalid_argument);
65 CHECK(kblib::parse_integer<long>("0", 10) == 0);
66 CHECK(kblib::parse_integer<long>("1'0000'0000'0000'0000", 2) == 65536);
67
68 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("2135agfd"),
69 std::invalid_argument,
70 Message("invalid character in integer"));
71 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0-1"),
72 std::invalid_argument,
73 Message("unexpected - in integer"));
74 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0-"), std::invalid_argument,
75 Message("unexpected - in integer"));
76 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0x-1"),
77 std::invalid_argument,
78 Message("unexpected - in integer"));
79 CHECK_THROWS_MATCHES(kblib::parse_integer<long>("0b-1"),
80 std::invalid_argument,
81 Message("unexpected - in integer"));
82 CHECK_THROWS_AS(kblib::parse_integer<long>("0+"), std::invalid_argument);
83 CHECK_THROWS_AS(kblib::parse_integer<long>("0+1"), std::invalid_argument);
84 CHECK_THROWS_AS(kblib::parse_integer<long>("0x+1"), std::invalid_argument);
85 CHECK_THROWS_AS(kblib::parse_integer<long>("0b+1"), std::invalid_argument);
86 CHECK_THROWS_AS(kblib::parse_integer<long>("--1"), std::invalid_argument);
87 CHECK_THROWS_AS(kblib::parse_integer<long>("-+1"), std::invalid_argument);
88 CHECK_THROWS_AS(kblib::parse_integer<long>("+-1"), std::invalid_argument);
89 CHECK_THROWS_AS(kblib::parse_integer<long>("++1"), std::invalid_argument);
90
91 // signed min: (NYI)
92 CHECK(kblib::parse_integer<std::int16_t>("-32768") == -32768);
93 CHECK(kblib::parse_integer<std::int32_t>("-0x80'00'00'00")
94 == static_cast<std::int32_t>(-0x80'00'00'00));
95}
96
97TEST_CASE("fromStr") {
98 using namespace std::literals;
99 const std::string str = "100";
100 CHECK(kblib::fromStr<std::string>(str) == str);
101 CHECK(kblib::fromStr<int>(str) == 100);
102 CHECK(kblib::fromStr<bool>("1") == true);
103 CHECK(kblib::fromStr<bool>("true") == true);
104 CHECK(kblib::fromStr<bool>("0") == false);
105 CHECK(kblib::fromStr<bool>("false") == false);
106
107#if KBLIB_USE_STRING_VIEW
108 const std::string_view strv = str;
109 CHECK(kblib::fromStr<std::string>(strv) == strv);
110 CHECK(kblib::fromStr<int>(strv) == 100);
111 CHECK(kblib::fromStr<bool>("1"sv) == true);
112 CHECK(kblib::fromStr<bool>("true"sv) == true);
113 CHECK(kblib::fromStr<bool>("0"sv) == false);
114 CHECK(kblib::fromStr<bool>("false"sv) == false);
115#endif
116}
#define CHECK_THROWS_AS(expr, exceptionType)
Definition: catch.hpp:17694
#define CHECK_THROWS_MATCHES(expr, exceptionType, matcher)
Definition: catch.hpp:17697
#define CHECK(...)
Definition: catch.hpp:17687
TEST_CASE("bases")
Definition: convert.cpp:6
Provides facilities to convert between various kinds of representations.
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4001
Exception::ExceptionMessageMatcher Message(std::string const &message)