Skip to content

Commit

Permalink
Add tests to check that isnan doesn't cause FP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdewar authored and vitaut committed May 2, 2024
1 parent 8a8f482 commit 9234fe8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <stdint.h> // uint32_t

#include <cfenv> // fegetexceptflag and FE_ALL_EXCEPT
#include <climits> // INT_MAX
#include <cmath> // std::signbit
#include <condition_variable> // std::condition_variable
Expand Down Expand Up @@ -109,6 +110,14 @@ TEST(float_test, isfinite) {
#endif
}

void check_no_fp_exception() {
fexcept_t fe;
fegetexceptflag(&fe, FE_ALL_EXCEPT);

// No exception flags should have been set
EXPECT_TRUE(fe == 0);
}

template <typename Float> void check_isnan() {
using fmt::detail::isnan;
EXPECT_FALSE(isnan(Float(0.0)));
Expand All @@ -121,6 +130,17 @@ template <typename Float> void check_isnan() {
EXPECT_FALSE(isnan(Float(-limits::infinity())));
EXPECT_TRUE(isnan(Float(limits::quiet_NaN())));
EXPECT_TRUE(isnan(Float(-limits::quiet_NaN())));

// Sanity check: make sure no error has occurred before we start
check_no_fp_exception();

// Check that no exception is raised for the non-NaN case
isnan(Float(42.0));
check_no_fp_exception();

// Check that no exception is raised for the NaN case
isnan(Float(limits::quiet_NaN()));
check_no_fp_exception();
}

TEST(float_test, isnan) {
Expand Down

0 comments on commit 9234fe8

Please sign in to comment.