Skip to content

Commit

Permalink
made requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oppiz committed Mar 8, 2019
1 parent a195ba6 commit f20692b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
4 changes: 2 additions & 2 deletions include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace elma {
Manager& schedule(Process& process, high_resolution_clock::duration period);
Manager& all(std::function<void(Process&)> f);

Manager& SetPriority(string name, int priority);
Manager& SortProcess();
Manager& set_priority(Process& process, int priority);
Manager& sort_processes();

Manager& init();
Manager& start();
Expand Down
22 changes: 6 additions & 16 deletions src/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace elma {
//! Initialize all processes. Usually called before run()
//! \return A reference to the manager, for chaining
Manager& Manager::init() {
SortProcess();
sort_processes();
return all([](Process& p) { p._init();});
}

Expand Down Expand Up @@ -123,7 +123,7 @@ namespace elma {

//! sort _Processes based on _priority to ensure higher priority process are updated first.
//! \return A reference to the manager, for chaining
Manager& Manager::SortProcess() {
Manager& Manager::sort_processes() {

std::sort(_processes.begin(), _processes.end(),[](const Process * lhs, const Process * rhs){
return lhs->_priority > rhs->_priority;
Expand All @@ -135,27 +135,17 @@ namespace elma {
//! Set Process Priority and sort _Processes to ensure higher priority are updated first.
//! Priority may be set -5 (low priority) to 15 (high priority)
//! This should allow priority adjustment while running.
//! \param name The name of the process you want to adjust priority level.
//! \param process The process you want to adjust priority level.
//! \param priority, a integer between -5 and 15
//! \return A reference to the manager, for chaining
Manager& Manager::SetPriority(string name, int priority) {
Manager& Manager::set_priority(Process& process, int priority) {

if (-5 <= priority && priority <= 15 ){
//Find process to adjust
auto it = std::find_if(_processes.begin(), _processes.end(), [name](const Process * n) {
return n->_name == name;
});

if (it != _processes.end()) {
(*it)->_priority = priority;
SortProcess();
}else{
throw Exception("Tried to access an unregistered or non-existant process.");
}
process._priority = priority;
sort_processes();
}else{
throw Exception("Priority must be between -5(low priority) and 15(high priority)");
}

return *this;
}

Expand Down
73 changes: 69 additions & 4 deletions test/priority.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,72 @@ namespace {

}

TEST(Priority, NoProcess) {
TEST(Priority, SetPriorityMethod) {

static vector<string> test1;
static vector<string> ans1 = {"lilly", "boby", "james", "lilly", "boby", "james", "lilly", "boby", "james" };

class Tester: public elma::Process {
public:
Tester(string name, int n = 0) : Process(name, n) {}
void init() {}
void start() {}
void update() {
test1.push_back("james");
//std::cout << "james" << "\n";
}
void stop() {}

};

class Tester2: public elma::Process {
public:
Tester2(string name, int n = 0) : Process(name, n) {}
void init() {}
void start() {}
void update() {
test1.push_back("lilly");
//std::cout << "lilly" << "\n";
}
void stop() {}

};

class Tester3: public elma::Process {
public:
Tester3(string name, int n = 0) : Process(name, n) {}
void init() {}
void start() {}
void update() {
test1.push_back("boby");
//std::cout << "boby" << "\n";
}
void stop() {}

};



elma::Manager m;
Tester james("james");
Tester2 lily("lily");
Tester3 boby("boby");

m.schedule(james, MS(30))
.schedule(lily, MS(30))
.schedule(boby, MS(30));

m.set_priority(james, 0);
m.set_priority(lily, 2);
m.set_priority(boby, 1);

m.init().run(MS(100));
EXPECT_EQ(test1, ans1);

}

//This is no longer valid now that the process is passed not its name
/* TEST(Priority, NoProcess) {
class Tester: public elma::Process {
Expand All @@ -91,8 +156,8 @@ namespace {
m.schedule(james, MS(30));
EXPECT_ANY_THROW(m.SetPriority("jams", 3));
}
//EXPECT_ANY_THROW(m.set_priority("jams", 3));
}*/

TEST(Priority, LookSort) {

Expand Down Expand Up @@ -180,7 +245,7 @@ namespace {
Tester james("james",19);
EXPECT_ANY_THROW( m.schedule(james, MS(30)));
Tester bob("bob");
EXPECT_ANY_THROW(m.SetPriority("bob", 23));
EXPECT_ANY_THROW(m.set_priority(bob, 23));
}

}

0 comments on commit f20692b

Please sign in to comment.