60 changes: 31 additions & 29 deletions libc/test/src/math/LdExpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
using FPBits = __llvm_libc::fputil::FPBits<T>;
using NormalFloat = __llvm_libc::fputil::NormalFloat<T>;
using UIntType = typename FPBits::UIntType;
static constexpr UIntType mantissaWidth =
static constexpr UIntType MANTISSA_WIDTH =
__llvm_libc::fputil::MantissaWidth<T>::VALUE;
// A normalized mantissa to be used with tests.
static constexpr UIntType mantissa = NormalFloat::ONE + 0x1234;
static constexpr UIntType MANTISSA = NormalFloat::ONE + 0x1234;

const T zero = T(__llvm_libc::fputil::FPBits<T>::zero());
const T neg_zero = T(__llvm_libc::fputil::FPBits<T>::neg_zero());
Expand All @@ -38,8 +38,8 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
typedef T (*LdExpFunc)(T, int);

void testSpecialNumbers(LdExpFunc func) {
int expArray[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX};
for (int exp : expArray) {
int exp_array[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX};
for (int exp : exp_array) {
ASSERT_FP_EQ(zero, func(zero, exp));
ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));
ASSERT_FP_EQ(inf, func(inf, exp));
Expand All @@ -49,10 +49,10 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
}

void testPowersOfTwo(LdExpFunc func) {
int32_t expArray[5] = {1, 2, 3, 4, 5};
int32_t valArray[6] = {1, 2, 4, 8, 16, 32};
for (int32_t exp : expArray) {
for (int32_t val : valArray) {
int32_t exp_array[5] = {1, 2, 3, 4, 5};
int32_t val_array[6] = {1, 2, 4, 8, 16, 32};
for (int32_t exp : exp_array) {
for (int32_t val : val_array) {
ASSERT_FP_EQ(T(val << exp), func(T(val), exp));
ASSERT_FP_EQ(T(-1 * (val << exp)), func(T(-val), exp));
}
Expand All @@ -70,37 +70,39 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
void testUnderflowToZeroOnNormal(LdExpFunc func) {
// In this test, we pass a normal nubmer to func and expect zero
// to be returned due to underflow.
int32_t baseExponent = FPBits::EXPONENT_BIAS + mantissaWidth;
int32_t expArray[] = {baseExponent + 5, baseExponent + 4, baseExponent + 3,
baseExponent + 2, baseExponent + 1};
T x = NormalFloat(0, mantissa, 0);
for (int32_t exp : expArray) {
int32_t base_exponent = FPBits::EXPONENT_BIAS + MANTISSA_WIDTH;
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
base_exponent + 3, base_exponent + 2,
base_exponent + 1};
T x = NormalFloat(0, MANTISSA, 0);
for (int32_t exp : exp_array) {
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
}
}

void testUnderflowToZeroOnSubnormal(LdExpFunc func) {
// In this test, we pass a normal nubmer to func and expect zero
// to be returned due to underflow.
int32_t baseExponent = FPBits::EXPONENT_BIAS + mantissaWidth;
int32_t expArray[] = {baseExponent + 5, baseExponent + 4, baseExponent + 3,
baseExponent + 2, baseExponent + 1};
T x = NormalFloat(-FPBits::EXPONENT_BIAS, mantissa, 0);
for (int32_t exp : expArray) {
int32_t base_exponent = FPBits::EXPONENT_BIAS + MANTISSA_WIDTH;
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
base_exponent + 3, base_exponent + 2,
base_exponent + 1};
T x = NormalFloat(-FPBits::EXPONENT_BIAS, MANTISSA, 0);
for (int32_t exp : exp_array) {
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
}
}

void testNormalOperation(LdExpFunc func) {
T valArray[] = {
T val_array[] = {
// Normal numbers
NormalFloat(100, mantissa, 0), NormalFloat(-100, mantissa, 0),
NormalFloat(100, mantissa, 1), NormalFloat(-100, mantissa, 1),
NormalFloat(100, MANTISSA, 0), NormalFloat(-100, MANTISSA, 0),
NormalFloat(100, MANTISSA, 1), NormalFloat(-100, MANTISSA, 1),
// Subnormal numbers
NormalFloat(-FPBits::EXPONENT_BIAS, mantissa, 0),
NormalFloat(-FPBits::EXPONENT_BIAS, mantissa, 1)};
for (int32_t exp = 0; exp <= static_cast<int32_t>(mantissaWidth); ++exp) {
for (T x : valArray) {
NormalFloat(-FPBits::EXPONENT_BIAS, MANTISSA, 0),
NormalFloat(-FPBits::EXPONENT_BIAS, MANTISSA, 1)};
for (int32_t exp = 0; exp <= static_cast<int32_t>(MANTISSA_WIDTH); ++exp) {
for (T x : val_array) {
// We compare the result of ldexp with the result
// of the native multiplication/division instruction.
ASSERT_FP_EQ(func(x, exp), x * (UIntType(1) << exp));
Expand All @@ -118,13 +120,13 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
x = NormalFloat(FPBits::EXPONENT_BIAS, NormalFloat::ONE, 0);
int exp = -FPBits::MAX_EXPONENT - 5;
T result = func(x, exp);
FPBits resultBits(result);
ASSERT_FALSE(resultBits.is_zero());
FPBits result_bits(result);
ASSERT_FALSE(result_bits.is_zero());
// Verify that the result is indeed subnormal.
ASSERT_EQ(resultBits.get_unbiased_exponent(), uint16_t(0));
ASSERT_EQ(result_bits.get_unbiased_exponent(), uint16_t(0));
// But if the exp is so less that normalization leads to zero, then
// the result should be zero.
result = func(x, -FPBits::MAX_EXPONENT - int(mantissaWidth) - 5);
result = func(x, -FPBits::MAX_EXPONENT - int(MANTISSA_WIDTH) - 5);
ASSERT_TRUE(FPBits(result).is_zero());

// Start with a subnormal number but pass a very high number for exponent.
Expand Down
8 changes: 4 additions & 4 deletions libc/test/src/math/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template <typename T> class LogbTest : public __llvm_libc::testing::Test {

DECLARE_SPECIAL_CONSTANTS(T)

static constexpr UIntType HiddenBit =
static constexpr UIntType HIDDEN_BIT =
UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::VALUE;

public:
Expand Down Expand Up @@ -72,9 +72,9 @@ template <typename T> class LogbTest : public __llvm_libc::testing::Test {

void testRange(LogbFunc func) {
using UIntType = typename FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = static_cast<T>(FPBits(v));
if (isnan(x) || isinf(x) || x == 0.0l)
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/ModfTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ template <typename T> class ModfTest : public __llvm_libc::testing::Test {
}

void testRange(ModfFunc func) {
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x) || x == T(0.0))
continue;
Expand Down
116 changes: 59 additions & 57 deletions libc/test/src/math/NextAfterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class NextAfterTestTemplate : public __llvm_libc::testing::Test {
using MantissaWidth = __llvm_libc::fputil::MantissaWidth<T>;
using UIntType = typename FPBits::UIntType;

static constexpr int bitWidthOfType =
static constexpr int BIT_WIDTH_OF_TYPE =
__llvm_libc::fputil::FloatProperties<T>::BIT_WIDTH;

const T zero = T(FPBits::zero());
const T neg_zero = T(FPBits::neg_zero());
const T inf = T(FPBits::inf());
const T neg_inf = T(FPBits::neg_inf());
const T nan = T(FPBits::build_nan(1));
const UIntType MIN_SUBNORMAL = FPBits::MIN_SUBNORMAL;
const UIntType MAX_SUBNORMAL = FPBits::MAX_SUBNORMAL;
const UIntType MIN_NORMAL = FPBits::MIN_NORMAL;
const UIntType MAX_NORMAL = FPBits::MAX_NORMAL;
const UIntType min_subnormal = FPBits::MIN_SUBNORMAL;
const UIntType max_subnormal = FPBits::MAX_SUBNORMAL;
const UIntType min_normal = FPBits::MIN_NORMAL;
const UIntType max_normal = FPBits::MAX_NORMAL;

public:
typedef T (*NextAfterFunc)(T, T);
Expand All @@ -50,89 +50,91 @@ class NextAfterTestTemplate : public __llvm_libc::testing::Test {
// 'from' is zero|neg_zero.
T x = zero;
T result = func(x, T(1));
UIntType expectedBits = 1;
T expected = *reinterpret_cast<T *>(&expectedBits);
UIntType expected_bits = 1;
T expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, T(-1));
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

x = neg_zero;
result = func(x, 1);
expectedBits = 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, -1);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

// 'from' is max subnormal value.
x = *reinterpret_cast<const T *>(&MAX_SUBNORMAL);
x = *reinterpret_cast<const T *>(&max_subnormal);
result = func(x, 1);
expected = *reinterpret_cast<const T *>(&MIN_NORMAL);
expected = *reinterpret_cast<const T *>(&min_normal);
ASSERT_FP_EQ(result, expected);

result = func(x, 0);
expectedBits = MAX_SUBNORMAL - 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = max_subnormal - 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

x = -x;

result = func(x, -1);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + MIN_NORMAL;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_normal;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, 0);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + MAX_SUBNORMAL - 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits =
(UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal - 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

// 'from' is min subnormal value.
x = *reinterpret_cast<const T *>(&MIN_SUBNORMAL);
x = *reinterpret_cast<const T *>(&min_subnormal);
result = func(x, 1);
expectedBits = MIN_SUBNORMAL + 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = min_subnormal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, 0), 0);

x = -x;
result = func(x, -1);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + MIN_SUBNORMAL + 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits =
(UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_subnormal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, 0), T(-0.0));

// 'from' is min normal.
x = *reinterpret_cast<const T *>(&MIN_NORMAL);
x = *reinterpret_cast<const T *>(&min_normal);
result = func(x, 0);
expectedBits = MAX_SUBNORMAL;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = max_subnormal;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, inf);
expectedBits = MIN_NORMAL + 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = min_normal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

x = -x;
result = func(x, 0);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + MAX_SUBNORMAL;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, -inf);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + MIN_NORMAL + 1;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_normal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);

// 'from' is max normal and 'to' is infinity.
x = *reinterpret_cast<const T *>(&MAX_NORMAL);
x = *reinterpret_cast<const T *>(&max_normal);
result = func(x, inf);
ASSERT_FP_EQ(result, inf);

Expand All @@ -142,48 +144,48 @@ class NextAfterTestTemplate : public __llvm_libc::testing::Test {
// 'from' is infinity.
x = inf;
result = func(x, 0);
expectedBits = MAX_NORMAL;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = max_normal;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, inf), inf);

x = neg_inf;
result = func(x, 0);
expectedBits = (UIntType(1) << (bitWidthOfType - 1)) + MAX_NORMAL;
expected = *reinterpret_cast<T *>(&expectedBits);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_normal;
expected = *reinterpret_cast<T *>(&expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, neg_inf), neg_inf);

// 'from' is a power of 2.
x = T(32.0);
result = func(x, 0);
FPBits xBits = FPBits(x);
FPBits resultBits = FPBits(result);
ASSERT_EQ(resultBits.get_unbiased_exponent(),
uint16_t(xBits.get_unbiased_exponent() - 1));
ASSERT_EQ(resultBits.get_mantissa(),
FPBits x_bits = FPBits(x);
FPBits result_bits = FPBits(result);
ASSERT_EQ(result_bits.get_unbiased_exponent(),
uint16_t(x_bits.get_unbiased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
(UIntType(1) << MantissaWidth::VALUE) - 1);

result = func(x, T(33.0));
resultBits = FPBits(result);
ASSERT_EQ(resultBits.get_unbiased_exponent(),
xBits.get_unbiased_exponent());
ASSERT_EQ(resultBits.get_mantissa(), xBits.get_mantissa() + UIntType(1));
result_bits = FPBits(result);
ASSERT_EQ(result_bits.get_unbiased_exponent(),
x_bits.get_unbiased_exponent());
ASSERT_EQ(result_bits.get_mantissa(), x_bits.get_mantissa() + UIntType(1));

x = -x;

result = func(x, 0);
resultBits = FPBits(result);
ASSERT_EQ(resultBits.get_unbiased_exponent(),
uint16_t(xBits.get_unbiased_exponent() - 1));
ASSERT_EQ(resultBits.get_mantissa(),
result_bits = FPBits(result);
ASSERT_EQ(result_bits.get_unbiased_exponent(),
uint16_t(x_bits.get_unbiased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
(UIntType(1) << MantissaWidth::VALUE) - 1);

result = func(x, T(-33.0));
resultBits = FPBits(result);
ASSERT_EQ(resultBits.get_unbiased_exponent(),
xBits.get_unbiased_exponent());
ASSERT_EQ(resultBits.get_mantissa(), xBits.get_mantissa() + UIntType(1));
result_bits = FPBits(result);
ASSERT_EQ(result_bits.get_unbiased_exponent(),
x_bits.get_unbiased_exponent());
ASSERT_EQ(result_bits.get_mantissa(), x_bits.get_mantissa() + UIntType(1));
}
};

Expand Down
66 changes: 33 additions & 33 deletions libc/test/src/math/RIntTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

namespace mpfr = __llvm_libc::testing::mpfr;

static constexpr int roundingModes[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
FE_TONEAREST};
static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
FE_TONEAREST};

template <typename T>
class RIntTestTemplate : public __llvm_libc::testing::Test {
Expand All @@ -39,7 +39,7 @@ class RIntTestTemplate : public __llvm_libc::testing::Test {
const T neg_inf = T(FPBits::neg_inf());
const T nan = T(FPBits::build_nan(1));

static inline mpfr::RoundingMode toMPFRRoundingMode(int mode) {
static inline mpfr::RoundingMode to_mpfr_rounding_mode(int mode) {
switch (mode) {
case FE_UPWARD:
return mpfr::RoundingMode::Upward;
Expand All @@ -56,7 +56,7 @@ class RIntTestTemplate : public __llvm_libc::testing::Test {

public:
void testSpecialNumbers(RIntFunc func) {
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
ASSERT_FP_EQ(inf, func(inf));
ASSERT_FP_EQ(neg_inf, func(neg_inf));
Expand All @@ -67,61 +67,61 @@ class RIntTestTemplate : public __llvm_libc::testing::Test {
}

void testRoundNumbers(RIntFunc func) {
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
mpfr::RoundingMode mpfrMode = toMPFRRoundingMode(mode);
ASSERT_FP_EQ(func(T(1.0)), mpfr::Round(T(1.0), mpfrMode));
ASSERT_FP_EQ(func(T(-1.0)), mpfr::Round(T(-1.0), mpfrMode));
ASSERT_FP_EQ(func(T(10.0)), mpfr::Round(T(10.0), mpfrMode));
ASSERT_FP_EQ(func(T(-10.0)), mpfr::Round(T(-10.0), mpfrMode));
ASSERT_FP_EQ(func(T(1234.0)), mpfr::Round(T(1234.0), mpfrMode));
ASSERT_FP_EQ(func(T(-1234.0)), mpfr::Round(T(-1234.0), mpfrMode));
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(T(1.0)), mpfr::Round(T(1.0), mpfr_mode));
ASSERT_FP_EQ(func(T(-1.0)), mpfr::Round(T(-1.0), mpfr_mode));
ASSERT_FP_EQ(func(T(10.0)), mpfr::Round(T(10.0), mpfr_mode));
ASSERT_FP_EQ(func(T(-10.0)), mpfr::Round(T(-10.0), mpfr_mode));
ASSERT_FP_EQ(func(T(1234.0)), mpfr::Round(T(1234.0), mpfr_mode));
ASSERT_FP_EQ(func(T(-1234.0)), mpfr::Round(T(-1234.0), mpfr_mode));
}
}

void testFractions(RIntFunc func) {
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
mpfr::RoundingMode mpfrMode = toMPFRRoundingMode(mode);
ASSERT_FP_EQ(func(T(0.5)), mpfr::Round(T(0.5), mpfrMode));
ASSERT_FP_EQ(func(T(-0.5)), mpfr::Round(T(-0.5), mpfrMode));
ASSERT_FP_EQ(func(T(0.115)), mpfr::Round(T(0.115), mpfrMode));
ASSERT_FP_EQ(func(T(-0.115)), mpfr::Round(T(-0.115), mpfrMode));
ASSERT_FP_EQ(func(T(0.715)), mpfr::Round(T(0.715), mpfrMode));
ASSERT_FP_EQ(func(T(-0.715)), mpfr::Round(T(-0.715), mpfrMode));
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(T(0.5)), mpfr::Round(T(0.5), mpfr_mode));
ASSERT_FP_EQ(func(T(-0.5)), mpfr::Round(T(-0.5), mpfr_mode));
ASSERT_FP_EQ(func(T(0.115)), mpfr::Round(T(0.115), mpfr_mode));
ASSERT_FP_EQ(func(T(-0.115)), mpfr::Round(T(-0.115), mpfr_mode));
ASSERT_FP_EQ(func(T(0.715)), mpfr::Round(T(0.715), mpfr_mode));
ASSERT_FP_EQ(func(T(-0.715)), mpfr::Round(T(-0.715), mpfr_mode));
}
}

void testSubnormalRange(RIntFunc func) {
constexpr UIntType count = 1000001;
constexpr UIntType step =
(FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / count;
constexpr UIntType COUNT = 1000001;
constexpr UIntType STEP =
(FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
for (UIntType i = FPBits::MIN_SUBNORMAL; i <= FPBits::MAX_SUBNORMAL;
i += step) {
i += STEP) {
T x = T(FPBits(i));
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
mpfr::RoundingMode mpfrMode = toMPFRRoundingMode(mode);
ASSERT_FP_EQ(func(x), mpfr::Round(x, mpfrMode));
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(x), mpfr::Round(x, mpfr_mode));
}
}
}

void testNormalRange(RIntFunc func) {
constexpr UIntType count = 1000001;
constexpr UIntType step = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / count;
for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += step) {
constexpr UIntType COUNT = 1000001;
constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += STEP) {
T x = T(FPBits(i));
// In normal range on x86 platforms, the long double implicit 1 bit can be
// zero making the numbers NaN. We will skip them.
if (isnan(x)) {
continue;
}

for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
mpfr::RoundingMode mpfrMode = toMPFRRoundingMode(mode);
ASSERT_FP_EQ(func(x), mpfr::Round(x, mpfrMode));
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
ASSERT_FP_EQ(func(x), mpfr::Round(x, mpfr_mode));
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions libc/test/src/math/RemQuoTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ class RemQuoTestTemplate : public __llvm_libc::testing::Test {
}

void testSubnormalRange(RemQuoFunc func) {
constexpr UIntType count = 1000001;
constexpr UIntType step =
(FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / count;
constexpr UIntType COUNT = 1000001;
constexpr UIntType STEP =
(FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
for (UIntType v = FPBits::MIN_SUBNORMAL, w = FPBits::MAX_SUBNORMAL;
v <= FPBits::MAX_SUBNORMAL && w >= FPBits::MIN_SUBNORMAL;
v += step, w -= step) {
v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
mpfr::BinaryOutput<T> result;
mpfr::BinaryInput<T> input{x, y};
Expand All @@ -110,11 +110,11 @@ class RemQuoTestTemplate : public __llvm_libc::testing::Test {
}

void testNormalRange(RemQuoFunc func) {
constexpr UIntType count = 1000001;
constexpr UIntType step = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / count;
constexpr UIntType COUNT = 1000001;
constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;
v += step, w -= step) {
v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
mpfr::BinaryOutput<T> result;
mpfr::BinaryInput<T> input{x, y};
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/RoundTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ template <typename T> class RoundTest : public __llvm_libc::testing::Test {
}

void testRange(RoundFunc func) {
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
156 changes: 78 additions & 78 deletions libc/test/src/math/RoundToIntegerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

namespace mpfr = __llvm_libc::testing::mpfr;

static constexpr int roundingModes[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
FE_TONEAREST};
static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
FE_TONEAREST};

template <typename F, typename I, bool TestModes = false>
class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
Expand All @@ -40,11 +40,11 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
const F inf = F(__llvm_libc::fputil::FPBits<F>::inf());
const F neg_inf = F(__llvm_libc::fputil::FPBits<F>::neg_inf());
const F nan = F(__llvm_libc::fputil::FPBits<F>::build_nan(1));
static constexpr I IntegerMin = I(1) << (sizeof(I) * 8 - 1);
static constexpr I IntegerMax = -(IntegerMin + 1);
static constexpr I INTEGER_MIN = I(1) << (sizeof(I) * 8 - 1);
static constexpr I INTEGER_MAX = -(INTEGER_MIN + 1);

void testOneInput(RoundToIntegerFunc func, F input, I expected,
bool expectError) {
void test_one_input(RoundToIntegerFunc func, F input, I expected,
bool expectError) {
#if math_errhandling & MATH_ERRNO
errno = 0;
#endif
Expand All @@ -71,7 +71,7 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
}
}

static inline mpfr::RoundingMode toMPFRRoundingMode(int mode) {
static inline mpfr::RoundingMode to_mpfr_rounding_mode(int mode) {
switch (mode) {
case FE_UPWARD:
return mpfr::RoundingMode::Upward;
Expand All @@ -96,32 +96,32 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
#endif
}

void doInfinityAndNaNTest(RoundToIntegerFunc func) {
testOneInput(func, inf, IntegerMax, true);
testOneInput(func, neg_inf, IntegerMin, true);
testOneInput(func, nan, IntegerMax, true);
void do_infinity_and_na_n_test(RoundToIntegerFunc func) {
test_one_input(func, inf, INTEGER_MAX, true);
test_one_input(func, neg_inf, INTEGER_MIN, true);
test_one_input(func, nan, INTEGER_MAX, true);
}

void testInfinityAndNaN(RoundToIntegerFunc func) {
if (TestModes) {
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
doInfinityAndNaNTest(func);
do_infinity_and_na_n_test(func);
}
} else {
doInfinityAndNaNTest(func);
do_infinity_and_na_n_test(func);
}
}

void doRoundNumbersTest(RoundToIntegerFunc func) {
testOneInput(func, zero, I(0), false);
testOneInput(func, neg_zero, I(0), false);
testOneInput(func, F(1.0), I(1), false);
testOneInput(func, F(-1.0), I(-1), false);
testOneInput(func, F(10.0), I(10), false);
testOneInput(func, F(-10.0), I(-10), false);
testOneInput(func, F(1234.0), I(1234), false);
testOneInput(func, F(-1234.0), I(-1234), false);
void do_round_numbers_test(RoundToIntegerFunc func) {
test_one_input(func, zero, I(0), false);
test_one_input(func, neg_zero, I(0), false);
test_one_input(func, F(1.0), I(1), false);
test_one_input(func, F(-1.0), I(-1), false);
test_one_input(func, F(10.0), I(10), false);
test_one_input(func, F(-10.0), I(-10), false);
test_one_input(func, F(1234.0), I(1234), false);
test_one_input(func, F(-1234.0), I(-1234), false);

// The rest of this this function compares with an equivalent MPFR function
// which rounds floating point numbers to long values. There is no MPFR
Expand All @@ -131,58 +131,58 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
if (sizeof(I) > sizeof(long))
return;

constexpr int exponentLimit = sizeof(I) * 8 - 1;
constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
// We start with 1.0 so that the implicit bit for x86 long doubles
// is set.
FPBits bits(F(1.0));
bits.set_unbiased_exponent(exponentLimit + FPBits::EXPONENT_BIAS);
bits.set_unbiased_exponent(EXPONENT_LIMIT + FPBits::EXPONENT_BIAS);
bits.set_sign(1);
bits.set_mantissa(0);

F x = F(bits);
long mpfrResult;
bool erangeflag = mpfr::RoundToLong(x, mpfrResult);
long mpfr_result;
bool erangeflag = mpfr::RoundToLong(x, mpfr_result);
ASSERT_FALSE(erangeflag);
testOneInput(func, x, mpfrResult, false);
test_one_input(func, x, mpfr_result, false);
}

void testRoundNumbers(RoundToIntegerFunc func) {
if (TestModes) {
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
doRoundNumbersTest(func);
do_round_numbers_test(func);
}
} else {
doRoundNumbersTest(func);
do_round_numbers_test(func);
}
}

void doFractionsTest(RoundToIntegerFunc func, int mode) {
constexpr F fractions[] = {0.5, -0.5, 0.115, -0.115, 0.715, -0.715};
for (F x : fractions) {
long mpfrLongResult;
void do_fractions_test(RoundToIntegerFunc func, int mode) {
constexpr F FRACTIONS[] = {0.5, -0.5, 0.115, -0.115, 0.715, -0.715};
for (F x : FRACTIONS) {
long mpfr_long_result;
bool erangeflag;
if (TestModes)
erangeflag =
mpfr::RoundToLong(x, toMPFRRoundingMode(mode), mpfrLongResult);
mpfr::RoundToLong(x, to_mpfr_rounding_mode(mode), mpfr_long_result);
else
erangeflag = mpfr::RoundToLong(x, mpfrLongResult);
erangeflag = mpfr::RoundToLong(x, mpfr_long_result);
ASSERT_FALSE(erangeflag);
I mpfrResult = mpfrLongResult;
testOneInput(func, x, mpfrResult, false);
I mpfr_result = mpfr_long_result;
test_one_input(func, x, mpfr_result, false);
}
}

void testFractions(RoundToIntegerFunc func) {
if (TestModes) {
for (int mode : roundingModes) {
for (int mode : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(mode);
doFractionsTest(func, mode);
do_fractions_test(func, mode);
}
} else {
// Passing 0 for mode has no effect as it is not used in doFractionsTest
// when `TestModes` is false;
doFractionsTest(func, 0);
do_fractions_test(func, 0);
}
}

Expand All @@ -195,65 +195,65 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
if (sizeof(I) > sizeof(long))
return;

constexpr int exponentLimit = sizeof(I) * 8 - 1;
constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
// We start with 1.0 so that the implicit bit for x86 long doubles
// is set.
FPBits bits(F(1.0));
bits.set_unbiased_exponent(exponentLimit + FPBits::EXPONENT_BIAS);
bits.set_unbiased_exponent(EXPONENT_LIMIT + FPBits::EXPONENT_BIAS);
bits.set_sign(1);
bits.set_mantissa(UIntType(0x1)
<< (__llvm_libc::fputil::MantissaWidth<F>::VALUE - 1));

F x = F(bits);
if (TestModes) {
for (int m : roundingModes) {
for (int m : ROUNDING_MODES) {
__llvm_libc::fputil::set_round(m);
long mpfrLongResult;
long mpfr_long_result;
bool erangeflag =
mpfr::RoundToLong(x, toMPFRRoundingMode(m), mpfrLongResult);
mpfr::RoundToLong(x, to_mpfr_rounding_mode(m), mpfr_long_result);
ASSERT_TRUE(erangeflag);
testOneInput(func, x, IntegerMin, true);
test_one_input(func, x, INTEGER_MIN, true);
}
} else {
long mpfrLongResult;
bool erangeflag = mpfr::RoundToLong(x, mpfrLongResult);
long mpfr_long_result;
bool erangeflag = mpfr::RoundToLong(x, mpfr_long_result);
ASSERT_TRUE(erangeflag);
testOneInput(func, x, IntegerMin, true);
test_one_input(func, x, INTEGER_MIN, true);
}
}

void testSubnormalRange(RoundToIntegerFunc func) {
constexpr UIntType count = 1000001;
constexpr UIntType step =
(FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / count;
constexpr UIntType COUNT = 1000001;
constexpr UIntType STEP =
(FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
for (UIntType i = FPBits::MIN_SUBNORMAL; i <= FPBits::MAX_SUBNORMAL;
i += step) {
i += STEP) {
F x = F(FPBits(i));
if (x == F(0.0))
continue;
// All subnormal numbers should round to zero.
if (TestModes) {
if (x > 0) {
__llvm_libc::fputil::set_round(FE_UPWARD);
testOneInput(func, x, I(1), false);
test_one_input(func, x, I(1), false);
__llvm_libc::fputil::set_round(FE_DOWNWARD);
testOneInput(func, x, I(0), false);
test_one_input(func, x, I(0), false);
__llvm_libc::fputil::set_round(FE_TOWARDZERO);
testOneInput(func, x, I(0), false);
test_one_input(func, x, I(0), false);
__llvm_libc::fputil::set_round(FE_TONEAREST);
testOneInput(func, x, I(0), false);
test_one_input(func, x, I(0), false);
} else {
__llvm_libc::fputil::set_round(FE_UPWARD);
testOneInput(func, x, I(0), false);
test_one_input(func, x, I(0), false);
__llvm_libc::fputil::set_round(FE_DOWNWARD);
testOneInput(func, x, I(-1), false);
test_one_input(func, x, I(-1), false);
__llvm_libc::fputil::set_round(FE_TOWARDZERO);
testOneInput(func, x, I(0), false);
test_one_input(func, x, I(0), false);
__llvm_libc::fputil::set_round(FE_TONEAREST);
testOneInput(func, x, I(0), false);
test_one_input(func, x, I(0), false);
}
} else {
testOneInput(func, x, 0L, false);
test_one_input(func, x, 0L, false);
}
}
}
Expand All @@ -267,9 +267,9 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
if (sizeof(I) > sizeof(long))
return;

constexpr UIntType count = 1000001;
constexpr UIntType step = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / count;
for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += step) {
constexpr UIntType COUNT = 1000001;
constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += STEP) {
F x = F(FPBits(i));
// In normal range on x86 platforms, the long double implicit 1 bit can be
// zero making the numbers NaN. We will skip them.
Expand All @@ -278,25 +278,25 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
}

if (TestModes) {
for (int m : roundingModes) {
long mpfrLongResult;
for (int m : ROUNDING_MODES) {
long mpfr_long_result;
bool erangeflag =
mpfr::RoundToLong(x, toMPFRRoundingMode(m), mpfrLongResult);
I mpfrResult = mpfrLongResult;
mpfr::RoundToLong(x, to_mpfr_rounding_mode(m), mpfr_long_result);
I mpfr_result = mpfr_long_result;
__llvm_libc::fputil::set_round(m);
if (erangeflag)
testOneInput(func, x, x > 0 ? IntegerMax : IntegerMin, true);
test_one_input(func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true);
else
testOneInput(func, x, mpfrResult, false);
test_one_input(func, x, mpfr_result, false);
}
} else {
long mpfrLongResult;
bool erangeflag = mpfr::RoundToLong(x, mpfrLongResult);
I mpfrResult = mpfrLongResult;
long mpfr_long_result;
bool erangeflag = mpfr::RoundToLong(x, mpfr_long_result);
I mpfr_result = mpfr_long_result;
if (erangeflag)
testOneInput(func, x, x > 0 ? IntegerMax : IntegerMin, true);
test_one_input(func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true);
else
testOneInput(func, x, mpfrResult, false);
test_one_input(func, x, mpfr_result, false);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions libc/test/src/math/SqrtTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ template <typename T> class SqrtTest : public __llvm_libc::testing::Test {

DECLARE_SPECIAL_CONSTANTS(T)

static constexpr UIntType HiddenBit =
static constexpr UIntType HIDDEN_BIT =
UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::VALUE;

public:
Expand All @@ -37,26 +37,26 @@ template <typename T> class SqrtTest : public __llvm_libc::testing::Test {
}

void testDenormalValues(SqrtFunc func) {
for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
for (UIntType mant = 1; mant < HIDDEN_BIT; mant <<= 1) {
FPBits denormal(T(0.0));
denormal.set_mantissa(mant);

ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, T(denormal), func(T(denormal)),
T(0.5));
}

constexpr UIntType count = 1'000'001;
constexpr UIntType step = HiddenBit / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 1'000'001;
constexpr UIntType STEP = HIDDEN_BIT / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = *reinterpret_cast<T *>(&v);
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, func(x), 0.5);
}
}

void testNormalRange(SqrtFunc func) {
constexpr UIntType count = 10'000'001;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10'000'001;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = *reinterpret_cast<T *>(&v);
if (isnan(x) || (x < 0)) {
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/TruncTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ template <typename T> class TruncTest : public __llvm_libc::testing::Test {
}

void testRange(TruncFunc func) {
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/cos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ DECLARE_SPECIAL_CONSTANTS(double)

TEST(LlvmLibccosTest, Range) {
static constexpr double _2pi = 6.283185307179586;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
double x = double(FPBits(v));
// TODO: Expand the range of testing after range reduction is implemented.
if (isnan(x) || isinf(x) || x > _2pi || x < -_2pi)
Expand Down
10 changes: 5 additions & 5 deletions libc/test/src/math/cosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <errno.h>
#include <stdint.h>

using __llvm_libc::testing::sdcomp26094Values;
using __llvm_libc::testing::SDCOMP26094_VALUES;
using FPBits = __llvm_libc::fputil::FPBits<float>;

namespace mpfr = __llvm_libc::testing::mpfr;
Expand Down Expand Up @@ -47,9 +47,9 @@ TEST(LlvmLibcCosfTest, SpecialNumbers) {
}

TEST(LlvmLibcCosfTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand All @@ -73,7 +73,7 @@ TEST(LlvmLibcCosfTest, SmallValues) {
// SDCOMP-26094: check cosf in the cases for which the range reducer
// returns values furthest beyond its nominal upper bound of pi/4.
TEST(LlvmLibcCosfTest, SDCOMP_26094) {
for (uint32_t v : sdcomp26094Values) {
for (uint32_t v : SDCOMP26094_VALUES) {
float x = float(FPBits(v));
ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), 1.0);
}
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/exp2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ TEST(LlvmLibcExpfTest, Underflow) {
}

TEST(LlvmLibcexp2fTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/expf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ TEST(LlvmLibcExpfTest, Borderline) {
}

TEST(LlvmLibcExpfTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/expm1f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ TEST(LlvmLibcExpm1fTest, Borderline) {
}

TEST(LlvmLibcExpm1fTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
12 changes: 7 additions & 5 deletions libc/test/src/math/fdim_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

using LlvmLibcFDimTest = FDimTestTemplate<double>;

TEST_F(LlvmLibcFDimTest, NaNArg_fdim) { testNaNArg(&__llvm_libc::fdim); }
TEST_F(LlvmLibcFDimTest, NaNArg_fdim) { test_na_n_arg(&__llvm_libc::fdim); }

TEST_F(LlvmLibcFDimTest, InfArg_fdim) { testInfArg(&__llvm_libc::fdim); }
TEST_F(LlvmLibcFDimTest, InfArg_fdim) { test_inf_arg(&__llvm_libc::fdim); }

TEST_F(LlvmLibcFDimTest, NegInfArg_fdim) { testNegInfArg(&__llvm_libc::fdim); }
TEST_F(LlvmLibcFDimTest, NegInfArg_fdim) {
test_neg_inf_arg(&__llvm_libc::fdim);
}

TEST_F(LlvmLibcFDimTest, BothZero_fdim) { testBothZero(&__llvm_libc::fdim); }
TEST_F(LlvmLibcFDimTest, BothZero_fdim) { test_both_zero(&__llvm_libc::fdim); }

TEST_F(LlvmLibcFDimTest, InDoubleRange_fdim) {
testInRange(&__llvm_libc::fdim);
test_in_range(&__llvm_libc::fdim);
}
12 changes: 7 additions & 5 deletions libc/test/src/math/fdimf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

using LlvmLibcFDimTest = FDimTestTemplate<float>;

TEST_F(LlvmLibcFDimTest, NaNArg_fdimf) { testNaNArg(&__llvm_libc::fdimf); }
TEST_F(LlvmLibcFDimTest, NaNArg_fdimf) { test_na_n_arg(&__llvm_libc::fdimf); }

TEST_F(LlvmLibcFDimTest, InfArg_fdimf) { testInfArg(&__llvm_libc::fdimf); }
TEST_F(LlvmLibcFDimTest, InfArg_fdimf) { test_inf_arg(&__llvm_libc::fdimf); }

TEST_F(LlvmLibcFDimTest, NegInfArg_fdimf) {
testNegInfArg(&__llvm_libc::fdimf);
test_neg_inf_arg(&__llvm_libc::fdimf);
}

TEST_F(LlvmLibcFDimTest, BothZero_fdimf) { testBothZero(&__llvm_libc::fdimf); }
TEST_F(LlvmLibcFDimTest, BothZero_fdimf) {
test_both_zero(&__llvm_libc::fdimf);
}

TEST_F(LlvmLibcFDimTest, InFloatRange_fdimf) {
testInRange(&__llvm_libc::fdimf);
test_in_range(&__llvm_libc::fdimf);
}
12 changes: 7 additions & 5 deletions libc/test/src/math/fdiml_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

using LlvmLibcFDimTest = FDimTestTemplate<long double>;

TEST_F(LlvmLibcFDimTest, NaNArg_fdiml) { testNaNArg(&__llvm_libc::fdiml); }
TEST_F(LlvmLibcFDimTest, NaNArg_fdiml) { test_na_n_arg(&__llvm_libc::fdiml); }

TEST_F(LlvmLibcFDimTest, InfArg_fdiml) { testInfArg(&__llvm_libc::fdiml); }
TEST_F(LlvmLibcFDimTest, InfArg_fdiml) { test_inf_arg(&__llvm_libc::fdiml); }

TEST_F(LlvmLibcFDimTest, NegInfArg_fdiml) {
testNegInfArg(&__llvm_libc::fdiml);
test_neg_inf_arg(&__llvm_libc::fdiml);
}

TEST_F(LlvmLibcFDimTest, BothZero_fdiml) { testBothZero(&__llvm_libc::fdiml); }
TEST_F(LlvmLibcFDimTest, BothZero_fdiml) {
test_both_zero(&__llvm_libc::fdiml);
}

TEST_F(LlvmLibcFDimTest, InLongDoubleRange_fdiml) {
testInRange(&__llvm_libc::fdiml);
test_in_range(&__llvm_libc::fdiml);
}
6 changes: 3 additions & 3 deletions libc/test/src/math/fma_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
using LlvmLibcFmaTest = FmaTestTemplate<double>;

TEST_F(LlvmLibcFmaTest, SpecialNumbers) {
testSpecialNumbers(&__llvm_libc::fma);
test_special_numbers(&__llvm_libc::fma);
}

TEST_F(LlvmLibcFmaTest, SubnormalRange) {
testSubnormalRange(&__llvm_libc::fma);
test_subnormal_range(&__llvm_libc::fma);
}

TEST_F(LlvmLibcFmaTest, NormalRange) { testNormalRange(&__llvm_libc::fma); }
TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&__llvm_libc::fma); }
6 changes: 3 additions & 3 deletions libc/test/src/math/fmaf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
using LlvmLibcFmaTest = FmaTestTemplate<float>;

TEST_F(LlvmLibcFmaTest, SpecialNumbers) {
testSpecialNumbers(&__llvm_libc::fmaf);
test_special_numbers(&__llvm_libc::fmaf);
}

TEST_F(LlvmLibcFmaTest, SubnormalRange) {
testSubnormalRange(&__llvm_libc::fmaf);
test_subnormal_range(&__llvm_libc::fmaf);
}

TEST_F(LlvmLibcFmaTest, NormalRange) { testNormalRange(&__llvm_libc::fmaf); }
TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&__llvm_libc::fmaf); }
8 changes: 5 additions & 3 deletions libc/test/src/math/hypot_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
using LlvmLibcHypotTest = HypotTestTemplate<double>;

TEST_F(LlvmLibcHypotTest, SpecialNumbers) {
testSpecialNumbers(&__llvm_libc::hypot);
test_special_numbers(&__llvm_libc::hypot);
}

TEST_F(LlvmLibcHypotTest, SubnormalRange) {
testSubnormalRange(&__llvm_libc::hypot);
test_subnormal_range(&__llvm_libc::hypot);
}

TEST_F(LlvmLibcHypotTest, NormalRange) { testNormalRange(&__llvm_libc::hypot); }
TEST_F(LlvmLibcHypotTest, NormalRange) {
test_normal_range(&__llvm_libc::hypot);
}
6 changes: 3 additions & 3 deletions libc/test/src/math/hypotf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
using LlvmLibcHypotfTest = HypotTestTemplate<float>;

TEST_F(LlvmLibcHypotfTest, SpecialNumbers) {
testSpecialNumbers(&__llvm_libc::hypotf);
test_special_numbers(&__llvm_libc::hypotf);
}

TEST_F(LlvmLibcHypotfTest, SubnormalRange) {
testSubnormalRange(&__llvm_libc::hypotf);
test_subnormal_range(&__llvm_libc::hypotf);
}

TEST_F(LlvmLibcHypotfTest, NormalRange) {
testNormalRange(&__llvm_libc::hypotf);
test_normal_range(&__llvm_libc::hypotf);
}
10 changes: 5 additions & 5 deletions libc/test/src/math/ilogb_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
#include <math.h>

TEST_F(LlvmLibcILogbTest, SpecialNumbers_ilogb) {
testSpecialNumbers<double>(&__llvm_libc::ilogb);
test_special_numbers<double>(&__llvm_libc::ilogb);
}

TEST_F(LlvmLibcILogbTest, PowersOfTwo_ilogb) {
testPowersOfTwo<double>(&__llvm_libc::ilogb);
test_powers_of_two<double>(&__llvm_libc::ilogb);
}

TEST_F(LlvmLibcILogbTest, SomeIntegers_ilogb) {
testSomeIntegers<double>(&__llvm_libc::ilogb);
test_some_integers<double>(&__llvm_libc::ilogb);
}

TEST_F(LlvmLibcILogbTest, SubnormalRange_ilogb) {
testSubnormalRange<double>(&__llvm_libc::ilogb);
test_subnormal_range<double>(&__llvm_libc::ilogb);
}

TEST_F(LlvmLibcILogbTest, NormalRange_ilogb) {
testNormalRange<double>(&__llvm_libc::ilogb);
test_normal_range<double>(&__llvm_libc::ilogb);
}
10 changes: 5 additions & 5 deletions libc/test/src/math/ilogbf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
#include <math.h>

TEST_F(LlvmLibcILogbTest, SpecialNumbers_ilogbf) {
testSpecialNumbers<float>(&__llvm_libc::ilogbf);
test_special_numbers<float>(&__llvm_libc::ilogbf);
}

TEST_F(LlvmLibcILogbTest, PowersOfTwo_ilogbf) {
testPowersOfTwo<float>(&__llvm_libc::ilogbf);
test_powers_of_two<float>(&__llvm_libc::ilogbf);
}

TEST_F(LlvmLibcILogbTest, SomeIntegers_ilogbf) {
testSomeIntegers<float>(&__llvm_libc::ilogbf);
test_some_integers<float>(&__llvm_libc::ilogbf);
}

TEST_F(LlvmLibcILogbTest, SubnormalRange_ilogbf) {
testSubnormalRange<float>(&__llvm_libc::ilogbf);
test_subnormal_range<float>(&__llvm_libc::ilogbf);
}

TEST_F(LlvmLibcILogbTest, NormalRange_ilogbf) {
testNormalRange<float>(&__llvm_libc::ilogbf);
test_normal_range<float>(&__llvm_libc::ilogbf);
}
10 changes: 5 additions & 5 deletions libc/test/src/math/ilogbl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
using RunContext = __llvm_libc::testing::RunContext;

TEST_F(LlvmLibcILogbTest, SpecialNumbers_ilogbl) {
testSpecialNumbers<long double>(&__llvm_libc::ilogbl);
test_special_numbers<long double>(&__llvm_libc::ilogbl);
}

TEST_F(LlvmLibcILogbTest, PowersOfTwo_ilogbl) {
testPowersOfTwo<long double>(&__llvm_libc::ilogbl);
test_powers_of_two<long double>(&__llvm_libc::ilogbl);
}

TEST_F(LlvmLibcILogbTest, SomeIntegers_ilogbl) {
testSomeIntegers<long double>(&__llvm_libc::ilogbl);
test_some_integers<long double>(&__llvm_libc::ilogbl);
}

TEST_F(LlvmLibcILogbTest, SubnormalRange_ilogbl) {
testSubnormalRange<long double>(&__llvm_libc::ilogbl);
test_subnormal_range<long double>(&__llvm_libc::ilogbl);
}

TEST_F(LlvmLibcILogbTest, NormalRange_ilogbl) {
testNormalRange<long double>(&__llvm_libc::ilogbl);
test_normal_range<long double>(&__llvm_libc::ilogbl);
}
6 changes: 3 additions & 3 deletions libc/test/src/math/logf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ TEST(LlvmLibcLogfTest, TrickyInputs) {
}

TEST(LlvmLibcLogfTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/sdcomp26094.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace __llvm_libc {
namespace testing {

static constexpr __llvm_libc::cpp::Array<uint32_t, 10> sdcomp26094Values{
static constexpr __llvm_libc::cpp::Array<uint32_t, 10> SDCOMP26094_VALUES{
0x46427f1b, 0x4647e568, 0x46428bac, 0x4647f1f9, 0x4647fe8a,
0x45d8d7f1, 0x45d371a4, 0x45ce0b57, 0x45d35882, 0x45cdf235,
};
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/sin_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ DECLARE_SPECIAL_CONSTANTS(double)

TEST(LlvmLibcSinTest, Range) {
static constexpr double _2pi = 6.283185307179586;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
double x = double(FPBits(v));
// TODO: Expand the range of testing after range reduction is implemented.
if (isnan(x) || isinf(x) || x > _2pi || x < -_2pi)
Expand Down
10 changes: 5 additions & 5 deletions libc/test/src/math/sincosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <errno.h>
#include <stdint.h>

using __llvm_libc::testing::sdcomp26094Values;
using __llvm_libc::testing::SDCOMP26094_VALUES;
using FPBits = __llvm_libc::fputil::FPBits<float>;

namespace mpfr = __llvm_libc::testing::mpfr;
Expand Down Expand Up @@ -58,9 +58,9 @@ TEST(LlvmLibcSinCosfTest, SpecialNumbers) {
}

TEST(LlvmLibcSinCosfTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits((v)));
if (isnan(x) || isinf(x))
continue;
Expand Down Expand Up @@ -95,7 +95,7 @@ TEST(LlvmLibcSinCosfTest, SmallValues) {
// SDCOMP-26094: check sinf in the cases for which the range reducer
// returns values furthest beyond its nominal upper bound of pi/4.
TEST(LlvmLibcSinCosfTest, SDCOMP_26094) {
for (uint32_t v : sdcomp26094Values) {
for (uint32_t v : SDCOMP26094_VALUES) {
float x = float(FPBits((v)));
float sin, cos;
__llvm_libc::sincosf(x, &sin, &cos);
Expand Down
10 changes: 5 additions & 5 deletions libc/test/src/math/sinf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <errno.h>
#include <stdint.h>

using __llvm_libc::testing::sdcomp26094Values;
using __llvm_libc::testing::SDCOMP26094_VALUES;
using FPBits = __llvm_libc::fputil::FPBits<float>;

namespace mpfr = __llvm_libc::testing::mpfr;
Expand Down Expand Up @@ -47,9 +47,9 @@ TEST(LlvmLibcSinfTest, SpecialNumbers) {
}

TEST(LlvmLibcSinfTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
constexpr uint32_t COUNT = 1000000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down Expand Up @@ -78,7 +78,7 @@ TEST(LlvmLibcSinfTest, SmallValues) {
// SDCOMP-26094: check sinf in the cases for which the range reducer
// returns values furthest beyond its nominal upper bound of pi/4.
TEST(LlvmLibcSinfTest, SDCOMP_26094) {
for (uint32_t v : sdcomp26094Values) {
for (uint32_t v : SDCOMP26094_VALUES) {
float x = float(FPBits((v)));
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
}
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/tan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ DECLARE_SPECIAL_CONSTANTS(double)

TEST(LlvmLibctanTest, Range) {
static constexpr double _2pi = 6.283185307179586;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
constexpr UIntType COUNT = 10000000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
double x = double(FPBits(v));
// TODO: Expand the range of testing after range reduction is implemented.
if (isnan(x) || isinf(x) || x > _2pi || x < -_2pi)
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/signal/sigprocmask_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class LlvmLibcSignalTest : public __llvm_libc::testing::Test {
sigset_t oldSet;

public:
void SetUp() override { __llvm_libc::sigprocmask(0, nullptr, &oldSet); }
void set_up() override { __llvm_libc::sigprocmask(0, nullptr, &oldSet); }

void TearDown() override {
void tear_down() override {
__llvm_libc::sigprocmask(SIG_SETMASK, &oldSet, nullptr);
}
};
Expand Down
24 changes: 12 additions & 12 deletions libc/test/src/stdlib/atof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@
// This is just a simple test to make sure that this function works at all. It's
// functionally identical to strtod so the bulk of the testing is there.
TEST(LlvmLibcAToFTest, SimpleTest) {
__llvm_libc::fputil::FPBits<double> expectedFP =
__llvm_libc::fputil::FPBits<double> expected_fp =
__llvm_libc::fputil::FPBits<double>(uint64_t(0x405ec00000000000));

errno = 0;
double result = __llvm_libc::atof("123");

__llvm_libc::fputil::FPBits<double> actualFP =
__llvm_libc::fputil::FPBits<double> actual_fp =
__llvm_libc::fputil::FPBits<double>(result);

EXPECT_EQ(actualFP.bits, expectedFP.bits);
EXPECT_EQ(actualFP.get_sign(), expectedFP.get_sign());
EXPECT_EQ(actualFP.get_exponent(), expectedFP.get_exponent());
EXPECT_EQ(actualFP.get_mantissa(), expectedFP.get_mantissa());
EXPECT_EQ(actual_fp.bits, expected_fp.bits);
EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
EXPECT_EQ(errno, 0);
}

TEST(LlvmLibcAToFTest, FailedParsingTest) {
__llvm_libc::fputil::FPBits<double> expectedFP =
__llvm_libc::fputil::FPBits<double> expected_fp =
__llvm_libc::fputil::FPBits<double>(uint64_t(0));

errno = 0;
double result = __llvm_libc::atof("???");

__llvm_libc::fputil::FPBits<double> actualFP =
__llvm_libc::fputil::FPBits<double> actual_fp =
__llvm_libc::fputil::FPBits<double>(result);

EXPECT_EQ(actualFP.bits, expectedFP.bits);
EXPECT_EQ(actualFP.get_sign(), expectedFP.get_sign());
EXPECT_EQ(actualFP.get_exponent(), expectedFP.get_exponent());
EXPECT_EQ(actualFP.get_mantissa(), expectedFP.get_mantissa());
EXPECT_EQ(actual_fp.bits, expected_fp.bits);
EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
EXPECT_EQ(errno, 0);
}
28 changes: 14 additions & 14 deletions libc/test/src/stdlib/bsearch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,44 @@ TEST(LlvmLibcBsearchTest, ErrorInputs) {
}

TEST(LlvmLibcBsearchTest, IntegerArray) {
constexpr int array[25] = {10, 23, 33, 35, 55, 70, 71,
constexpr int ARRAY[25] = {10, 23, 33, 35, 55, 70, 71,
100, 110, 123, 133, 135, 155, 170,
171, 1100, 1110, 1123, 1133, 1135, 1155,
1170, 1171, 11100, 12310};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(ARRAY) / sizeof(int);

for (size_t s = 1; s <= array_size; ++s) {
for (size_t s = 1; s <= ARRAY_SIZE; ++s) {
for (size_t i = 0; i < s; ++i) {
int key = array[i];
int key = ARRAY[i];
void *elem =
__llvm_libc::bsearch(&key, array, s, sizeof(int), int_compare);
__llvm_libc::bsearch(&key, ARRAY, s, sizeof(int), int_compare);
ASSERT_EQ(*reinterpret_cast<int *>(elem), key);
}
}

// Non existent keys
for (size_t s = 1; s <= array_size; ++s) {
for (size_t s = 1; s <= ARRAY_SIZE; ++s) {
int key = 5;
ASSERT_TRUE(__llvm_libc::bsearch(&key, &array, s, sizeof(int),
ASSERT_TRUE(__llvm_libc::bsearch(&key, &ARRAY, s, sizeof(int),
int_compare) == nullptr);

key = 125;
ASSERT_TRUE(__llvm_libc::bsearch(&key, &array, s, sizeof(int),
ASSERT_TRUE(__llvm_libc::bsearch(&key, &ARRAY, s, sizeof(int),
int_compare) == nullptr);

key = 136;
ASSERT_TRUE(__llvm_libc::bsearch(&key, &array, s, sizeof(int),
ASSERT_TRUE(__llvm_libc::bsearch(&key, &ARRAY, s, sizeof(int),
int_compare) == nullptr);
key = 12345;
ASSERT_TRUE(__llvm_libc::bsearch(&key, &array, s, sizeof(int),
ASSERT_TRUE(__llvm_libc::bsearch(&key, &ARRAY, s, sizeof(int),
int_compare) == nullptr);
}
}

TEST(LlvmLibcBsearchTest, SameKeyAndArray) {
constexpr int array[5] = {1, 2, 3, 4, 5};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr int ARRAY[5] = {1, 2, 3, 4, 5};
constexpr size_t ARRAY_SIZE = sizeof(ARRAY) / sizeof(int);
void *elem =
__llvm_libc::bsearch(array, array, array_size, sizeof(int), int_compare);
EXPECT_EQ(*reinterpret_cast<int *>(elem), array[0]);
__llvm_libc::bsearch(ARRAY, ARRAY, ARRAY_SIZE, sizeof(int), int_compare);
EXPECT_EQ(*reinterpret_cast<int *>(elem), ARRAY[0]);
}
74 changes: 37 additions & 37 deletions libc/test/src/stdlib/qsort_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ TEST(LlvmLibcQsortTest, SortedArray) {
int array[25] = {10, 23, 33, 35, 55, 70, 71, 100, 110,
123, 133, 135, 155, 170, 171, 1100, 1110, 1123,
1133, 1135, 1155, 1170, 1171, 11100, 12310};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 10);
ASSERT_LE(array[1], 23);
Expand Down Expand Up @@ -61,32 +61,32 @@ TEST(LlvmLibcQsortTest, SortedArray) {
TEST(LlvmLibcQsortTest, ReverseSortedArray) {
int array[25] = {25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

for (int i = 0; i < int(array_size - 1); ++i)
for (int i = 0; i < int(ARRAY_SIZE - 1); ++i)
ASSERT_LE(array[i], i + 1);
}

TEST(LlvmLibcQsortTest, AllEqualElements) {
int array[25] = {100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

for (size_t i = 0; i < array_size - 1; ++i)
for (size_t i = 0; i < ARRAY_SIZE - 1; ++i)
ASSERT_LE(array[i], 100);
}

TEST(LlvmLibcQsortTest, UnsortedArray1) {
int array[25] = {10, 23, 8, 35, 55, 45, 40, 100, 110, 123, 90, 80, 70,
60, 171, 11, 1, -1, -5, -10, 1155, 1170, 1171, 12, -100};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], -100);
ASSERT_LE(array[1], -10);
Expand Down Expand Up @@ -117,9 +117,9 @@ TEST(LlvmLibcQsortTest, UnsortedArray1) {

TEST(LlvmLibcQsortTest, UnsortedArray2) {
int array[7] = {10, 40, 45, 55, 35, 23, 60};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 10);
ASSERT_LE(array[1], 23);
Expand All @@ -132,9 +132,9 @@ TEST(LlvmLibcQsortTest, UnsortedArray2) {

TEST(LlvmLibcQsortTest, UnsortedArrayDuplicateElements1) {
int array[6] = {10, 10, 20, 20, 5, 5};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 5);
ASSERT_LE(array[1], 5);
Expand All @@ -146,9 +146,9 @@ TEST(LlvmLibcQsortTest, UnsortedArrayDuplicateElements1) {

TEST(LlvmLibcQsortTest, UnsortedArrayDuplicateElements2) {
int array[10] = {20, 10, 10, 10, 10, 20, 21, 21, 21, 21};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 10);
ASSERT_LE(array[1], 10);
Expand All @@ -164,9 +164,9 @@ TEST(LlvmLibcQsortTest, UnsortedArrayDuplicateElements2) {

TEST(LlvmLibcQsortTest, UnsortedArrayDuplicateElements3) {
int array[10] = {20, 30, 30, 30, 30, 20, 21, 21, 21, 21};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 20);
ASSERT_LE(array[1], 20);
Expand All @@ -182,9 +182,9 @@ TEST(LlvmLibcQsortTest, UnsortedArrayDuplicateElements3) {

TEST(LlvmLibcQsortTest, UnsortedThreeElementArray1) {
int array[3] = {14999024, 0, 3};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 0);
ASSERT_LE(array[1], 3);
Expand All @@ -193,9 +193,9 @@ TEST(LlvmLibcQsortTest, UnsortedThreeElementArray1) {

TEST(LlvmLibcQsortTest, UnsortedThreeElementArray2) {
int array[3] = {3, 14999024, 0};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 0);
ASSERT_LE(array[1], 3);
Expand All @@ -204,9 +204,9 @@ TEST(LlvmLibcQsortTest, UnsortedThreeElementArray2) {

TEST(LlvmLibcQsortTest, UnsortedThreeElementArray3) {
int array[3] = {3, 0, 14999024};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 0);
ASSERT_LE(array[1], 3);
Expand All @@ -215,9 +215,9 @@ TEST(LlvmLibcQsortTest, UnsortedThreeElementArray3) {

TEST(LlvmLibcQsortTest, SameElementThreeElementArray) {
int array[3] = {12345, 12345, 12345};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 12345);
ASSERT_LE(array[1], 12345);
Expand All @@ -226,40 +226,40 @@ TEST(LlvmLibcQsortTest, SameElementThreeElementArray) {

TEST(LlvmLibcQsortTest, UnsortedTwoElementArray1) {
int array[2] = {14999024, 0};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 0);
ASSERT_LE(array[1], 14999024);
}

TEST(LlvmLibcQsortTest, UnsortedTwoElementArray2) {
int array[2] = {0, 14999024};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 0);
ASSERT_LE(array[1], 14999024);
}

TEST(LlvmLibcQsortTest, SameElementTwoElementArray) {
int array[2] = {12345, 12345};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], 12345);
ASSERT_LE(array[1], 12345);
}

TEST(LlvmLibcQSortTest, SingleElementArray) {
constexpr int elem = 12345;
int array[1] = {elem};
constexpr size_t array_size = sizeof(array) / sizeof(int);
constexpr int ELEM = 12345;
int array[1] = {ELEM};
constexpr size_t ARRAY_SIZE = sizeof(array) / sizeof(int);

__llvm_libc::qsort(array, array_size, sizeof(int), int_compare);
__llvm_libc::qsort(array, ARRAY_SIZE, sizeof(int), int_compare);

ASSERT_LE(array[0], elem);
ASSERT_LE(array[0], ELEM);
}
59 changes: 30 additions & 29 deletions libc/test/src/stdlib/strtod_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

class LlvmLibcStrToDTest : public __llvm_libc::testing::Test {
public:
void runTest(const char *inputString, const ptrdiff_t expectedStrLen,
const uint64_t expectedRawData, const int expectedErrno = 0) {
void run_test(const char *inputString, const ptrdiff_t expectedStrLen,
const uint64_t expectedRawData, const int expectedErrno = 0) {
// expectedRawData is the expected double result as a uint64_t, organized
// according to IEEE754:
//
Expand All @@ -33,58 +33,59 @@ class LlvmLibcStrToDTest : public __llvm_libc::testing::Test {
// +-- 11 Exponent Bits
//
// This is so that the result can be compared in parts.
char *strEnd = nullptr;
char *str_end = nullptr;

__llvm_libc::fputil::FPBits<double> expectedFP =
__llvm_libc::fputil::FPBits<double> expected_fp =
__llvm_libc::fputil::FPBits<double>(expectedRawData);

errno = 0;
double result = __llvm_libc::strtod(inputString, &strEnd);
double result = __llvm_libc::strtod(inputString, &str_end);

__llvm_libc::fputil::FPBits<double> actualFP =
__llvm_libc::fputil::FPBits<double> actual_fp =
__llvm_libc::fputil::FPBits<double>(result);

EXPECT_EQ(strEnd - inputString, expectedStrLen);
EXPECT_EQ(str_end - inputString, expectedStrLen);

EXPECT_EQ(actualFP.bits, expectedFP.bits);
EXPECT_EQ(actualFP.get_sign(), expectedFP.get_sign());
EXPECT_EQ(actualFP.get_exponent(), expectedFP.get_exponent());
EXPECT_EQ(actualFP.get_mantissa(), expectedFP.get_mantissa());
EXPECT_EQ(actual_fp.bits, expected_fp.bits);
EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
EXPECT_EQ(errno, expectedErrno);
}
};

TEST_F(LlvmLibcStrToDTest, SimpleTest) {
runTest("123", 3, uint64_t(0x405ec00000000000));
run_test("123", 3, uint64_t(0x405ec00000000000));

// This should fail on Eisel-Lemire, forcing a fallback to simple decimal
// conversion.
runTest("12345678901234549760", 20, uint64_t(0x43e56a95319d63d8));
run_test("12345678901234549760", 20, uint64_t(0x43e56a95319d63d8));

// Found while looking for difficult test cases here:
// https://github.com/nigeltao/parse-number-fxx-test-data/blob/main/more-test-cases/golang-org-issue-36657.txt
runTest("1090544144181609348835077142190", 31, uint64_t(0x462b8779f2474dfb));
run_test("1090544144181609348835077142190", 31, uint64_t(0x462b8779f2474dfb));

runTest("0x123", 5, uint64_t(0x4072300000000000));
run_test("0x123", 5, uint64_t(0x4072300000000000));
}

// These are tests that have caused problems in the past.
TEST_F(LlvmLibcStrToDTest, SpecificFailures) {
runTest("3E70000000000000", 16, uint64_t(0x7FF0000000000000), ERANGE);
runTest("358416272e-33", 13, uint64_t(0x3adbbb2a68c9d0b9));
runTest("2.16656806400000023841857910156251e9", 36,
uint64_t(0x41e0246690000001));
runTest("27949676547093071875", 20, uint64_t(0x43f83e132bc608c9));
run_test("3E70000000000000", 16, uint64_t(0x7FF0000000000000), ERANGE);
run_test("358416272e-33", 13, uint64_t(0x3adbbb2a68c9d0b9));
run_test("2.16656806400000023841857910156251e9", 36,
uint64_t(0x41e0246690000001));
run_test("27949676547093071875", 20, uint64_t(0x43f83e132bc608c9));
}

TEST_F(LlvmLibcStrToDTest, FuzzFailures) {
runTest("-\xff\xff\xff\xff\xff\xff\xff\x01", 0, uint64_t(0));
runTest("-.????", 0, uint64_t(0));
runTest("44444444444444444444444444444444444444444444444444A44444444444444444"
"44444444444*\x99\xff\xff\xff\xff",
50, uint64_t(0x4a3e68fdd0e0b2d8));
runTest("-NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNKNNNNNNNNNNNNNNNNNN?"
"NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN?",
0, uint64_t(0));
runTest("0x.666E40", 9, uint64_t(0x3fd99b9000000000));
run_test("-\xff\xff\xff\xff\xff\xff\xff\x01", 0, uint64_t(0));
run_test("-.????", 0, uint64_t(0));
run_test(
"44444444444444444444444444444444444444444444444444A44444444444444444"
"44444444444*\x99\xff\xff\xff\xff",
50, uint64_t(0x4a3e68fdd0e0b2d8));
run_test("-NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNKNNNNNNNNNNNNNNNNNN?"
"NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN?",
0, uint64_t(0));
run_test("0x.666E40", 9, uint64_t(0x3fd99b9000000000));
}
162 changes: 82 additions & 80 deletions libc/test/src/stdlib/strtof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

class LlvmLibcStrToFTest : public __llvm_libc::testing::Test {
public:
void runTest(const char *inputString, const ptrdiff_t expectedStrLen,
const uint32_t expectedRawData, const int expectedErrno = 0) {
void run_test(const char *inputString, const ptrdiff_t expectedStrLen,
const uint32_t expectedRawData, const int expectedErrno = 0) {
// expectedRawData is the expected float result as a uint32_t, organized
// according to IEEE754:
//
Expand All @@ -33,23 +33,23 @@ class LlvmLibcStrToFTest : public __llvm_libc::testing::Test {
// +-- 8 Exponent Bits
//
// This is so that the result can be compared in parts.
char *strEnd = nullptr;
char *str_end = nullptr;

__llvm_libc::fputil::FPBits<float> expectedFP =
__llvm_libc::fputil::FPBits<float> expected_fp =
__llvm_libc::fputil::FPBits<float>(expectedRawData);

errno = 0;
float result = __llvm_libc::strtof(inputString, &strEnd);
float result = __llvm_libc::strtof(inputString, &str_end);

__llvm_libc::fputil::FPBits<float> actualFP =
__llvm_libc::fputil::FPBits<float> actual_fp =
__llvm_libc::fputil::FPBits<float>(result);

EXPECT_EQ(strEnd - inputString, expectedStrLen);
EXPECT_EQ(str_end - inputString, expectedStrLen);

EXPECT_EQ(actualFP.bits, expectedFP.bits);
EXPECT_EQ(actualFP.get_sign(), expectedFP.get_sign());
EXPECT_EQ(actualFP.get_exponent(), expectedFP.get_exponent());
EXPECT_EQ(actualFP.get_mantissa(), expectedFP.get_mantissa());
EXPECT_EQ(actual_fp.bits, expected_fp.bits);
EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
EXPECT_EQ(errno, expectedErrno);
}
};
Expand All @@ -59,115 +59,117 @@ class LlvmLibcStrToFTest : public __llvm_libc::testing::Test {
// them.

TEST_F(LlvmLibcStrToFTest, BasicDecimalTests) {
runTest("1", 1, 0x3f800000);
runTest("123", 3, 0x42f60000);
runTest("1234567890", 10, 0x4e932c06u);
runTest("123456789012345678901", 21, 0x60d629d4);
runTest("0.1", 3, 0x3dcccccdu);
runTest(".1", 2, 0x3dcccccdu);
runTest("-0.123456789", 12, 0xbdfcd6eau);
runTest("0.11111111111111111111", 22, 0x3de38e39u);
runTest("0.0000000000000000000000001", 27, 0x15f79688u);
run_test("1", 1, 0x3f800000);
run_test("123", 3, 0x42f60000);
run_test("1234567890", 10, 0x4e932c06u);
run_test("123456789012345678901", 21, 0x60d629d4);
run_test("0.1", 3, 0x3dcccccdu);
run_test(".1", 2, 0x3dcccccdu);
run_test("-0.123456789", 12, 0xbdfcd6eau);
run_test("0.11111111111111111111", 22, 0x3de38e39u);
run_test("0.0000000000000000000000001", 27, 0x15f79688u);
}

TEST_F(LlvmLibcStrToFTest, DecimalOutOfRangeTests) {
runTest("555E36", 6, 0x7f800000, ERANGE);
runTest("1e-10000", 8, 0x0, ERANGE);
run_test("555E36", 6, 0x7f800000, ERANGE);
run_test("1e-10000", 8, 0x0, ERANGE);
}

TEST_F(LlvmLibcStrToFTest, DecimalsWithRoundingProblems) {
runTest("20040229", 8, 0x4b98e512);
runTest("20040401", 8, 0x4b98e568);
runTest("9E9", 3, 0x50061c46);
run_test("20040229", 8, 0x4b98e512);
run_test("20040401", 8, 0x4b98e568);
run_test("9E9", 3, 0x50061c46);
}

TEST_F(LlvmLibcStrToFTest, DecimalSubnormals) {
runTest("1.4012984643248170709237295832899161312802619418765e-45", 55, 0x1,
ERANGE);
run_test("1.4012984643248170709237295832899161312802619418765e-45", 55, 0x1,
ERANGE);
}

TEST_F(LlvmLibcStrToFTest, DecimalWithLongExponent) {
runTest("1e2147483648", 12, 0x7f800000, ERANGE);
runTest("1e2147483646", 12, 0x7f800000, ERANGE);
runTest("100e2147483646", 14, 0x7f800000, ERANGE);
runTest("1e-2147483647", 13, 0x0, ERANGE);
runTest("1e-2147483649", 13, 0x0, ERANGE);
run_test("1e2147483648", 12, 0x7f800000, ERANGE);
run_test("1e2147483646", 12, 0x7f800000, ERANGE);
run_test("100e2147483646", 14, 0x7f800000, ERANGE);
run_test("1e-2147483647", 13, 0x0, ERANGE);
run_test("1e-2147483649", 13, 0x0, ERANGE);
}

TEST_F(LlvmLibcStrToFTest, BasicHexadecimalTests) {
runTest("0x1", 3, 0x3f800000);
runTest("0x10", 4, 0x41800000);
runTest("0x11", 4, 0x41880000);
runTest("0x0.1234", 8, 0x3d91a000);
run_test("0x1", 3, 0x3f800000);
run_test("0x10", 4, 0x41800000);
run_test("0x11", 4, 0x41880000);
run_test("0x0.1234", 8, 0x3d91a000);
}

TEST_F(LlvmLibcStrToFTest, HexadecimalSubnormalTests) {
runTest("0x0.0000000000000000000000000000000002", 38, 0x4000, ERANGE);
run_test("0x0.0000000000000000000000000000000002", 38, 0x4000, ERANGE);

// This is the largest subnormal number as represented in hex
runTest("0x0.00000000000000000000000000000003fffff8", 42, 0x7fffff, ERANGE);
run_test("0x0.00000000000000000000000000000003fffff8", 42, 0x7fffff, ERANGE);
}

TEST_F(LlvmLibcStrToFTest, HexadecimalSubnormalRoundingTests) {
// This is the largest subnormal number that gets rounded down to 0 (as a
// float)
runTest("0x0.00000000000000000000000000000000000004", 42, 0x0, ERANGE);
run_test("0x0.00000000000000000000000000000000000004", 42, 0x0, ERANGE);

// This is slightly larger, and thus rounded up
runTest("0x0.000000000000000000000000000000000000041", 43, 0x00000001,
ERANGE);
run_test("0x0.000000000000000000000000000000000000041", 43, 0x00000001,
ERANGE);

// These check that we're rounding to even properly
runTest("0x0.0000000000000000000000000000000000000b", 42, 0x00000001, ERANGE);
runTest("0x0.0000000000000000000000000000000000000c", 42, 0x00000002, ERANGE);
run_test("0x0.0000000000000000000000000000000000000b", 42, 0x00000001,
ERANGE);
run_test("0x0.0000000000000000000000000000000000000c", 42, 0x00000002,
ERANGE);

// These check that we're rounding to even properly even when the input bits
// are longer than the bit fields can contain.
runTest("0x1.000000000000000000000p-150", 30, 0x00000000, ERANGE);
runTest("0x1.000010000000000001000p-150", 30, 0x00000001, ERANGE);
runTest("0x1.000100000000000001000p-134", 30, 0x00008001, ERANGE);
runTest("0x1.FFFFFC000000000001000p-127", 30, 0x007FFFFF, ERANGE);
runTest("0x1.FFFFFE000000000000000p-127", 30, 0x00800000);
run_test("0x1.000000000000000000000p-150", 30, 0x00000000, ERANGE);
run_test("0x1.000010000000000001000p-150", 30, 0x00000001, ERANGE);
run_test("0x1.000100000000000001000p-134", 30, 0x00008001, ERANGE);
run_test("0x1.FFFFFC000000000001000p-127", 30, 0x007FFFFF, ERANGE);
run_test("0x1.FFFFFE000000000000000p-127", 30, 0x00800000);
}

TEST_F(LlvmLibcStrToFTest, HexadecimalNormalRoundingTests) {
// This also checks the round to even behavior by checking three adjacent
// numbers.
// This gets rounded down to even
runTest("0x123456500", 11, 0x4f91a2b2);
run_test("0x123456500", 11, 0x4f91a2b2);
// This doesn't get rounded at all
runTest("0x123456600", 11, 0x4f91a2b3);
run_test("0x123456600", 11, 0x4f91a2b3);
// This gets rounded up to even
runTest("0x123456700", 11, 0x4f91a2b4);
run_test("0x123456700", 11, 0x4f91a2b4);
// Correct rounding for long input
runTest("0x1.000001000000000000000", 25, 0x3f800000);
runTest("0x1.000001000000000000100", 25, 0x3f800001);
run_test("0x1.000001000000000000000", 25, 0x3f800000);
run_test("0x1.000001000000000000100", 25, 0x3f800001);
}

TEST_F(LlvmLibcStrToFTest, HexadecimalsWithRoundingProblems) {
runTest("0xFFFFFFFF", 10, 0x4f800000);
run_test("0xFFFFFFFF", 10, 0x4f800000);
}

TEST_F(LlvmLibcStrToFTest, HexadecimalOutOfRangeTests) {
runTest("0x123456789123456789123456789123456789", 38, 0x7f800000, ERANGE);
runTest("-0x123456789123456789123456789123456789", 39, 0xff800000, ERANGE);
runTest("0x0.00000000000000000000000000000000000001", 42, 0x0, ERANGE);
run_test("0x123456789123456789123456789123456789", 38, 0x7f800000, ERANGE);
run_test("-0x123456789123456789123456789123456789", 39, 0xff800000, ERANGE);
run_test("0x0.00000000000000000000000000000000000001", 42, 0x0, ERANGE);
}

TEST_F(LlvmLibcStrToFTest, InfTests) {
runTest("INF", 3, 0x7f800000);
runTest("INFinity", 8, 0x7f800000);
runTest("infnity", 3, 0x7f800000);
runTest("infinit", 3, 0x7f800000);
runTest("infinfinit", 3, 0x7f800000);
runTest("innf", 0, 0x0);
runTest("-inf", 4, 0xff800000);
runTest("-iNfInItY", 9, 0xff800000);
run_test("INF", 3, 0x7f800000);
run_test("INFinity", 8, 0x7f800000);
run_test("infnity", 3, 0x7f800000);
run_test("infinit", 3, 0x7f800000);
run_test("infinfinit", 3, 0x7f800000);
run_test("innf", 0, 0x0);
run_test("-inf", 4, 0xff800000);
run_test("-iNfInItY", 9, 0xff800000);
}

TEST_F(LlvmLibcStrToFTest, SimpleNaNTests) {
runTest("NaN", 3, 0x7fc00000);
runTest("-nAn", 4, 0xffc00000);
run_test("NaN", 3, 0x7fc00000);
run_test("-nAn", 4, 0xffc00000);
}

// These NaNs are of the form `NaN(n-character-sequence)` where the
Expand All @@ -178,26 +180,26 @@ TEST_F(LlvmLibcStrToFTest, SimpleNaNTests) {
// the result is put into the mantissa if it takes up the whole width of the
// parentheses.
TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesEmptyTest) {
runTest("NaN()", 5, 0x7fc00000);
run_test("NaN()", 5, 0x7fc00000);
}

TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesValidNumberTests) {
runTest("NaN(1234)", 9, 0x7fc004d2);
runTest("NaN(0x1234)", 11, 0x7fc01234);
runTest("NaN(01234)", 10, 0x7fc0029c);
run_test("NaN(1234)", 9, 0x7fc004d2);
run_test("NaN(0x1234)", 11, 0x7fc01234);
run_test("NaN(01234)", 10, 0x7fc0029c);
}

TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesInvalidSequenceTests) {
runTest("NaN( 1234)", 3, 0x7fc00000);
runTest("NaN(-1234)", 3, 0x7fc00000);
runTest("NaN(asd&f)", 3, 0x7fc00000);
runTest("NaN(123 )", 3, 0x7fc00000);
runTest("NaN(123+asdf)", 3, 0x7fc00000);
runTest("NaN(123", 3, 0x7fc00000);
run_test("NaN( 1234)", 3, 0x7fc00000);
run_test("NaN(-1234)", 3, 0x7fc00000);
run_test("NaN(asd&f)", 3, 0x7fc00000);
run_test("NaN(123 )", 3, 0x7fc00000);
run_test("NaN(123+asdf)", 3, 0x7fc00000);
run_test("NaN(123", 3, 0x7fc00000);
}

TEST_F(LlvmLibcStrToFTest, NaNWithParenthesesValidSequenceInvalidNumberTests) {
runTest("NaN(1a)", 7, 0x7fc00000);
runTest("NaN(asdf)", 9, 0x7fc00000);
runTest("NaN(1A1)", 8, 0x7fc00000);
run_test("NaN(1a)", 7, 0x7fc00000);
run_test("NaN(asdf)", 9, 0x7fc00000);
run_test("NaN(1A1)", 8, 0x7fc00000);
}
218 changes: 110 additions & 108 deletions libc/test/src/stdlib/strtold_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

class LlvmLibcStrToLDTest : public __llvm_libc::testing::Test {
public:
void runTest(const char *inputString, const ptrdiff_t expectedStrLen,
const uint64_t expectedRawData64,
const __uint128_t expectedRawData80,
const __uint128_t expectedRawData128,
const int expectedErrno64 = 0, const int expectedErrno80 = 0,
const int expectedErrno128 = 0) {
void run_test(const char *inputString, const ptrdiff_t expectedStrLen,
const uint64_t expectedRawData64,
const __uint128_t expectedRawData80,
const __uint128_t expectedRawData128,
const int expectedErrno64 = 0, const int expectedErrno80 = 0,
const int expectedErrno128 = 0) {
// expectedRawData64 is the expected long double result as a uint64_t,
// organized according to the IEEE754 double precision format:
//
Expand Down Expand Up @@ -61,161 +61,163 @@ class LlvmLibcStrToLDTest : public __llvm_libc::testing::Test {
// +------+------+
// |
// +-- 15 Exponent Bits
char *strEnd = nullptr;
char *str_end = nullptr;

#if defined(LONG_DOUBLE_IS_DOUBLE)
__llvm_libc::fputil::FPBits<long double> expectedFP =
__llvm_libc::fputil::FPBits<long double>(expectedRawData64);
const int expectedErrno = expectedErrno64;
#elif defined(SPECIAL_X86_LONG_DOUBLE)
__llvm_libc::fputil::FPBits<long double> expectedFP =
__llvm_libc::fputil::FPBits<long double> expected_fp =
__llvm_libc::fputil::FPBits<long double>(expectedRawData80);
const int expectedErrno = expectedErrno80;
const int expected_errno = expectedErrno80;
#else
__llvm_libc::fputil::FPBits<long double> expectedFP =
__llvm_libc::fputil::FPBits<long double>(expectedRawData128);
const int expectedErrno = expectedErrno128;
#endif

errno = 0;
long double result = __llvm_libc::strtold(inputString, &strEnd);
long double result = __llvm_libc::strtold(inputString, &str_end);

__llvm_libc::fputil::FPBits<long double> actualFP =
__llvm_libc::fputil::FPBits<long double> actual_fp =
__llvm_libc::fputil::FPBits<long double>();
actualFP = __llvm_libc::fputil::FPBits<long double>(result);
actual_fp = __llvm_libc::fputil::FPBits<long double>(result);

EXPECT_EQ(strEnd - inputString, expectedStrLen);
EXPECT_EQ(str_end - inputString, expectedStrLen);

EXPECT_EQ(actualFP.bits, expectedFP.bits);
EXPECT_EQ(actualFP.get_sign(), expectedFP.get_sign());
EXPECT_EQ(actualFP.get_exponent(), expectedFP.get_exponent());
EXPECT_EQ(actualFP.get_mantissa(), expectedFP.get_mantissa());
EXPECT_EQ(errno, expectedErrno);
EXPECT_EQ(actual_fp.bits, expected_fp.bits);
EXPECT_EQ(actual_fp.get_sign(), expected_fp.get_sign());
EXPECT_EQ(actual_fp.get_exponent(), expected_fp.get_exponent());
EXPECT_EQ(actual_fp.get_mantissa(), expected_fp.get_mantissa());
EXPECT_EQ(errno, expected_errno);
}
};

TEST_F(LlvmLibcStrToLDTest, SimpleTest) {
runTest("123", 3, uint64_t(0x405ec00000000000),
__uint128_t(0x4005f60000) << 40,
__uint128_t(0x4005ec0000000000) << 64);
run_test("123", 3, uint64_t(0x405ec00000000000),
__uint128_t(0x4005f60000) << 40,
__uint128_t(0x4005ec0000000000) << 64);

// This should fail on Eisel-Lemire, forcing a fallback to simple decimal
// conversion.
runTest("12345678901234549760", 20, uint64_t(0x43e56a95319d63d8),
(__uint128_t(0x403eab54a9) << 40) + __uint128_t(0x8ceb1ec400),
(__uint128_t(0x403e56a95319d63d) << 64) +
__uint128_t(0x8800000000000000));
run_test("12345678901234549760", 20, uint64_t(0x43e56a95319d63d8),
(__uint128_t(0x403eab54a9) << 40) + __uint128_t(0x8ceb1ec400),
(__uint128_t(0x403e56a95319d63d) << 64) +
__uint128_t(0x8800000000000000));

// Found while looking for difficult test cases here:
// https://github.com/nigeltao/parse-number-fxx-test-data/blob/main/more-test-cases/golang-org-issue-36657.txt
runTest("1090544144181609348835077142190", 31, uint64_t(0x462b8779f2474dfb),
(__uint128_t(0x4062dc3bcf) << 40) + __uint128_t(0x923a6fd402),
(__uint128_t(0x4062b8779f2474df) << 64) +
__uint128_t(0xa804bfd8c6d5c000));

runTest("0x123", 5, uint64_t(0x4072300000000000),
(__uint128_t(0x4007918000) << 40),
(__uint128_t(0x4007230000000000) << 64));
run_test("1090544144181609348835077142190", 31, uint64_t(0x462b8779f2474dfb),
(__uint128_t(0x4062dc3bcf) << 40) + __uint128_t(0x923a6fd402),
(__uint128_t(0x4062b8779f2474df) << 64) +
__uint128_t(0xa804bfd8c6d5c000));

run_test("0x123", 5, uint64_t(0x4072300000000000),
(__uint128_t(0x4007918000) << 40),
(__uint128_t(0x4007230000000000) << 64));
}

// These are tests that have caused problems for doubles in the past.
TEST_F(LlvmLibcStrToLDTest, Float64SpecificFailures) {
runTest("3E70000000000000", 16, uint64_t(0x7FF0000000000000),
(__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7fff000000000000) << 64), ERANGE, ERANGE, ERANGE);
runTest("358416272e-33", 13, uint64_t(0x3adbbb2a68c9d0b9),
(__uint128_t(0x3fadddd953) << 40) + __uint128_t(0x464e85c400),
(__uint128_t(0x3fadbbb2a68c9d0b) << 64) +
__uint128_t(0x8800e7969e1c5fc8));
runTest(
run_test("3E70000000000000", 16, uint64_t(0x7FF0000000000000),
(__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7fff000000000000) << 64), ERANGE, ERANGE, ERANGE);
run_test("358416272e-33", 13, uint64_t(0x3adbbb2a68c9d0b9),
(__uint128_t(0x3fadddd953) << 40) + __uint128_t(0x464e85c400),
(__uint128_t(0x3fadbbb2a68c9d0b) << 64) +
__uint128_t(0x8800e7969e1c5fc8));
run_test(
"2.16656806400000023841857910156251e9", 36, uint64_t(0x41e0246690000001),
(__uint128_t(0x401e812334) << 40) + __uint128_t(0x8000000400),
(__uint128_t(0x401e024669000000) << 64) + __uint128_t(0x800000000000018));
runTest("27949676547093071875", 20, uint64_t(0x43f83e132bc608c9),
(__uint128_t(0x403fc1f099) << 40) + __uint128_t(0x5e30464402),
(__uint128_t(0x403f83e132bc608c) << 64) +
__uint128_t(0x8803000000000000));
run_test("27949676547093071875", 20, uint64_t(0x43f83e132bc608c9),
(__uint128_t(0x403fc1f099) << 40) + __uint128_t(0x5e30464402),
(__uint128_t(0x403f83e132bc608c) << 64) +
__uint128_t(0x8803000000000000));
}

TEST_F(LlvmLibcStrToLDTest, MaxSizeNumbers) {
runTest("1.1897314953572317650e4932", 26, uint64_t(0x7FF0000000000000),
(__uint128_t(0x7ffeffffff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7ffeffffffffffff) << 64) +
__uint128_t(0xfffd57322e3f8675),
ERANGE, 0, 0);
runTest("1.18973149535723176508e4932", 27, uint64_t(0x7FF0000000000000),
(__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7ffeffffffffffff) << 64) +
__uint128_t(0xffffd2478338036c),
ERANGE, ERANGE, 0);
run_test("1.1897314953572317650e4932", 26, uint64_t(0x7FF0000000000000),
(__uint128_t(0x7ffeffffff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7ffeffffffffffff) << 64) +
__uint128_t(0xfffd57322e3f8675),
ERANGE, 0, 0);
run_test("1.18973149535723176508e4932", 27, uint64_t(0x7FF0000000000000),
(__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7ffeffffffffffff) << 64) +
__uint128_t(0xffffd2478338036c),
ERANGE, ERANGE, 0);
}

// These tests check subnormal behavior for 80 bit and 128 bit floats. They will
// be too small for 64 bit floats.
TEST_F(LlvmLibcStrToLDTest, SubnormalTests) {
runTest("1e-4950", 7, uint64_t(0), (__uint128_t(0x00000000000000000003)),
(__uint128_t(0x000000000000000000057c9647e1a018)), ERANGE, ERANGE,
ERANGE);
runTest("1.89e-4951", 10, uint64_t(0), (__uint128_t(0x00000000000000000001)),
(__uint128_t(0x0000000000000000000109778a006738)), ERANGE, ERANGE,
ERANGE);
runTest("4e-4966", 7, uint64_t(0), (__uint128_t(0)),
(__uint128_t(0x00000000000000000000000000000001)), ERANGE, ERANGE,
ERANGE);
run_test("1e-4950", 7, uint64_t(0), (__uint128_t(0x00000000000000000003)),
(__uint128_t(0x000000000000000000057c9647e1a018)), ERANGE, ERANGE,
ERANGE);
run_test("1.89e-4951", 10, uint64_t(0), (__uint128_t(0x00000000000000000001)),
(__uint128_t(0x0000000000000000000109778a006738)), ERANGE, ERANGE,
ERANGE);
run_test("4e-4966", 7, uint64_t(0), (__uint128_t(0)),
(__uint128_t(0x00000000000000000000000000000001)), ERANGE, ERANGE,
ERANGE);
}

TEST_F(LlvmLibcStrToLDTest, SmallNormalTests) {
runTest("3.37e-4932", 10, uint64_t(0),
(__uint128_t(0x1804cf7) << 40) + __uint128_t(0x908850712),
(__uint128_t(0x10099ee12110a) << 64) + __uint128_t(0xe24b75c0f50dc0c),
ERANGE, 0, 0);
run_test("3.37e-4932", 10, uint64_t(0),
(__uint128_t(0x1804cf7) << 40) + __uint128_t(0x908850712),
(__uint128_t(0x10099ee12110a) << 64) +
__uint128_t(0xe24b75c0f50dc0c),
ERANGE, 0, 0);
}

TEST_F(LlvmLibcStrToLDTest, ComplexHexadecimalTests) {
runTest("0x1p16383", 9, 0x7ff0000000000000, (__uint128_t(0x7ffe800000) << 40),
(__uint128_t(0x7ffe000000000000) << 64));
runTest("0x123456789abcdef", 17, 0x43723456789abcdf,
(__uint128_t(0x403791a2b3) << 40) + __uint128_t(0xc4d5e6f780),
(__uint128_t(0x403723456789abcd) << 64) +
__uint128_t(0xef00000000000000));
runTest("0x123456789abcdef0123456789ABCDEF", 33, 0x7ff0000000000000,
(__uint128_t(0x407791a2b3) << 40) + __uint128_t(0xc4d5e6f781),
(__uint128_t(0x407723456789abcd) << 64) +
__uint128_t(0xef0123456789abce));
run_test("0x1p16383", 9, 0x7ff0000000000000,
(__uint128_t(0x7ffe800000) << 40),
(__uint128_t(0x7ffe000000000000) << 64));
run_test("0x123456789abcdef", 17, 0x43723456789abcdf,
(__uint128_t(0x403791a2b3) << 40) + __uint128_t(0xc4d5e6f780),
(__uint128_t(0x403723456789abcd) << 64) +
__uint128_t(0xef00000000000000));
run_test("0x123456789abcdef0123456789ABCDEF", 33, 0x7ff0000000000000,
(__uint128_t(0x407791a2b3) << 40) + __uint128_t(0xc4d5e6f781),
(__uint128_t(0x407723456789abcd) << 64) +
__uint128_t(0xef0123456789abce));
}

TEST_F(LlvmLibcStrToLDTest, InfTests) {
runTest("INF", 3, 0x7ff0000000000000, (__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7fff000000000000) << 64));
runTest("INFinity", 8, 0x7ff0000000000000, (__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7fff000000000000) << 64));
runTest("-inf", 4, 0xfff0000000000000, (__uint128_t(0xffff800000) << 40),
(__uint128_t(0xffff000000000000) << 64));
run_test("INF", 3, 0x7ff0000000000000, (__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7fff000000000000) << 64));
run_test("INFinity", 8, 0x7ff0000000000000, (__uint128_t(0x7fff800000) << 40),
(__uint128_t(0x7fff000000000000) << 64));
run_test("-inf", 4, 0xfff0000000000000, (__uint128_t(0xffff800000) << 40),
(__uint128_t(0xffff000000000000) << 64));
}

TEST_F(LlvmLibcStrToLDTest, NaNTests) {
runTest("NaN", 3, 0x7ff8000000000000, (__uint128_t(0x7fffc00000) << 40),
(__uint128_t(0x7fff800000000000) << 64));
runTest("-nAn", 4, 0xfff8000000000000, (__uint128_t(0xffffc00000) << 40),
(__uint128_t(0xffff800000000000) << 64));
runTest("NaN()", 5, 0x7ff8000000000000, (__uint128_t(0x7fffc00000) << 40),
(__uint128_t(0x7fff800000000000) << 64));
runTest("NaN(1234)", 9, 0x7ff80000000004d2,
(__uint128_t(0x7fffc00000) << 40) + __uint128_t(0x4d2),
(__uint128_t(0x7fff800000000000) << 64) + __uint128_t(0x4d2));
runTest("NaN(0xffffffffffff)", 19, 0x7ff8ffffffffffff,
(__uint128_t(0x7fffc000ff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7fff800000000000) << 64) +
__uint128_t(0xffffffffffff));
runTest("NaN(0xfffffffffffff)", 20, 0x7fffffffffffffff,
(__uint128_t(0x7fffc00fff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7fff800000000000) << 64) +
__uint128_t(0xfffffffffffff));
runTest("NaN(0xffffffffffffffff)", 23, 0x7fffffffffffffff,
(__uint128_t(0x7fffffffff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7fff800000000000) << 64) +
__uint128_t(0xffffffffffffffff));
runTest("NaN( 1234)", 3, 0x7ff8000000000000,
(__uint128_t(0x7fffc00000) << 40),
(__uint128_t(0x7fff800000000000) << 64));
run_test("NaN", 3, 0x7ff8000000000000, (__uint128_t(0x7fffc00000) << 40),
(__uint128_t(0x7fff800000000000) << 64));
run_test("-nAn", 4, 0xfff8000000000000, (__uint128_t(0xffffc00000) << 40),
(__uint128_t(0xffff800000000000) << 64));
run_test("NaN()", 5, 0x7ff8000000000000, (__uint128_t(0x7fffc00000) << 40),
(__uint128_t(0x7fff800000000000) << 64));
run_test("NaN(1234)", 9, 0x7ff80000000004d2,
(__uint128_t(0x7fffc00000) << 40) + __uint128_t(0x4d2),
(__uint128_t(0x7fff800000000000) << 64) + __uint128_t(0x4d2));
run_test("NaN(0xffffffffffff)", 19, 0x7ff8ffffffffffff,
(__uint128_t(0x7fffc000ff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7fff800000000000) << 64) +
__uint128_t(0xffffffffffff));
run_test("NaN(0xfffffffffffff)", 20, 0x7fffffffffffffff,
(__uint128_t(0x7fffc00fff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7fff800000000000) << 64) +
__uint128_t(0xfffffffffffff));
run_test("NaN(0xffffffffffffffff)", 23, 0x7fffffffffffffff,
(__uint128_t(0x7fffffffff) << 40) + __uint128_t(0xffffffffff),
(__uint128_t(0x7fff800000000000) << 64) +
__uint128_t(0xffffffffffffffff));
run_test("NaN( 1234)", 3, 0x7ff8000000000000,
(__uint128_t(0x7fffc00000) << 40),
(__uint128_t(0x7fff800000000000) << 64));
}
14 changes: 7 additions & 7 deletions libc/test/src/string/bcmp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ TEST(LlvmLibcBcmpTest, LhsAfterRhsLexically) {
}

TEST(LlvmLibcBcmpTest, Sweep) {
static constexpr size_t kMaxSize = 1024;
char lhs[kMaxSize];
char rhs[kMaxSize];
static constexpr size_t K_MAX_SIZE = 1024;
char lhs[K_MAX_SIZE];
char rhs[K_MAX_SIZE];

const auto reset = [](char *const ptr) {
for (size_t i = 0; i < kMaxSize; ++i)
for (size_t i = 0; i < K_MAX_SIZE; ++i)
ptr[i] = 'a';
};

reset(lhs);
reset(rhs);
for (size_t i = 0; i < kMaxSize; ++i)
for (size_t i = 0; i < K_MAX_SIZE; ++i)
EXPECT_EQ(__llvm_libc::bcmp(lhs, rhs, i), 0);

reset(lhs);
reset(rhs);
for (size_t i = 0; i < kMaxSize; ++i) {
for (size_t i = 0; i < K_MAX_SIZE; ++i) {
rhs[i] = 'b';
EXPECT_NE(__llvm_libc::bcmp(lhs, rhs, kMaxSize), 0);
EXPECT_NE(__llvm_libc::bcmp(lhs, rhs, K_MAX_SIZE), 0);
rhs[i] = 'a';
}
}
6 changes: 3 additions & 3 deletions libc/test/src/string/bzero_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ using __llvm_libc::cpp::Array;
using __llvm_libc::cpp::ArrayRef;
using Data = Array<char, 2048>;

static const ArrayRef<char> kDeadcode("DEADC0DE", 8);
static const ArrayRef<char> k_deadcode("DEADC0DE", 8);

// Returns a Data object filled with a repetition of `filler`.
Data getData(ArrayRef<char> filler) {
Data get_data(ArrayRef<char> filler) {
Data out;
for (size_t i = 0; i < out.size(); ++i)
out[i] = filler[i % filler.size()];
return out;
}

TEST(LlvmLibcBzeroTest, Thorough) {
const Data dirty = getData(kDeadcode);
const Data dirty = get_data(k_deadcode);
for (size_t count = 0; count < 1024; ++count) {
for (size_t align = 0; align < 64; ++align) {
auto buffer = dirty;
Expand Down
14 changes: 7 additions & 7 deletions libc/test/src/string/memcmp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ TEST(LlvmLibcMemcmpTest, LhsAfterRhsLexically) {
}

TEST(LlvmLibcMemcmpTest, Sweep) {
static constexpr size_t kMaxSize = 1024;
char lhs[kMaxSize];
char rhs[kMaxSize];
static constexpr size_t K_MAX_SIZE = 1024;
char lhs[K_MAX_SIZE];
char rhs[K_MAX_SIZE];

const auto reset = [](char *const ptr) {
for (size_t i = 0; i < kMaxSize; ++i)
for (size_t i = 0; i < K_MAX_SIZE; ++i)
ptr[i] = 'a';
};

reset(lhs);
reset(rhs);
for (size_t i = 0; i < kMaxSize; ++i)
for (size_t i = 0; i < K_MAX_SIZE; ++i)
EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, i), 0);

reset(lhs);
reset(rhs);
for (size_t i = 0; i < kMaxSize; ++i) {
for (size_t i = 0; i < K_MAX_SIZE; ++i) {
rhs[i] = 'z';
EXPECT_LT(__llvm_libc::memcmp(lhs, rhs, kMaxSize), 0);
EXPECT_LT(__llvm_libc::memcmp(lhs, rhs, K_MAX_SIZE), 0);
rhs[i] = 'a';
}
}
10 changes: 5 additions & 5 deletions libc/test/src/string/memcpy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ using __llvm_libc::cpp::Array;
using __llvm_libc::cpp::ArrayRef;
using Data = Array<char, 2048>;

static const ArrayRef<char> kNumbers("0123456789", 10);
static const ArrayRef<char> kDeadcode("DEADC0DE", 8);
static const ArrayRef<char> k_numbers("0123456789", 10);
static const ArrayRef<char> k_deadcode("DEADC0DE", 8);

// Returns a Data object filled with a repetition of `filler`.
Data getData(ArrayRef<char> filler) {
Data get_data(ArrayRef<char> filler) {
Data out;
for (size_t i = 0; i < out.size(); ++i)
out[i] = filler[i % filler.size()];
return out;
}

TEST(LlvmLibcMemcpyTest, Thorough) {
const Data groundtruth = getData(kNumbers);
const Data dirty = getData(kDeadcode);
const Data groundtruth = get_data(k_numbers);
const Data dirty = get_data(k_deadcode);
for (size_t count = 0; count < 1024; ++count) {
for (size_t align = 0; align < 64; ++align) {
auto buffer = dirty;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/string/memset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ using __llvm_libc::cpp::Array;
using __llvm_libc::cpp::ArrayRef;
using Data = Array<char, 2048>;

static const ArrayRef<char> kDeadcode("DEADC0DE", 8);
static const ArrayRef<char> k_deadcode("DEADC0DE", 8);

// Returns a Data object filled with a repetition of `filler`.
Data getData(ArrayRef<char> filler) {
Data get_data(ArrayRef<char> filler) {
Data out;
for (size_t i = 0; i < out.size(); ++i)
out[i] = filler[i % filler.size()];
return out;
}

TEST(LlvmLibcMemsetTest, Thorough) {
const Data dirty = getData(kDeadcode);
const Data dirty = get_data(k_deadcode);
for (int value = -1; value <= 1; ++value) {
for (size_t count = 0; count < 1024; ++count) {
for (size_t align = 0; align < 64; ++align) {
Expand Down
12 changes: 6 additions & 6 deletions libc/test/src/string/stpcpy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@

TEST(LlvmLibcStpCpyTest, EmptySrc) {
const char *empty = "";
size_t srcSize = __llvm_libc::internal::string_length(empty);
size_t src_size = __llvm_libc::internal::string_length(empty);
char dest[4] = {'a', 'b', 'c', '\0'};

char *result = __llvm_libc::stpcpy(dest, empty);
ASSERT_EQ(dest + srcSize, result);
ASSERT_EQ(dest + src_size, result);
ASSERT_EQ(result[0], '\0');
ASSERT_STREQ(dest, empty);
}

TEST(LlvmLibcStpCpyTest, EmptyDest) {
const char *abc = "abc";
size_t srcSize = __llvm_libc::internal::string_length(abc);
size_t src_size = __llvm_libc::internal::string_length(abc);
char dest[4];

char *result = __llvm_libc::stpcpy(dest, abc);
ASSERT_EQ(dest + srcSize, result);
ASSERT_EQ(dest + src_size, result);
ASSERT_EQ(result[0], '\0');
ASSERT_STREQ(dest, abc);
}

TEST(LlvmLibcStpCpyTest, OffsetDest) {
const char *abc = "abc";
size_t srcSize = __llvm_libc::internal::string_length(abc);
size_t src_size = __llvm_libc::internal::string_length(abc);
char dest[7] = {'x', 'y', 'z'};

char *result = __llvm_libc::stpcpy(dest + 3, abc);
ASSERT_EQ(dest + 3 + srcSize, result);
ASSERT_EQ(dest + 3 + src_size, result);
ASSERT_EQ(result[0], '\0');
ASSERT_STREQ(dest, "xyzabc");
}
8 changes: 4 additions & 4 deletions libc/test/src/threads/call_once_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <stdatomic.h>

static constexpr unsigned int num_threads = 5;
static constexpr unsigned int NUM_THREADS = 5;
static atomic_uint thread_count;

static unsigned int call_count;
Expand All @@ -38,13 +38,13 @@ TEST(LlvmLibcCallOnceTest, CallFrom5Threads) {
call_count = 0;
thread_count = 0;

thrd_t threads[num_threads];
for (unsigned int i = 0; i < num_threads; ++i) {
thrd_t threads[NUM_THREADS];
for (unsigned int i = 0; i < NUM_THREADS; ++i) {
ASSERT_EQ(__llvm_libc::thrd_create(threads + i, func, nullptr),
static_cast<int>(thrd_success));
}

for (unsigned int i = 0; i < num_threads; ++i) {
for (unsigned int i = 0; i < NUM_THREADS; ++i) {
int retval;
ASSERT_EQ(__llvm_libc::thrd_join(threads + i, &retval),
static_cast<int>(thrd_success));
Expand Down
Loading