blob: 008cbe5a69c2bb690253f3c24535fd2e3bef3604 [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
7namespace stdplus
8{
9
10TEST(IntToStr, Uint8_10)
11{
12 IntToStr<10, uint8_t> enc;
William A. Kennington III2ff4b362023-01-04 14:35:59 -080013 static_assert(enc.buf_size == 4);
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080014 char buf[enc.buf_size];
15 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
16 EXPECT_EQ("42", std::string_view(buf, enc(buf, 42)));
17 EXPECT_EQ("255", std::string_view(buf, enc(buf, 255)));
18 EXPECT_EQ("000", std::string_view(buf, enc(buf, 0, 3)));
19 EXPECT_EQ("255", std::string_view(buf, enc(buf, 255, 3)));
20}
21
22TEST(IntToStr, Int8_10)
23{
24 IntToStr<10, int8_t> enc;
William A. Kennington III2ff4b362023-01-04 14:35:59 -080025 static_assert(enc.buf_size == 5);
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080026 char buf[enc.buf_size];
27 EXPECT_EQ("42", std::string_view(buf, enc(buf, 42)));
28 EXPECT_EQ("-127", std::string_view(buf, enc(buf, -127)));
29}
30
31TEST(IntToStr, Uint16_10)
32{
33 IntToStr<10, uint16_t> enc;
William A. Kennington III2ff4b362023-01-04 14:35:59 -080034 static_assert(enc.buf_size == 6);
William A. Kennington IIIcda3c1e2022-12-03 16:56:19 -080035 char buf[enc.buf_size];
36 EXPECT_EQ("55255", std::string_view(buf, enc(buf, 55255, 3)));
37}
38
39TEST(IntToStr, Uint32_10)
40{
41 IntToStr<10, uint32_t> enc;
42 static_assert(enc.buf_size == 10);
43 char buf[enc.buf_size];
44 EXPECT_EQ("55255", std::string_view(buf, enc(buf, 55255, 3)));
45 EXPECT_EQ("055255", std::string_view(buf, enc(buf, 55255, 6)));
46 EXPECT_EQ("255255", std::string_view(buf, enc(buf, 255255, 3)));
47}
48
49TEST(IntToStr, Uint8_16)
50{
51 IntToStr<16, uint8_t> enc;
52 static_assert(enc.buf_size == 2);
53 char buf[enc.buf_size];
54 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
55 EXPECT_EQ("2a", std::string_view(buf, enc(buf, 42)));
56 EXPECT_EQ("ff", std::string_view(buf, enc(buf, 255)));
57 EXPECT_EQ("00", std::string_view(buf, enc(buf, 0, 2)));
58 EXPECT_EQ("02", std::string_view(buf, enc(buf, 2, 2)));
59 EXPECT_EQ("ff", std::string_view(buf, enc(buf, 255, 2)));
60}
61
62TEST(IntToStr, Uint8_8)
63{
64 IntToStr<8, uint8_t> enc;
65 static_assert(enc.buf_size == 3);
66 char buf[enc.buf_size];
67 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
68 EXPECT_EQ("7", std::string_view(buf, enc(buf, 7)));
69 EXPECT_EQ("10", std::string_view(buf, enc(buf, 8)));
70 EXPECT_EQ("377", std::string_view(buf, enc(buf, 255)));
71}
72
73TEST(IntToStr, Uint8_11)
74{
75 IntToStr<11, uint8_t> enc;
76 static_assert(enc.buf_size == 3);
77 char buf[enc.buf_size];
78 EXPECT_EQ("0", std::string_view(buf, enc(buf, 0)));
79 EXPECT_EQ("39", std::string_view(buf, enc(buf, 42)));
80 EXPECT_EQ("212", std::string_view(buf, enc(buf, 255)));
81}
82
83TEST(ToString, Int)
84{
85 EXPECT_EQ("10", stdplus::toStr(size_t{10}));
86 EXPECT_EQ(L"10", stdplus::toBasicStr<wchar_t>(size_t{10}));
87 EXPECT_EQ("-10", stdplus::toStr(ssize_t{-10}));
88 EXPECT_EQ(L"-10", stdplus::toBasicStr<wchar_t>(ssize_t{-10}));
89}
90
91TEST(ToString, perf)
92{
93 GTEST_SKIP();
94 IntToStr<16, size_t> enc;
95 char buf[enc.buf_size];
96 for (size_t i = 0; i < 100000000; ++i)
97 {
98 enc(buf, i);
99 }
100 EXPECT_TRUE(false);
101}
102
103} // namespace stdplus