File: detail_operators_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (143 lines) | stat: -rw-r--r-- 4,285 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2019 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/histogram/detail/operators.hpp>
#include <limits>
#include <ostream>

using namespace boost::histogram::detail;

struct TotallyOrdered : totally_ordered<TotallyOrdered, TotallyOrdered, void> {
  TotallyOrdered(int i) : x(i) {}

  bool operator<(const TotallyOrdered& o) const noexcept { return x < o.x; }
  bool operator==(const TotallyOrdered& o) const noexcept { return x == o.x; }
  bool operator<(const int& o) const noexcept { return x < o; }
  bool operator>(const int& o) const noexcept { return x > o; }
  bool operator==(const int& o) const noexcept { return x == o; }

  int x;
};

std::ostream& operator<<(std::ostream& os, const TotallyOrdered& t) {
  os << t.x;
  return os;
}

struct PartiallyOrdered : partially_ordered<PartiallyOrdered, PartiallyOrdered, void> {
  PartiallyOrdered(double i) : x(i) {}

  bool operator<(const PartiallyOrdered& o) const noexcept { return x < o.x; }
  bool operator==(const PartiallyOrdered& o) const noexcept { return x == o.x; }
  bool operator<(const double& o) const noexcept { return x < o; }
  bool operator>(const double& o) const noexcept { return x > o; }
  bool operator==(const double& o) const noexcept { return x == o; }

  double x;
};

std::ostream& operator<<(std::ostream& os, const PartiallyOrdered& t) {
  os << t.x;
  return os;
}

template <class T, class U>
void test_nan(std::false_type) {}

template <class T, class U>
void test_nan(std::true_type) {
  const U nan = std::numeric_limits<U>::quiet_NaN();

  // IEEE 754: Any comparison with nan is false, except != which is true.
  // But some of the following tests fail on MSVC:
  //   BOOST_TEST_NOT(nan < nan); // true, should be false
  //   BOOST_TEST_NOT(nan > nan);
  //   BOOST_TEST_NOT(nan == nan);
  //   BOOST_TEST_NOT(nan <= nan); // true, should be false
  //   BOOST_TEST_NOT(nan >= nan);
  //   BOOST_TEST(nan != nan);
  // Probably related:
  // https://developercommunity.visualstudio.com/content/problem/445462/nan-nan-is-constant-folded-to-true-but-should-prob.html
  // The tests below don't fail probably because constant-folding doesn't happen.

  BOOST_TEST_NOT(T(1.0) < T(nan));
  BOOST_TEST_NOT(T(1.0) > T(nan));
  BOOST_TEST_NOT(T(1.0) == T(nan));
  BOOST_TEST_NOT(T(1.0) <= T(nan));
  BOOST_TEST_NOT(T(1.0) >= T(nan));
  BOOST_TEST(T(1.0) != T(nan));

  BOOST_TEST_NOT(T(nan) < 1.0);
  BOOST_TEST_NOT(T(nan) > 1.0);
  BOOST_TEST_NOT(T(nan) == 1.0);
  BOOST_TEST_NOT(T(nan) <= 1.0);
  BOOST_TEST_NOT(T(nan) >= 1.0);
  BOOST_TEST(T(nan) != 1.0);

  BOOST_TEST_NOT(1.0 < T(nan));
  BOOST_TEST_NOT(1.0 > T(nan));
  BOOST_TEST_NOT(1.0 == T(nan));
  BOOST_TEST_NOT(1.0 <= T(nan));
  BOOST_TEST_NOT(1.0 >= T(nan));
  BOOST_TEST(1.0 != T(nan));

  BOOST_TEST_NOT(T(nan) < T(nan));
  BOOST_TEST_NOT(T(nan) > T(nan));
  BOOST_TEST_NOT(T(nan) == T(nan));
  BOOST_TEST_NOT(T(nan) <= T(nan));
  BOOST_TEST_NOT(T(nan) >= T(nan));
  BOOST_TEST(T(nan) != T(nan));

  BOOST_TEST_NOT(T(nan) < nan);
  BOOST_TEST_NOT(T(nan) > nan);
  BOOST_TEST_NOT(T(nan) == nan);
  BOOST_TEST_NOT(T(nan) <= nan);
  BOOST_TEST_NOT(T(nan) >= nan);
  BOOST_TEST(T(nan) != nan);

  BOOST_TEST_NOT(nan < T(nan));
  BOOST_TEST_NOT(nan > T(nan));
  BOOST_TEST_NOT(nan == T(nan));
  BOOST_TEST_NOT(nan <= T(nan));
  BOOST_TEST_NOT(nan >= T(nan));
  BOOST_TEST(nan != T(nan));
}

template <class T, class U>
void test() {
  T x{1};
  U e{1}, l{0}, u{2};
  BOOST_TEST_EQ(x, e);
  BOOST_TEST_NE(x, l);
  BOOST_TEST_LT(x, u);
  BOOST_TEST_GT(x, l);
  BOOST_TEST_LE(x, e);
  BOOST_TEST_LE(x, u);
  BOOST_TEST_GE(x, e);
  BOOST_TEST_GE(x, l);

  BOOST_TEST_EQ(e, x);
  BOOST_TEST_NE(l, x);
  BOOST_TEST_LT(l, x);
  BOOST_TEST_GT(u, x);
  BOOST_TEST_LE(e, x);
  BOOST_TEST_LE(l, x);
  BOOST_TEST_GE(e, x);
  BOOST_TEST_GE(u, x);

  test_nan<T, U>(std::is_floating_point<U>{});
}

int main() {
  test<TotallyOrdered, TotallyOrdered>();
  test<TotallyOrdered, int>();
  test<PartiallyOrdered, PartiallyOrdered>();
  test<PartiallyOrdered, double>();

  return boost::report_errors();
}