Skip to content

Commit 555128c

Browse files
committed
better PositionTest
1 parent 3b20bc3 commit 555128c

File tree

6 files changed

+215
-20
lines changed

6 files changed

+215
-20
lines changed

src/backend/position/position.h

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ class Vector::Iterator {
7878
yNeg = 1 - 2 * (vector.dy < 0);
7979
end = (yNeg * vector.dy + 1) * width - 1;
8080
}
81+
inline bool operator==(const Iterator& other) const {
82+
return (
83+
xNeg == other.xNeg && yNeg == other.yNeg &&
84+
end == other.end && cur == other.cur &&
85+
width == other.width && notDone == other.notDone
86+
);
87+
}
88+
inline bool operator!=(const Iterator& other) const {
89+
return (
90+
xNeg != other.xNeg || yNeg != other.yNeg ||
91+
end != other.end || cur != other.cur ||
92+
width != other.width || notDone != other.notDone
93+
);
94+
}
8195
inline Iterator& operator++() {
8296
next();
8397
return *this;
@@ -106,8 +120,8 @@ class Vector::Iterator {
106120
cur += notDone;
107121
}
108122
inline void prev() {
123+
cur -= notDone && (cur != 0);
109124
notDone = true;
110-
cur -= cur != 0;
111125
}
112126
std::uint8_t xNeg;
113127
std::uint8_t yNeg;
@@ -254,6 +268,18 @@ class Position::Iterator {
254268
this->start.y = start.y;
255269
}
256270
}
271+
inline bool operator==(const Iterator& other) const {
272+
return (
273+
end == other.end && cur == other.cur &&
274+
width == other.width && notDone == other.notDone
275+
);
276+
}
277+
inline bool operator!=(const Iterator& other) const {
278+
return (
279+
end != other.end || cur != other.cur ||
280+
width != other.width || notDone != other.notDone
281+
);
282+
}
257283
inline Iterator& operator++() noexcept {
258284
next();
259285
return *this;
@@ -282,7 +308,7 @@ class Position::Iterator {
282308
cur += notDone;
283309
}
284310
inline void prev() {
285-
cur -= notDone && cur != 0;
311+
cur -= notDone && (cur != 0);
286312
notDone = true;
287313
}
288314
Position start;
@@ -436,6 +462,18 @@ class Size::Iterator {
436462
width = size.w;
437463
end = size.area() - 1;
438464
}
465+
inline bool operator==(const Iterator& other) const {
466+
return (
467+
end == other.end && cur == other.cur &&
468+
width == other.width && notDone == other.notDone
469+
);
470+
}
471+
inline bool operator!=(const Iterator& other) const {
472+
return (
473+
end != other.end || cur != other.cur ||
474+
width != other.width || notDone != other.notDone
475+
);
476+
}
439477
inline Iterator& operator++() {
440478
next();
441479
return *this;
@@ -466,13 +504,12 @@ class Size::Iterator {
466504
// inline Vector operator->() const { return *(*this); }
467505

468506
private:
469-
470507
inline void next() {
471508
notDone = cur != end;
472509
cur += notDone;
473510
}
474511
inline void prev() {
475-
cur -= cur != 0;
512+
cur -= notDone && (cur != 0);
476513
notDone = (bool)end;
477514
}
478515
unsigned int end;

tests/circuitTest.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "circuitTest.h"
22

3-
Position randPos() {
4-
return Position(rand() % 100000, rand() % 100000);
5-
}
6-
3+
#include "randomGens.h"
74
const int loopsPerTest = 100; // upped
85

96
void CircuitTest::SetUp() {

tests/positionTest.cpp

Lines changed: 162 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#include "positionTest.h"
2-
#include "util/uuid.h"
32

4-
void PositionTest::SetUp() { }
5-
6-
void PositionTest::TearDown() {
7-
8-
}
3+
#include "randomGens.h"
94

105
TEST_F(PositionTest, VectorBasicOperations) {
116
Vector a(5, 5);
@@ -162,3 +157,164 @@ TEST_F(PositionTest, FPositionComparisons) {
162157
ASSERT_TRUE(a2 == FPosition(3.2, 6.1));
163158
ASSERT_TRUE(a * 3.1 == FPosition(19.84, 28.52));
164159
}
160+
161+
TEST_F(PositionTest, ZeroConstructor) {
162+
Position zeroPos;
163+
ASSERT_EQ(zeroPos.x, 0);
164+
ASSERT_EQ(zeroPos.y, 0);
165+
166+
FPosition zeroFPos;
167+
ASSERT_EQ(zeroFPos.x, 0);
168+
ASSERT_EQ(zeroFPos.y, 0);
169+
ASSERT_TRUE(zeroFPos.isValid());
170+
171+
Vector zeroVec;
172+
ASSERT_EQ(zeroVec.dx, 0);
173+
ASSERT_EQ(zeroVec.dy, 0);
174+
175+
FVector zeroFVec;
176+
ASSERT_EQ(zeroFVec.dx, 0);
177+
ASSERT_EQ(zeroFVec.dy, 0);
178+
179+
Size zeroSize;
180+
ASSERT_EQ(zeroSize.w, 0);
181+
ASSERT_EQ(zeroSize.h, 0);
182+
183+
FSize zeroFSize;
184+
ASSERT_EQ(zeroFSize.w, 0);
185+
ASSERT_EQ(zeroFSize.h, 0);
186+
ASSERT_TRUE(zeroFSize.isValid());
187+
188+
Orientation zeroOrientation;
189+
ASSERT_EQ(zeroOrientation.rotation, Rotation::ZERO);
190+
ASSERT_EQ(zeroOrientation.flipped, false);
191+
}
192+
193+
TEST_F(PositionTest, FPositionInvalid) {
194+
FPosition zeroVec = FPosition::getInvalid();
195+
ASSERT_TRUE(zeroVec.isInvalid());
196+
ASSERT_FALSE(zeroVec.isValid());
197+
zeroVec = FPosition(1, 2);
198+
ASSERT_EQ(zeroVec.x, 1);
199+
ASSERT_EQ(zeroVec.y, 2);
200+
ASSERT_FALSE(zeroVec.isInvalid());
201+
ASSERT_TRUE(zeroVec.isValid());
202+
zeroVec.setInvalid();
203+
ASSERT_TRUE(zeroVec.isInvalid());
204+
ASSERT_FALSE(zeroVec.isValid());
205+
zeroVec.x = 0;
206+
ASSERT_TRUE(zeroVec.isInvalid());
207+
ASSERT_FALSE(zeroVec.isValid());
208+
zeroVec.y = 0;
209+
ASSERT_EQ(zeroVec.x, 0);
210+
ASSERT_EQ(zeroVec.y, 0);
211+
ASSERT_FALSE(zeroVec.isInvalid());
212+
ASSERT_TRUE(zeroVec.isValid());
213+
zeroVec.setInvalid();
214+
ASSERT_TRUE(zeroVec.isInvalid());
215+
ASSERT_FALSE(zeroVec.isValid());
216+
zeroVec.y = 0;
217+
ASSERT_TRUE(zeroVec.isInvalid());
218+
ASSERT_FALSE(zeroVec.isValid());
219+
}
220+
221+
TEST_F(PositionTest, toString) {
222+
Position pos;
223+
ASSERT_EQ(pos.toString(), "(0, 0)");
224+
pos.x = 10;
225+
ASSERT_EQ(pos.toString(), "(10, 0)");
226+
pos.y = -51;
227+
ASSERT_EQ(pos.toString(), "(10, -51)");
228+
pos.x = -1161231;
229+
ASSERT_EQ(pos.toString(), "(-1161231, -51)");
230+
231+
Vector vec;
232+
ASSERT_EQ(vec.toString(), "<0, 0>");
233+
vec.dx = 10;
234+
ASSERT_EQ(vec.toString(), "<10, 0>");
235+
vec.dy = -51;
236+
ASSERT_EQ(vec.toString(), "<10, -51>");
237+
vec.dx = -1161231;
238+
ASSERT_EQ(vec.toString(), "<-1161231, -51>");
239+
240+
Size size;
241+
ASSERT_EQ(size.toString(), "0x0");
242+
size.w = 10;
243+
ASSERT_EQ(size.toString(), "10x0");
244+
size.h = 14371;
245+
ASSERT_EQ(size.toString(), "10x14371");
246+
size.h = -51;
247+
ASSERT_EQ(size.toString(), "10x-51");
248+
size.w = -1161231;
249+
ASSERT_EQ(size.toString(), "-1161231x-51");
250+
251+
Orientation orientation;
252+
253+
ASSERT_EQ(orientation.toString(), "(r:0, f:0)");
254+
}
255+
256+
TEST_F(PositionTest, vectorIter) {
257+
for (unsigned int i = 0; i < 50; i++) {
258+
Vector v = randVec();
259+
v.dx %= 1000; // takes too long otherwise
260+
v.dy %= 1000;
261+
262+
unsigned long long area = 0;
263+
for (Vector::Iterator iter = v.iter(); iter; iter++) {
264+
Vector::Iterator curIter = iter;
265+
Vector::Iterator nextIter = (++iter)--;
266+
ASSERT_EQ(iter++, curIter);
267+
ASSERT_EQ(iter--, nextIter);
268+
ASSERT_EQ(++iter, nextIter);
269+
ASSERT_EQ(--iter, curIter);
270+
area++;
271+
}
272+
ASSERT_EQ(area, (v.dx+1)*(v.dy+1));
273+
}
274+
}
275+
276+
TEST_F(PositionTest, sizeIter) {
277+
for (unsigned int i = 0; i < 50; i++) {
278+
Size s = randSize();
279+
s.w = abs(s.w) % 1000 + 1; // takes too long otherwise, move than 1 w&h
280+
s.h = abs(s.h) % 1000 + 1;
281+
ASSERT_TRUE(s.isValid());
282+
283+
unsigned long long area = 0;
284+
for (Size::Iterator iter = s.iter(); iter; iter++) {
285+
Size::Iterator curIter = iter;
286+
Size::Iterator nextIter = (++iter)--;
287+
ASSERT_EQ(iter++, curIter);
288+
ASSERT_EQ(iter--, nextIter);
289+
ASSERT_EQ(++iter, nextIter);
290+
ASSERT_EQ(--iter, curIter);
291+
area++;
292+
}
293+
ASSERT_EQ(area, s.w*s.h);
294+
ASSERT_EQ(area, s.area());
295+
}
296+
}
297+
298+
TEST_F(PositionTest, positionIter) {
299+
for (unsigned int i = 0; i < 50; i++) {
300+
Position p1 = randPos();
301+
Position p2 = randPos();
302+
p1.x %= 1000; // takes too long otherwise
303+
p1.y %= 1000; // takes too long otherwise
304+
p2.x %= 1000; // takes too long otherwise
305+
p2.y %= 1000; // takes too long otherwise
306+
307+
unsigned long long area = 0;
308+
// ASSERT_EQ(*(p1.iterTo(p2)), p1); // not guaranteed. Should it be?
309+
for (Position::Iterator iter = p1.iterTo(p2); iter; iter++) {
310+
Position::Iterator curIter = iter;
311+
Position::Iterator nextIter = (++iter)--;
312+
ASSERT_EQ(iter++, curIter);
313+
ASSERT_EQ(iter--, nextIter);
314+
ASSERT_EQ(++iter, nextIter);
315+
ASSERT_EQ(--iter, curIter);
316+
area++;
317+
}
318+
ASSERT_EQ(area, (abs(p1.x - p2.x)+1) * (abs(p1.y - p2.y)+1));
319+
}
320+
}

tests/positionTest.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
#define positionTests_h
33

44
#include <gtest/gtest.h>
5-
#include "backend/position/position.h"
65

7-
class PositionTest : public ::testing::Test {
8-
protected:
9-
void SetUp() override;
10-
void TearDown() override;
11-
};
6+
class PositionTest : public ::testing::Test {};
127

138
#endif /* PositionTests_h */

tests/randomGens.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "randomGens.h"
2+
3+
Position randPos() { return Position(rand() % 100000, rand() % 100000); }
4+
Vector randVec() { return Vector(rand() % 100000, rand() % 100000); }
5+
Size randSize() { return Size(rand() % 100000, rand() % 100000); }

tests/randomGens.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "backend/position/position.h"
2+
3+
Position randPos();
4+
Vector randVec();
5+
Size randSize();

0 commit comments

Comments
 (0)