Rally X
ELEN3009 Project by Justin Wernick and David Schneider
|
00001 00017 #include <cstdio> 00018 #include <fstream> 00019 using namespace std; 00020 00021 #include <gtest/gtest.h> 00022 00023 #include "../source/data/Config.h" 00024 #include "../source/data/LevelReader.h" 00025 #include "../source/logic/PlayerCar.h" 00026 #include "../source/logic/EnemyCar.h" 00027 #include "../source/logic/Checkpoint.h" 00028 #include "../source/logic/Rock.h" 00029 #include "../source/logic/Smokescreen.h" 00030 #include "../source/logic/Maze.h" 00031 00035 TEST(Config, readsSettingsCorrectly) 00036 { 00037 string testFilepath = "testConfig.txt"; 00038 ofstream testFile(testFilepath.c_str()); 00039 00040 testFile << "screen_width=123" << endl; 00041 testFile << "screen_height=345" << endl; 00042 testFile << "fullscreen=true" << endl; 00043 00044 testFile.close(); 00045 00046 Config testConfig(testFilepath); 00047 00048 EXPECT_EQ((unsigned)(123), testConfig.screenWidth()); 00049 EXPECT_EQ((unsigned)(345), testConfig.screenHeight()); 00050 EXPECT_TRUE(testConfig.fullscreen()); 00051 00052 remove(testFilepath.c_str()); 00053 } 00054 00058 TEST(Config, createsFileIfNeeded) 00059 { 00060 string testFilepath = "testConfig.txt"; 00061 Config testConfig(testFilepath); 00062 00063 ifstream testFile(testFilepath.c_str()); 00064 EXPECT_TRUE(testFile); 00065 testFile.close(); 00066 00067 //test for default values 00068 EXPECT_EQ((unsigned)(800), testConfig.screenWidth()); 00069 EXPECT_EQ((unsigned)(600), testConfig.screenHeight()); 00070 EXPECT_FALSE(testConfig.fullscreen()); 00071 00072 remove(testFilepath.c_str()); 00073 } 00074 00078 TEST(Config, incompleteFileFilled) 00079 { 00080 string testFilepath = "testConfig.txt"; 00081 ofstream testFile(testFilepath.c_str()); 00082 00083 testFile << "screen_height=345" << endl; 00084 00085 testFile.close(); 00086 00087 Config testConfig(testFilepath); 00088 00089 EXPECT_EQ((unsigned)(800), testConfig.screenWidth()); 00090 EXPECT_EQ((unsigned)(345), testConfig.screenHeight()); 00091 EXPECT_FALSE(testConfig.fullscreen()); 00092 00093 remove(testFilepath.c_str()); 00094 } 00095 00099 TEST(LevelReader, readsFileInfoObjects) 00100 { 00101 string testFilepath = "testMaze.lvl"; 00102 ofstream testFile(testFilepath.c_str()); 00103 00104 testFile << " P X " << endl; 00105 testFile << " " << endl; 00106 testFile << " @ " << endl; 00107 testFile << " X" << endl; 00108 testFile << "## " << endl; 00109 testFile << " P " << endl; 00110 testFile << " O " << endl; 00111 00112 testFile.close(); 00113 00114 LevelReader testReader(testFilepath); 00115 Maze maze; 00116 list<PlayerCar> players; 00117 list<EnemyCar> enemies; 00118 list<Checkpoint> checkpoints; 00119 list<Rock> rocks; 00120 testReader.readLevel(maze, players, enemies, checkpoints, rocks); 00121 00122 list<PlayerCar> expectedPlayers; 00123 list<EnemyCar> expectedEnemies; 00124 list<Checkpoint> expectedCheckpoints; 00125 list<Rock> expectedRocks; 00126 00127 expectedPlayers.push_back(PlayerCar(3,2)); 00128 expectedEnemies.push_back(EnemyCar(3,0)); 00129 expectedEnemies.push_back(EnemyCar(5,3)); 00130 expectedCheckpoints.push_back(Checkpoint(2,0)); 00131 expectedCheckpoints.push_back(Checkpoint(3,5)); 00132 expectedRocks.push_back(Rock(2,6)); 00133 00134 //eqality operator was not implemented for the GameObject class or its subclasses 00135 //because it would not be meaningful. Two objects with the same position, type, and facing 00136 //are still two different objects. 00137 //iterators were used because the list type does not have an 'at' function. 00138 list<PlayerCar>::const_iterator playIter = players.begin(); 00139 EXPECT_FLOAT_EQ(playIter->x(), 3); 00140 EXPECT_FLOAT_EQ(playIter->y(), 2); 00141 ++playIter; 00142 EXPECT_EQ(playIter, players.end()); 00143 00144 list<EnemyCar>::const_iterator enemyIter = enemies.begin(); 00145 EXPECT_FLOAT_EQ(enemyIter->x(), 3); 00146 EXPECT_FLOAT_EQ(enemyIter->y(), 0); 00147 ++enemyIter; 00148 EXPECT_FLOAT_EQ(enemyIter->x(), 5); 00149 EXPECT_FLOAT_EQ(enemyIter->y(), 3); 00150 ++enemyIter; 00151 EXPECT_EQ(enemyIter, enemies.end()); 00152 00153 list<Checkpoint>::const_iterator checkIter = checkpoints.begin(); 00154 EXPECT_FLOAT_EQ(checkIter->x(), 1); 00155 EXPECT_FLOAT_EQ(checkIter->y(), 0); 00156 ++checkIter; 00157 EXPECT_FLOAT_EQ(checkIter->x(), 3); 00158 EXPECT_FLOAT_EQ(checkIter->y(), 5); 00159 ++checkIter; 00160 EXPECT_EQ(checkIter, checkpoints.end()); 00161 00162 list<Rock>::const_iterator rockIter = rocks.begin(); 00163 EXPECT_FLOAT_EQ(rockIter->x(), 2); 00164 EXPECT_FLOAT_EQ(rockIter->y(), 6); 00165 ++rockIter; 00166 EXPECT_EQ(rockIter, rocks.end()); 00167 00168 remove(testFilepath.c_str()); 00169 } 00170 00174 TEST(LevelReader, throwsExceptionOnBadFilename) 00175 { 00176 string testFilepath = "testLevel.lvl"; 00177 LevelReader testReader(testFilepath); 00178 Maze maze; 00179 list<PlayerCar> players; 00180 list<EnemyCar> enemies; 00181 list<Checkpoint> checkpoints; 00182 list<Rock> rocks; 00183 EXPECT_ANY_THROW(testReader.readLevel(maze, players, enemies, checkpoints, rocks)); 00184 }