Charconv.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/std/ranges/ranges.h>
  10. #include <AzCore/std/utility/charconv.h>
  11. namespace UnitTest
  12. {
  13. class CharconvTestFixture
  14. : public UnitTest::LeakDetectionFixture
  15. {};
  16. TEST_F(CharconvTestFixture, ToChars_CanConvertIntegralValue_Success)
  17. {
  18. constexpr int8_t int8Value = -2;
  19. constexpr uint8_t uint8Value = 2;
  20. constexpr int16_t int16Value = -256 - 2;
  21. constexpr uint16_t uint16Value = 256 + 2;
  22. constexpr int32_t int32Value = -65536 - 2;
  23. constexpr uint32_t uint32Value = 65536 + 2;
  24. constexpr int64_t int64Value = -4294967296 - 2;
  25. constexpr uint64_t uint64Value = 4294967296 + 2;
  26. {
  27. char convertedInt[32];
  28. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  29. AZStd::ranges::end(convertedInt), int8Value);
  30. EXPECT_EQ(AZStd::errc{}, result.ec);
  31. // Null-terminate the buffer
  32. // Assert that the result ptr not outside of the convertedInt array range
  33. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  34. *result.ptr = '\0';
  35. EXPECT_STREQ("-2", convertedInt);
  36. }
  37. {
  38. char convertedInt[32];
  39. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  40. AZStd::ranges::end(convertedInt), uint8Value);
  41. EXPECT_EQ(AZStd::errc{}, result.ec);
  42. // Null-terminate the buffer
  43. // Assert that the result ptr not outside of the convertedInt array range
  44. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  45. *result.ptr = '\0';
  46. EXPECT_STREQ("2", convertedInt);
  47. }
  48. {
  49. char convertedInt[32];
  50. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  51. AZStd::ranges::end(convertedInt), int16Value);
  52. EXPECT_EQ(AZStd::errc{}, result.ec);
  53. // Null-terminate the buffer
  54. // Assert that the result ptr not outside of the convertedInt array range
  55. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  56. *result.ptr = '\0';
  57. EXPECT_STREQ("-258", convertedInt);
  58. }
  59. {
  60. char convertedInt[32];
  61. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  62. AZStd::ranges::end(convertedInt), uint16Value);
  63. EXPECT_EQ(AZStd::errc{}, result.ec);
  64. // Null-terminate the buffer
  65. // Assert that the result ptr not outside of the convertedInt array range
  66. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  67. *result.ptr = '\0';
  68. EXPECT_STREQ("258", convertedInt);
  69. }
  70. {
  71. char convertedInt[32];
  72. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  73. AZStd::ranges::end(convertedInt), int32Value);
  74. EXPECT_EQ(AZStd::errc{}, result.ec);
  75. // Null-terminate the buffer
  76. // Assert that the result ptr not outside of the convertedInt array range
  77. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  78. *result.ptr = '\0';
  79. EXPECT_STREQ("-65538", convertedInt);
  80. }
  81. {
  82. char convertedInt[32];
  83. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  84. AZStd::ranges::end(convertedInt), uint32Value);
  85. EXPECT_EQ(AZStd::errc{}, result.ec);
  86. // Null-terminate the buffer
  87. // Assert that the result ptr not outside of the convertedInt array range
  88. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  89. *result.ptr = '\0';
  90. EXPECT_STREQ("65538", convertedInt);
  91. }
  92. {
  93. char convertedInt[32];
  94. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  95. AZStd::ranges::end(convertedInt), int64Value);
  96. EXPECT_EQ(AZStd::errc{}, result.ec);
  97. // Null-terminate the buffer
  98. // Assert that the result ptr not outside of the convertedInt array range
  99. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  100. *result.ptr = '\0';
  101. EXPECT_STREQ("-4294967298", convertedInt);
  102. }
  103. {
  104. char convertedInt[32];
  105. AZStd::to_chars_result result = AZStd::to_chars(AZStd::ranges::begin(convertedInt),
  106. AZStd::ranges::end(convertedInt), uint64Value);
  107. EXPECT_EQ(AZStd::errc{}, result.ec);
  108. // Null-terminate the buffer
  109. // Assert that the result ptr not outside of the convertedInt array range
  110. ASSERT_LT(result.ptr - convertedInt, AZStd::ranges::size(convertedInt));
  111. *result.ptr = '\0';
  112. EXPECT_STREQ("4294967298", convertedInt);
  113. }
  114. }
  115. TEST_F(CharconvTestFixture, FromChars_CanConvertString_Success)
  116. {
  117. {
  118. int8_t intValue{};
  119. AZStd::string_view numberString = "-2";
  120. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  121. numberString.data() + numberString.size(), intValue);
  122. EXPECT_EQ(AZStd::errc{}, result.ec);
  123. EXPECT_EQ(numberString.end(), result.ptr);
  124. EXPECT_EQ(-2, intValue);
  125. }
  126. {
  127. uint8_t intValue{};
  128. AZStd::string_view numberString = "2";
  129. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  130. numberString.data() + numberString.size(), intValue);
  131. EXPECT_EQ(AZStd::errc{}, result.ec);
  132. EXPECT_EQ(numberString.end(), result.ptr);
  133. EXPECT_EQ(2, intValue);
  134. }
  135. {
  136. int16_t intValue{};
  137. AZStd::string_view numberString = "-258";
  138. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  139. numberString.data() + numberString.size(), intValue);
  140. EXPECT_EQ(AZStd::errc{}, result.ec);
  141. EXPECT_EQ(numberString.end(), result.ptr);
  142. EXPECT_EQ(-258, intValue);
  143. }
  144. {
  145. uint16_t intValue{};
  146. AZStd::string_view numberString = "258";
  147. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  148. numberString.data() + numberString.size(), intValue);
  149. EXPECT_EQ(AZStd::errc{}, result.ec);
  150. EXPECT_EQ(numberString.end(), result.ptr);
  151. EXPECT_EQ(258, intValue);
  152. }
  153. {
  154. int32_t intValue{};
  155. AZStd::string_view numberString = "-65538";
  156. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  157. numberString.data() + numberString.size(), intValue);
  158. EXPECT_EQ(AZStd::errc{}, result.ec);
  159. EXPECT_EQ(numberString.end(), result.ptr);
  160. EXPECT_EQ(-65538, intValue);
  161. }
  162. {
  163. uint32_t intValue{};
  164. AZStd::string_view numberString = "65538";
  165. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  166. numberString.data() + numberString.size(), intValue);
  167. EXPECT_EQ(AZStd::errc{}, result.ec);
  168. EXPECT_EQ(numberString.end(), result.ptr);
  169. EXPECT_EQ(65538, intValue);
  170. }
  171. {
  172. int64_t intValue{};
  173. AZStd::string_view numberString = "-4294967298";
  174. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  175. numberString.data() + numberString.size(), intValue);
  176. EXPECT_EQ(AZStd::errc{}, result.ec);
  177. EXPECT_EQ(numberString.end(), result.ptr);
  178. EXPECT_EQ(-4294967298, intValue);
  179. }
  180. {
  181. uint64_t intValue{};
  182. AZStd::string_view numberString = "4294967298";
  183. AZStd::from_chars_result result = AZStd::from_chars(numberString.data(),
  184. numberString.data() + numberString.size(), intValue);
  185. EXPECT_EQ(AZStd::errc{}, result.ec);
  186. EXPECT_EQ(numberString.end(), result.ptr);
  187. EXPECT_EQ(4294967298, intValue);
  188. }
  189. }
  190. }