60 changes: 60 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,66 @@ add_fp_unittest(
libc.src.__support.integer_literals
)

add_fp_unittest(
iscanonical_test
SUITE
libc-math-smoke-tests
SRCS
iscanonical_test.cpp
HDRS
IsCanonicalTest.h
DEPENDS
libc.src.math.iscanonical
)

add_fp_unittest(
iscanonicalf_test
SUITE
libc-math-smoke-tests
SRCS
iscanonicalf_test.cpp
HDRS
IsCanonicalTest.h
DEPENDS
libc.src.math.iscanonicalf
)

add_fp_unittest(
iscanonicall_test
SUITE
libc-math-smoke-tests
SRCS
iscanonicall_test.cpp
HDRS
IsCanonicalTest.h
DEPENDS
libc.src.math.iscanonicall
)

add_fp_unittest(
iscanonicalf16_test
SUITE
libc-math-smoke-tests
SRCS
iscanonicalf16_test.cpp
HDRS
IsCanonicalTest.h
DEPENDS
libc.src.math.iscanonicalf16
)

add_fp_unittest(
iscanonicalf128_test
SUITE
libc-math-smoke-tests
SRCS
iscanonicalf128_test.cpp
HDRS
IsCanonicalTest.h
DEPENDS
libc.src.math.iscanonicalf128
)

add_fp_unittest(
ceil_test
SUITE
Expand Down
58 changes: 58 additions & 0 deletions libc/test/src/math/smoke/IsCanonicalTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===-- Utility class to test different flavors of iscanonical --*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_ISCANONICALTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_ISCANONICALTEST_H

#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

template <typename T>
class IsCanonicalTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {

DECLARE_SPECIAL_CONSTANTS(T)

public:
typedef int (*IsCanonicalFunc)(T);

void testSpecialNumbers(IsCanonicalFunc func) {
EXPECT_EQ(func(aNaN), 1);
EXPECT_EQ(func(neg_aNaN), 1);
EXPECT_EQ(func(sNaN), 0);
EXPECT_EQ(func(neg_sNaN), 0);
EXPECT_EQ(func(inf), 1);
EXPECT_EQ(func(neg_inf), 1);
EXPECT_EQ(func(min_normal), 1);
EXPECT_EQ(func(max_normal), 1);
EXPECT_EQ(func(neg_max_normal), 1);
EXPECT_EQ(func(min_denormal), 1);
EXPECT_EQ(func(neg_min_denormal), 1);
EXPECT_EQ(func(max_denormal), 1);
EXPECT_EQ(func(zero), 1);
EXPECT_EQ(func(neg_zero), 1);
}

void testRoundedNumbers(IsCanonicalFunc func) {
EXPECT_EQ(func(T(1.0)), 1);
EXPECT_EQ(func(T(-1.0)), 1);
EXPECT_EQ(func(T(10.0)), 1);
EXPECT_EQ(func(T(-10.0)), 1);
EXPECT_EQ(func(T(1234.0)), 1);
EXPECT_EQ(func(T(-1234.0)), 1);
}
};

#define LIST_ISCANONICAL_TESTS(T, func) \
using LlvmLibcIsCanonicalTest = IsCanonicalTest<T>; \
TEST_F(LlvmLibcIsCanonicalTest, SpecialNumbers) { \
testSpecialNumbers(&func); \
} \
TEST_F(LlvmLibcIsCanonicalTest, RoundedNubmers) { testRoundedNumbers(&func); }

#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_ISCANONICALTEST_H
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/iscanonical_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for iscanonical -----------------------------------------===//
//
// 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 "IsCanonicalTest.h"

#include "src/math/iscanonical.h"

LIST_ISCANONICAL_TESTS(double, LIBC_NAMESPACE::iscanonical)
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/iscanonicalf128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for iscanonicalf128 -------------------------------------===//
//
// 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 "IsCanonicalTest.h"

#include "src/math/iscanonicalf128.h"

LIST_ISCANONICAL_TESTS(float128, LIBC_NAMESPACE::iscanonicalf128)
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/iscanonicalf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for iscanonicalf16 --------------------------------------===//
//
// 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 "IsCanonicalTest.h"

#include "src/math/iscanonicalf16.h"

LIST_ISCANONICAL_TESTS(float16, LIBC_NAMESPACE::iscanonicalf16)
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/iscanonicalf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for iscanonicalf ----------------------------------------===//
//
// 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 "IsCanonicalTest.h"

#include "src/math/iscanonicalf.h"

LIST_ISCANONICAL_TESTS(float, LIBC_NAMESPACE::iscanonicalf)
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/iscanonicall_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for iscanonicall ----------------------------------------===//
//
// 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 "IsCanonicalTest.h"

#include "src/math/iscanonicall.h"

LIST_ISCANONICAL_TESTS(long double, LIBC_NAMESPACE::iscanonicall)