blob: 94ccde9971b3b1d8fd742bb7fb7aad577a4550c7 [file] [log] [blame]
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -08001#include <stdplus/numeric/str.hpp>
2
3#include <string_view>
4
5#include <gtest/gtest.h>
6
William A. Kennington IIIb0bcbaf2022-12-09 02:17:22 -08007using std::literals::string_view_literals::operator""sv;
8
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -08009namespace stdplus
10{
11
12TEST(IntToStr, Uint8_10)
13{
14 IntToStr<10, uint8_t> enc;
William A. Kennington III2ff4b362023-01-04 14:35:59 -080015 static_assert(enc.buf_size == 4);
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080016 char buf[enc.buf_size];
17 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
18 EXPECT_EQ("42", std::string_view(buf, enc(buf, 42)));
19 EXPECT_EQ("255", std::string_view(buf, enc(buf, 255)));
20 EXPECT_EQ("000", std::string_view(buf, enc(buf, 0, 3)));
21 EXPECT_EQ("255", std::string_view(buf, enc(buf, 255, 3)));
22}
23
24TEST(IntToStr, Int8_10)
25{
26 IntToStr<10, int8_t> enc;
William A. Kennington III2ff4b362023-01-04 14:35:59 -080027 static_assert(enc.buf_size == 5);
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080028 char buf[enc.buf_size];
29 EXPECT_EQ("42", std::string_view(buf, enc(buf, 42)));
30 EXPECT_EQ("-127", std::string_view(buf, enc(buf, -127)));
31}
32
33TEST(IntToStr, Uint16_10)
34{
35 IntToStr<10, uint16_t> enc;
William A. Kennington III2ff4b362023-01-04 14:35:59 -080036 static_assert(enc.buf_size == 6);
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080037 char buf[enc.buf_size];
38 EXPECT_EQ("55255", std::string_view(buf, enc(buf, 55255, 3)));
William A. Kennington III6317f352023-07-11 14:05:18 -070039 ToStrHandle<IntToStr<10, uint16_t>> tsh;
40 EXPECT_EQ("55255", tsh(55255));
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080041}
42
43TEST(IntToStr, Uint32_10)
44{
45 IntToStr<10, uint32_t> enc;
46 static_assert(enc.buf_size == 10);
47 char buf[enc.buf_size];
48 EXPECT_EQ("55255", std::string_view(buf, enc(buf, 55255, 3)));
49 EXPECT_EQ("055255", std::string_view(buf, enc(buf, 55255, 6)));
50 EXPECT_EQ("255255", std::string_view(buf, enc(buf, 255255, 3)));
51}
52
53TEST(IntToStr, Uint8_16)
54{
55 IntToStr<16, uint8_t> enc;
56 static_assert(enc.buf_size == 2);
57 char buf[enc.buf_size];
58 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
59 EXPECT_EQ("2a", std::string_view(buf, enc(buf, 42)));
60 EXPECT_EQ("ff", std::string_view(buf, enc(buf, 255)));
61 EXPECT_EQ("00", std::string_view(buf, enc(buf, 0, 2)));
62 EXPECT_EQ("02", std::string_view(buf, enc(buf, 2, 2)));
63 EXPECT_EQ("ff", std::string_view(buf, enc(buf, 255, 2)));
William A. Kennington III6317f352023-07-11 14:05:18 -070064 ToStrHandle<IntToStr<16, uint8_t>> tsh;
65 EXPECT_EQ("2a", tsh(0x2a));
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080066}
67
68TEST(IntToStr, Uint8_8)
69{
70 IntToStr<8, uint8_t> enc;
71 static_assert(enc.buf_size == 3);
72 char buf[enc.buf_size];
73 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
74 EXPECT_EQ("7", std::string_view(buf, enc(buf, 7)));
75 EXPECT_EQ("10", std::string_view(buf, enc(buf, 8)));
76 EXPECT_EQ("377", std::string_view(buf, enc(buf, 255)));
77}
78
79TEST(IntToStr, Uint8_11)
80{
81 IntToStr<11, uint8_t> enc;
82 static_assert(enc.buf_size == 3);
83 char buf[enc.buf_size];
84 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
85 EXPECT_EQ("39", std::string_view(buf, enc(buf, 42)));
86 EXPECT_EQ("212", std::string_view(buf, enc(buf, 255)));
87}
88
89TEST(ToString, Int)
90{
91 EXPECT_EQ("10", stdplus::toStr(size_t{10}));
92 EXPECT_EQ(L"10", stdplus::toBasicStr<wchar_t>(size_t{10}));
93 EXPECT_EQ("-10", stdplus::toStr(ssize_t{-10}));
94 EXPECT_EQ(L"-10", stdplus::toBasicStr<wchar_t>(ssize_t{-10}));
95}
96
97TEST(ToString, perf)
98{
99 GTEST_SKIP();
100 IntToStr<16, size_t> enc;
101 char buf[enc.buf_size];
102 for (size_t i = 0; i < 100000000; ++i)
103 {
104 enc(buf, i);
105 }
106 EXPECT_TRUE(false);
107}
108
William A. Kennington IIIb0bcbaf2022-12-09 02:17:22 -0800109TEST(StrToInt, Uint8_10)
110{
111 StrToInt<10, uint8_t> dec;
112 EXPECT_EQ(42, dec("42"sv));
113 EXPECT_EQ(255, dec("255"sv));
114 EXPECT_THROW(dec(""sv), std::invalid_argument);
115 EXPECT_THROW(dec("-1"sv), std::invalid_argument);
116 EXPECT_THROW(dec("a0"sv), std::invalid_argument);
117 EXPECT_THROW(dec(".0"sv), std::invalid_argument);
118 EXPECT_THROW(dec("257"sv), std::overflow_error);
119 EXPECT_THROW(dec("300"sv), std::overflow_error);
120}
121
122TEST(StrToInt, Uint8_11)
123{
124 StrToInt<11, uint8_t> dec;
125 EXPECT_EQ(112, dec("a2"sv));
126 EXPECT_EQ(255, dec("212"sv));
127 EXPECT_THROW(dec(""sv), std::invalid_argument);
128 EXPECT_THROW(dec("-1"sv), std::invalid_argument);
129 EXPECT_THROW(dec("b0"sv), std::invalid_argument);
130 EXPECT_THROW(dec(".0"sv), std::invalid_argument);
131 EXPECT_THROW(dec("213"sv), std::overflow_error);
132 EXPECT_THROW(dec("300"sv), std::overflow_error);
133}
134
135TEST(StrToInt, Uint16_16)
136{
137 StrToInt<16, uint16_t> dec;
138 EXPECT_EQ(0x42, dec("42"sv));
139 EXPECT_EQ(0xfacf, dec("facf"sv));
140 EXPECT_THROW(dec(""sv), std::invalid_argument);
141 EXPECT_THROW(dec("-1"sv), std::invalid_argument);
142 EXPECT_THROW(dec("g0"sv), std::invalid_argument);
143 EXPECT_THROW(dec(".0"sv), std::invalid_argument);
144 EXPECT_THROW(dec("10000"sv), std::overflow_error);
145}
146
147TEST(StrToInt, Uint16_8)
148{
149 StrToInt<8, uint16_t> dec;
150 EXPECT_EQ(042, dec("42"sv));
151 EXPECT_EQ(0177777, dec("177777"sv));
152 EXPECT_THROW(dec(""sv), std::invalid_argument);
153 EXPECT_THROW(dec("-1"sv), std::invalid_argument);
154 EXPECT_THROW(dec("g0"sv), std::invalid_argument);
155 EXPECT_THROW(dec(".0"sv), std::invalid_argument);
156 EXPECT_THROW(dec("277777"sv), std::overflow_error);
157}
158
159TEST(StrToInt, Int8_16)
160{
161 StrToInt<16, int8_t> dec;
162 EXPECT_EQ(-1, dec("-1"sv));
163 EXPECT_EQ(0x42, dec("42"sv));
164 EXPECT_EQ(-0x7f, dec("-7f"sv));
165 EXPECT_THROW(dec(""sv), std::invalid_argument);
166 EXPECT_THROW(dec("--1"sv), std::invalid_argument);
167 EXPECT_THROW(dec("g0"sv), std::invalid_argument);
168 EXPECT_THROW(dec(".0"sv), std::invalid_argument);
169 EXPECT_THROW(dec("ff"sv), std::overflow_error);
170 EXPECT_THROW(dec("10000"sv), std::overflow_error);
171}
172
173TEST(StrToInt, Int8_0)
174{
175 StrToInt<0, int8_t> dec;
176 EXPECT_EQ(-1, dec("-1"sv));
177 EXPECT_EQ(42, dec("42"sv));
178 EXPECT_EQ(0x42, dec("0x42"sv));
179 EXPECT_EQ(-42, dec("-42"sv));
180 EXPECT_EQ(-0x42, dec("-0x42"sv));
181 EXPECT_THROW(dec(""sv), std::invalid_argument);
182 EXPECT_THROW(dec("--1"sv), std::invalid_argument);
183 EXPECT_THROW(dec("ac"sv), std::invalid_argument);
184 EXPECT_THROW(dec(".0"sv), std::invalid_argument);
185 EXPECT_THROW(dec("0xff"sv), std::overflow_error);
186 EXPECT_THROW(dec("10000"sv), std::overflow_error);
187}
188
189TEST(StrToInt, Perf)
190{
191 GTEST_SKIP();
192 StrToInt<16, size_t> dec;
193 for (size_t i = 0; i < 100000000; ++i)
194 {
195 dec("53036893"sv);
196 }
197 EXPECT_TRUE(false);
198}
199
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -0800200} // namespace stdplus