Adding epsilon test for Position. Also adding tests for GridPosition and Position.

This commit is contained in:
Ólafur Waage 2021-09-06 16:24:30 +02:00
parent 4c47509d1b
commit 00bfd15074
2 changed files with 47 additions and 5 deletions

View file

@ -31,11 +31,13 @@ constexpr bool operator!=(const GridPosition & a, const GridPosition & b) {
return !(a == b);
}
constexpr bool operator==(const Position & a, const Position & b) {
return a.x == b.x && a.y == b.y;
inline bool operator==(const Position & a, const Position & b) {
// This is ok as a test unless x and y become very large.
constexpr double epsilon = std::numeric_limits<double>::epsilon();
return std::abs(a.x - b.x) <= epsilon && std::abs(a.y - b.y) <= epsilon;
}
constexpr bool operator!=(const Position & a, const Position & b) {
inline bool operator!=(const Position & a, const Position & b) {
return !(a == b);
}

View file

@ -1,6 +1,46 @@
#include "Position.hpp"
#include <gtest/gtest.h>
TEST(PositionTest, Init) {
EXPECT_EQ(1, 1);
TEST(PositionTest, PositionInit) {
pacman::Position pos;
EXPECT_DOUBLE_EQ(pos.x, 0.0);
EXPECT_DOUBLE_EQ(pos.y, 0.0);
pacman::Position pos2{ 10.0, 20.0 };
EXPECT_DOUBLE_EQ(pos2.x, 10.0);
EXPECT_DOUBLE_EQ(pos2.y, 20.0);
}
TEST(PositionTest, GridPositionInit) {
pacman::GridPosition gridPos{ 10, 20 };
EXPECT_EQ(gridPos.x, 10);
EXPECT_EQ(gridPos.y, 20);
}
TEST(PositionTest, ConvertPositionToGridPosition) {
pacman::Position pos{ 10.0, 20.0 };
const auto gridPos = pacman::positionToGridPosition(pos);
EXPECT_EQ(gridPos.x, 10);
EXPECT_EQ(gridPos.y, 20);
}
TEST(PositionTest, ConvertGridPositionToPosition) {
pacman::GridPosition gridPos{ 10, 20 };
const auto pos = pacman::gridPositionToPosition(gridPos);
EXPECT_DOUBLE_EQ(pos.x, 10.0);
EXPECT_DOUBLE_EQ(pos.y, 20.0);
}
TEST(PositionTest, PositionEquality) {
pacman::Position pos1{ 10.0, 20.0 };
pacman::Position pos2{ 10.0, 20.0 };
EXPECT_TRUE(pos1 == pos2);
pacman::Position pos3{ 9.9, 19.9 };
EXPECT_FALSE(pos1 == pos3);
pos3.x += 0.1;
pos3.y += 0.1;
EXPECT_TRUE(pos1 == pos3);
}