-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetroNetInputTests.cpp
205 lines (175 loc) · 7.91 KB
/
MetroNetInputTests.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <gtest/gtest.h>
#include "MetroNet.h"
#include "MetroNetUtils.h"
#include "MetroNetImporter.h"
class MetroNetInputTests: public ::testing::Test {
protected:
friend class MetroNet;
virtual void SetUp() {
delete metronet;
metronet = new MetroNet();
}
MetroNet* metronet = new MetroNet();
};
TEST_F(MetroNetInputTests, InputHappyDay) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
std::ofstream ofile;
SuccessEnum importResult;
ofile.open("_testInput/MetroNetInputTests/correctInput.xml");
ofile << "<?xml version=\"1.0\" ?>" << std::endl
<< "<METRONET>\n\t<STATION>\n"
<< "\t\t<naam>A</naam>\n\t\t<type>Metrostation</type>\n"
<< "\t\t<SPOOR>\n\t\t\t<spoor>12</spoor>\n"
<< "\t\t\t<volgende>B</volgende>\n\t\t\t<vorige>B</vorige>\n"
<< "\t\t</SPOOR>\n\t</STATION>\n\t<STATION>\n"
<< "\t\t<naam>B</naam>\n\t\t<type>Halte</type>\n"
<< "\t\t<SPOOR>\n\t\t\t<spoor>12</spoor>\n"
<< "\t\t\t<volgende>A</volgende>\n\t\t\t<vorige>A</vorige>\n"
<< "\t\t</SPOOR>\n\t</STATION>\n\t<TRAM>\n"
<< "\t\t<lijnNr>12</lijnNr>\n\t\t<zitplaatsen>32</zitplaatsen>\n"
<< "\t\t<snelheid>60</snelheid>\n\t\t<beginStation>A</beginStation>\n"
<< "\t\t<type>PCC</type>\n\t\t<voertuigNr>1</voertuigNr>\n"
<< "\t</TRAM>\n</METRONET>" << std::endl;
ofile.close();
ofile.open("_testOutput/metronetInputTestsError.txt");
importResult = MetroNetImporter::importMetroNet("_testInput/MetroNetInputTests/correctInput.xml", ofile, *metronet);
ofile.close();
EXPECT_TRUE(importResult == Success);
EXPECT_TRUE(FileIsEmpty("_testOutput/metronetInputTestsError.txt"));
}
TEST_F(MetroNetInputTests, correctInput) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
std::ofstream ofile;
SuccessEnum importResult;
int fileCounter = 1;
std::string fileName = "_testInput/MetroNetInputTests/correctInput" + std::to_string(fileCounter) + ".xml";
while (FileExists(fileName)) {
SetUp();
ofile.open("_testOutput/metronetInputTestsError.txt");
importResult = MetroNetImporter::importMetroNet(fileName.c_str(), ofile, *metronet);
ofile.close();
EXPECT_TRUE(importResult == Success);
EXPECT_TRUE(FileIsEmpty("_testOutput/metronetInputTestsError.txt"));
fileCounter++;
fileName = "_testInput/MetroNetInputTests/correctInput" + std::to_string(fileCounter) + ".xml";
}
EXPECT_TRUE(fileCounter == 3);
}
TEST_F(MetroNetInputTests, wrongInputSyntaxError) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
ASSERT_TRUE(DirectoryExists("_testOutput/MetroNetInputTestsExpected"));
std::ofstream ofile;
SuccessEnum importResult;
int fileCounter = 1;
std::string fileName = "_testInput/MetroNetInputTests/syntaxErrorInput" + std::to_string(fileCounter) + ".xml";
while (FileExists(fileName)) {
ofile.open("_testOutput/metronetInputTestsError.txt");
importResult = MetroNetImporter::importMetroNet(fileName.c_str(), ofile, *metronet);
ofile.close();
EXPECT_TRUE(importResult == ImportAborted);
std::string expectedFileName = "_testOutput/MetroNetInputTestsExpected/syntaxError" + std::to_string(fileCounter) + ".txt";
EXPECT_TRUE(FileCompare("_testOutput/metronetInputTestsError.txt", expectedFileName));
fileCounter++;
fileName = "_testInput/MetroNetInputTests/syntaxErrorInput" + std::to_string(fileCounter) + ".xml";
}
EXPECT_TRUE(fileCounter == 6);
}
TEST_F(MetroNetInputTests, wrongInputNoCrash) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
ASSERT_TRUE(DirectoryExists("_testOutput/MetroNetInputTestsExpected"));
std::ofstream ofile;
SuccessEnum importResult;
int fileCounter = 1;
std::string fileName = "_testInput/MetroNetInputTests/wrongInput" + std::to_string(fileCounter) + ".xml";
while (FileExists(fileName)) {
SetUp();
ofile.open("_testOutput/metronetInputTestsError.txt");
importResult = MetroNetImporter::importMetroNet(fileName.c_str(), ofile, *metronet);
ofile.close();
EXPECT_TRUE(importResult == PartialImport);
std::string expectedFileName = "_testOutput/MetroNetInputTestsExpected/wrongInput" + std::to_string(fileCounter) + ".txt";
EXPECT_TRUE(FileCompare("_testOutput/metronetInputTestsError.txt", expectedFileName));
fileCounter++;
fileName = "_testInput/MetroNetInputTests/wrongInput" + std::to_string(fileCounter) + ".xml";
}
EXPECT_TRUE(fileCounter == 40);
}
TEST_F(MetroNetInputTests, inconsistent) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
ASSERT_TRUE(DirectoryExists("_testOutput/MetroNetInputTestsExpected"));
std::ofstream ofile;
int fileCounter = 1;
std::string fileName = "_testInput/MetroNetInputTests/inconsistent" + std::to_string(fileCounter) + ".xml";
while (FileExists(fileName)) {
SetUp();
ofile.open("_testOutput/metronetInputTestsError.txt");
EXPECT_DEATH(MetroNetImporter::importMetroNet(fileName.c_str(), ofile, *metronet), "MetroNet is inconsistent");
ofile.close();
std::string expectedFileName = "_testOutput/MetroNetInputTestsExpected/inconsistent" + std::to_string(fileCounter) + ".txt";
EXPECT_TRUE(FileCompare("_testOutput/metronetInputTestsError.txt", expectedFileName));
fileCounter++;
fileName = "_testInput/MetroNetInputTests/inconsistent" + std::to_string(fileCounter) + ".xml";
}
EXPECT_TRUE(fileCounter == 7);
}
TEST_F(MetroNetInputTests, wrongInputCrash) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
ASSERT_TRUE(DirectoryExists("_testOutput/MetroNetInputTestsExpected"));
std::string errors[3] =
{
"BeginStation of newTram isn't empty",
"This MetroNet already contains a Tram with this voertuigNr",
"This MetroNet already contains a station with this name"
};
std::ofstream ofile;
int fileCounter = 1;
std::string fileName = "_testInput/MetroNetInputTests/crashInput" + std::to_string(fileCounter) + ".xml";
while (FileExists(fileName)) {
SetUp();
ofile.open("_testOutput/metronetInputTestsError.txt");
EXPECT_DEATH(MetroNetImporter::importMetroNet(fileName.c_str(), ofile, *metronet), errors[fileCounter-1]);
ofile.close();
std::string expectedFileName = "_testOutput/MetroNetInputTestsExpected/crash" + std::to_string(fileCounter) + ".txt";
EXPECT_TRUE(FileCompare("_testOutput/metronetInputTestsError.txt", expectedFileName));
fileCounter++;
fileName = "_testInput/MetroNetInputTests/crashInput" + std::to_string(fileCounter) + ".xml";
}
EXPECT_TRUE(fileCounter == 4);
}
TEST_F(MetroNetInputTests, multipleWrongInputs) {
ASSERT_TRUE(DirectoryExists("_testInput"));
ASSERT_TRUE(DirectoryExists("_testInput/MetroNetInputTests"));
ASSERT_TRUE(DirectoryExists("_testOutput"));
ASSERT_TRUE(DirectoryExists("_testOutput/MetroNetInputTestsExpected"));
std::ofstream ofile;
SuccessEnum importResult;
int fileCounter = 1;
std::string fileName = "_testInput/MetroNetInputTests/multipleWrongInputs" + std::to_string(fileCounter) + ".xml";
while (FileExists(fileName)) {
SetUp();
ofile.open("_testOutput/metronetInputTestsError.txt");
importResult = MetroNetImporter::importMetroNet(fileName.c_str(), ofile, *metronet);
ofile.close();
EXPECT_TRUE(importResult == PartialImport);
std::string expectedFileName = "_testOutput/MetroNetInputTestsExpected/multipleWrongInputs" + std::to_string(fileCounter) + ".txt";
EXPECT_TRUE(FileCompare("_testOutput/metronetInputTestsError.txt", expectedFileName));
fileCounter++;
fileName = "_testInput/MetroNetInputTests/multipleWrongInputs" + std::to_string(fileCounter) + ".xml";
}
EXPECT_TRUE(fileCounter == 7);
}