Add unit tests for the scheduler
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
|
|
||||||
#include "sched.hpp"
|
#include "sched.hpp"
|
||||||
|
#include "streambuffer.hpp"
|
||||||
|
#include "luastack.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
bool SchedEntry::operator < (const SchedEntry &other) const {
|
bool SchedEntry::operator < (const SchedEntry &other) const {
|
||||||
if (clock_ < other.clock_) return true;
|
if (clock_ < other.clock_) return true;
|
||||||
@@ -11,6 +15,12 @@ bool SchedEntry::operator < (const SchedEntry &other) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string SchedEntry::debug_string() const {
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "(" << clock_ << "," << thread_id_ << "," << place_id_ << ")";
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
void Schedule::add(int64_t clk, int64_t thid, int64_t plid) {
|
void Schedule::add(int64_t clk, int64_t thid, int64_t plid) {
|
||||||
schedule_.insert(SchedEntry(clk, thid, plid));
|
schedule_.insert(SchedEntry(clk, thid, plid));
|
||||||
}
|
}
|
||||||
@@ -20,11 +30,20 @@ bool Schedule::ready(int64_t clk) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SchedEntry Schedule::pop() {
|
SchedEntry Schedule::pop() {
|
||||||
|
assert(!schedule_.empty());
|
||||||
SchedEntry result = *schedule_.begin();
|
SchedEntry result = *schedule_.begin();
|
||||||
schedule_.erase(schedule_.begin());
|
schedule_.erase(schedule_.begin());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Schedule::debug_string() {
|
||||||
|
std::ostringstream oss;
|
||||||
|
for (const SchedEntry &se : schedule_) {
|
||||||
|
oss << se.debug_string();
|
||||||
|
}
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
void Schedule::serialize(StreamBuffer *sb) {
|
void Schedule::serialize(StreamBuffer *sb) {
|
||||||
sb->write_uint32(schedule_.size());
|
sb->write_uint32(schedule_.size());
|
||||||
for (const SchedEntry &entry : schedule_) {
|
for (const SchedEntry &entry : schedule_) {
|
||||||
@@ -44,3 +63,41 @@ void Schedule::deserialize(StreamBuffer *sb) {
|
|||||||
add(clock, thread_id, place_id);
|
add(clock, thread_id, place_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LuaDefine(unittests_scheduler, "c") {
|
||||||
|
Schedule s, xs;
|
||||||
|
StreamBuffer sb;
|
||||||
|
|
||||||
|
// Put some stuff into a scheduler.
|
||||||
|
s.add(1, 5, 3);
|
||||||
|
s.add(4, 3, 2);
|
||||||
|
s.add(1, 3, 2);
|
||||||
|
s.add(4, 2, 3);
|
||||||
|
|
||||||
|
// Test serialize and deserialize.
|
||||||
|
LuaAssert(L, s.debug_string() == "(1,3,2)(1,5,3)(4,2,3)(4,3,2)");
|
||||||
|
s.serialize(&sb);
|
||||||
|
xs.deserialize(&sb);
|
||||||
|
LuaAssert(L, xs.debug_string() == "(1,3,2)(1,5,3)(4,2,3)(4,3,2)");
|
||||||
|
|
||||||
|
// Verify that pop, ready, and empty do the right thing.
|
||||||
|
LuaAssert(L, s.ready(0) == false)
|
||||||
|
LuaAssert(L, s.ready(1) == true)
|
||||||
|
LuaAssert(L, s.pop().debug_string() == "(1,3,2)");
|
||||||
|
LuaAssert(L, s.ready(0) == false)
|
||||||
|
LuaAssert(L, s.ready(1) == true)
|
||||||
|
LuaAssert(L, s.pop().debug_string() == "(1,5,3)");
|
||||||
|
LuaAssert(L, s.debug_string() == "(4,2,3)(4,3,2)");
|
||||||
|
LuaAssert(L, s.pop().debug_string() == "(4,2,3)");
|
||||||
|
LuaAssert(L, s.pop().debug_string() == "(4,3,2)");
|
||||||
|
LuaAssert(L, s.ready(10000) == false);
|
||||||
|
LuaAssert(L, s.empty());
|
||||||
|
LuaAssert(L, s.debug_string() == "");
|
||||||
|
|
||||||
|
// Test serialization of an empty streambuffer.
|
||||||
|
s.serialize(&sb);
|
||||||
|
xs.deserialize(&sb);
|
||||||
|
LuaAssert(L, xs.debug_string() == "");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator < (const SchedEntry &other) const;
|
bool operator < (const SchedEntry &other) const;
|
||||||
|
|
||||||
|
std::string debug_string() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Schedule {
|
class Schedule {
|
||||||
@@ -32,7 +34,9 @@ private:
|
|||||||
public:
|
public:
|
||||||
void add(int64_t clk, int64_t thid, int64_t plid);
|
void add(int64_t clk, int64_t thid, int64_t plid);
|
||||||
bool ready(int64_t clk) const;
|
bool ready(int64_t clk) const;
|
||||||
|
bool empty() const { return schedule_.empty(); }
|
||||||
SchedEntry pop();
|
SchedEntry pop();
|
||||||
|
std::string debug_string();
|
||||||
|
|
||||||
void serialize(StreamBuffer *sb);
|
void serialize(StreamBuffer *sb);
|
||||||
void deserialize(StreamBuffer *sb);
|
void deserialize(StreamBuffer *sb);
|
||||||
|
|||||||
Reference in New Issue
Block a user