summaryrefslogtreecommitdiff
path: root/tests/dataTests.cpp
blob: 6bf553e96dc4cf78f11e2a04e312faf6392857a4 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @file dataTests.cpp
* @brief Unit tests for the data layer of a Rally-X game.
*
* The functionality of each class in the data layer was tested.
*
* The Config class was tested in terms of its ability to read an existing config file,
* as well as its ability to create a new file with default values, or fill missing parameters
* with default values.
*
* The LevelReader class was tested in the normal case, where a correct level file is given,
* and in the case where a file that does not exist is given.
*
* @author Justin Wernick
* @author David Schneider
*/

#include <cstdio>
#include <fstream>
using namespace std;

#include <gtest/gtest.h>

#include "../source/data/Config.h"
#include "../source/data/LevelReader.h"
#include "../source/logic/PlayerCar.h"
#include "../source/logic/EnemyCar.h"
#include "../source/logic/Checkpoint.h"
#include "../source/logic/Rock.h"
#include "../source/logic/Smokescreen.h"
#include "../source/logic/Maze.h"

/**
* @brief Tests that a normal complete config file can be read.
*/
TEST(Config, readsSettingsCorrectly)
{
    string testFilepath = "testConfig.txt";
    ofstream testFile(testFilepath.c_str());

    testFile << "screen_width=123" << endl;
    testFile << "screen_height=345" << endl;
    testFile << "fullscreen=true" << endl;

    testFile.close();

    Config testConfig(testFilepath);

    EXPECT_EQ((unsigned)(123), testConfig.screenWidth());
    EXPECT_EQ((unsigned)(345), testConfig.screenHeight());
    EXPECT_TRUE(testConfig.fullscreen());

    remove(testFilepath.c_str());
}

/**
* @brief Tests that, if the config file does not exist, it is created with default values.
*/
TEST(Config, createsFileIfNeeded)
{
    string testFilepath = "testConfig.txt";
    Config testConfig(testFilepath);

    ifstream testFile(testFilepath.c_str());
    EXPECT_TRUE(testFile);
    testFile.close();

    //test for default values
    EXPECT_EQ((unsigned)(800), testConfig.screenWidth());
    EXPECT_EQ((unsigned)(600), testConfig.screenHeight());
    EXPECT_FALSE(testConfig.fullscreen());

    remove(testFilepath.c_str());
}

/**
* @brief Tests that an incomplete config file is loaded, with defaults for the missing values.
*/
TEST(Config, incompleteFileFilled)
{
    string testFilepath = "testConfig.txt";
    ofstream testFile(testFilepath.c_str());

    testFile << "screen_height=345" << endl;

    testFile.close();

    Config testConfig(testFilepath);

    EXPECT_EQ((unsigned)(800), testConfig.screenWidth());
    EXPECT_EQ((unsigned)(345), testConfig.screenHeight());
    EXPECT_FALSE(testConfig.fullscreen());

    remove(testFilepath.c_str());
}

/**
* @brief Tests that a level can be loaded correctly from a file.
*/
TEST(LevelReader, readsFileInfoObjects)
{
    string testFilepath = "testMaze.lvl";
    ofstream testFile(testFilepath.c_str());

    testFile << " P X  " << endl;
    testFile << "      " << endl;
    testFile << "   @  " << endl;
    testFile << "     X" << endl;
    testFile << "##    " << endl;
    testFile << "   P  " << endl;
    testFile << "  O   " << endl;

    testFile.close();

    LevelReader testReader(testFilepath);
    Maze maze;
    list<PlayerCar> players;
    list<EnemyCar> enemies;
    list<Checkpoint> checkpoints;
    list<Rock> rocks;
    testReader.readLevel(maze, players, enemies, checkpoints, rocks);

    list<PlayerCar> expectedPlayers;
    list<EnemyCar> expectedEnemies;
    list<Checkpoint> expectedCheckpoints;
    list<Rock> expectedRocks;

    expectedPlayers.push_back(PlayerCar(3,2));
    expectedEnemies.push_back(EnemyCar(3,0));
    expectedEnemies.push_back(EnemyCar(5,3));
    expectedCheckpoints.push_back(Checkpoint(2,0));
    expectedCheckpoints.push_back(Checkpoint(3,5));
    expectedRocks.push_back(Rock(2,6));

    //eqality operator was not implemented for the GameObject class or its subclasses
    //because it would not be meaningful. Two objects with the same position, type, and facing
    //are still two different objects.
    //iterators were used because the list type does not have an 'at' function.
    list<PlayerCar>::const_iterator playIter = players.begin();
    EXPECT_FLOAT_EQ(playIter->x(), 3);
    EXPECT_FLOAT_EQ(playIter->y(), 2);
    ++playIter;
    EXPECT_EQ(playIter, players.end());

    list<EnemyCar>::const_iterator enemyIter = enemies.begin();
    EXPECT_FLOAT_EQ(enemyIter->x(), 3);
    EXPECT_FLOAT_EQ(enemyIter->y(), 0);
    ++enemyIter;
    EXPECT_FLOAT_EQ(enemyIter->x(), 5);
    EXPECT_FLOAT_EQ(enemyIter->y(), 3);
    ++enemyIter;
    EXPECT_EQ(enemyIter, enemies.end());

    list<Checkpoint>::const_iterator checkIter = checkpoints.begin();
    EXPECT_FLOAT_EQ(checkIter->x(), 1);
    EXPECT_FLOAT_EQ(checkIter->y(), 0);
    ++checkIter;
    EXPECT_FLOAT_EQ(checkIter->x(), 3);
    EXPECT_FLOAT_EQ(checkIter->y(), 5);
    ++checkIter;
    EXPECT_EQ(checkIter, checkpoints.end());

    list<Rock>::const_iterator rockIter = rocks.begin();
    EXPECT_FLOAT_EQ(rockIter->x(), 2);
    EXPECT_FLOAT_EQ(rockIter->y(), 6);
    ++rockIter;
    EXPECT_EQ(rockIter, rocks.end());

    remove(testFilepath.c_str());
}

/**
* @brief Tests that an exception is throws if the selected file does not exist.
*/
TEST(LevelReader, throwsExceptionOnBadFilename)
{
    string testFilepath = "testLevel.lvl";
    LevelReader testReader(testFilepath);
    Maze maze;
    list<PlayerCar> players;
    list<EnemyCar> enemies;
    list<Checkpoint> checkpoints;
    list<Rock> rocks;
    EXPECT_ANY_THROW(testReader.readLevel(maze, players, enemies, checkpoints, rocks));
}