Skip to content

Commit

Permalink
test: Add tests for f{32,64}.nearest instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 14, 2020
1 parent 0744187 commit e1b4367
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/unittests/execute_floating_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ TYPED_TEST(execute_floating_point_types, unop_nan_propagation)
Instr::f32_ceil,
Instr::f32_floor,
Instr::f32_trunc,
Instr::f32_nearest,
Instr::f32_sqrt,
};

Expand Down Expand Up @@ -533,6 +534,43 @@ TYPED_TEST(execute_floating_point_types, trunc)
}
}

TYPED_TEST(execute_floating_point_types, nearest)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_nearest)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };

for (const auto& [arg, expected_trunc] : this->positive_trunc_tests)
{
ASSERT_EQ(expected_trunc, std::trunc(expected_trunc));
const auto is_even = std::fmod(expected_trunc, TypeParam{2}) == TypeParam{0};

// Computing "expected" is expanded to individual cases to check code coverage.
TypeParam expected;
if (arg == expected_trunc)
{
expected = expected_trunc;
}
else
{
const auto diff = arg - expected_trunc;
ASSERT_LT(diff, TypeParam{1});
ASSERT_GT(diff, TypeParam{0});

if (diff < TypeParam{0.5})
expected = expected_trunc; // NOLINT
else if (diff > TypeParam{0.5})
expected = expected_trunc + TypeParam{1}; // NOLINT
else if (is_even)
expected = expected_trunc;
else
expected = expected_trunc + TypeParam{1};
}

EXPECT_THAT(exec(arg), Result(expected)) << arg << ": " << expected;
EXPECT_THAT(exec(-arg), Result(-expected)) << -arg << ": " << -expected;
}
}


TYPED_TEST(execute_floating_point_types, sqrt)
{
Expand Down

0 comments on commit e1b4367

Please sign in to comment.