-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECElevatorTest.cpp
More file actions
183 lines (166 loc) · 5.83 KB
/
ECElevatorTest.cpp
File metadata and controls
183 lines (166 loc) · 5.83 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
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
// Testing elevators
#include <vector>
#include <iostream>
#include "ECElevatorSim.h"
using namespace std;
// Test utility
template<class T>
void ASSERT_EQ(T x, T y)
{
if( x == y )
{
cout << "Test passed: equal: " << x << " " << y << endl;
}
else
{
cout << "Test FAILED: equal: " << x << " " << y << endl;
}
}
static void RunTest(int numFloors, int timeSim, vector<ECElevatorSimRequest> &listRequests, vector<int> &listArriveTime )
{
// simulate
ECElevatorSim sim(numFloors, listRequests );
sim.Simulate(timeSim);
// status of requests
for(unsigned int i=0; i<listRequests.size(); ++i)
{
//cout << "Request " << i << ": ";
int tmDone = listRequests[i].GetArriveTime();
ASSERT_EQ(tmDone, listArriveTime[i] );
#if 0
if( tmDone >= 0 )
{
cout << "arrived at time " << tmDone;
}
else
{
cout << " hasn't arrived yet";
}
cout << endl;
#endif
}
}
// a simple test: a single passenger going from floor 3 to 1
// this passenger arrived time 7:
// (i) elevator gets to floor 3 at time 4 (received request from this passenger at time 2);
// (ii) wait for time 1 to let the passenger in the elevator (now time 5)
// (iii) move to floor 1 from 3, which takes time 6 and 7; so arrive at floor 1 (destination) at time 7
static void Test0()
{
cout << "\n****** TEST 0\n";
// test setup
const int NUM_FLOORS = 7;
const int timeSim = 10;
ECElevatorSimRequest r1(2, 3, 1);
vector<ECElevatorSimRequest> listRequests;
listRequests.push_back(r1);
vector<int> listArriveTime;
listArriveTime.push_back(7);
// request for elevator at time 2, arrive at time 7
RunTest(NUM_FLOORS, timeSim, listRequests, listArriveTime);
}
// Two passengers:
// Passenger 1: arrived time 7:
// (i) elevator gets to floor 3 at time 4 (received request from this passenger at time 2);
// (ii) wait for time 1 to let the passenger in the elevator (now time 5)
// (iii) move to floor 3 from 5, which takes time 6 and 7; so arrive at floor 5 (destination) at time 7
// Passenger 2:
// (i) elevator arrives at floor 6 at time 9 (note: unloading at time 8 at floor 5)
// (ii) move downwards starting at time 10; take 6 time units to get to floor 1 (arrived at floor 1 at time 15)
static void Test1()
{
cout << "\n****** TEST 1\n";
// test setup
const int NUM_FLOORS = 7;
const int timeSim = 20;
ECElevatorSimRequest r1(2, 3, 5), r2(2, 6, 1);
vector<ECElevatorSimRequest> listRequests;
listRequests.push_back(r1);
listRequests.push_back(r2);
vector<int> listArriveTime;
listArriveTime.push_back(7);
listArriveTime.push_back(15);
// simulate
RunTest(NUM_FLOORS, timeSim, listRequests, listArriveTime);
}
// Two passengers:
// (i) elevator gets to floor 4 at time 5 to pick up passenger 1 (received request from this passenger at time 2);
// (ii) wait for time 1 to let passenger 1 in the elevator (now time 6)
// (iii) move to floor 4 from 5 (arrived at time 7), so to pick up passenger 2;
// (iv) at time 8 moving downwards from floor 5
// (v) elevator arrives at floor 2 at time 11 (so to unload passenger 2; i.e., passenger 2 arrives at time 11)
// (vi) move downwards at time 12 and arrive at floor 1 at time 13; unload passenger 1 (i.e. passenger 1 arrives at time 13)
static void Test2()
{
cout << "\n****** TEST 2\n";
// test setup
const int NUM_FLOORS = 7;
const int timeSim = 20;
ECElevatorSimRequest r1(2, 4, 1), r2(3, 5, 2);
vector<ECElevatorSimRequest> listRequests;
listRequests.push_back(r1);
listRequests.push_back(r2);
vector<int> listArriveTime;
listArriveTime.push_back(13);
listArriveTime.push_back(11);
// simulate
RunTest(NUM_FLOORS, timeSim, listRequests, listArriveTime);
}
// Three passengers:
// (i) elevator starts to move upwards from floor 1 at time 2
// (ii) pick up passenger 2 at time 3 at floor 2 (still going up)
// (iii) arrive at floor 4 at time 6, so to pick up passenger 1;
// (iv) at time 8 arrives at floor 5 to unload passenger 2 (passenger 2 arrives at time 8) and moving downwards at time 9 (note: passenger 3 is not ready to go yet)
// (v) arrives at floor 1 at time 13 (so to unload passenger 1; i.e., passenger 1 arrives at time 13)
// (vi) move upwards starting at time 14 and arrive at floor 5 at time 18 and pick up passenger 3
// (vii) move downwards starting at time 19 and arrives at floor 1 at time 23 (passenger 3 arrives at time 23)
static void Test3()
{
cout << "\n****** TEST 3\n";
// test setup
const int NUM_FLOORS = 8;
const int timeSim = 25;
ECElevatorSimRequest r1(2, 4, 1), r2(3, 2, 5), r3(12, 5, 1);
vector<ECElevatorSimRequest> listRequests;
listRequests.push_back(r1);
listRequests.push_back(r2);
listRequests.push_back(r3);
vector<int> listArriveTime;
listArriveTime.push_back(13);
listArriveTime.push_back(8);
listArriveTime.push_back(23);
// simulate
RunTest(NUM_FLOORS, timeSim, listRequests, listArriveTime);
}
// Four passengers:
// passengers 1 and 2 both arrives at time 13
// passenger 3 arrives at time 16
// passenger 4 arrives at time 26
static void Test4()
{
cout << "\n****** TEST 4\n";
// test setup
const int NUM_FLOORS = 8;
const int timeSim = 35;
ECElevatorSimRequest r1(2, 3, 1), r2(3, 5, 1), r3(8, 2, 3), r4(10, 6, 1);
vector<ECElevatorSimRequest> listRequests;
listRequests.push_back(r1);
listRequests.push_back(r2);
listRequests.push_back(r3);
listRequests.push_back(r4);
vector<int> listArriveTime;
listArriveTime.push_back(13);
listArriveTime.push_back(13);
listArriveTime.push_back(16);
listArriveTime.push_back(26);
// simulate
RunTest(NUM_FLOORS, timeSim, listRequests, listArriveTime);
}
int main()
{
Test0();
Test1();
Test2();
Test3();
Test4();
}