Skip to content

Commit dd48667

Browse files
committed
adds tests for equals operator
1 parent 791d655 commit dd48667

File tree

4 files changed

+77
-24
lines changed

4 files changed

+77
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
build
1+
build
2+
.vscode

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ if (NOT DEFINED CMAKE_CXX_STANDARD)
1010
set(CMAKE_CXX_STANDARD 11)
1111
endif()
1212

13+
if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
14+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
15+
endif()
16+
1317
include_directories( include )
1418
add_subdirectory( tests )

include/attwoodn/expression_tree.hpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,25 @@ namespace attwoodn::expression_tree {
77
namespace op {
88

99
template<typename A, typename B>
10-
class less_than {
11-
bool operator()(A a, B b) {
12-
return a < b;
13-
}
14-
};
10+
inline bool less_than(A a, B b) {
11+
return a < b;
12+
}
1513

1614
template<typename A, typename B>
17-
class greater_than {
18-
bool operator()(A a, B b) {
19-
return a > b;
20-
}
21-
};
15+
inline bool greater_than(A a, B b) {
16+
return a > b;
17+
}
2218

2319
template<typename A, typename B>
24-
class equals {
25-
bool operator()(A a, B b) {
26-
return a == b;
27-
}
28-
};
20+
inline bool equals(A a, B b) {
21+
return a == b;
22+
}
2923

3024
template<typename A, typename B>
31-
class not_equals {
32-
bool operator()(A a, B b) {
33-
return a != b;
34-
}
35-
};
25+
inline bool not_equals(A a, B b) {
26+
return a != b;
27+
}
28+
3629
}
3730

3831
static inline void say_hello() {

tests/operators.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,67 @@
11
#include <attwoodn/expression_tree.hpp>
2+
#include <limits>
3+
#include <iomanip>
24
#include <cassert>
35

46
using namespace attwoodn::expression_tree;
57

6-
int main(int argc, char** argv) {
7-
std::string hello = "hello world";
8+
void test_equals();
89

9-
//assert(op::equals(hello, hello));
10+
int main(int argc, char** argv) {
11+
test_equals();
1012

1113
return EXIT_SUCCESS;
14+
}
15+
16+
void test_equals() {
17+
// test string equality/inequality
18+
{
19+
std::string hello = "hello world";
20+
21+
assert(op::equals(hello, hello));
22+
assert(op::equals(hello, "hello world"));
23+
assert(op::equals("hello world", "hello world"));
24+
assert(op::equals(" ", " "));
25+
26+
assert(!op::equals(" ", " "));
27+
assert(!op::equals("hello world", "hey, world"));
28+
assert(!op::equals("test", " test "));
29+
}
30+
31+
// test integer equality/inequality
32+
{
33+
assert(op::equals(5, 5));
34+
assert(op::equals(0, 0));
35+
assert(op::equals(-5, -5));
36+
assert(op::equals(123456789, 123456789));
37+
assert(op::equals(123456789, 123456789L));
38+
assert(op::equals(255, 0xff));
39+
assert(op::equals(0xbeef, 0xbeef));
40+
assert(op::equals(std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max()));
41+
assert(op::equals(std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint16_t>::max()));
42+
assert(op::equals(std::numeric_limits<int16_t>::max(), std::numeric_limits<uint16_t>::max() / 2));
43+
44+
assert(!op::equals(-5, 5));
45+
assert(!op::equals(0, 1));
46+
assert(!op::equals(254, 0xff));
47+
assert(!op::equals(123456789, 123456789000L));
48+
assert(!op::equals(std::numeric_limits<int16_t>::max(), std::numeric_limits<uint16_t>::max() / 2.0));
49+
}
50+
51+
// test float equality/inequality
52+
{
53+
assert(op::equals(5.0, 5.0));
54+
assert(op::equals(0.000, 0.000));
55+
assert(op::equals(-5.0, -5.0));
56+
assert(op::equals(3.400f, 3.4f));
57+
assert(op::equals((float) 99999.0, (double) 99999.0));
58+
assert(op::equals(12345.999f, 12345.999f));
59+
60+
assert(!op::equals(5.0, -5.0));
61+
assert(!op::equals(0.000, 0.000001));
62+
assert(!op::equals(-5.0, -4.999999));
63+
64+
// there are some understandable difficulties with comparing floats directly. These should be equal
65+
assert(!op::equals((float) 99999.999, (double) 99999.999));
66+
}
1267
}

0 commit comments

Comments
 (0)