1 //===-- Unittests for atof ------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/__support/FPUtil/FPBits.h" 10 #include "src/stdlib/atof.h" 11 12 #include "utils/UnitTest/Test.h" 13 14 #include <errno.h> 15 #include <limits.h> 16 #include <stddef.h> 17 18 // This is just a simple test to make sure that this function works at all. It's 19 // functionally identical to strtod so the bulk of the testing is there. 20 TEST(LlvmLibcAToFTest, SimpleTest) { 21 __llvm_libc::fputil::FPBits<double> expectedFP = 22 __llvm_libc::fputil::FPBits<double>(uint64_t(0x405ec00000000000)); 23 24 errno = 0; 25 double result = __llvm_libc::atof("123"); 26 27 __llvm_libc::fputil::FPBits<double> actualFP = 28 __llvm_libc::fputil::FPBits<double>(result); 29 30 EXPECT_EQ(actualFP.bits, expectedFP.bits); 31 EXPECT_EQ(actualFP.getSign(), expectedFP.getSign()); 32 EXPECT_EQ(actualFP.getExponent(), expectedFP.getExponent()); 33 EXPECT_EQ(actualFP.getMantissa(), expectedFP.getMantissa()); 34 EXPECT_EQ(errno, 0); 35 } 36 37 TEST(LlvmLibcAToFTest, FailedParsingTest) { 38 __llvm_libc::fputil::FPBits<double> expectedFP = 39 __llvm_libc::fputil::FPBits<double>(uint64_t(0)); 40 41 errno = 0; 42 double result = __llvm_libc::atof("???"); 43 44 __llvm_libc::fputil::FPBits<double> actualFP = 45 __llvm_libc::fputil::FPBits<double>(result); 46 47 EXPECT_EQ(actualFP.bits, expectedFP.bits); 48 EXPECT_EQ(actualFP.getSign(), expectedFP.getSign()); 49 EXPECT_EQ(actualFP.getExponent(), expectedFP.getExponent()); 50 EXPECT_EQ(actualFP.getMantissa(), expectedFP.getMantissa()); 51 EXPECT_EQ(errno, 0); 52 } 53