1
1
#include < iomanip>
2
+ #include < cassert>
2
3
#include < string>
3
4
#include < iostream>
4
5
#include < queue>
@@ -17,30 +18,54 @@ struct ProcessResult : public Process {
17
18
uint32_t turn_around_time;
18
19
uint32_t waiting_time;
19
20
20
- ProcessResult (const Process& process, uint32_t time_elapsed) : Process(process) {
21
- completion_time = time_elapsed;
21
+ ProcessResult (const Process& process, uint32_t completion_time)
22
+ : Process(process), completion_time(completion_time) {
22
23
turn_around_time = completion_time - arrival_time;
23
24
waiting_time = turn_around_time - burst_time;
24
25
}
26
+
27
+ bool operator ==(const ProcessResult& p) const {
28
+ return id == p.id && arrival_time == p.arrival_time &&
29
+ burst_time == p.burst_time &&
30
+ completion_time == p.completion_time &&
31
+ turn_around_time == p.turn_around_time &&
32
+ waiting_time == p.waiting_time ;
33
+ }
25
34
};
26
35
27
36
std::vector<ProcessResult> RRExecute (const std::vector<Process>& processes,
28
37
uint32_t time_slice);
29
-
30
38
std::ostream& operator <<(std::ostream& ostream,
31
39
const std::vector<ProcessResult>& results);
40
+ void Test ();
41
+
42
+ bool CompareAT (const Process& p1, const Process& p2) {
43
+ return p1.arrival_time < p2.arrival_time ;
44
+ }
32
45
33
46
int main () {
34
- std::vector<Process> processes{{0 , 3 , 3 }, {1 , 8 , 5 }, {2 , 5 , 4 }};
35
- const uint32_t kTimeSlice {2 };
36
- std::vector<ProcessResult> results = RRExecute (processes, kTimeSlice );
37
- std::cout << results;
38
- std::getchar ();
47
+ Test ();
39
48
return 0 ;
40
49
}
41
50
42
- bool CompareAT (const Process& p1, const Process& p2) {
43
- return p1.arrival_time < p2.arrival_time ;
51
+ void Test () {
52
+ std::vector<Process> processes{
53
+ {0 , 70 , 3 }, {1 , 9 , 2 }, {2 , 3 , 39 }, {3 , 5 , 29 }, {4 , 30 , 90 }};
54
+ const uint32_t kTimeSlice {3 };
55
+ std::vector<ProcessResult> results = RRExecute (processes, kTimeSlice );
56
+
57
+ std::vector<uint32_t > completion_times ({80 , 14 , 100 , 82 , 166 });
58
+ std::vector<ProcessResult> correct_results;
59
+ for (size_t i = 0 ; i < processes.size (); i++) {
60
+ correct_results.emplace_back (processes[i], completion_times[i]);
61
+ }
62
+
63
+ std::sort (results.begin (), results.end (), CompareAT);
64
+ std::sort (correct_results.begin (), correct_results.end (), CompareAT);
65
+
66
+ std::cout << results;
67
+ assert (results == correct_results);
68
+ std::cout << " All test passed" ;
44
69
}
45
70
46
71
void CheckArriveProcess (const std::vector<Process>& processes,
@@ -90,6 +115,9 @@ std::ostream& operator<<(std::ostream& ostream,
90
115
ostream << std::setw (17 ) << std::left << str;
91
116
};
92
117
118
+ std::vector<ProcessResult> sorted = results;
119
+ std::sort (sorted.begin (), sorted.end (), CompareAT);
120
+
93
121
PrintCell (" Process ID" );
94
122
PrintCell (" Arrival Time" );
95
123
PrintCell (" Burst Time" );
@@ -98,7 +126,7 @@ std::ostream& operator<<(std::ostream& ostream,
98
126
PrintCell (" Waiting Time" );
99
127
ostream << std::endl;
100
128
101
- for (auto & p : results ) {
129
+ for (auto & p : sorted ) {
102
130
PrintCell (std::to_string (p.id ));
103
131
PrintCell (std::to_string (p.arrival_time ));
104
132
PrintCell (std::to_string (p.burst_time ));
0 commit comments