From f20692bf537539551c817a52e93b67ebceb5757a Mon Sep 17 00:00:00 2001 From: oppiz Date: Thu, 7 Mar 2019 22:32:56 -0800 Subject: [PATCH] made requested changes --- include/manager.h | 4 +-- src/manager.cc | 22 ++++---------- test/priority.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 77 insertions(+), 22 deletions(-) diff --git a/include/manager.h b/include/manager.h index e6145d7..e8078d6 100644 --- a/include/manager.h +++ b/include/manager.h @@ -33,8 +33,8 @@ namespace elma { Manager& schedule(Process& process, high_resolution_clock::duration period); Manager& all(std::function f); - Manager& SetPriority(string name, int priority); - Manager& SortProcess(); + Manager& set_priority(Process& process, int priority); + Manager& sort_processes(); Manager& init(); Manager& start(); diff --git a/src/manager.cc b/src/manager.cc index a72c6f6..90805e4 100644 --- a/src/manager.cc +++ b/src/manager.cc @@ -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();}); } @@ -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; @@ -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; } diff --git a/test/priority.cc b/test/priority.cc index 90f9370..731dfb6 100644 --- a/test/priority.cc +++ b/test/priority.cc @@ -73,7 +73,72 @@ namespace { } - TEST(Priority, NoProcess) { + TEST(Priority, SetPriorityMethod) { + + static vector test1; + static vector 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 { @@ -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) { @@ -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)); } } \ No newline at end of file