Skip to content

Commit d3e229a

Browse files
committed
Add test section for total_greater
1 parent a037194 commit d3e229a

3 files changed

Lines changed: 63 additions & 36 deletions

File tree

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ add_executable(main-tests
187187
comparators/flip_not.cpp
188188
comparators/natural_less.cpp
189189
comparators/projection_compare.cpp
190-
comparators/total_less.cpp
190+
comparators/total_order.cpp
191191
comparators/transparent_comparators.cpp
192192

193193
# Distributions tests

tests/comparators/total_less.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/comparators/total_order.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2016-2026 Morwenn
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
#include <cmath>
6+
#include <limits>
7+
#include <catch2/catch_test_macros.hpp>
8+
#include <cpp-sort/comparators/total_greater.h>
9+
#include <cpp-sort/comparators/total_less.h>
10+
#include <cpp-sort/sorters/heap_sorter.h>
11+
12+
TEST_CASE( "IEEE 754 totalOrder implementation", "[comparison]" )
13+
{
14+
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
15+
constexpr double inf = std::numeric_limits<double>::infinity();
16+
17+
double array[] = { +1.0, +inf, -1.0, -nan, +0.0, -inf, +nan, -0.0 };
18+
19+
SECTION( "total_less" )
20+
{
21+
cppsort::heap_sort(array, cppsort::total_less);
22+
23+
// Check for IEEE 754 totalOrder,
24+
// ignore quiet vs. signaling NaNs
25+
CHECK( std::isnan(array[0]) );
26+
CHECK( std::signbit(array[0]) );
27+
CHECK( std::isinf(array[1]) );
28+
CHECK( std::signbit(array[1]) );
29+
CHECK( array[2] == -1.0 );
30+
CHECK( array[3] == 0.0 );
31+
CHECK( std::signbit(array[3]) );
32+
CHECK( array[4] == 0.0 );
33+
CHECK( not std::signbit(array[4]) );
34+
CHECK( array[5] == +1.0 );
35+
CHECK( std::isinf(array[6]) );
36+
CHECK( not std::signbit(array[6]) );
37+
CHECK( std::isnan(array[7]) );
38+
CHECK( not std::signbit(array[7]) );
39+
}
40+
41+
SECTION( "total_greater" )
42+
{
43+
cppsort::heap_sort(array, cppsort::total_greater);
44+
45+
// Check for IEEE 754 totalOrder,
46+
// ignore quiet vs. signaling NaNs
47+
CHECK( not std::signbit(array[0]) );
48+
CHECK( std::isnan(array[0]) );
49+
CHECK( std::isinf(array[1]) );
50+
CHECK( not std::signbit(array[1]) );
51+
CHECK( array[2] == +1.0 );
52+
CHECK( array[3] == 0.0 );
53+
CHECK( not std::signbit(array[3]) );
54+
CHECK( array[4] == 0.0 );
55+
CHECK( std::signbit(array[4]) );
56+
CHECK( array[5] == -1.0 );
57+
CHECK( std::isinf(array[6]) );
58+
CHECK( std::signbit(array[6]) );
59+
CHECK( std::isnan(array[7]) );
60+
CHECK( std::signbit(array[7]) );
61+
}
62+
}

0 commit comments

Comments
 (0)