rippled
beast_Zero_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/beast/utility/Zero.h>
21 
22 #include <ripple/beast/unit_test.h>
23 
24 namespace beast {
25 
26 struct adl_tester
27 {
28 };
29 
31 {
32  return 0;
33 }
34 
35 namespace inner_adl_test {
36 
38 {
39 };
40 
42 {
43  return 0;
44 }
45 
46 } // namespace inner_adl_test
47 
48 class Zero_test : public beast::unit_test::suite
49 {
50 private:
52  {
53  int value;
54 
55  IntegerWrapper(int v) : value(v)
56  {
57  }
58 
59  int
60  signum() const
61  {
62  return value;
63  }
64  };
65 
66 public:
67  void
68  expect_same(bool result, bool correct, char const* message)
69  {
70  expect(result == correct, message);
71  }
72 
73  void
75  {
76  expect_same(x >= zero, x.signum() >= 0, "lhs greater-than-or-equal-to");
77  expect_same(x > zero, x.signum() > 0, "lhs greater than");
78  expect_same(x == zero, x.signum() == 0, "lhs equal to");
79  expect_same(x != zero, x.signum() != 0, "lhs not equal to");
80  expect_same(x < zero, x.signum() < 0, "lhs less than");
81  expect_same(x <= zero, x.signum() <= 0, "lhs less-than-or-equal-to");
82  }
83 
84  void
86  {
87  testcase("lhs zero");
88 
89  test_lhs_zero(-7);
90  test_lhs_zero(0);
91  test_lhs_zero(32);
92  }
93 
94  void
96  {
97  expect_same(zero >= x, 0 >= x.signum(), "rhs greater-than-or-equal-to");
98  expect_same(zero > x, 0 > x.signum(), "rhs greater than");
99  expect_same(zero == x, 0 == x.signum(), "rhs equal to");
100  expect_same(zero != x, 0 != x.signum(), "rhs not equal to");
101  expect_same(zero < x, 0 < x.signum(), "rhs less than");
102  expect_same(zero <= x, 0 <= x.signum(), "rhs less-than-or-equal-to");
103  }
104 
105  void
107  {
108  testcase("rhs zero");
109 
110  test_rhs_zero(-4);
111  test_rhs_zero(0);
112  test_rhs_zero(64);
113  }
114 
115  void
117  {
118  expect(adl_tester{} == zero, "ADL failure!");
119  expect(inner_adl_test::adl_tester2{} == zero, "ADL failure!");
120  }
121 
122  void
123  run() override
124  {
125  test_lhs_zero();
126  test_rhs_zero();
127  test_adl();
128  }
129 };
130 
131 BEAST_DEFINE_TESTSUITE(Zero, types, beast);
132 
133 } // namespace beast
beast::Zero_test::test_lhs_zero
void test_lhs_zero()
Definition: beast_Zero_test.cpp:85
beast::Zero_test::test_adl
void test_adl()
Definition: beast_Zero_test.cpp:116
beast::signum
auto signum(T const &t)
Default implementation of signum calls the method on the class.
Definition: Zero.h:54
beast::Zero_test::IntegerWrapper::signum
int signum() const
Definition: beast_Zero_test.cpp:60
beast::Zero_test::test_lhs_zero
void test_lhs_zero(IntegerWrapper x)
Definition: beast_Zero_test.cpp:74
beast::Zero_test::IntegerWrapper::IntegerWrapper
IntegerWrapper(int v)
Definition: beast_Zero_test.cpp:55
beast::adl_tester
Definition: beast_Zero_test.cpp:26
beast::inner_adl_test::signum
int signum(adl_tester2)
Definition: beast_Zero_test.cpp:41
beast::Zero_test::IntegerWrapper
Definition: beast_Zero_test.cpp:51
beast::Zero_test::test_rhs_zero
void test_rhs_zero()
Definition: beast_Zero_test.cpp:106
beast::inner_adl_test::adl_tester2
Definition: beast_Zero_test.cpp:37
beast::Zero_test::IntegerWrapper::value
int value
Definition: beast_Zero_test.cpp:53
beast::Zero_test::test_rhs_zero
void test_rhs_zero(IntegerWrapper x)
Definition: beast_Zero_test.cpp:95
beast::Zero_test::run
void run() override
Definition: beast_Zero_test.cpp:123
beast::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(aged_set, container, beast)
beast::Zero_test::expect_same
void expect_same(bool result, bool correct, char const *message)
Definition: beast_Zero_test.cpp:68
beast::Zero_test
Definition: beast_Zero_test.cpp:48
beast
Definition: base_uint.h:641