123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/UnitTest/TestTypes.h>
- #include <AzCore/std/ranges/ranges.h>
- #include <AzCore/std/utility/charconv.h>
- namespace UnitTest
- {
- class CharconvTestFixture
- : public UnitTest::LeakDetectionFixture
- {};
- TEST_F(CharconvTestFixture, ToChars_CanConvertIntegralValue_Success)
- {
- constexpr int8_t int8Value = -2;
- constexpr uint8_t uint8Value = 2;
- constexpr int16_t int16Value = -256 - 2;
- constexpr uint16_t uint16Value = 256 + 2;
- constexpr int32_t int32Value = -65536 - 2;
- constexpr uint32_t uint32Value = 65536 + 2;
- constexpr int64_t int64Value = -4294967296 - 2;
- constexpr uint64_t uint64Value = 4294967296 + 2;
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), int8Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("-2", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), uint8Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("2", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), int16Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("-258", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), uint16Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("258", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), int32Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("-65538", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), uint32Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("65538", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), int64Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("-4294967298", convertedInt);
- }
- {
- char convertedInt[32];
- AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
- AZStd::ranges::end(convertedInt), uint64Value);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- // Null-terminate the buffer
- // Assert that the result ptr not outside of the convertedInt array range
- ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
- *result.ptr = '\0';
- EXPECT_STREQ("4294967298", convertedInt);
- }
- }
- TEST_F(CharconvTestFixture, FromChars_CanConvertString_Success)
- {
- {
- int8_t intValue{};
- AZStd::string_view numberString = "-2";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(-2, intValue);
- }
- {
- uint8_t intValue{};
- AZStd::string_view numberString = "2";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(2, intValue);
- }
- {
- int16_t intValue{};
- AZStd::string_view numberString = "-258";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(-258, intValue);
- }
- {
- uint16_t intValue{};
- AZStd::string_view numberString = "258";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(258, intValue);
- }
- {
- int32_t intValue{};
- AZStd::string_view numberString = "-65538";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(-65538, intValue);
- }
- {
- uint32_t intValue{};
- AZStd::string_view numberString = "65538";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(65538, intValue);
- }
- {
- int64_t intValue{};
- AZStd::string_view numberString = "-4294967298";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(-4294967298, intValue);
- }
- {
- uint64_t intValue{};
- AZStd::string_view numberString = "4294967298";
- AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
- numberString.data() + numberString.size(), intValue);
- EXPECT_EQ(AZStd::errc{}, result.ec);
- EXPECT_EQ(numberString.end(), result.ptr);
- EXPECT_EQ(4294967298, intValue);
- }
- }
- }
|