Skip to content

Commit 4885fac

Browse files
committed
test: create self-test implementation for round-robin cpu scheduling
1 parent 803d707 commit 4885fac

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

cpu_scheduling_algorithms/round_robin_scheduling.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iomanip>
2+
#include <cassert>
23
#include <string>
34
#include <iostream>
45
#include <queue>
@@ -17,30 +18,54 @@ struct ProcessResult : public Process {
1718
uint32_t turn_around_time;
1819
uint32_t waiting_time;
1920

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) {
2223
turn_around_time = completion_time - arrival_time;
2324
waiting_time = turn_around_time - burst_time;
2425
}
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+
}
2534
};
2635

2736
std::vector<ProcessResult> RRExecute(const std::vector<Process>& processes,
2837
uint32_t time_slice);
29-
3038
std::ostream& operator<<(std::ostream& ostream,
3139
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+
}
3245

3346
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();
3948
return 0;
4049
}
4150

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";
4469
}
4570

4671
void CheckArriveProcess(const std::vector<Process>& processes,
@@ -90,6 +115,9 @@ std::ostream& operator<<(std::ostream& ostream,
90115
ostream << std::setw(17) << std::left << str;
91116
};
92117

118+
std::vector<ProcessResult> sorted = results;
119+
std::sort(sorted.begin(), sorted.end(), CompareAT);
120+
93121
PrintCell("Process ID");
94122
PrintCell("Arrival Time");
95123
PrintCell("Burst Time");
@@ -98,7 +126,7 @@ std::ostream& operator<<(std::ostream& ostream,
98126
PrintCell("Waiting Time");
99127
ostream << std::endl;
100128

101-
for (auto& p : results) {
129+
for (auto& p : sorted) {
102130
PrintCell(std::to_string(p.id));
103131
PrintCell(std::to_string(p.arrival_time));
104132
PrintCell(std::to_string(p.burst_time));

0 commit comments

Comments
 (0)