From 328a65424d81d0f9189e4ae6e98a00e16649ad57 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Tue, 9 May 2023 23:23:10 +1000 Subject: [PATCH] Add cases for non-arrow missiles --- test/missiles_test.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/test/missiles_test.cpp b/test/missiles_test.cpp index f9208a478..e4ef2ac35 100644 --- a/test/missiles_test.cpp +++ b/test/missiles_test.cpp @@ -11,7 +11,7 @@ using ::testing::Lt; using ::testing::Pair; using ::testing::UnorderedElementsAre; -void TestMissileRotatesUniformly(Missile &missile, int startingFrame, int leftFrame, int rightFrame) +void TestArrowRotatesUniformly(Missile &missile, int startingFrame, int leftFrame, int rightFrame) { std::unordered_map observed {}; for (auto i = 0; i < 100; i++) { @@ -23,6 +23,18 @@ void TestMissileRotatesUniformly(Missile &missile, int startingFrame, int leftFr EXPECT_THAT(observed, UnorderedElementsAre(Pair(leftFrame, AllOf(Gt(30), Lt(70))), Pair(rightFrame, AllOf(Gt(30), Lt(70))))) << "Arrows should rotate either direction roughly 50% of the time"; } +void TestAnimatedMissileRotatesUniformly(Missile &missile, int startingDir, int leftDir, int rightDir) +{ + std::unordered_map observed {}; + for (auto i = 0; i < 100; i++) { + missile._mimfnum = startingDir; + TestRotateBlockedMissile(missile); + observed[missile._mimfnum]++; + } + + EXPECT_THAT(observed, UnorderedElementsAre(Pair(leftDir, AllOf(Gt(30), Lt(70))), Pair(rightDir, AllOf(Gt(30), Lt(70))))) << "Animated missiles should rotate either direction roughly 50% of the time"; +} + TEST(Missiles, RotateBlockedMissileArrow) { Players.resize(1); @@ -33,11 +45,20 @@ TEST(Missiles, RotateBlockedMissileArrow) Player &player = Players[0]; // missile can be a copy or a reference, there's no nullptr check and the functions that use it don't expect the instance to be part of a global structure so it doesn't really matter for this use. Missile missile = *AddMissile({ 0, 0 }, { 0, 0 }, Direction::South, MissileID::Arrow, TARGET_MONSTERS, player.getId(), 0, 0); + + // Arrows have a hardcoded frame count and use 1-indexed sprites EXPECT_EQ(missile._miAnimFrame, 1); - TestMissileRotatesUniformly(missile, 5, 4, 6); - TestMissileRotatesUniformly(missile, 1, 16, 2); - TestMissileRotatesUniformly(missile, 16, 15, 1); + TestArrowRotatesUniformly(missile, 5, 4, 6); + TestArrowRotatesUniformly(missile, 1, 16, 2); + TestArrowRotatesUniformly(missile, 16, 15, 1); + + // All other missiles use the number of 0-indexed sprites defined in MissileSpriteData + missile = *AddMissile({ 0, 0 }, { 0, 0 }, Direction::South, MissileID::Firebolt, TARGET_MONSTERS, player.getId(), 0, 0); + EXPECT_EQ(missile._mimfnum, 0); + TestAnimatedMissileRotatesUniformly(missile, 5, 4, 6); + TestAnimatedMissileRotatesUniformly(missile, 0, 15, 1); + TestAnimatedMissileRotatesUniformly(missile, 15, 14, 0); } TEST(Missiles, GetDirection8)