-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertionsort.test.cpp
172 lines (133 loc) · 5.82 KB
/
insertionsort.test.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <gtest/gtest.h>
#include "insertionsort.h"
using test_data_type = int;
using test_data_count = size_t;
class InsertionSortTest : public ::testing::Test {
protected:
InsertionSortTest() :
array_values{ 3, 6, min_value_second, 5, min_value, 4, max_value_second, 7, max_value, 6 }
{
}
const test_data_type max_value{ 100 };
const test_data_type max_value_second{ 99 };
const test_data_type min_value{ 1 };
const test_data_type min_value_second{ 2 };
static const test_data_count array_values_elements_count{ 10 };
test_data_type array_values[array_values_elements_count];
public:
class ObjectA {
public:
CONSTEXPR20 ObjectA() = default;
CONSTEXPR20 explicit ObjectA(test_data_type value) :
value{ value }
{
}
NODISCARD CONSTEXPR20 bool operator> (const ObjectA& obj) const {
return this->value > obj.value;
}
NODISCARD CONSTEXPR20 bool operator< (const ObjectA& obj) const {
return this->value < obj.value;
}
// NODISCARD CONSTEXPR20 bool operator== (const ObjectA& obj) {
// return this->value == obj.value;
// }
NODISCARD CONSTEXPR20 test_data_type get() const {
return value;
}
private:
test_data_type value{};
};
};
// GCC: "undefined reference" if variables defined inside InsertionSortTest
const test_data_count InsertionSortTest::array_values_elements_count;
NODISCARD CONSTEXPR20 bool operator==(const InsertionSortTest::ObjectA& obj1, const InsertionSortTest::ObjectA& obj2) {
return obj1.get() == obj2.get();
}
NODISCARD CONSTEXPR20 bool operator>=(const InsertionSortTest::ObjectA& obj1, const InsertionSortTest::ObjectA& obj2) {
return obj1.get() >= obj2.get();
}
NODISCARD CONSTEXPR20 bool operator<=(const InsertionSortTest::ObjectA& obj1, const InsertionSortTest::ObjectA& obj2) {
return obj1.get() <= obj2.get();
}
using object_type = InsertionSortTest::ObjectA;
using store_smart_ptr_type = std::shared_ptr<object_type>;
TEST_F(InsertionSortTest, Greater) {
std::vector<test_data_type> vec(array_values, array_values + array_values_elements_count);
insertionSort(vec, ComparatorGreater<test_data_type>());
auto prev_value = vec[0];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
auto current_value = vec[index];
ASSERT_GE(prev_value, current_value);
prev_value = current_value;
}
}
TEST_F(InsertionSortTest, Less) {
std::vector<test_data_type> vec(array_values, array_values + array_values_elements_count);
insertionSort(vec, ComparatorLess<test_data_type>());
auto prev_value = vec[0];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
auto current_value = vec[index];
ASSERT_LE(prev_value, current_value);
prev_value = current_value;
}
}
TEST_F(InsertionSortTest, GreaterObject) {
object_type objects_array[array_values_elements_count];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
objects_array[index] = object_type{ array_values[index] };
}
std::vector<object_type> vec(objects_array, objects_array + array_values_elements_count);
insertionSort(vec, ComparatorGreater<object_type>());
auto prev_obj = vec[0];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
auto current_obj = vec[index];
ASSERT_GE(prev_obj, current_obj);
prev_obj = current_obj;
}
}
TEST_F(InsertionSortTest, LessObject) {
object_type objects_array[array_values_elements_count];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
objects_array[index] = object_type{ array_values[index] };
}
std::vector<object_type> vec(objects_array, objects_array + array_values_elements_count);
insertionSort(vec, ComparatorLess<object_type>());
auto prev_obj = vec[0];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
auto current_obj = vec[index];
ASSERT_LE(prev_obj, current_obj);
prev_obj = current_obj;
}
}
TEST_F(InsertionSortTest, GreaterPointersWithCustomComparator) {
store_smart_ptr_type objects_array[array_values_elements_count];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
objects_array[index] = std::make_shared<object_type>(array_values[index]);
}
std::vector<store_smart_ptr_type> vec(objects_array, objects_array + array_values_elements_count);
insertionSort(vec, compPtrMax<store_smart_ptr_type>);
auto prev_obj = vec[0];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
auto current_obj_ptr = vec[index];
ASSERT_GE(*prev_obj, *current_obj_ptr) << "heapify is incorrect when deleting.";
prev_obj = current_obj_ptr;
}
}
TEST_F(InsertionSortTest, LessPointersWithCustomComparator) {
store_smart_ptr_type objects_array[array_values_elements_count];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
objects_array[index] = std::make_shared<object_type>(array_values[index]);
}
std::vector<store_smart_ptr_type> vec(objects_array, objects_array + array_values_elements_count);
insertionSort(vec, compPtrMin<store_smart_ptr_type>);
auto prev_obj = vec[0];
for (test_data_count index = 0; index < array_values_elements_count; index++) {
auto current_obj_ptr = vec[index];
ASSERT_LE(*prev_obj, *current_obj_ptr) << "heapify is incorrect when deleting.";
prev_obj = current_obj_ptr;
}
}
TEST_F(InsertionSortTest, SizeLessTwo) {
std::vector<test_data_type> vec(array_values, array_values + 1);
insertionSort(vec, ComparatorGreater<test_data_type>());
}