Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 14 | |
| 15 | #include "pw_string/type_to_string.h" |
| 16 | |
| 17 | #include <cmath> |
| 18 | #include <cstring> |
| 19 | #include <limits> |
Wyatt Hepler | 58823c1 | 2019-11-13 14:27:31 -0800 | [diff] [blame] | 20 | #include <string_view> |
Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 21 | |
| 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | namespace pw::string { |
| 25 | namespace { |
| 26 | |
| 27 | TEST(Digits, DecimalDigits_AllOneDigit) { |
| 28 | for (uint64_t i = 0; i < 10; ++i) { |
| 29 | ASSERT_EQ(1u, DecimalDigitCount(i)); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | TEST(Digits, DecimalDigits_AllTwoDigit) { |
| 34 | for (uint64_t i = 10; i < 100u; ++i) { |
| 35 | ASSERT_EQ(2u, DecimalDigitCount(i)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | TEST(Digits, DecimalDigits_1To19Digits) { |
| 40 | uint64_t value = 1; |
| 41 | for (unsigned digits = 1; digits <= 19u; ++digits) { |
| 42 | ASSERT_EQ(digits, DecimalDigitCount(value)); |
| 43 | ASSERT_EQ(digits, DecimalDigitCount(value + 1)); |
| 44 | |
| 45 | value *= 10; |
| 46 | ASSERT_EQ(digits, DecimalDigitCount(value - 1)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | TEST(Digits, DecimalDigits_20) { |
| 51 | for (uint64_t i : { |
| 52 | 10'000'000'000'000'000'000llu, |
| 53 | 10'000'000'000'000'000'001llu, |
| 54 | std::numeric_limits<unsigned long long>::max(), |
| 55 | }) { |
| 56 | ASSERT_EQ(20u, DecimalDigitCount(i)); |
| 57 | } |
| 58 | } |
| 59 | |
Wyatt Hepler | 190fecc | 2019-11-13 18:10:57 -0800 | [diff] [blame] | 60 | TEST(Digits, HexDigits_AllOneDigit) { |
| 61 | for (uint64_t i = 0; i < 0x10; ++i) { |
| 62 | ASSERT_EQ(1u, HexDigitCount(i)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | TEST(Digits, HexDigits_AllTwoDigit) { |
| 67 | for (uint64_t i = 0x10; i < 0x100u; ++i) { |
| 68 | ASSERT_EQ(2u, HexDigitCount(i)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | TEST(Digits, HexDigits_1To15Digits) { |
| 73 | uint64_t value = 1; |
| 74 | for (unsigned digits = 1; digits <= 15u; ++digits) { |
| 75 | ASSERT_EQ(digits, HexDigitCount(value)); |
| 76 | ASSERT_EQ(digits, HexDigitCount(value + 1)); |
| 77 | |
| 78 | value *= 0x10; |
| 79 | ASSERT_EQ(digits, HexDigitCount(value - 1)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | TEST(Digits, HexDigits_16) { |
| 84 | for (uint64_t i : { |
| 85 | 0x1000000000000000llu, |
| 86 | 0x1000000000000001llu, |
| 87 | std::numeric_limits<unsigned long long>::max(), |
| 88 | }) { |
| 89 | ASSERT_EQ(16u, HexDigitCount(i)); |
| 90 | } |
| 91 | } |
| 92 | |
Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 93 | class TestWithBuffer : public ::testing::Test { |
| 94 | protected: |
| 95 | static constexpr char kStartingString[] = "!@#$%^&*()!@#$%^&*()"; |
| 96 | static constexpr char kUint64Max[] = "18446744073709551615"; |
| 97 | static constexpr char kInt64Min[] = "-9223372036854775808"; |
| 98 | static constexpr char kInt64Max[] = "9223372036854775807"; |
| 99 | |
| 100 | static_assert(sizeof(kStartingString) == sizeof(kUint64Max)); |
| 101 | |
| 102 | TestWithBuffer() { std::memcpy(buffer_, kStartingString, sizeof(buffer_)); } |
| 103 | |
| 104 | char buffer_[sizeof(kUint64Max)]; |
| 105 | }; |
| 106 | |
| 107 | class IntToStringTest : public TestWithBuffer {}; |
| 108 | |
| 109 | TEST_F(IntToStringTest, Unsigned_EmptyBuffer_WritesNothing) { |
| 110 | auto result = IntToString(9u, span(buffer_, 0)); |
| 111 | EXPECT_EQ(0u, result.size()); |
| 112 | EXPECT_FALSE(result.ok()); |
| 113 | EXPECT_STREQ(kStartingString, buffer_); |
| 114 | } |
| 115 | |
| 116 | TEST_F(IntToStringTest, Unsigned_TooSmall_1Char_OnlyNullTerminates) { |
| 117 | auto result = IntToString(9u, span(buffer_, 1)); |
| 118 | EXPECT_EQ(0u, result.size()); |
| 119 | EXPECT_FALSE(result.ok()); |
| 120 | EXPECT_STREQ("", buffer_); |
| 121 | } |
| 122 | |
| 123 | TEST_F(IntToStringTest, Unsigned_TooSmall_2Chars_OnlyNullTerminates) { |
| 124 | auto result = IntToString(10u, span(buffer_, 2)); |
| 125 | EXPECT_EQ(0u, result.size()); |
| 126 | EXPECT_FALSE(result.ok()); |
| 127 | EXPECT_STREQ("", buffer_); |
| 128 | } |
| 129 | |
| 130 | TEST_F(IntToStringTest, Unsigned_TooSmall_3Chars_OnlyNullTerminates) { |
| 131 | auto result = IntToString(123u, span(buffer_, 3)); |
| 132 | EXPECT_EQ(0u, result.size()); |
| 133 | EXPECT_FALSE(result.ok()); |
| 134 | EXPECT_STREQ("", buffer_); |
| 135 | } |
| 136 | |
| 137 | TEST_F(IntToStringTest, Unsigned_1Char_FitsExactly) { |
| 138 | auto result = IntToString(0u, span(buffer_, 2)); |
| 139 | EXPECT_EQ(1u, result.size()); |
| 140 | EXPECT_TRUE(result.ok()); |
| 141 | EXPECT_STREQ("0", buffer_); |
| 142 | |
| 143 | result = IntToString(9u, span(buffer_, 2)); |
| 144 | EXPECT_EQ(1u, result.size()); |
| 145 | EXPECT_TRUE(result.ok()); |
| 146 | EXPECT_STREQ("9", buffer_); |
| 147 | } |
| 148 | |
| 149 | TEST_F(IntToStringTest, Unsigned_2Chars_FitsExactly) { |
| 150 | auto result = IntToString(10u, span(buffer_, 3)); |
| 151 | EXPECT_EQ(2u, result.size()); |
| 152 | EXPECT_STREQ("10", buffer_); |
| 153 | } |
| 154 | |
| 155 | TEST_F(IntToStringTest, Unsigned_MaxFitsExactly) { |
| 156 | EXPECT_EQ(20u, |
| 157 | IntToString(std::numeric_limits<uint64_t>::max(), |
| 158 | span(buffer_, sizeof(kUint64Max))) |
| 159 | .size()); |
| 160 | EXPECT_STREQ(kUint64Max, buffer_); |
| 161 | } |
| 162 | |
| 163 | TEST_F(IntToStringTest, SignedPositive_EmptyBuffer_WritesNothing) { |
| 164 | auto result = IntToString(9, span(buffer_, 0)); |
| 165 | EXPECT_EQ(0u, result.size()); |
| 166 | EXPECT_FALSE(result.ok()); |
| 167 | EXPECT_STREQ(kStartingString, buffer_); |
| 168 | } |
| 169 | |
| 170 | TEST_F(IntToStringTest, SignedPositive_TooSmall_NullTerminates) { |
| 171 | auto result = IntToString(9, span(buffer_, 1)); |
| 172 | EXPECT_EQ(0u, result.size()); |
| 173 | EXPECT_FALSE(result.ok()); |
| 174 | EXPECT_STREQ("", buffer_); |
| 175 | } |
| 176 | |
| 177 | TEST_F(IntToStringTest, SignedPositive_TooSmall_DoesNotWritePastEnd) { |
| 178 | EXPECT_EQ(0u, IntToString(9, span(buffer_, 1)).size()); |
| 179 | EXPECT_EQ(0, std::memcmp("\0@#$%^&*()!@#$%^&*()", buffer_, sizeof(buffer_))); |
| 180 | } |
| 181 | |
| 182 | TEST_F(IntToStringTest, SignedPositive_1Char_FitsExactly) { |
| 183 | auto result = IntToString(0, span(buffer_, 2)); |
| 184 | EXPECT_EQ(1u, result.size()); |
| 185 | EXPECT_TRUE(result.ok()); |
| 186 | EXPECT_STREQ("0", buffer_); |
| 187 | |
| 188 | result = IntToString(9, span(buffer_, 2)); |
| 189 | EXPECT_EQ(1u, result.size()); |
| 190 | EXPECT_TRUE(result.ok()); |
| 191 | EXPECT_STREQ("9", buffer_); |
| 192 | } |
| 193 | |
| 194 | TEST_F(IntToStringTest, SignedPositive_2Chars_FitsExactly) { |
| 195 | auto result = IntToString(10, span(buffer_, 4)); |
| 196 | EXPECT_EQ(2u, result.size()); |
| 197 | EXPECT_TRUE(result.ok()); |
| 198 | EXPECT_STREQ("10", buffer_); |
| 199 | } |
| 200 | |
| 201 | TEST_F(IntToStringTest, SignedPositive_MaxFitsExactly) { |
| 202 | auto result = IntToString(std::numeric_limits<int64_t>::max(), |
| 203 | span(buffer_, sizeof(kInt64Min))); |
| 204 | EXPECT_EQ(19u, result.size()); |
| 205 | EXPECT_STREQ(kInt64Max, buffer_); |
| 206 | } |
| 207 | |
| 208 | TEST_F(IntToStringTest, SignedNegative_EmptyBuffer_WritesNothing) { |
| 209 | auto result = IntToString(-9, span(buffer_, 0)); |
| 210 | EXPECT_EQ(0u, result.size()); |
| 211 | EXPECT_FALSE(result.ok()); |
| 212 | EXPECT_STREQ(kStartingString, buffer_); |
| 213 | } |
| 214 | |
| 215 | TEST_F(IntToStringTest, SignedNegative_TooSmall_NullTerminates) { |
| 216 | auto result = IntToString(-9, span(buffer_, 1)); |
| 217 | EXPECT_EQ(0u, result.size()); |
| 218 | EXPECT_FALSE(result.ok()); |
| 219 | EXPECT_STREQ("", buffer_); |
| 220 | } |
| 221 | |
| 222 | TEST_F(IntToStringTest, SignedNegative_TooSmall_DoesNotWritePastEnd) { |
| 223 | // Note that two \0 are written due to the unsigned IntToString call. |
| 224 | EXPECT_EQ(0u, IntToString(-9, span(buffer_, 2)).size()); |
| 225 | EXPECT_EQ(0, std::memcmp("\0\0#$%^&*()!@#$%^&*()", buffer_, sizeof(buffer_))); |
| 226 | } |
| 227 | |
| 228 | TEST_F(IntToStringTest, SignedNegative_FitsExactly) { |
| 229 | auto result = IntToString(-9, span(buffer_, 3)); |
| 230 | EXPECT_EQ(2u, result.size()); |
| 231 | EXPECT_STREQ("-9", buffer_); |
| 232 | result = IntToString(-99, span(buffer_, 4)); |
| 233 | EXPECT_EQ(3u, result.size()); |
| 234 | EXPECT_STREQ("-99", buffer_); |
| 235 | result = IntToString(-123, span(buffer_, 5)); |
| 236 | EXPECT_EQ(4u, result.size()); |
| 237 | EXPECT_STREQ("-123", buffer_); |
| 238 | } |
| 239 | |
| 240 | TEST_F(IntToStringTest, SignedNegative_MinFitsExactly) { |
| 241 | auto result = IntToString(std::numeric_limits<int64_t>::min(), |
| 242 | span(buffer_, sizeof(kInt64Min))); |
| 243 | EXPECT_EQ(20u, result.size()); |
| 244 | EXPECT_STREQ(kInt64Min, buffer_); |
| 245 | } |
| 246 | |
| 247 | TEST(IntToString, SignedSweep) { |
| 248 | for (int i = -1002; i <= 1002; ++i) { |
| 249 | char buffer[6]; |
| 250 | char printf_buffer[6]; |
| 251 | int written = std::snprintf(printf_buffer, sizeof(printf_buffer), "%d", i); |
| 252 | auto result = IntToString(i, buffer); |
| 253 | ASSERT_EQ(static_cast<size_t>(written), result.size()); |
| 254 | ASSERT_STREQ(printf_buffer, buffer); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | TEST(IntToString, UnsignedSweep) { |
| 259 | for (unsigned i = 0; i <= 1002u; ++i) { |
| 260 | char buffer[5]; |
| 261 | char printf_buffer[5]; |
| 262 | int written = std::snprintf(printf_buffer, sizeof(printf_buffer), "%u", i); |
| 263 | auto result = IntToString(i, buffer); |
| 264 | ASSERT_EQ(static_cast<size_t>(written), result.size()); |
| 265 | ASSERT_STREQ(printf_buffer, buffer); |
| 266 | } |
| 267 | } |
| 268 | |
Wyatt Hepler | 190fecc | 2019-11-13 18:10:57 -0800 | [diff] [blame] | 269 | class IntToHexStringTest : public TestWithBuffer {}; |
| 270 | |
| 271 | TEST_F(IntToHexStringTest, Sweep) { |
| 272 | for (unsigned i = 0; i < 1030; ++i) { |
| 273 | char hex[16]; |
| 274 | int bytes = std::snprintf(hex, sizeof(hex), "%x", static_cast<unsigned>(i)); |
| 275 | |
| 276 | auto result = IntToHexString(i, buffer_); |
| 277 | EXPECT_EQ(static_cast<size_t>(bytes), result.size()); |
| 278 | EXPECT_TRUE(result.ok()); |
| 279 | EXPECT_STREQ(hex, buffer_); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | TEST_F(IntToHexStringTest, Uint32Max) { |
| 284 | EXPECT_EQ( |
| 285 | 8u, |
| 286 | IntToHexString(std::numeric_limits<uint32_t>::max() - 1, buffer_).size()); |
| 287 | EXPECT_STREQ("fffffffe", buffer_); |
| 288 | |
| 289 | EXPECT_EQ( |
| 290 | 8u, IntToHexString(std::numeric_limits<uint32_t>::max(), buffer_).size()); |
| 291 | EXPECT_STREQ("ffffffff", buffer_); |
| 292 | } |
| 293 | |
| 294 | TEST_F(IntToHexStringTest, Uint64Max) { |
| 295 | EXPECT_EQ( |
| 296 | 16u, |
| 297 | IntToHexString(std::numeric_limits<uint64_t>::max() - 1, buffer_).size()); |
| 298 | EXPECT_STREQ("fffffffffffffffe", buffer_); |
| 299 | |
| 300 | EXPECT_EQ( |
| 301 | 16u, |
| 302 | IntToHexString(std::numeric_limits<uint64_t>::max(), buffer_).size()); |
| 303 | EXPECT_STREQ("ffffffffffffffff", buffer_); |
| 304 | } |
| 305 | |
| 306 | TEST_F(IntToHexStringTest, EmptyBuffer_WritesNothing) { |
| 307 | auto result = IntToHexString(0xbeef, span(buffer_, 0)); |
| 308 | EXPECT_EQ(0u, result.size()); |
| 309 | EXPECT_FALSE(result.ok()); |
| 310 | EXPECT_STREQ(kStartingString, buffer_); |
| 311 | } |
| 312 | |
| 313 | TEST_F(IntToHexStringTest, TooSmall_Truncates) { |
| 314 | auto result = IntToHexString(0xbeef, span(buffer_, 3)); |
| 315 | EXPECT_EQ(0u, result.size()); |
| 316 | EXPECT_FALSE(result.ok()); |
| 317 | EXPECT_STREQ("", buffer_); |
| 318 | } |
| 319 | |
Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 320 | class FloatAsIntToStringTest : public TestWithBuffer {}; |
| 321 | |
| 322 | TEST_F(FloatAsIntToStringTest, PositiveInfinity) { |
| 323 | EXPECT_EQ(3u, FloatAsIntToString(INFINITY, buffer_).size()); |
| 324 | EXPECT_STREQ("inf", buffer_); |
| 325 | } |
| 326 | |
| 327 | TEST_F(FloatAsIntToStringTest, NegativeInfinity) { |
| 328 | EXPECT_EQ(4u, FloatAsIntToString(-INFINITY, buffer_).size()); |
| 329 | EXPECT_STREQ("-inf", buffer_); |
| 330 | } |
| 331 | |
| 332 | TEST_F(FloatAsIntToStringTest, PositiveNan) { |
| 333 | EXPECT_EQ(3u, FloatAsIntToString(NAN, buffer_).size()); |
| 334 | EXPECT_STREQ("NaN", buffer_); |
| 335 | } |
| 336 | |
| 337 | TEST_F(FloatAsIntToStringTest, NegativeNan) { |
| 338 | EXPECT_EQ(4u, FloatAsIntToString(-NAN, buffer_).size()); |
| 339 | EXPECT_STREQ("-NaN", buffer_); |
| 340 | } |
| 341 | |
| 342 | TEST_F(FloatAsIntToStringTest, RoundDown_PrintsNearestInt) { |
| 343 | EXPECT_EQ(1u, FloatAsIntToString(1.23, buffer_).size()); |
| 344 | EXPECT_STREQ("1", buffer_); |
| 345 | } |
| 346 | |
| 347 | TEST_F(FloatAsIntToStringTest, RoundUp_PrintsNearestInt) { |
| 348 | EXPECT_EQ(4u, FloatAsIntToString(1234.5, buffer_).size()); |
| 349 | EXPECT_STREQ("1235", buffer_); |
| 350 | } |
| 351 | |
| 352 | TEST_F(FloatAsIntToStringTest, RoundsToNegativeZero_PrintsZero) { |
| 353 | EXPECT_EQ(1u, FloatAsIntToString(-3.14e-20f, buffer_).size()); |
| 354 | EXPECT_STREQ("0", buffer_); |
| 355 | } |
| 356 | |
| 357 | TEST_F(FloatAsIntToStringTest, RoundsToPositiveZero_PrintsZero) { |
| 358 | EXPECT_EQ(1u, FloatAsIntToString(3.14e-20f, buffer_).size()); |
| 359 | EXPECT_STREQ("0", buffer_); |
| 360 | } |
| 361 | |
| 362 | TEST_F(FloatAsIntToStringTest, RoundDownNegative_PrintsNearestInt) { |
| 363 | volatile float x = -5.9; |
| 364 | EXPECT_EQ(2u, FloatAsIntToString(x, buffer_).size()); |
| 365 | EXPECT_STREQ("-6", buffer_); |
| 366 | } |
| 367 | |
| 368 | TEST_F(FloatAsIntToStringTest, RoundUpNegative_PrintsNearestInt) { |
| 369 | EXPECT_EQ(9u, FloatAsIntToString(-50000000.1, buffer_).size()); |
| 370 | EXPECT_STREQ("-50000000", buffer_); |
| 371 | } |
| 372 | |
| 373 | TEST_F(FloatAsIntToStringTest, LargerThanInteger) { |
| 374 | EXPECT_EQ(3u, FloatAsIntToString(3.14e20f, buffer_).size()); |
| 375 | EXPECT_STREQ("inf", buffer_); |
| 376 | } |
| 377 | |
| 378 | TEST_F(FloatAsIntToStringTest, SmallerThanInteger) { |
| 379 | EXPECT_EQ(4u, FloatAsIntToString(-3.14e20f, buffer_).size()); |
| 380 | EXPECT_STREQ("-inf", buffer_); |
| 381 | } |
| 382 | |
| 383 | TEST_F(FloatAsIntToStringTest, TooSmall_Numeric_NullTerminates) { |
| 384 | auto result = FloatAsIntToString(-3.14e20f, span(buffer_, 1)); |
| 385 | EXPECT_EQ(0u, result.size()); |
| 386 | EXPECT_FALSE(result.ok()); |
| 387 | EXPECT_STREQ("", buffer_); |
| 388 | } |
| 389 | |
| 390 | TEST_F(FloatAsIntToStringTest, TooSmall_Infinity_NullTerminates) { |
| 391 | auto result = FloatAsIntToString(-INFINITY, span(buffer_, 3)); |
| 392 | EXPECT_EQ(0u, result.size()); |
| 393 | EXPECT_FALSE(result.ok()); |
| 394 | EXPECT_STREQ("", buffer_); |
| 395 | } |
| 396 | |
| 397 | TEST_F(FloatAsIntToStringTest, TooSmall_NaN_NullTerminates) { |
| 398 | auto result = FloatAsIntToString(NAN, span(buffer_, 2)); |
| 399 | EXPECT_EQ(0u, result.size()); |
| 400 | EXPECT_FALSE(result.ok()); |
| 401 | EXPECT_STREQ("", buffer_); |
| 402 | } |
| 403 | |
Wyatt Hepler | 58823c1 | 2019-11-13 14:27:31 -0800 | [diff] [blame] | 404 | class CopyStringTest : public TestWithBuffer {}; |
| 405 | |
| 406 | using namespace std::literals::string_view_literals; |
| 407 | |
| 408 | TEST_F(CopyStringTest, EmptyStringView_WritesNullTerminator) { |
| 409 | EXPECT_EQ(0u, CopyString("", buffer_).size()); |
| 410 | EXPECT_EQ('\0', buffer_[0]); |
| 411 | } |
| 412 | |
| 413 | TEST_F(CopyStringTest, EmptyBuffer_WritesNothing) { |
| 414 | auto result = CopyString("Hello", span(buffer_, 0)); |
| 415 | EXPECT_EQ(0u, result.size()); |
| 416 | EXPECT_FALSE(result.ok()); |
| 417 | EXPECT_STREQ(kStartingString, buffer_); |
| 418 | } |
| 419 | |
| 420 | TEST_F(CopyStringTest, TooSmall_Truncates) { |
| 421 | auto result = CopyString("Hi!", span(buffer_, 3)); |
| 422 | EXPECT_EQ(2u, result.size()); |
| 423 | EXPECT_FALSE(result.ok()); |
| 424 | EXPECT_STREQ("Hi", buffer_); |
| 425 | } |
| 426 | |
| 427 | TEST_F(CopyStringTest, ExactFit) { |
| 428 | auto result = CopyString("Hi!", span(buffer_, 4)); |
| 429 | EXPECT_EQ(3u, result.size()); |
| 430 | EXPECT_TRUE(result.ok()); |
| 431 | EXPECT_STREQ("Hi!", buffer_); |
| 432 | } |
| 433 | |
| 434 | TEST_F(CopyStringTest, NullTerminatorsInString) { |
| 435 | ASSERT_EQ(4u, CopyString("\0!\0\0"sv, span(buffer_, 5)).size()); |
| 436 | EXPECT_EQ("\0!\0\0"sv, std::string_view(buffer_, 4)); |
| 437 | } |
| 438 | |
| 439 | class CopyEntireStringTest : public TestWithBuffer {}; |
| 440 | |
| 441 | TEST_F(CopyEntireStringTest, EmptyStringView_WritesNullTerminator) { |
| 442 | EXPECT_EQ(0u, CopyEntireString("", buffer_).size()); |
| 443 | EXPECT_EQ('\0', buffer_[0]); |
| 444 | } |
| 445 | |
| 446 | TEST_F(CopyEntireStringTest, EmptyBuffer_WritesNothing) { |
| 447 | auto result = CopyEntireString("Hello", span(buffer_, 0)); |
| 448 | EXPECT_EQ(0u, result.size()); |
| 449 | EXPECT_FALSE(result.ok()); |
| 450 | EXPECT_STREQ(kStartingString, buffer_); |
| 451 | } |
| 452 | |
| 453 | TEST_F(CopyEntireStringTest, TooSmall_WritesNothing) { |
| 454 | auto result = CopyEntireString("Hi!", span(buffer_, 3)); |
| 455 | EXPECT_EQ(0u, result.size()); |
| 456 | EXPECT_FALSE(result.ok()); |
| 457 | EXPECT_STREQ("", buffer_); |
| 458 | } |
| 459 | |
| 460 | TEST_F(CopyEntireStringTest, ExactFit) { |
| 461 | auto result = CopyEntireString("Hi!", span(buffer_, 4)); |
| 462 | EXPECT_EQ(3u, result.size()); |
| 463 | EXPECT_TRUE(result.ok()); |
| 464 | EXPECT_STREQ("Hi!", buffer_); |
| 465 | } |
| 466 | |
| 467 | TEST_F(CopyEntireStringTest, NullTerminatorsInString) { |
| 468 | ASSERT_EQ(4u, CopyEntireString("\0!\0\0"sv, span(buffer_, 5)).size()); |
| 469 | EXPECT_EQ("\0!\0\0"sv, std::string_view(buffer_, 4)); |
| 470 | } |
| 471 | |
| 472 | class PointerToStringTest : public TestWithBuffer {}; |
| 473 | |
| 474 | TEST_F(PointerToStringTest, Nullptr_WritesNull) { |
Wyatt Hepler | 38426d7 | 2019-11-20 10:21:55 -0800 | [diff] [blame] | 475 | EXPECT_EQ(6u, PointerToString(nullptr, span(buffer_, 7)).size()); |
| 476 | EXPECT_STREQ("(null)", buffer_); |
Wyatt Hepler | 58823c1 | 2019-11-13 14:27:31 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | TEST_F(PointerToStringTest, WritesAddress) { |
Wyatt Hepler | 190fecc | 2019-11-13 18:10:57 -0800 | [diff] [blame] | 480 | const void* pointer = reinterpret_cast<void*>(0xbeef); |
| 481 | EXPECT_EQ(4u, PointerToString(pointer, buffer_).size()); |
| 482 | EXPECT_STREQ("beef", buffer_); |
Wyatt Hepler | 58823c1 | 2019-11-13 14:27:31 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | class BoolToStringTest : public TestWithBuffer {}; |
| 486 | |
| 487 | TEST_F(BoolToStringTest, ExactFit) { |
| 488 | EXPECT_EQ(4u, BoolToString(true, span(buffer_, 5)).size()); |
| 489 | EXPECT_STREQ("true", buffer_); |
| 490 | |
| 491 | EXPECT_EQ(5u, BoolToString(false, span(buffer_, 6)).size()); |
| 492 | EXPECT_STREQ("false", buffer_); |
| 493 | } |
| 494 | |
| 495 | TEST_F(BoolToStringTest, True_TooSmall_WritesNullTerminator) { |
| 496 | auto result = BoolToString(true, span(buffer_, 4)); |
| 497 | EXPECT_EQ(0u, result.size()); |
| 498 | EXPECT_FALSE(result.ok()); |
| 499 | EXPECT_STREQ("", buffer_); |
| 500 | } |
| 501 | |
| 502 | TEST_F(BoolToStringTest, False_TooSmall_WritesNullTerminator) { |
| 503 | auto result = BoolToString(false, span(buffer_, 5)); |
| 504 | EXPECT_EQ(0u, result.size()); |
| 505 | EXPECT_FALSE(result.ok()); |
| 506 | EXPECT_STREQ("", buffer_); |
| 507 | } |
| 508 | |
| 509 | TEST_F(BoolToStringTest, EmptyBuffer_WritesNothing) { |
| 510 | EXPECT_EQ(0u, BoolToString(true, span(buffer_, 0)).size()); |
| 511 | EXPECT_STREQ(kStartingString, buffer_); |
| 512 | |
| 513 | EXPECT_EQ(0u, BoolToString(false, span(buffer_, 0)).size()); |
| 514 | EXPECT_STREQ(kStartingString, buffer_); |
| 515 | } |
| 516 | |
Wyatt Hepler | 48db4d6 | 2019-11-11 10:32:45 -0800 | [diff] [blame] | 517 | } // namespace |
| 518 | } // namespace pw::string |