OTest2
A C++ testing framework
comparisons.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef OTest2__INCLUDE_OTEST2_COMPARISONS_H_
20 #define OTest2__INCLUDE_OTEST2_COMPARISONS_H_
21 
22 #include <cmath>
23 #include <cstring>
24 
25 #include <otest2/typetraits.h>
26 
27 namespace OTest2 {
28 
29 template<typename A_, typename B_>
30 struct Equal {
31  bool operator()(
32  typename TypeTrait<A_>::BestArg a_,
33  typename TypeTrait<B_>::BestArg b_) const {
34  return a_ == b_;
35  }
36 };
37 
38 template<>
39 struct Equal<const char*, const char*> {
40  bool operator()(
41  const char* a_,
42  const char* b_) const {
43  return std::strcmp(a_, b_) == 0;
44  }
45 };
46 
47 template<typename A_, typename B_, typename Precision_>
48 struct EqualFP {
49  bool operator()(
50  typename TypeTrait<A_>::BestArg a_,
51  typename TypeTrait<B_>::BestArg b_,
52  typename TypeTrait<Precision_>::BestArg precision_) const {
53  return std::fabs(a_ - b_) <= precision_;
54  }
55 };
56 
57 template<typename A_, typename B_>
58 struct NotEqual {
59  bool operator()(
60  typename TypeTrait<A_>::BestArg a_,
61  typename TypeTrait<B_>::BestArg b_) const {
62  return a_ != b_;
63  }
64 };
65 
66 template<>
67 struct NotEqual<const char*, const char*> {
68  bool operator()(
69  const char* a_,
70  const char* b_) const {
71  return std::strcmp(a_, b_) != 0;
72  }
73 };
74 
75 template<typename A_, typename B_, typename Precision_>
76 struct NotEqualFP {
77  bool operator()(
78  typename TypeTrait<A_>::BestArg a_,
79  typename TypeTrait<B_>::BestArg b_,
80  typename TypeTrait<Precision_>::BestArg precision_) const {
81  return std::fabs(a_ - b_) > precision_;
82  }
83 };
84 
85 template<typename A_, typename B_>
86 struct Less {
87  bool operator()(
88  typename TypeTrait<A_>::BestArg a_,
89  typename TypeTrait<B_>::BestArg b_) const {
90  return a_ < b_;
91  }
92 };
93 
94 template<>
95 struct Less<const char*, const char*> {
96  bool operator()(
97  const char* a_,
98  const char* b_) const {
99  return std::strcmp(a_, b_) < 0;
100  }
101 };
102 
103 template<typename A_, typename B_>
104 struct LessOrEqual {
106  typename TypeTrait<A_>::BestArg a_,
107  typename TypeTrait<B_>::BestArg b_) const {
108  return a_ <= b_;
109  }
110 };
111 
112 template<>
113 struct LessOrEqual<const char*, const char*> {
115  const char* a_,
116  const char* b_) const {
117  return std::strcmp(a_, b_) <= 0;
118  }
119 };
120 
121 template<typename A_, typename B_>
122 struct Greater {
124  typename TypeTrait<A_>::BestArg a_,
125  typename TypeTrait<B_>::BestArg b_) const {
126  return a_ > b_;
127  }
128 };
129 
130 template<>
131 struct Greater<const char*, const char*> {
133  const char* a_,
134  const char* b_) const {
135  return std::strcmp(a_, b_) > 0;
136  }
137 };
138 
139 template<typename A_, typename B_>
142  typename TypeTrait<A_>::BestArg a_,
143  typename TypeTrait<B_>::BestArg b_) const {
144  return a_ >= b_;
145  }
146 };
147 
148 template<>
149 struct GreaterOrEqual<const char*, const char*> {
151  const char* a_,
152  const char* b_) const {
153  return std::strcmp(a_, b_) >= 0;
154  }
155 };
156 
157 } /* -- namespace OTest2 */
158 
159 #endif /* OTest2__INCLUDE_OTEST2_COMPARISONS_H_ */
OTest2::Less< const char *, const char * >::operator()
bool operator()(const char *a_, const char *b_) const
Definition: comparisons.h:96
OTest2::Equal< const char *, const char * >::operator()
bool operator()(const char *a_, const char *b_) const
Definition: comparisons.h:40
OTest2::GreaterOrEqual
Definition: comparisons.h:140
OTest2::TypeTrait::BestArg
Private::SelectArgType< Type_, std::is_scalar< Type_ >::value >::Value BestArg
Definition: typetraits.h:49
OTest2::NotEqualFP
Definition: comparisons.h:76
OTest2::Greater::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_) const
Definition: comparisons.h:123
OTest2::GreaterOrEqual< const char *, const char * >::operator()
bool operator()(const char *a_, const char *b_) const
Definition: comparisons.h:150
OTest2::EqualFP
Definition: comparisons.h:48
typetraits.h
OTest2::Equal::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_) const
Definition: comparisons.h:31
OTest2::Greater< const char *, const char * >::operator()
bool operator()(const char *a_, const char *b_) const
Definition: comparisons.h:132
OTest2::NotEqual< const char *, const char * >::operator()
bool operator()(const char *a_, const char *b_) const
Definition: comparisons.h:68
OTest2::Less
Definition: comparisons.h:86
OTest2::NotEqual
Definition: comparisons.h:58
OTest2::GreaterOrEqual::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_) const
Definition: comparisons.h:141
OTest2::NotEqualFP::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_, typename TypeTrait< Precision_ >::BestArg precision_) const
Definition: comparisons.h:77
OTest2::Equal
Definition: comparisons.h:30
OTest2::Less::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_) const
Definition: comparisons.h:87
OTest2
Definition: assertbean.h:25
OTest2::LessOrEqual< const char *, const char * >::operator()
bool operator()(const char *a_, const char *b_) const
Definition: comparisons.h:114
OTest2::Greater
Definition: comparisons.h:122
OTest2::LessOrEqual
Definition: comparisons.h:104
OTest2::EqualFP::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_, typename TypeTrait< Precision_ >::BestArg precision_) const
Definition: comparisons.h:49
OTest2::NotEqual::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_) const
Definition: comparisons.h:59
OTest2::LessOrEqual::operator()
bool operator()(typename TypeTrait< A_ >::BestArg a_, typename TypeTrait< B_ >::BestArg b_) const
Definition: comparisons.h:105