Skip to content

Commit 7099e88

Browse files
committed
add: tests for bitsu{fx} and make the code consistent with {fx}bits
Signed-off-by: krishna2803 <[email protected]>
1 parent 752ea6d commit 7099e88

File tree

13 files changed

+131
-45
lines changed

13 files changed

+131
-45
lines changed

libc/test/src/stdfix/BitsFxTest.h

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,47 @@
1010

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

13-
template <typename From, typename To>
13+
template <typename T, typename XType>
1414
class BitsFxTest : public LIBC_NAMESPACE::testing::Test {
1515

16-
using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<From>;
17-
static constexpr From zero = FXRep::ZERO();
18-
static constexpr From max = FXRep::MAX();
19-
static constexpr From min = FXRep::MIN();
20-
static constexpr From one_half = FXRep::ONE_HALF();
21-
static constexpr From one_fourth = FXRep::ONE_FOURTH();
22-
static constexpr From eps = FXRep::EPS();
16+
using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
17+
static constexpr T zero = FXRep::ZERO();
18+
static constexpr T max = FXRep::MAX();
19+
static constexpr T min = FXRep::MIN();
20+
static constexpr T one_half = FXRep::ONE_HALF();
21+
static constexpr T one_fourth = FXRep::ONE_FOURTH();
22+
static constexpr T eps = FXRep::EPS();
23+
2324
// (0.42)_10 =
2425
// (0.0110101110000101000111101011100001010001111010111000010100011110)_2 =
2526
// (0.0x6b851eb851eb851e)_16
2627
static constexpr unsigned long long zero_point_forty_two =
27-
0x6b851eb851eb851eull;
28-
29-
static constexpr unsigned long long maxval = ~(1ULL << FXRep::VALUE_LEN);
30-
static constexpr unsigned long long minval = -(maxval + 1ULL);
28+
0x6b851eb851eb851eULL;
3129

3230
public:
33-
typedef To (*BitsFxFunc)(From);
31+
typedef XType (*BitsFxFunc)(T);
3432

3533
void testSpecialNumbers(BitsFxFunc func) {
36-
EXPECT_EQ(static_cast<To>(0), func(zero));
37-
EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 1)), func(one_half));
38-
EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 2)),
34+
EXPECT_EQ(static_cast<XType>(0), func(zero));
35+
EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 1)),
36+
func(one_half));
37+
EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 2)),
3938
func(one_fourth));
40-
EXPECT_EQ(static_cast<To>(1), func(eps));
41-
EXPECT_EQ(static_cast<To>(maxval), func(max));
42-
EXPECT_EQ(static_cast<To>(minval), func(min));
39+
EXPECT_EQ(static_cast<XType>(1), func(eps));
4340

4441
// (0.6875)_10 = (0.1011)_2
45-
EXPECT_EQ(static_cast<To>(11 << (FXRep::FRACTION_LEN - 4)), func(0.6875));
42+
EXPECT_EQ(static_cast<XType>(11ULL << (FXRep::FRACTION_LEN - 4)),
43+
func(0.6875));
4644

4745
EXPECT_EQ(
48-
static_cast<To>(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)),
46+
static_cast<XType>(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)),
4947
func(0.42));
50-
51-
// EXPECT_EQ(static_cast<To>(0), func(5));
52-
53-
// if constexpr (static_cast<int>(min) <= -16)
54-
// EXPECT_EQ(static_cast<To>(0), func(-16));
55-
56-
// if constexpr (static_cast<int>(min) <= -10)
57-
// EXPECT_EQ(static_cast<To>(0), func(-10));
58-
59-
// EXPECT_EQ(static_cast<To>(), func(max));
60-
61-
// if constexpr (static_cast<int>(max) >= 16.25)
62-
// EXPECT_EQ(static_cast<To>(0), func(16.25));
63-
64-
// if constexpr (static_cast<int>(max) > -10)
65-
// EXPECT_EQ(static_cast<To>(0), func(16));
6648
}
6749
};
6850

69-
#define LIST_BITSFX_TESTS(From, To, func) \
70-
using LlvmLibcBitsFxTest = BitsFxTest<From, To>; \
71-
TEST_F(LlvmLibcBitsFxTest, SpecialNumbers) { testSpecialNumbers(&func); } \
51+
#define LIST_BITSFX_TESTS(Name, T, XType, func) \
52+
using LlvmLibcBits##Name##Test = BitsFxTest<T, XType>; \
53+
TEST_F(LlvmLibcBits##Name##Test, SpecialNumbers) { \
54+
testSpecialNumbers(&func); \
55+
} \
7256
static_assert(true, "Require semicolon.")

libc/test/src/stdfix/bitshk_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#include "llvm-libc-types/stdfix-types.h" // int_hk_t
1212
#include "src/stdfix/bitshk.h"
1313

14-
LIST_BITSFX_TESTS(short accum, int_hk_t, LIBC_NAMESPACE::bitshk);
14+
LIST_BITSFX_TESTS(hk, short accum, int_hk_t, LIBC_NAMESPACE::bitshk);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitshr ----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // int_hr_t
12+
#include "src/stdfix/bitshr.h"
13+
14+
LIST_BITSFX_TESTS(hr, short fract, int_hr_t, LIBC_NAMESPACE::bitshr);

libc/test/src/stdfix/bitsk_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#include "llvm-libc-types/stdfix-types.h" // int_k_t
1212
#include "src/stdfix/bitsk.h"
1313

14-
LIST_BITSFX_TESTS(accum, int_k_t, LIBC_NAMESPACE::bitsk);
14+
LIST_BITSFX_TESTS(k, accum, int_k_t, LIBC_NAMESPACE::bitsk);

libc/test/src/stdfix/bitslk_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#include "llvm-libc-types/stdfix-types.h" // int_lk_t
1212
#include "src/stdfix/bitslk.h"
1313

14-
LIST_BITSFX_TESTS(long accum, int_lk_t, LIBC_NAMESPACE::bitslk);
14+
LIST_BITSFX_TESTS(lk, long accum, int_lk_t, LIBC_NAMESPACE::bitslk);

libc/test/src/stdfix/bitslr_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#include "llvm-libc-types/stdfix-types.h" // int_lr_t
1212
#include "src/stdfix/bitslr.h"
1313

14-
LIST_BITSFX_TESTS(long fract, int_lr_t, LIBC_NAMESPACE::bitslr);
14+
LIST_BITSFX_TESTS(hk, long fract, int_lr_t, LIBC_NAMESPACE::bitslr);

libc/test/src/stdfix/bitsr_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#include "llvm-libc-types/stdfix-types.h" // int_r_t
1212
#include "src/stdfix/bitsr.h"
1313

14-
LIST_BITSFX_TESTS(fract, int_r_t, LIBC_NAMESPACE::bitsr);
14+
LIST_BITSFX_TESTS(r, fract, int_r_t, LIBC_NAMESPACE::bitsr);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- Unittests for bitsuhk ---------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // uint_uhk_t
12+
#include "src/stdfix/bitsuhk.h"
13+
14+
LIST_BITSFX_TESTS(uhk, unsigned short accum, uint_uhk_t,
15+
LIBC_NAMESPACE::bitsuhk);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- Unittests for bitsuhr ---------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // uint_uhr_t
12+
#include "src/stdfix/bitsuhr.h"
13+
14+
LIST_BITSFX_TESTS(uhr, unsigned short fract, uint_uhr_t,
15+
LIBC_NAMESPACE::bitsuhr);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitsuk ----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // uint_uk_t
12+
#include "src/stdfix/bitsuk.h"
13+
14+
LIST_BITSFX_TESTS(uk, unsigned accum, uint_uk_t, LIBC_NAMESPACE::bitsuk);

0 commit comments

Comments
 (0)