Skip to content

Commit

Permalink
Add operator<=() and operator>=() to approx per issue #38
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Jan 25, 2017
1 parent b56ae07 commit 1f4a035
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/lest/lest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ class approx
friend bool operator != ( double lhs, approx const & rhs ) { return !operator==( lhs, rhs ); }
friend bool operator != ( approx const & lhs, double rhs ) { return !operator==( rhs, lhs ); }

friend bool operator <= ( double lhs, approx const & rhs ) { return lhs < rhs.magnitude_ || lhs == rhs; }
friend bool operator <= ( approx const & lhs, double rhs ) { return lhs.magnitude_ < rhs || lhs == rhs; }
friend bool operator >= ( double lhs, approx const & rhs ) { return lhs > rhs.magnitude_ || lhs == rhs; }
friend bool operator >= ( approx const & lhs, double rhs ) { return lhs.magnitude_ > rhs || lhs == rhs; }

private:
double epsilon_;
double scale_;
Expand Down
5 changes: 5 additions & 0 deletions include/lest/lest_cpp03.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ class approx
friend bool operator != ( double lhs, approx const & rhs ) { return !operator==( lhs, rhs ); }
friend bool operator != ( approx const & lhs, double rhs ) { return !operator==( rhs, lhs ); }

friend bool operator <= ( double lhs, approx const & rhs ) { return lhs < rhs.magnitude_ || lhs == rhs; }
friend bool operator <= ( approx const & lhs, double rhs ) { return lhs.magnitude_ < rhs || lhs == rhs; }
friend bool operator >= ( double lhs, approx const & rhs ) { return lhs > rhs.magnitude_ || lhs == rhs; }
friend bool operator >= ( approx const & lhs, double rhs ) { return lhs.magnitude_ > rhs || lhs == rhs; }

private:
double epsilon_;
double scale_;
Expand Down
11 changes: 9 additions & 2 deletions test/test_lest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,20 @@ const lest::test specification[] =
CASE( "Approximate compares properly" )
{
EXPECT( 1.23 == approx( 1.23 ) );
EXPECT( 1.23 <= approx( 1.23 ) );
EXPECT( 1.23 >= approx( 1.23 ) );
EXPECT( 1.23 != approx( 1.24 ) );

EXPECT_NOT( 1.24 <= approx( 1.23 ) );
EXPECT_NOT( 1.23 >= approx( 1.24 ) );
},

CASE( "Approximate using epsilon compares properly" )
{
EXPECT( 1.23 != approx( 1.231 ) );
EXPECT( 1.23 == approx( 1.231 ).epsilon( 0.1 ) );
EXPECT( 1.23 != approx( 1.231 ) );
EXPECT( 1.231 >= approx( 1.23 ) );
EXPECT( 1.23 == approx( 1.231 ).epsilon( 0.1 ) );
EXPECT( 1.23 <= approx( 1.231 ).epsilon( 0.1 ) );
},

CASE( "Approximate using custom epsilon compares properly" )
Expand Down
11 changes: 9 additions & 2 deletions test/test_lest_cpp03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,20 @@ CASE( "Has single expression evaluation" )
CASE( "Approximate compares properly [approx][basic]" )
{
EXPECT( 1.23 == approx( 1.23 ) );
EXPECT( 1.23 <= approx( 1.23 ) );
EXPECT( 1.23 >= approx( 1.23 ) );
EXPECT( 1.23 != approx( 1.24 ) );

EXPECT_NOT( 1.24 <= approx( 1.23 ) );
EXPECT_NOT( 1.23 >= approx( 1.24 ) );
}

CASE( "Approximate using epsilon compares properly [approx][epsilon]" )
{
EXPECT( 1.23 != approx( 1.231 ) );
EXPECT( 1.23 == approx( 1.231 ).epsilon( 0.1 ) );
EXPECT( 1.23 != approx( 1.231 ) );
EXPECT( 1.231 >= approx( 1.23 ) );
EXPECT( 1.23 == approx( 1.231 ).epsilon( 0.1 ) );
EXPECT( 1.23 <= approx( 1.231 ).epsilon( 0.1 ) );
}

CASE( "Approximate using custom epsilon compares properly [approx][custom]" )
Expand Down

0 comments on commit 1f4a035

Please sign in to comment.