Dominik Laskowski | 4188ffd | 2021-02-07 22:15:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <ftl/string.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cstdint> |
| 22 | #include <iterator> |
| 23 | #include <limits> |
| 24 | #include <sstream> |
| 25 | #include <type_traits> |
| 26 | |
| 27 | namespace android::test { |
| 28 | |
| 29 | // Keep in sync with example usage in header file. |
| 30 | TEST(String, ToChars) { |
| 31 | ftl::to_chars_buffer_t<> buffer; |
| 32 | |
| 33 | EXPECT_EQ(ftl::to_chars(buffer, 123u), "123"); |
| 34 | EXPECT_EQ(ftl::to_chars(buffer, -42, ftl::Radix::kBin), "-0b101010"); |
| 35 | EXPECT_EQ(ftl::to_chars(buffer, 0xcafe, ftl::Radix::kHex), "0xcafe"); |
| 36 | EXPECT_EQ(ftl::to_chars(buffer, '*', ftl::Radix::kHex), "0x2a"); |
| 37 | } |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | template <typename F, typename T> |
| 42 | void ToCharsTest() { |
| 43 | constexpr auto kRadix = F::kRadix; |
| 44 | |
| 45 | using Limits = std::numeric_limits<T>; |
| 46 | constexpr auto kMin = Limits::min(); |
| 47 | constexpr auto kMax = Limits::max(); |
| 48 | constexpr auto kNeg = static_cast<T>(-42); |
| 49 | constexpr auto kPos = static_cast<T>(123); |
| 50 | |
| 51 | ftl::to_chars_buffer_t<T> buffer; |
| 52 | |
| 53 | EXPECT_EQ(ftl::to_chars(buffer, kMin, kRadix), F{}(kMin)); |
| 54 | EXPECT_EQ(ftl::to_chars(buffer, kMax, kRadix), F{}(kMax)); |
| 55 | EXPECT_EQ(ftl::to_chars(buffer, kNeg, kRadix), F{}(kNeg)); |
| 56 | EXPECT_EQ(ftl::to_chars(buffer, kPos, kRadix), F{}(kPos)); |
| 57 | } |
| 58 | |
| 59 | template <typename...> |
| 60 | struct Types {}; |
| 61 | |
| 62 | template <typename F, typename Types> |
| 63 | struct ToCharsTests; |
| 64 | |
| 65 | template <typename F, typename T, typename... Ts> |
| 66 | struct ToCharsTests<F, Types<T, Ts...>> { |
| 67 | static void test() { |
| 68 | ToCharsTest<F, T>(); |
| 69 | ToCharsTests<F, Types<Ts...>>::test(); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | template <typename F> |
| 74 | struct ToCharsTests<F, Types<>> { |
| 75 | static void test() {} |
| 76 | }; |
| 77 | |
| 78 | template <typename T, typename U = std::make_unsigned_t<T>> |
| 79 | U to_unsigned(std::ostream& stream, T v) { |
| 80 | if (std::is_same_v<T, U>) return v; |
| 81 | |
| 82 | if (v < 0) { |
| 83 | stream << '-'; |
| 84 | return std::numeric_limits<U>::max() - static_cast<U>(v) + 1; |
| 85 | } else { |
| 86 | return static_cast<U>(v); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | struct Bin { |
| 91 | static constexpr auto kRadix = ftl::Radix::kBin; |
| 92 | |
| 93 | template <typename T> |
| 94 | std::string operator()(T v) const { |
| 95 | std::ostringstream stream; |
| 96 | auto u = to_unsigned(stream, v); |
| 97 | stream << "0b"; |
| 98 | |
| 99 | if (u == 0) { |
| 100 | stream << 0; |
| 101 | } else { |
| 102 | std::ostringstream digits; |
| 103 | do { |
| 104 | digits << (u & 1); |
| 105 | } while (u >>= 1); |
| 106 | |
| 107 | const auto str = digits.str(); |
| 108 | std::copy(str.rbegin(), str.rend(), std::ostream_iterator<char>(stream)); |
| 109 | } |
| 110 | |
| 111 | return stream.str(); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | struct Dec { |
| 116 | static constexpr auto kRadix = ftl::Radix::kDec; |
| 117 | |
| 118 | template <typename T> |
| 119 | std::string operator()(T v) const { |
| 120 | return std::to_string(v); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | struct Hex { |
| 125 | static constexpr auto kRadix = ftl::Radix::kHex; |
| 126 | |
| 127 | template <typename T> |
| 128 | std::string operator()(T v) const { |
| 129 | std::ostringstream stream; |
| 130 | const auto u = to_unsigned(stream, v); |
| 131 | stream << "0x" << std::hex << std::nouppercase; |
| 132 | stream << (sizeof(T) == 1 ? static_cast<unsigned>(u) : u); |
| 133 | return stream.str(); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | using IntegerTypes = |
| 138 | Types<char, unsigned char, signed char, std::uint8_t, std::uint16_t, std::uint32_t, |
| 139 | std::uint64_t, std::int8_t, std::int16_t, std::int32_t, std::int64_t>; |
| 140 | |
| 141 | } // namespace |
| 142 | |
| 143 | TEST(String, ToCharsBin) { |
| 144 | ToCharsTests<Bin, IntegerTypes>::test(); |
| 145 | |
| 146 | { |
| 147 | const std::uint8_t x = 0b1111'1111; |
| 148 | ftl::to_chars_buffer_t<decltype(x)> buffer; |
| 149 | EXPECT_EQ(ftl::to_chars(buffer, x, ftl::Radix::kBin), "0b11111111"); |
| 150 | } |
| 151 | { |
| 152 | const std::int16_t x = -0b1000'0000'0000'0000; |
| 153 | ftl::to_chars_buffer_t<decltype(x)> buffer; |
| 154 | EXPECT_EQ(ftl::to_chars(buffer, x, ftl::Radix::kBin), "-0b1000000000000000"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | TEST(String, ToCharsDec) { |
| 159 | ToCharsTests<Dec, IntegerTypes>::test(); |
| 160 | |
| 161 | { |
| 162 | const std::uint32_t x = UINT32_MAX; |
| 163 | ftl::to_chars_buffer_t<decltype(x)> buffer; |
| 164 | EXPECT_EQ(ftl::to_chars(buffer, x), "4294967295"); |
| 165 | } |
| 166 | { |
| 167 | const std::int32_t x = INT32_MIN; |
| 168 | ftl::to_chars_buffer_t<decltype(x)> buffer; |
| 169 | EXPECT_EQ(ftl::to_chars(buffer, x), "-2147483648"); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | TEST(String, ToCharsHex) { |
| 174 | ToCharsTests<Hex, IntegerTypes>::test(); |
| 175 | |
| 176 | { |
| 177 | const std::uint16_t x = 0xfade; |
| 178 | ftl::to_chars_buffer_t<decltype(x)> buffer; |
| 179 | EXPECT_EQ(ftl::to_chars(buffer, x, ftl::Radix::kHex), "0xfade"); |
| 180 | } |
| 181 | { |
| 182 | ftl::to_chars_buffer_t<> buffer; |
| 183 | EXPECT_EQ(ftl::to_chars(buffer, INT64_MIN, ftl::Radix::kHex), "-0x8000000000000000"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } // namespace android::test |