-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
122 lines (103 loc) · 3.57 KB
/
tests.cpp
File metadata and controls
122 lines (103 loc) · 3.57 KB
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
// tests.cpp: some doctest unit tests for StrongIndex.
//
// Copyright 2020 Charles Hussong
// Apache 2.0
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "strong-index.hpp"
#include <sstream>
using Underlying = std::size_t;
using Basic = StrongIndex::Basic<struct BasicTag, Underlying>;
using Incrementable = StrongIndex::Incrementable<struct IncTag, Underlying>;
using FullArithmetic = StrongIndex::FullArithmetic<struct FullArTag, Underlying>;
// For testing, we will want to be able to assert that
// indices have particular underlying values.
bool operator==(const Basic& index, const Underlying& value) {
return static_cast<Underlying>(index) == value;
}
bool operator==(const Incrementable& index, const Underlying& value) {
return static_cast<Underlying>(index) == value;
}
bool operator==(const FullArithmetic& index, const Underlying& value) {
return static_cast<Underlying>(index) == value;
}
template<class Index>
void test_basic(const Index& index, Underlying expectedValue) {
REQUIRE(index == expectedValue);
Index sameIndex(expectedValue);
Index differentIndex(expectedValue+1);
CHECK(index == sameIndex);
CHECK(index != differentIndex);
differentIndex = sameIndex;
CHECK(index == differentIndex);
std::stringstream ss;
ss << index;
CHECK(ss.str() == std::to_string(expectedValue));
}
template<class Index>
void test_incrementable(Index index, Underlying expectedValue) {
REQUIRE(index == expectedValue);
Index preInc(++index);
CHECK(preInc == expectedValue+1);
CHECK(index == expectedValue+1);
Index postInc(index++);
CHECK(postInc == expectedValue+1);
CHECK(index == expectedValue+2);
Index preDec(--index);
CHECK(preDec == expectedValue+1);
CHECK(index == expectedValue+1);
Index postDec(index--);
CHECK(postDec == expectedValue+1);
CHECK(index == expectedValue);
index += 2;
CHECK(index == expectedValue+2);
index -= 3;
CHECK(index == expectedValue-1);
CHECK(index+3 == expectedValue+2);
CHECK(index-4 == expectedValue-5);
}
template<class Index>
void test_full_arithmetic(Index index, Underlying expectedValue) {
REQUIRE(index == expectedValue);
REQUIRE(expectedValue >= 1);
Index twoLess(expectedValue-2);
Index almostDouble = index + twoLess;
CHECK(almostDouble == 2*expectedValue - 2);
index += twoLess;
CHECK(index == almostDouble);
Index originalIndex = almostDouble - twoLess;
CHECK(originalIndex == expectedValue);
index -= twoLess;
CHECK(index == originalIndex);
Index quadruple = 2*almostDouble + 4;
CHECK(quadruple == 4*expectedValue);
index *= 4;
CHECK(index == quadruple);
index -= 1;
Index three = index%4;
CHECK(three == static_cast<Underlying>(3));
CHECK(index/4 == expectedValue-1);
index /= 4;
CHECK(index == expectedValue-1);
}
TEST_CASE("Basic operations work") {
REQUIRE(sizeof(Basic) == sizeof(Underlying));
static constexpr Underlying value = 12;
Basic index(value);
test_basic(index, value);
}
TEST_CASE("Increment operations work") {
REQUIRE(sizeof(Incrementable) == sizeof(Underlying));
static constexpr Underlying value = 61;
Incrementable index(value);
test_basic(index, value);
test_incrementable(index, value);
}
TEST_CASE("Full arithmetic operations work") {
REQUIRE(sizeof(FullArithmetic) == sizeof(Underlying));
static constexpr Underlying value = 107792;
FullArithmetic index(value);
test_basic(index, value);
test_incrementable(index, value);
test_full_arithmetic(index, value);
}