diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt --- a/libc/test/src/stdlib/CMakeLists.txt +++ b/libc/test/src/stdlib/CMakeLists.txt @@ -88,6 +88,30 @@ libc.src.errno.errno ) +add_libc_test( + strtoint32_test + SUITE + libc-stdlib-tests + SRCS + strtoint32_test.cpp + DEPENDS + libc.src.__support.str_to_integer + libc.src.errno.errno + .strtol_test_support +) + +add_libc_test( + strtoint64_test + SUITE + libc-stdlib-tests + SRCS + strtoint64_test.cpp + DEPENDS + libc.src.__support.str_to_integer + libc.src.errno.errno + .strtol_test_support +) + add_libc_test( strtol_test SUITE diff --git a/libc/test/src/stdlib/StrtolTest.h b/libc/test/src/stdlib/StrtolTest.h --- a/libc/test/src/stdlib/StrtolTest.h +++ b/libc/test/src/stdlib/StrtolTest.h @@ -325,7 +325,8 @@ ((is_signed_v && sizeof(ReturnT) == 4) ? T_MAX : ReturnT(0xFFFFFFFF))); - ASSERT_EQ(libc_errno, 0); + ASSERT_EQ(libc_errno, + is_signed_v && sizeof(ReturnT) == 4 ? ERANGE : 0); EXPECT_EQ(str_end - max_32_bit_value, ptrdiff_t(10)); const char *negative_max_32_bit_value = "-0xFFFFFFFF"; @@ -334,7 +335,8 @@ ((is_signed_v && sizeof(ReturnT) == 4) ? T_MIN : -ReturnT(0xFFFFFFFF))); - ASSERT_EQ(libc_errno, 0); + ASSERT_EQ(libc_errno, + is_signed_v && sizeof(ReturnT) == 4 ? ERANGE : 0); EXPECT_EQ(str_end - negative_max_32_bit_value, ptrdiff_t(11)); // Max size for signed 32 bit numbers @@ -357,30 +359,41 @@ const char *max_64_bit_value = "0xFFFFFFFFFFFFFFFF"; libc_errno = 0; ASSERT_EQ(func(max_64_bit_value, &str_end, 0), - (is_signed_v ? T_MAX : ReturnT(0xFFFFFFFFFFFFFFFF))); - ASSERT_EQ(libc_errno, (is_signed_v ? ERANGE : 0)); + (is_signed_v || sizeof(ReturnT) < 8 + ? T_MAX + : ReturnT(0xFFFFFFFFFFFFFFFF))); + ASSERT_EQ(libc_errno, + (is_signed_v || sizeof(ReturnT) < 8 ? ERANGE : 0)); EXPECT_EQ(str_end - max_64_bit_value, ptrdiff_t(18)); + // See the end of CleanBase10Decode for an explanation of how this large + // negative number can end up as T_MAX. const char *negative_max_64_bit_value = "-0xFFFFFFFFFFFFFFFF"; libc_errno = 0; - ASSERT_EQ(func(negative_max_64_bit_value, &str_end, 0), - (is_signed_v ? T_MIN : -ReturnT(0xFFFFFFFFFFFFFFFF))); - ASSERT_EQ(libc_errno, (is_signed_v ? ERANGE : 0)); + ASSERT_EQ( + func(negative_max_64_bit_value, &str_end, 0), + (is_signed_v + ? T_MIN + : (sizeof(ReturnT) < 8 ? T_MAX : -ReturnT(0xFFFFFFFFFFFFFFFF)))); + ASSERT_EQ(libc_errno, + (is_signed_v || sizeof(ReturnT) < 8 ? ERANGE : 0)); EXPECT_EQ(str_end - negative_max_64_bit_value, ptrdiff_t(19)); // Max size for signed 64 bit numbers const char *max_63_bit_value = "0x7FFFFFFFFFFFFFFF"; libc_errno = 0; - ASSERT_EQ(func(max_63_bit_value, &str_end, 0), ReturnT(0x7FFFFFFFFFFFFFFF)); - ASSERT_EQ(libc_errno, 0); + ASSERT_EQ(func(max_63_bit_value, &str_end, 0), + (sizeof(ReturnT) < 8 ? T_MAX : ReturnT(0x7FFFFFFFFFFFFFFF))); + ASSERT_EQ(libc_errno, sizeof(ReturnT) < 8 ? ERANGE : 0); EXPECT_EQ(str_end - max_63_bit_value, ptrdiff_t(18)); const char *negative_max_63_bit_value = "-0x7FFFFFFFFFFFFFFF"; libc_errno = 0; ASSERT_EQ(func(negative_max_63_bit_value, &str_end, 0), - -ReturnT(0x7FFFFFFFFFFFFFFF)); - ASSERT_EQ(libc_errno, 0); + (sizeof(ReturnT) >= 8 ? -ReturnT(0x7FFFFFFFFFFFFFFF) + : (is_signed_v ? T_MIN : T_MAX))); + ASSERT_EQ(libc_errno, sizeof(ReturnT) < 8 ? ERANGE : 0); EXPECT_EQ(str_end - negative_max_63_bit_value, ptrdiff_t(19)); } diff --git a/libc/test/src/stdlib/atoi_test.cpp b/libc/test/src/stdlib/atoi_test.cpp --- a/libc/test/src/stdlib/atoi_test.cpp +++ b/libc/test/src/stdlib/atoi_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for atoi -----------------------------------------------===// +//===-- Unittests for atoi ------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/libc/test/src/stdlib/atol_test.cpp b/libc/test/src/stdlib/atol_test.cpp --- a/libc/test/src/stdlib/atol_test.cpp +++ b/libc/test/src/stdlib/atol_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for atol -----------------------------------------------===// +//===-- Unittests for atol ------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/libc/test/src/stdlib/strtod_test.cpp b/libc/test/src/stdlib/strtod_test.cpp --- a/libc/test/src/stdlib/strtod_test.cpp +++ b/libc/test/src/stdlib/strtod_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for strtod ---------------------------------------------===// +//===-- Unittests for strtod ----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/libc/test/src/stdlib/strtof_test.cpp b/libc/test/src/stdlib/strtof_test.cpp --- a/libc/test/src/stdlib/strtof_test.cpp +++ b/libc/test/src/stdlib/strtof_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for strtof ---------------------------------------------===// +//===-- Unittests for strtof ----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/libc/test/src/stdlib/strtoint32_test.cpp b/libc/test/src/stdlib/strtoint32_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/stdlib/strtoint32_test.cpp @@ -0,0 +1,45 @@ +//===-- Unittests for strtoint32 ------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include + +#include "src/__support/str_to_integer.h" +#include "src/errno/libc_errno.h" + +#include "StrtolTest.h" +#include "test/UnitTest/Test.h" + +namespace __llvm_libc { + +int32_t strtoint32(const char *__restrict str, char **__restrict str_end, + int base) { + auto result = internal::strtointeger(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast(str + result.parsed_len); + + return result; +} + +uint32_t strtouint32(const char *__restrict str, char **__restrict str_end, + int base) { + auto result = internal::strtointeger(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast(str + result.parsed_len); + + return result; +} +} // namespace __llvm_libc + +STRTOL_TEST(Strtoint32, __llvm_libc::strtoint32) +STRTOL_TEST(Strtouint32, __llvm_libc::strtouint32) diff --git a/libc/test/src/stdlib/strtoint64_test.cpp b/libc/test/src/stdlib/strtoint64_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/stdlib/strtoint64_test.cpp @@ -0,0 +1,45 @@ +//===-- Unittests for strtoint64 ------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include + +#include "src/__support/str_to_integer.h" +#include "src/errno/libc_errno.h" + +#include "StrtolTest.h" +#include "test/UnitTest/Test.h" + +namespace __llvm_libc { + +int64_t strtoint64(const char *__restrict str, char **__restrict str_end, + int base) { + auto result = internal::strtointeger(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast(str + result.parsed_len); + + return result; +} + +uint64_t strtouint64(const char *__restrict str, char **__restrict str_end, + int base) { + auto result = internal::strtointeger(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast(str + result.parsed_len); + + return result; +} +} // namespace __llvm_libc + +STRTOL_TEST(Strtoint64, __llvm_libc::strtoint64) +STRTOL_TEST(Strtouint64, __llvm_libc::strtouint64) diff --git a/libc/test/src/stdlib/strtol_test.cpp b/libc/test/src/stdlib/strtol_test.cpp --- a/libc/test/src/stdlib/strtol_test.cpp +++ b/libc/test/src/stdlib/strtol_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for strtol ---------------------------------------------===// +//===-- Unittests for strtol ----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information.