-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTesting.cpp
More file actions
144 lines (130 loc) · 4.74 KB
/
UnitTesting.cpp
File metadata and controls
144 lines (130 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "UnitTesting.h"
#include <iostream>
void UnitTest::runTest()
{
UnitTest::testRange();
UnitTest::testDamage();
UnitTest::testHandleCollision();
UnitTest::testMove();
UnitTest::testCollision();
std::cout << "All Tests Passed Successfully :)" << std::endl;
}
void UnitTest::testRange()
{
Weapon *weapon = new Weapon(10, 1, 1, 1, 10);
// Testing basic calculate range functionality
if (weapon->calculateRange(sf::Vector2f(0, 0), sf::Vector2f(3, 4)) != sf::Vector2f(6, 8))
{
std::cout << "ERROR: Calculate Range Test 1 Failed!" << std::endl;
}
// Testing along the y-axis
if (weapon->calculateRange(sf::Vector2f(0, 0), sf::Vector2f(0, 2)) != sf::Vector2f(0, 10))
{
std::cout << "ERROR: Calculate Range Test 2 Failed" << std::endl;
}
// Testing along the x-axis
if (weapon->calculateRange(sf::Vector2f(0, 0), sf::Vector2f(2, 0)) != sf::Vector2f(10, 0))
{
std::cout << "ERROR: Calculate Range Test 3 Failed" << std::endl;
}
// Testing when the current position of the character is negative
if (weapon->calculateRange(sf::Vector2f(-6, -8), sf::Vector2f(0, 0)) != sf::Vector2f(0, 0))
{
std::cout << "ERROR: Calculate Range Test 4 Failed" << std::endl;
}
}
void UnitTest::testDamage()
{
Weapon *weapon = new Weapon(1, 0, 1, 1, 10);
Player *player = new Player(10, sf::Vector2f(0, 0), 3, 50, weapon);
player->takeDamage(10);
// Testing basic damage functionality
if (player->getHealth() != 40 || player->getIsDestroyed())
{
std::cout << "ERROR: Test Damage Test 1 Failed!" << std::endl;
}
// Testing negative damage (health boost)
player->takeDamage(-5);
if (player->getHealth() != 45 || player->getIsDestroyed())
{
std::cout << "ERROR: Test Damage Test 2 Failed!" << std::endl;
}
// Testing situation where no damage is done
player->takeDamage(0);
if (player->getHealth() != 45 || player->getIsDestroyed())
{
std::cout << "ERROR: Test Damage Test 3 Failed!" << std::endl;
}
// Testing situation resulting in IsDestroyed becoming true
player->takeDamage(50);
if (player->getHealth() != -5 || !player->getIsDestroyed())
{
std::cout << "ERROR: Test Damage Test 4 Failed!" << std::endl;
}
}
void UnitTest::testHandleCollision()
{
Weapon *weapon = new Weapon(10, 0, 1, 1, 10);
Weapon* weapon2 = new Weapon(10, 0, 1, 1, 10);
Player *player = new Player(10, sf::Vector2f(0, 0), 3, 50, weapon);
Zombie *zombie = new Zombie(10, sf::Vector2f(0, 0), 1, 10, weapon2, 1, 1);
sf::Clock clock;
std::optional<Projectile> attack = zombie->attack(sf::Vector2f(0, 0));
if (attack.has_value())
{
player->handleCollision(new Projectile(attack.value()),clock);
}
// Testing a basic projectile hit against a player
if (player->getHealth() != 40|| player->getIsDestroyed())
{
std::cout << "ERROR: Test Handle Collision Test 1 Failed!" << std::endl;
}
if (attack.has_value())
{
player->handleCollision(new Projectile(attack.value()),clock);
player->handleCollision(new Projectile(attack.value()),clock);
player->handleCollision(new Projectile(attack.value()),clock);
player->handleCollision(new Projectile(attack.value()),clock);
}
// Testing a fatal projectile hit against a player
if (player->getHealth() != 0 || !player->getIsDestroyed())
{
std::cout << "ERROR: Test Handle Collision Test 2 Failed!" << std::endl;
}
delete player;
delete zombie;
}
void UnitTest::testMove()
{
Weapon *weapon = new Weapon(1, 1, 1, 1, 10);
Zombie *zombie = new Zombie(10, sf::Vector2f(0, 0), 1, 10, weapon, 1, 1);
zombie->updateAI(sf::Vector2f(0, 0));
// Testing the zombie moves away from its current position in a randomised direction
if (zombie->getPosition() != sf::Vector2f(0, 0))
{
std::cout << "ERROR: Test Move Test 1 Failed!" << std::endl;
}
delete zombie;
}
void UnitTest::testCollision()
{
Weapon* weapon1 = new Weapon(1, 1, 1, 1, 10);
Weapon* weapon2 = new Weapon(1, 1, 1, 1, 10);
Weapon* weapon3 = new Weapon(1, 1, 1, 1, 10);
Zombie* zombie = new Zombie(10, sf::Vector2f(0, 0), 1, 10, weapon1, 1, 1);
Player* player = new Player(10, sf::Vector2f(0, 0), 3, 50, weapon2);
Player* player2 = new Player(10, sf::Vector2f(0, 100), 3, 50, weapon3);
// Test 1: Should collide
if (!player->checkCollision(zombie))
{
std::cout << "ERROR: Test Collision Test 1 Failed!" << std::endl;
}
// Test 2: Should NOT collide
if (player2->checkCollision(zombie))
{
std::cout << "ERROR: Test Collision Test 2 Failed!" << std::endl;
}
delete zombie;
delete player;
delete player2;
}