Add EffectTests.cpp to project.
All checks were successful
Emscripten Build / Build_and_Deploy_Web_Build (push) Successful in 7m59s

This commit is contained in:
AMay 2026-04-07 14:28:18 -05:00
parent 582e4ef439
commit ac2f0290d7

View File

@ -0,0 +1,212 @@
#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2026 Amy Sigona <sigonasr2@gmail.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#include "CppUnitTest.h"
#include "AdventuresInLestoria.h"
#include "Tutorial.h"
#include <random>
#include "ItemDrop.h"
#include"SoundEffect.h"
#include"GameHelper.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace olc::utils;
INCLUDE_GFX
INCLUDE_ITEM_DATA
INCLUDE_DAMAGENUMBER_LIST
INCLUDE_INITIALIZEGAMECONFIGURATIONS
INCLUDE_MONSTER_LIST
extern std::mt19937 rng;
namespace EffectTests
{
TEST_CLASS(EffectTest)
{
public:
std::unique_ptr<AiL>testGame;
InputGroup testKeyboardInput;
Player*player;
HWButton*testKey;
TEST_METHOD_INITIALIZE(EffectTestInitialize){
InitializeGameConfigurations();
rng=std::mt19937{57189U};//Establish a fixed random seed on setup so the exact same results are generated every test run.
testGame.reset(new AiL(true));
ItemAttribute::Initialize();
ItemInfo::InitializeItems();
testGame->InitializeGraphics();
testGame->InitializeClasses();
sig::Animation::InitializeAnimations();
testGame->InitializeDefaultKeybinds();
testGame->InitializePlayer();
sig::Animation::SetupPlayerAnimations();
Menu::InitializeMenus();
Tutorial::Initialize();
Stats::InitializeDamageReductionTable();
Monster::InitializeStrategies();
SoundEffect::Initialize();
GameState::Initialize();
GameState::STATE=GameState::states.at(States::State::GAME_RUN);
ItemDrop::Initialize();
testGame->ResetLevelStates();
#pragma region Setup a fake test map and test monster
game->MAP_DATA.Unlock();
game->MAP_DATA["CAMPAIGN_1_1"];
game->MAP_DATA.at("CAMPAIGN_1_1")._SetMapData(MapTag{24*24,24*24,24,24});
ItemDrop::ClearDrops();
MonsterData testMonsterData{"TestName","Test Monster",1000,10,5,{MonsterDropData{"Health Potion",100.f,1,1}},200.f};
MONSTER_DATA["TestName"]=testMonsterData;
#pragma endregion
testGame->InitializeCamera();
player=testGame->GetPlayer();
//Setup key "0" as a test input
testKeyboardInput.AddKeybind(Input{InputType::KEY,0});
testKey=testGame->GetKeyboardState(0);
testGame->olc_UpdateKeyFocus(true); //Force the game to be "focused" for tests. Required if we want keyboard inputs to work.
testKey->bHeld=true; //Assume key is held for every test unless otherwise needs to be changed.
Menu::themes.SetInitialized();
GFX.SetInitialized();
DAMAGENUMBER_LIST.clear();
game->MAP_DATA.SetInitialized();
}
TEST_METHOD_CLEANUP(EffectTestCleanup){
testGame->EndGame();
testGame->OnUserUpdate(0.f);
testGame.reset();
}
TEST_METHOD(EffectListsStartEmpty){
Assert::AreEqual(size_t(0),EffectManager::GetBackgroundEffectCount(),L"Background effects list initializes as size 0.");
Assert::AreEqual(size_t(0),EffectManager::GetForegroundEffectCount(),L"Foreground effects list initializes as size 0.");
}
TEST_METHOD(EffectListCountsAdjustWhenAddingAndRemovingEffects){
Assert::AreEqual(size_t(0),EffectManager::GetBackgroundEffectCount(),L"Background effects list initializes as size 0.");
Assert::AreEqual(size_t(0),EffectManager::GetForegroundEffectCount(),L"Foreground effects list initializes as size 0.");
const auto&foregroundEffRef{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
Assert::AreEqual(size_t(1),EffectManager::GetForegroundEffectCount(),L"Foreground effect size increased to 1.");
Assert::AreEqual(size_t(0),EffectManager::GetBackgroundEffectCount(),L"Background effects list initializes as size 0.");
Assert::AreEqual(0,foregroundEffRef.slotId,L"First generated effect should be inserted in slot 0.");
Assert::AreEqual(0,foregroundEffRef.effectId,L"First generated effect should be effect ID 0.");
Assert::AreEqual(true,foregroundEffRef.foregroundEffect,L"First generated effect should not be a background effect.");
Assert::AreEqual(false,foregroundEffRef.expired(),L"Effect should not be expired.");
const auto&foregroundEffRef2{game->AddEffect(Effect{vf2d{},1.f,"pixel.png",false,0.f,0.5f})};
const auto&backgroundEffRef{game->AddEffect(Effect{vf2d{},1.f,"pixel.png",false,0.f},true)};
Assert::AreEqual(size_t(2),EffectManager::GetForegroundEffectCount(),L"Foreground effect size increased to 2.");
Assert::AreEqual(size_t(1),EffectManager::GetBackgroundEffectCount(),L"Background effect size increased to 1.");
Assert::AreEqual(0,backgroundEffRef.slotId,L"First background generated effect should be inserted in slot 0.");
Assert::AreEqual(2,backgroundEffRef.effectId,L"First background generated effect should be effect ID 2.");
Assert::AreEqual(false,backgroundEffRef.foregroundEffect,L"First background generated effect should be a background effect.");
Assert::AreEqual(false,backgroundEffRef.expired(),L"First background generated effect should not be expired.");
Assert::AreEqual(false,foregroundEffRef2.expired(),L"Foreground generated effect should not be expired.");
Game::Update(0.5f);
Assert::AreEqual(size_t(1),EffectManager::GetForegroundEffectCount(),L"Foreground effect count reduced by 1.");
Assert::AreEqual(size_t(1),EffectManager::GetBackgroundEffectCount(),L"Background effect size remains the same.");
Assert::AreEqual(true,foregroundEffRef.expired(),L"Original foreground effect is now expired.");
Assert::AreEqual(false,foregroundEffRef2.expired(),L"Foreground generated effect should not be expired.");
Game::Update(0.5f);
Assert::AreEqual(size_t(1),EffectManager::GetForegroundEffectCount(),L"Foreground effect count remains at 1.");
Assert::AreEqual(size_t(0),EffectManager::GetBackgroundEffectCount(),L"Background effect size is now 0.");
const auto&foregroundEffRef3{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
Assert::AreEqual(size_t(2),EffectManager::GetForegroundEffectCount(),L"Foreground effect count increases by 1.");
Assert::AreEqual(size_t(0),EffectManager::GetBackgroundEffectCount(),L"Background effect size is now 0.");
Assert::AreEqual(0,foregroundEffRef3.slotId,L"New effect should be inserted in now cleared slot 0.");
Assert::AreEqual(3,foregroundEffRef3.effectId,L"Effect ID should be 3.");
Assert::AreEqual(true,foregroundEffRef.expired(),L"Effect should now be expired.");
Assert::AreEqual(true,foregroundEffRef3.foregroundEffect,L"Should not be a background effect.");
Assert::AreEqual(false,foregroundEffRef3.expired(),L"Foreground generated effect should not be expired.");
Game::Update(0.5f);
Assert::AreEqual(size_t(0),EffectManager::GetForegroundEffectCount(),L"Effect Count sizes are zero.");
Assert::AreEqual(size_t(0),EffectManager::GetBackgroundEffectCount(),L"Effect Count sizes are zero.");
Assert::AreEqual(true,foregroundEffRef3.expired(),L"Effect should now be expired.");
Assert::AreEqual(true,foregroundEffRef.expired(),L"Effect should now be expired.");
Assert::AreEqual(true,backgroundEffRef.expired(),L"Effect should now be expired");
Assert::AreEqual(true,foregroundEffRef2.expired(),L"Effect should now be expired");
}
TEST_METHOD(GettingExpiredEffectRefObjectFails){
Effect newEff{vf2d{},0.5f,"pixel.png",false,0.f};
auto backgroundEffRef{game->AddEffect(newEff)};
Game::Update(0.5f);
Assert::ExpectException<std::runtime_error>([&](){backgroundEffRef.get();},L"Expecting an expired effect to throw when attempting to retrieve it.");
}
TEST_METHOD(AddAndRemovalOfEffectsInMiddleOfArray){
auto eff1{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
auto eff2{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
auto eff3{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
auto eff4{game->AddEffect(Effect{vf2d{},0.25f,"pixel.png",false,0.f})};
auto eff5{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
Game::Update(0.25f);
auto eff6{game->AddEffect(Effect{vf2d{},0.25f,"pixel.png",false,0.f})};
auto eff7{game->AddEffect(Effect{vf2d{},0.25f,"pixel.png",false,0.f})};
Assert::AreEqual(eff6.slotId,eff4.slotId,L"New effect falls into old effect's slot.");
Assert::AreNotEqual(eff6.effectId,eff4.effectId,L"New effect effect ID should be different from old effect's ID.");
Assert::AreEqual(5,eff7.slotId,L"When entire list is occupied, item should be appended.");
}
TEST_METHOD(AddNewEffectWhenArrayIsFull){
for(int i:std::ranges::iota_view(0,EFFECT_LIMIT)){
game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f});
game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f},true);
}
EFF(*EffectManager::GetForegroundEffects()[50].effect).aliveTime=200.f;
int oldEffectId{EffectManager::GetForegroundEffects()[50].effectId};
EffectRef replacedEff{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
Assert::IsTrue(oldEffectId<replacedEff.effectId,L"New effect has a greater and different ID compared to old slot (Completely replaced). Oldest effect was removed first.");
}
TEST_METHOD(CheckForGapInExpiryTimes){
for(int i:std::ranges::iota_view(0,EFFECT_LIMIT)){
if(i==40){
game->AddEffect(Effect{vf2d{},0.1f,"pixel.png",false,0.f});
game->AddEffect(Effect{vf2d{},0.1f,"pixel.png",false,0.f},true);
}else{
game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f});
game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f},true);
}
}
Game::Update(0.1f);
Assert::AreEqual(false,bool(EffectManager::GetForegroundEffects()[40].effect),L"Expired effect should have been removed.");
Assert::AreEqual(false,bool(EffectManager::GetBackgroundEffects()[40].effect),L"Expired effect should have been removed.");
EffectRef foregroundEff{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f})};
EffectRef backgroundEff{game->AddEffect(Effect{vf2d{},0.5f,"pixel.png",false,0.f},true)};
Assert::AreEqual(40,foregroundEff.slotId,L"The slot taken over should match the expired effect.");
Assert::AreEqual(40,backgroundEff.slotId,L"The slot taken over should match the expired effect.");
}
};
};