Commit 2dc09bed authored by Alexis Lebis's avatar Alexis Lebis

Course implem ++ and several test / assert introduction. Competency operator...

Course implem ++ and several test / assert introduction. Competency operator == and debug on courseTemporalFrameExce
parent 11f56886
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cassert>
#include "model/magnitude.h" #include "model/magnitude.h"
#include "model/competency.h" #include "model/competency.h"
...@@ -13,6 +15,10 @@ int main(int argc, char* argv[]) ...@@ -13,6 +15,10 @@ int main(int argc, char* argv[])
Magnitude m = Magnitude::build(0.7); Magnitude m = Magnitude::build(0.7);
Competency c = Competency::build(0.5, ""); Competency c = Competency::build(0.5, "");
Competency c2 = Competency::build(m,""); Competency c2 = Competency::build(m,"");
Competency c3 = Competency::build(0.5);
assert(c3.name().compare("Competency#"+std::to_string(c3.id())) == 0);
std::cout << "Default naming ok" << std::endl;
std::cout << std::to_string(c.c_magnitude().value()) << std::endl ; std::cout << std::to_string(c.c_magnitude().value()) << std::endl ;
std::cout << c2.competencyValue() << std::endl; std::cout << c2.competencyValue() << std::endl;
...@@ -41,5 +47,15 @@ int main(int argc, char* argv[]) ...@@ -41,5 +47,15 @@ int main(int argc, char* argv[])
c.evolveTowards(-0.5); c.evolveTowards(-0.5);
std::cout << c << std::endl; std::cout << c << std::endl;
assert( c == c);
std::cout << "c == c OK" << std::endl ;
std::cout << "renaming c2 as c.name()" << std::endl;
c2.setName(c.name());
assert( c == c2);
std::cout << "c == c2 OK" << std::endl ;
assert ( !(c == c3) );
std::cout << "c != c3 OK" << std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
#include <eo> #include <eo>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cassert>
#include "model/course.h" #include "model/course.h"
#include "model/competency.h"
#include "model/exception/courseECTSException.h"
#include "model/exception/courseTemporalFrameException.h"
#include "model/exception/competencyEvolvingException.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Course c = Course::build(); Course c = Course::build();
std::cout << c << std::endl; std::cout << c << std::endl;
assert(c.prerequisites().size() == 0);
std::cout << "Length of prereq: " << c.prerequisites().size() << std::endl; std::cout << "Length of prereq: " << c.prerequisites().size() << std::endl;
c = Course::build(); c = Course::build();
std::cout << c << std::endl; std::cout << c << std::endl;
std::cout << "Length of prereq: " << c.prerequisites().size() << std::endl;
assert(c.prerequisites().size() == 0);
std::cout << "Length of prereq still : " << c.prerequisites().size() << " after redefining." << std::endl;
c = Course::build(5); c = Course::build(5);
std::cout << c << std::endl; assert(c.ects() == 5);
std::cout << "C's ects is equal to five" << std::endl;
c = Course::build(7,"Testdel'oeuf"); c = Course::build(7,"Testdel'oeuf");
std::cout << c << std::endl; std::cout << c << std::endl;
assert(c.ects() == 7 && (c.name().compare("Testdel'oeuf") == 0) );
std::cout << "Comp constructor ok" << std::endl;
try
{
c = Course::build(-1, "lol"); c = Course::build(-1, "lol");
}
catch(CourseECTSException & e)
{
assert(e.getCourse().ects() == -1);
std::cout << "Last ects set correctly taken into account" << std::endl;
std::cout << e.what() << std::endl;
std::cout << c << std::endl; std::cout << c << std::endl;
}
Competency comp1 = Competency::build(0.2);
Competency comp2 = Competency::build(0.4);
Competency comp3 = Competency::build(0.7);
c.addPrerequisite(comp1);
assert(c.prerequisites().size() == 1);
assert(c.prerequisites().at(0) == comp1 );
std::cout << "Prereq add OK" << std::endl;
c.addPrerequisite(comp2);
Competency * ptr_c = c.rmPrerequisite(comp1);
assert(ptr_c != NULL);
assert(c.prerequisites().size() == 1);
assert(c.prerequisites().at(0) == comp2);
std::cout << "Prereq rm 1/2 OK" << std::endl ;
c.addPrerequisite(comp1);
ptr_c = c.rmPrerequisite(comp3);
assert(ptr_c == NULL);
assert(c.prerequisites().size() == 2);
assert(c.prerequisites().at(0) == comp2 );
assert(c.prerequisites().at(1) == comp1);
std::cout << "Prereq rm 2/2 OK" << std::endl ;
c.addTemporalFrame(0);
c.addTemporalFrame(2);
assert(c.timeFrame().size() == 2);
c.addTemporalFrame(2);
assert(c.timeFrame().size() == 2);
std::cout << "_temporalAvailability is protected against duplicate" << std::endl;
try{
c.addTemporalFrame(-2);
}
catch(CourseTemporalFrameException & e)
{
std::cout << e.what() << std::endl;
assert(e.getTime() == -2);
assert(c.timeFrame().size() == 2);
std::cout << "Excepetion handling ok" << std::endl;
}
// int * ptr_time = c.rmTemporalFrameValue(2);
// assert(ptr_time != NULL && c.timeFrame().size()==1);
// std::cout << "Temporal rm by value 1/2 ok" << std::endl;;
// ptr_time = c.rmTemporalFrameValue(2);
// assert(ptr_time == NULL && c.timeFrame().size() == 1);
// std::cout << "Temp rm by val 2/2 ok" << std::endl;
// c.addTemporalFrame(7);
// ptr_time = c.rmTemporalFrameIndex(1);
// assert(ptr_time != NULL && c.timeFrame().size()==2 && c.timeFrame().at(0) == 0);
// std::cout << "Temporal rm by index ok" << std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
...@@ -10,7 +10,7 @@ int Competency::COMPETENCY_COUNTER = 0; ...@@ -10,7 +10,7 @@ int Competency::COMPETENCY_COUNTER = 0;
// === FACTORY // === FACTORY
Competency Competency::build(Magnitude & m, std::string name = "") Competency Competency::build(Magnitude & m, std::string name)
{ {
int id = Competency::assignID(); int id = Competency::assignID();
if(name.empty()) if(name.empty())
...@@ -19,7 +19,7 @@ Competency Competency::build(Magnitude & m, std::string name = "") ...@@ -19,7 +19,7 @@ Competency Competency::build(Magnitude & m, std::string name = "")
return Competency(id, m, name); return Competency(id, m, name);
} }
Competency Competency::build(double d = 0, std::string name = "") Competency Competency::build(double d = 0, std::string name)
{ {
int id = Competency::assignID(); int id = Competency::assignID();
if(name.empty()) if(name.empty())
...@@ -94,3 +94,8 @@ std::ostream& operator<<(std::ostream& Stream, const Competency & c) ...@@ -94,3 +94,8 @@ std::ostream& operator<<(std::ostream& Stream, const Competency & c)
Stream << s ; Stream << s ;
return Stream; return Stream;
} }
bool Competency::operator==(const Competency & c) const
{
return ( this->_id == c.id() || ( this->_name.compare(c.c_name()) == 0 ) );
}
\ No newline at end of file
...@@ -26,8 +26,8 @@ class Competency ...@@ -26,8 +26,8 @@ class Competency
static int COMPETENCY_COUNTER; static int COMPETENCY_COUNTER;
static int assignID(); static int assignID();
public: public:
static Competency build(Magnitude &, std::string); static Competency build(Magnitude &, std::string s = "");
static Competency build(double, std::string); static Competency build(double, std::string = "");
// === FUNCTION // === FUNCTION
/** evolveTowards allows the competency to increase or decrease according to the value passed in parameter (either in Magnitude._value or double). It represents the competency evolving towards a specfic state, here represented by this->_m.value() + m.value(). /** evolveTowards allows the competency to increase or decrease according to the value passed in parameter (either in Magnitude._value or double). It represents the competency evolving towards a specfic state, here represented by this->_m.value() + m.value().
...@@ -59,6 +59,10 @@ class Competency ...@@ -59,6 +59,10 @@ class Competency
void setMagnitude(Magnitude & m){this->_m = m;} void setMagnitude(Magnitude & m){this->_m = m;}
void setName(std::string s){this->_name = s;} void setName(std::string s){this->_name = s;}
// === OPERATOR
/// A competency is equal to another iff their id are the same, or their name
bool operator==(const Competency & c) const;
}; };
// === OPERATOR // === OPERATOR
std::ostream & operator<<(std::ostream& Stream, const Competency & c); std::ostream & operator<<(std::ostream& Stream, const Competency & c);
......
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <algorithm>
#include <iterator>
#include "course.h" #include "course.h"
#include "competency.h" #include "competency.h"
#include "exception/courseECTSException.h" #include "exception/courseECTSException.h"
#include "exception/courseTemporalFrameException.h"
int Course::COURSE_COUNTER = 0; int Course::COURSE_COUNTER = 0;
...@@ -50,24 +53,54 @@ Course::Course(int id, int ects, std::string name) ...@@ -50,24 +53,54 @@ Course::Course(int id, int ects, std::string name)
name = "Course#"+std::to_string(this->_id); name = "Course#"+std::to_string(this->_id);
this->_name = name; this->_name = name;
} }
void Course::setAvailabilities(std::vector<int> times) void Course::setAvailabilities(std::vector<int> & times)
{} {this->_temporalAvailability = times;}
void Course::setPrerequisites(std::vector<Competency> prereqs) void Course::setPrerequisites(std::vector<Competency> & prereqs)
{} {this->_prerequisites = prereqs;}
void Course::setTeachedComps(std::vector<std::pair<Competency,double>> wComps) void Course::setTeachedComps(std::vector<std::pair<Competency,double>> & wComps)
{} {this->_weightedTeached = wComps;}
// ADDER // ADDER
void Course::addTeachedComp(std::pair<Competency,double> wComp) void Course::addTeachedComp(std::pair<Competency,double> wComp)
{} {
if( _duplicataProtection(&(this->_weightedTeached), wComp.first))
return;
this->_weightedTeached.push_back(wComp);
}
void Course::addPrerequisite(Competency prereq) void Course::addPrerequisite(Competency prereq)
{} {
if( _duplicataProtection(&(this->_prerequisites), prereq) )
return;
this->_prerequisites.push_back(prereq);
}
void Course::addTemporalFrame(int time) void Course::addTemporalFrame(int time)
{} {
if(time < 0)
throw CourseTemporalFrameException(this, time);
if( _duplicataProtection(&(this->_temporalAvailability), time) )
return;
this->_temporalAvailability.push_back(time);
}
// DELETER // DELETER
Competency * Course::rmPrerequisite(Competency prereq) Competency * Course::rmPrerequisite(Competency prereq)
{return NULL;} {
std::vector<Competency>::iterator it =
std::find(
this->_prerequisites.begin(),
this->_prerequisites.end(),
prereq
);
if(it == this->_prerequisites.end())
return NULL;
int index = std::distance(this->_prerequisites.begin(), it);
Competency * c = &(this->_prerequisites.at(index));
this->_prerequisites.erase(this->_prerequisites.begin()+index);
return c;
}
std::pair<Competency,double> * Course::rmTeachedComp(Competency teached) std::pair<Competency,double> * Course::rmTeachedComp(Competency teached)
{return NULL;} {return NULL;}
int * Course::rmTemporalFrameIndex(int index) int * Course::rmTemporalFrameIndex(int index)
...@@ -81,10 +114,40 @@ Course::Course(int id, int ects, std::string name) ...@@ -81,10 +114,40 @@ Course::Course(int id, int ects, std::string name)
// === END MUTATOR // === END MUTATOR
// === FUNC
bool Course::_duplicataProtection(std::vector<int> * timeFrame, int time)
{
std::vector<int>::iterator it = std::find( timeFrame->begin(), timeFrame->end(), time);
return it != timeFrame->end();
}
bool Course::_duplicataProtection(std::vector<Competency> * prereq, Competency c)
{
std::vector<Competency>::iterator it = std::find( prereq->begin(), prereq->end(), c);
return it != prereq->end();
}
bool Course::_duplicataProtection(std::vector<std::pair<Competency,double>> *teached, Competency c)
{
std::vector<std::pair<Competency, double>>::iterator it = teached->begin();
int index = std::distance(teached->begin(), teached->begin());
Competency current = teached->at(index).first;
while(it != teached->end() && !( teached->at(index).first == c) )
{
it++;
index=std::distance(teached->begin(), teached->begin());
current = teached->at(index).first;
}
return it!=teached->end();
}
// === OPERATOR // === OPERATOR
std::ostream& operator<<(std::ostream& Stream, const Course & c) std::ostream& operator<<(std::ostream& Stream, const Course & c)
{ {
std::string s = "Competency\n\tid:"+std::to_string(c.id())+"\n\tname:"+c.name()+"\n\tECTS: "+std::to_string(c.ects()); std::string s = "Course\n\tid:"+std::to_string(c.id())+"\n\tname:"+c.name()+"\n\tECTS: "+std::to_string(c.ects());
Stream << s; Stream << s;
return Stream; return Stream;
} }
......
...@@ -39,6 +39,12 @@ class Course ...@@ -39,6 +39,12 @@ class Course
std::string _name; std::string _name;
int _id; int _id;
// === FUNC
/// _duplicataProtection returns true if the value (2nd param) searched into (1st param) is found
bool _duplicataProtection(std::vector<int> *, int);
bool _duplicataProtection(std::vector<Competency> *, Competency);
bool _duplicataProtection(std::vector<std::pair<Competency,double>> *, Competency);
// Static // Static
static int COURSE_COUNTER; static int COURSE_COUNTER;
static int assignID(); static int assignID();
...@@ -54,14 +60,15 @@ class Course ...@@ -54,14 +60,15 @@ class Course
const std::string name() const{return this->_name;}; const std::string name() const{return this->_name;};
const int ects() const{return this->_ects;} const int ects() const{return this->_ects;}
const std::vector<Competency> prerequisites() const {return this->_prerequisites;} const std::vector<Competency> prerequisites() const {return this->_prerequisites;}
const std::vector<int> timeFrame() const {return this->_temporalAvailability;}
// === MUTATOR // === MUTATOR
// SETTER // SETTER
void setECTS(int ects); void setECTS(int ects);
void setName(std::string); void setName(std::string);
void setAvailabilities(std::vector<int>); void setAvailabilities(std::vector<int>&);
void setPrerequisites(std::vector<Competency>); void setPrerequisites(std::vector<Competency>&);
void setTeachedComps(std::vector<std::pair<Competency,double>>); void setTeachedComps(std::vector<std::pair<Competency,double>>&);
// ADDER // ADDER
void addTeachedComp(std::pair<Competency,double>); void addTeachedComp(std::pair<Competency,double>);
......
...@@ -26,6 +26,8 @@ class CourseECTSException : public std::exception ...@@ -26,6 +26,8 @@ class CourseECTSException : public std::exception
{ {
//this->_course must not be free by ~this! //this->_course must not be free by ~this!
} }
Course & getCourse() const{return *(this->_course);}
}; };
#endif // SRC_MODEL_EXCEPTION_COURSE_ECTS_EXCEPTION_H_ #endif // SRC_MODEL_EXCEPTION_COURSE_ECTS_EXCEPTION_H_
\ No newline at end of file
#ifndef SRC_MODEL_EXCEPTION_COURSE_TEMPORAL_FRAME_EXCEPTION_H_
#define SRC_MODEL_EXCEPTION_COURSE_TEMPORAL_FRAME_EXCEPTION_H_
#include <exception>
#include <iostream>
#include <string>
#include "../course.h"
class CourseTemporalFrameException : public std::exception
{
private:
Course * _course;
int _time;
std::string _phrase;
public:
CourseTemporalFrameException(Course * c, int time) throw()
: _course(c)
{
this->_phrase = "Exception on time frame: value ("+std::to_string(time)+") is uncorrect. Can it be negative?";
this->_time = time;
}
virtual const char* what() const throw()
{return _phrase.c_str();}
virtual ~CourseTemporalFrameException() throw()
{
//this->_course must not be free by ~this!
}
Course & getCourse() const{return *(this->_course);}
const int getTime() const{return this->_time;}
};
#endif // SRC_MODEL_EXCEPTION_COURSE_TEMPORAL_FRAME_EXCEPTION_H_
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment