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