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.
TEST(LlvmLibcAToFTest,SimpleTest)20 TEST(LlvmLibcAToFTest, SimpleTest) {
21   __llvm_libc::fputil::FPBits<double> expected_fp =
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> actual_fp =
28       __llvm_libc::fputil::FPBits<double>(result);
29 
30   EXPECT_EQ(actual_fp.bits, expected_fp.bits);
31   EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
32   EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
33   EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
34   EXPECT_EQ(errno, 0);
35 }
36 
TEST(LlvmLibcAToFTest,FailedParsingTest)37 TEST(LlvmLibcAToFTest, FailedParsingTest) {
38   __llvm_libc::fputil::FPBits<double> expected_fp =
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> actual_fp =
45       __llvm_libc::fputil::FPBits<double>(result);
46 
47   EXPECT_EQ(actual_fp.bits, expected_fp.bits);
48   EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
49   EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
50   EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
51   EXPECT_EQ(errno, 0);
52 }
53