Line data Source code
1 : #include <gmock/gmock.h>
2 : #include <gtest/gtest.h>
3 :
4 : #include <cfloat>
5 : #include <iostream>
6 : #include <vector>
7 :
8 : #include "vectorf.hpp"
9 :
10 : #define DOUBLE_NEAR(x) DoubleNear((x), DBL_EPSILON)
11 :
12 : using namespace testing;
13 : using namespace std;
14 :
15 : class TestThatVectorF : public Test
16 : {
17 : public:
18 12 : void SetUp() override {}
19 12 : void TearDown() override {}
20 : };
21 :
22 2 : TEST_F(TestThatVectorF, CanConstructAnUninitializedVectorOfSpecifiedLength)
23 : {
24 1 : ASSERT_NO_THROW(vectorf<double> v(2));
25 : }
26 :
27 2 : TEST_F(TestThatVectorF, ConstructorSizeThrowsWheneverSizeIsNegative)
28 : {
29 3 : ASSERT_THROW(vectorf<double> v(-1), std::bad_array_new_length);
30 : }
31 :
32 2 : TEST_F(TestThatVectorF, ConstructorSizeThrowsWheneverSizeIsZero)
33 : {
34 3 : ASSERT_THROW(vectorf<double> v(0), std::exception);
35 : }
36 :
37 2 : TEST_F(TestThatVectorF, get_sizeWorks)
38 : {
39 2 : vectorf<double> v(4);
40 :
41 1 : EXPECT_THAT(v.get_size(), Eq(4));
42 1 : }
43 :
44 2 : TEST_F(TestThatVectorF,
45 : CanConstructAnUninitializedVectorSepcifyingSizeANDInitialiseIt)
46 : {
47 2 : vectorf<int> v(4);
48 :
49 : // Must be able to modify the element to do this
50 1 : v(1) = 1;
51 1 : v(2) = 2;
52 1 : v(3) = 3;
53 1 : v(4) = 4;
54 :
55 1 : EXPECT_THAT(v.get_size(), Eq(4));
56 :
57 : // Must be able to access, but not modify, the element to do this
58 5 : for (size_t i{1}; i <= v.get_size(); i++) EXPECT_THAT(v(i), Eq(i));
59 :
60 : // // Printing verification does work
61 : // std::cout << "v =\n"
62 : // << v << '\n';
63 1 : }
64 :
65 2 : TEST_F(TestThatVectorF, ImplementsCopyConstructorCorrectly)
66 : {
67 2 : vectorf<int> v1(4);
68 :
69 1 : v1(1) = 1;
70 1 : v1(2) = 2;
71 1 : v1(3) = 3;
72 1 : v1(4) = 4;
73 :
74 : // Test that vector inputed and retrieved correctly
75 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
76 :
77 : // Test the copy constructor
78 2 : vectorf<int> v2(v1);
79 :
80 : // Test that vector v1 hasn't changed
81 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
82 :
83 : // Test that vector v2 equals v1 (Copy performed correctly)
84 5 : for (size_t i{1}; i <= v2.get_size(); i++) EXPECT_THAT(v2(i), Eq(i));
85 :
86 : // Test that copies are independent, the default copy constructor overridden
87 1 : v2(1) = 3;
88 :
89 : // Test that vector v1 hasn't changed
90 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
91 :
92 : // Test that v2 has 3 in correct position
93 5 : for (size_t i{1}; i <= v2.get_size(); i++)
94 4 : if (i == 1)
95 : {
96 1 : EXPECT_THAT(v2(i), Eq(3));
97 : }
98 : else
99 : {
100 3 : EXPECT_THAT(v2(i), Eq(i));
101 : }
102 1 : }
103 :
104 2 : TEST_F(TestThatVectorF, ImplementsCopyAssignmentCorrectly)
105 : {
106 2 : vectorf<int> v1(4);
107 :
108 1 : v1(1) = 1;
109 1 : v1(2) = 2;
110 1 : v1(3) = 3;
111 1 : v1(4) = 4;
112 :
113 : // Test that vector inputed and retrieved correctly
114 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
115 :
116 : // Test the copy constructor
117 2 : vectorf<int> v2(v1);
118 :
119 : // If quick return doesn't work, them no code coverage! No Idea how else to
120 : // test this.
121 1 : v1 = v1;
122 :
123 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
124 :
125 1 : v2 = v1;
126 :
127 : // Test that vector v1 hasn't changed
128 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
129 :
130 : // Test that vector v2 equals v1 (Copy performed correctly)
131 5 : for (size_t i{1}; i <= v2.get_size(); i++) EXPECT_THAT(v2(i), Eq(i));
132 :
133 : // Test that copies are independent, the default copy constructor overridden
134 1 : v2(2) = 4;
135 :
136 : // Test that vector v1 hasn't changed
137 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
138 :
139 : // Test that v2 has 3 in correct position
140 5 : for (size_t i{1}; i <= v2.get_size(); i++)
141 4 : if (i == 2)
142 : {
143 1 : EXPECT_THAT(v2(i), Eq(4));
144 : }
145 : else
146 : {
147 3 : EXPECT_THAT(v2(i), Eq(i));
148 : }
149 1 : }
150 :
151 2 : TEST_F(TestThatVectorF, ImplementsMoveConstructorCorrectly)
152 : {
153 2 : vectorf<int> v1(4);
154 :
155 1 : v1(1) = 1;
156 1 : v1(2) = 2;
157 1 : v1(3) = 3;
158 1 : v1(4) = 4;
159 :
160 : // Test that vector inputed and retrieved correctly
161 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
162 :
163 : // Test the copy constructor
164 2 : vectorf<int> v2 = move(v1);
165 :
166 : // Test that vector v1 hasn't changed
167 1 : EXPECT_THAT(v1.get_size(), Eq(0));
168 1 : EXPECT_EQ(&v1(1), nullptr);
169 :
170 : // Test that vector v2 equals old v1 (Copy performed correctly)
171 5 : for (size_t i{1}; i <= v2.get_size(); i++) EXPECT_THAT(v2(i), Eq(i));
172 :
173 : // Test that copies are independent, the default copy constructor overridden
174 1 : v2(1) = 5;
175 :
176 : // Test that v2 has 5 in correct position
177 5 : for (size_t i{1}; i <= v2.get_size(); i++)
178 4 : if (i == 1)
179 : {
180 1 : EXPECT_THAT(v2(i), Eq(5));
181 : }
182 : else
183 : {
184 3 : EXPECT_THAT(v2(i), Eq(i));
185 : }
186 1 : }
187 :
188 2 : TEST_F(TestThatVectorF, ImplementsMoveAssignmentCorrectly)
189 : {
190 2 : vectorf<int> v1(4);
191 :
192 1 : v1(1) = 1;
193 1 : v1(2) = 2;
194 1 : v1(3) = 3;
195 1 : v1(4) = 4;
196 :
197 : // Test that vector inputed and retrieved correctly
198 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
199 :
200 : // Test the move assignment
201 2 : vectorf<int> v2(4);
202 :
203 : // If quick return doesn't work, them matrix erases itself and this test
204 : // cannot pass.
205 1 : v1 = move(v1);
206 :
207 : // Test that matrix inputed and retrieved correctly
208 5 : for (size_t i{1}; i <= v1.get_size(); i++) EXPECT_THAT(v1(i), Eq(i));
209 :
210 1 : v2 = move(v1);
211 :
212 : // Test that vector v1 hasn't changed
213 1 : EXPECT_THAT(v1.get_size(), Eq(0));
214 1 : EXPECT_EQ(&v1(1), nullptr);
215 :
216 : // Test that vector v2 equals old v1 (Copy performed correctly)
217 5 : for (size_t i{1}; i <= v2.get_size(); i++) EXPECT_THAT(v2(i), Eq(i));
218 :
219 : // Test that copies are independent, the default copy constructor overridden
220 1 : v2(1) = 5;
221 :
222 : // Test that v2 has 5 in correct position
223 5 : for (size_t i{1}; i <= v2.get_size(); i++)
224 4 : if (i == 1)
225 : {
226 1 : EXPECT_THAT(v2(i), Eq(5));
227 : }
228 : else
229 : {
230 3 : EXPECT_THAT(v2(i), Eq(i));
231 : }
232 1 : }
233 :
234 2 : TEST_F(TestThatVectorF, CannotDoCopyAssignmentWheneverSizesDonotAgree)
235 : {
236 1 : vectorf<double> v1(4);
237 1 : vectorf<double> v2(2);
238 :
239 2 : ASSERT_THROW(v1 = v2, std::exception);
240 : }
241 :
242 2 : TEST_F(TestThatVectorF, CannotDoMoveAssignmentWheneverSizesDonotAgree)
243 : {
244 1 : vectorf<double> v1(4);
245 1 : vectorf<double> v2(2);
246 :
247 2 : ASSERT_THROW(v1 = move(v2), std::exception);
248 : }
249 :
250 2 : TEST_F(TestThatVectorF, HasConstructorWithVector)
251 : {
252 2 : vector<int> vec{1, 2, 3, 4, 5, 6};
253 2 : vectorf<int> v(vec);
254 :
255 1 : EXPECT_THAT(v.get_size(), Eq(6));
256 :
257 1 : EXPECT_THAT(v(1), Eq(1));
258 1 : EXPECT_THAT(v(2), Eq(2));
259 1 : EXPECT_THAT(v(3), Eq(3));
260 1 : EXPECT_THAT(v(4), Eq(4));
261 1 : EXPECT_THAT(v(5), Eq(5));
262 1 : EXPECT_THAT(v(6), Eq(6));
263 1 : }
|