summaryrefslogtreecommitdiffstats
path: root/tests/PieceRotation/PieceRotationTest.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2017-02-13 18:08:27 +0100
committerGitHub <noreply@github.com>2017-02-13 18:08:27 +0100
commit7b8380c6df0d532419bee86009fbd3d77b0a818f (patch)
treede5267740459c2b9b6b6a1296e9d43eb7ac8ec28 /tests/PieceRotation/PieceRotationTest.cpp
parentMade survival the default gamemode (#3563) (diff)
parentPieceGenerator: Added rotation-aware vertical connectors. (diff)
downloadcuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.tar
cuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.tar.gz
cuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.tar.bz2
cuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.tar.lz
cuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.tar.xz
cuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.tar.zst
cuberite-7b8380c6df0d532419bee86009fbd3d77b0a818f.zip
Diffstat (limited to 'tests/PieceRotation/PieceRotationTest.cpp')
-rw-r--r--tests/PieceRotation/PieceRotationTest.cpp170
1 files changed, 170 insertions, 0 deletions
diff --git a/tests/PieceRotation/PieceRotationTest.cpp b/tests/PieceRotation/PieceRotationTest.cpp
new file mode 100644
index 000000000..3e325b54c
--- /dev/null
+++ b/tests/PieceRotation/PieceRotationTest.cpp
@@ -0,0 +1,170 @@
+// PieceRotation.cpp
+
+// Implements the tests for cPiece::cConnector::eDirection rotations
+
+#include "Globals.h"
+#include <exception>
+#include <map>
+#include "Generating/PieceGenerator.h"
+
+
+
+
+
+static cPiece::cConnector::eDirection g_AllDirections[] =
+{
+ cPiece::cConnector::dirXM,
+ cPiece::cConnector::dirXP,
+ cPiece::cConnector::dirYM,
+ cPiece::cConnector::dirYP,
+ cPiece::cConnector::dirZM,
+ cPiece::cConnector::dirZP,
+ cPiece::cConnector::dirYM_XM_ZM,
+ cPiece::cConnector::dirYM_XM_ZP,
+ cPiece::cConnector::dirYM_XP_ZM,
+ cPiece::cConnector::dirYM_XP_ZP,
+ cPiece::cConnector::dirYP_XM_ZM,
+ cPiece::cConnector::dirYP_XM_ZP,
+ cPiece::cConnector::dirYP_XP_ZM,
+ cPiece::cConnector::dirYP_XP_ZP,
+};
+
+
+
+
+
+class cTestFailure: public std::exception
+{
+public:
+ cTestFailure(const char * a_Expression, const char * a_FileName, const int a_Line):
+ std::exception(),
+ m_Expression(a_Expression),
+ m_FileName(a_FileName),
+ m_Line(a_Line)
+ {
+ }
+
+ const std::string m_Expression;
+ const std::string m_FileName;
+ const int m_Line;
+};
+
+
+
+
+
+#define EXPECT(X) if (!(X)) \
+ { \
+ ASSERT(X); \
+ throw cTestFailure(#X, __FILE__, __LINE__); \
+ }
+
+
+
+
+
+/** Tests that rotating a direction in one way and then the opposite way produces the original direction. */
+static void testBackAndForth()
+{
+ for (size_t i = 0; i < ARRAYCOUNT(g_AllDirections); ++i)
+ {
+ auto rotated = cPiece::cConnector::RotateDirectionCW(g_AllDirections[i]);
+ auto back = cPiece::cConnector::RotateDirectionCCW(rotated);
+ EXPECT(back == g_AllDirections[i]);
+ }
+ for (size_t i = 0; i < ARRAYCOUNT(g_AllDirections); ++i)
+ {
+ auto rotated = cPiece::cConnector::RotateDirectionCCW(g_AllDirections[i]);
+ auto back = cPiece::cConnector::RotateDirectionCW(rotated);
+ EXPECT(back == g_AllDirections[i]);
+ }
+}
+
+
+
+
+
+/** Tests rotating the direction 360 degrees. */
+static void testFullRotation()
+{
+ // Rotate 90 degrees CCW four times:
+ for (size_t i = 0; i < ARRAYCOUNT(g_AllDirections); ++i)
+ {
+ auto d = g_AllDirections[i];
+ for (int j = 0; j < 4; ++j)
+ {
+ d = cPiece::cConnector::RotateDirectionCCW(d);
+ }
+ EXPECT(d == g_AllDirections[i]);
+ }
+
+ // Rotate 90 degrees CW four times:
+ for (size_t i = 0; i < ARRAYCOUNT(g_AllDirections); ++i)
+ {
+ auto d = g_AllDirections[i];
+ for (int j = 0; j < 4; ++j)
+ {
+ d = cPiece::cConnector::RotateDirectionCW(d);
+ }
+ EXPECT(d == g_AllDirections[i]);
+ }
+
+ // Rotate 180 degrees twice:
+ for (size_t i = 0; i < ARRAYCOUNT(g_AllDirections); ++i)
+ {
+ auto d = g_AllDirections[i];
+ d = cPiece::cConnector::RotateDirection(d);
+ d = cPiece::cConnector::RotateDirection(d);
+ EXPECT(d == g_AllDirections[i]);
+ }
+}
+
+
+
+
+
+/** Tests that no two values are rotated onto the same destination value. */
+template <class Fn>
+static void testPermutation(Fn & a_Fn)
+{
+ std::map<cPiece::cConnector::eDirection, int> numDest;
+ for (size_t i = 0; i < ARRAYCOUNT(g_AllDirections); ++i)
+ {
+ auto d = a_Fn(g_AllDirections[i]);
+ EXPECT(numDest[d] == 0);
+ numDest[d] = 1;
+ }
+}
+
+
+
+
+
+int main(void)
+{
+ LOG("PieceRotation test starting.");
+ try
+ {
+ testBackAndForth();
+ testFullRotation();
+ testPermutation(cPiece::cConnector::RotateDirection);
+ testPermutation(cPiece::cConnector::RotateDirectionCW);
+ testPermutation(cPiece::cConnector::RotateDirectionCCW);
+ }
+ catch (const cTestFailure & f)
+ {
+ LOGERROR("Test failed:\tExpression: %s\n\tFile: %s (%d)", f.m_Expression.c_str(), f.m_FileName.c_str(), f.m_Line);
+ return 1;
+ }
+ catch (const std::exception & exc)
+ {
+ LOGERROR("Exception caught: %s", exc.what());
+ return 2;
+ }
+ LOG("PieceRotation test succeeded.");
+ return 0;
+}
+
+
+
+