1 change: 1 addition & 0 deletions libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_subdirectory(inttypes)
add_subdirectory(math)
add_subdirectory(search)
add_subdirectory(stdbit)
add_subdirectory(stdfix)
add_subdirectory(stdio)
add_subdirectory(stdlib)
add_subdirectory(string)
Expand Down
37 changes: 37 additions & 0 deletions libc/test/src/stdfix/AbsTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- Utility class to test fixed-point abs -------------------*- 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
//
//===----------------------------------------------------------------------===//

#include "test/UnitTest/Test.h"

#include "src/__support/fixed_point/fx_rep.h"

template <typename T> class AbsTest : public LIBC_NAMESPACE::testing::Test {

using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
static constexpr T zero = FXRep::ZERO();
static constexpr T min = FXRep::MIN();
static constexpr T max = FXRep::MAX();
static constexpr T half = static_cast<T>(0.5);
static constexpr T neg_half = static_cast<T>(-0.5);

public:
typedef T (*AbsFunc)(T);

void testSpecialNumbers(AbsFunc func) {
EXPECT_EQ(zero, func(zero));
EXPECT_EQ(max, func(min));
EXPECT_EQ(max, func(max));
EXPECT_EQ(half, func(half));
EXPECT_EQ(half, func(neg_half));
}
};

#define LIST_ABS_TESTS(T, func) \
using LlvmLibcAbsTest = AbsTest<T>; \
TEST_F(LlvmLibcAbsTest, SpecialNumbers) { testSpecialNumbers(&func); } \
static_assert(true, "Require semicolon.")
23 changes: 23 additions & 0 deletions libc/test/src/stdfix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
if(NOT LIBC_COMPILER_HAS_FIXED_POINT)
return()
endif()

add_custom_target(libc-stdfix-tests)

foreach(suffix IN ITEMS hr r lr hk k lk)
add_libc_test(
abs${suffix}_test
SUITE
libc-stdfix-tests
HDRS
AbsTest.h
SRCS
abs${suffix}_test.cpp
COMPILE_OPTIONS
-O3
-ffixed-point
DEPENDS
libc.src.stdfix.abs${suffix}
libc.src.__support.fixed_point.fx_bits
)
endforeach()
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/abshk_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for abshk -----------------------------------------------===//
//
// 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 "AbsTest.h"

#include "src/stdfix/abshk.h"

LIST_ABS_TESTS(short accum, LIBC_NAMESPACE::abshk);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/abshr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for abshr -----------------------------------------------===//
//
// 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 "AbsTest.h"

#include "src/stdfix/abshr.h"

LIST_ABS_TESTS(short fract, LIBC_NAMESPACE::abshr);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/absk_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for absk ------------------------------------------------===//
//
// 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 "AbsTest.h"

#include "src/stdfix/absk.h"

LIST_ABS_TESTS(accum, LIBC_NAMESPACE::absk);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/abslk_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for abslk -----------------------------------------------===//
//
// 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 "AbsTest.h"

#include "src/stdfix/abslk.h"

LIST_ABS_TESTS(long accum, LIBC_NAMESPACE::abslk);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/abslr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for abslr -----------------------------------------------===//
//
// 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 "AbsTest.h"

#include "src/stdfix/abslr.h"

LIST_ABS_TESTS(long fract, LIBC_NAMESPACE::abslr);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/absr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for absr ------------------------------------------------===//
//
// 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 "AbsTest.h"

#include "src/stdfix/absr.h"

LIST_ABS_TESTS(fract, LIBC_NAMESPACE::absr);