From 05a614c92a89b4eaac80dfbfe264eb3c613df2e7 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 13 Sep 2023 11:28:31 +0000 Subject: [PATCH] add: ex03 --- ex03/Makefile | 26 ++ ex03/src/AForm.cpp | 109 +++++++++ ex03/src/AForm.hpp | 57 +++++ ex03/src/Bureaucrat.cpp | 103 ++++++++ ex03/src/Bureaucrat.hpp | 46 ++++ ex03/src/Intern.cpp | 56 +++++ ex03/src/Intern.hpp | 17 ++ ex03/src/PresidentialPardonForm.cpp | 41 ++++ ex03/src/PresidentialPardonForm.hpp | 18 ++ ex03/src/RobotomyRequestForm.cpp | 49 ++++ ex03/src/RobotomyRequestForm.hpp | 18 ++ ex03/src/ShrubberyCreationForm.cpp | 71 ++++++ ex03/src/ShrubberyCreationForm.hpp | 18 ++ ex03/src/main.cpp | 360 ++++++++++++++++++++++++++++ 14 files changed, 989 insertions(+) create mode 100644 ex03/Makefile create mode 100644 ex03/src/AForm.cpp create mode 100644 ex03/src/AForm.hpp create mode 100644 ex03/src/Bureaucrat.cpp create mode 100644 ex03/src/Bureaucrat.hpp create mode 100644 ex03/src/Intern.cpp create mode 100644 ex03/src/Intern.hpp create mode 100644 ex03/src/PresidentialPardonForm.cpp create mode 100644 ex03/src/PresidentialPardonForm.hpp create mode 100644 ex03/src/RobotomyRequestForm.cpp create mode 100644 ex03/src/RobotomyRequestForm.hpp create mode 100644 ex03/src/ShrubberyCreationForm.cpp create mode 100644 ex03/src/ShrubberyCreationForm.hpp create mode 100644 ex03/src/main.cpp diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..024f170 --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,26 @@ +CXX := c++ +CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g +SRCDIR := src +OBJDIR := obj +NAME := ex03 + +SRCS := $(wildcard $(SRCDIR)/*.cpp) +OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + mkdir -p obj + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(NAME): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR) + +fclean: clean + rm -fr $(NAME) + +re: fclean + @make --no-print-directory all diff --git a/ex03/src/AForm.cpp b/ex03/src/AForm.cpp new file mode 100644 index 0000000..a0dbf6d --- /dev/null +++ b/ex03/src/AForm.cpp @@ -0,0 +1,109 @@ +#include "AForm.hpp" + +#include +#include +#include + +AForm::AForm() +{ + std::cout << "AForm()" << std::endl; +} + +AForm::AForm(const AForm& src): + _name(src._name), + _target(src._target) +{ + std::cout << "AForm(AForm)" << std::endl; + *this = src; +} + +AForm::AForm(const std::string& name, int to_execute, int to_sign, const std::string& target): + _name(name), + _signed(0), + _to_sign(to_sign), + _to_execute(to_execute), + _target(target) +{ + std::cout << "AForm(" << name << ", " << to_execute << ", " << to_sign << ")" << std::endl; + if (to_execute > 150 || to_sign < 1) + throw GradeTooLowException(); + if (to_execute < 1 || to_sign < 1) + throw GradeTooHighException(); +} + +AForm::~AForm() +{ + std::cout << "~AForm()" << std::endl; +} + +AForm& AForm::operator=(const AForm& src) +{ + this->_signed = src._signed; + this->_to_sign = src._to_sign; + this->_to_execute = src._to_execute; + + return *this; +} + +void AForm::beSigned(const Bureaucrat &bureaucrat) +{ + if (this->_signed == true) + { + std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the AForm is already sign" << std::endl; + return; + } + if (bureaucrat.getGrade() > this->_to_sign) + throw GradeTooLowException(); + this->_signed = true; + std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl; +} + +void AForm::execute(const Bureaucrat &exec) const +{ + if (!this->_signed) + throw NotSignedException(); + if (exec.getGrade() > this->_to_execute) + throw GradeTooLowException(); +} + +const std::string& AForm::getName() const +{ + return this->_name; +} + +bool AForm::getSigned() const +{ + return this->_signed; +} + +int AForm::getGradeToBeExecute() const +{ + return this->_to_execute; +} + +int AForm::getGradeToBeSign() const +{ + return this->_to_sign; +} + +std::ostream& operator<<(std::ostream& os, const AForm& src) +{ + os << src.getName() << " AForm signed: " << src.getSigned() << ", grade to be execute: " << src.getGradeToBeExecute() << ", grade to be sign: " << src.getGradeToBeSign() << "." << std::endl; + + return os; +} + +const char* AForm::GradeTooLowException::what() const throw() +{ + return "grade is to low"; +} + +const char* AForm::GradeTooHighException::what() const throw() +{ + return "grade is to high"; +} + +const char* AForm::NotSignedException::what() const throw() +{ + return "form not signed"; +} diff --git a/ex03/src/AForm.hpp b/ex03/src/AForm.hpp new file mode 100644 index 0000000..f6b87d0 --- /dev/null +++ b/ex03/src/AForm.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include +#include +#include + +#include "Bureaucrat.hpp" +class Bureaucrat; + +class AForm +{ + private: + const std::string _name; + bool _signed; + int _to_sign; + int _to_execute; + + protected: + const std::string _target; + + public: + AForm(); + AForm(const std::string& name, int to_execute, int to_sign, const std::string& target); + AForm(const AForm& src); + virtual ~AForm(); + + AForm& operator=(const AForm& src); + + const std::string& getName() const; + bool getSigned() const; + int getGradeToBeExecute() const; + int getGradeToBeSign() const; + + void beSigned(const Bureaucrat& bureaucrat); + virtual void execute(const Bureaucrat& exec) const = 0; + + class GradeTooHighException: public std::exception + { + public: + const char * what() const throw(); + }; + + class GradeTooLowException: public std::exception + { + public: + const char * what() const throw(); + }; + + class NotSignedException: public std::exception + { + public: + const char * what() const throw(); + }; + +}; + +std::ostream& operator<<(std::ostream& os, const AForm& src); diff --git a/ex03/src/Bureaucrat.cpp b/ex03/src/Bureaucrat.cpp new file mode 100644 index 0000000..3ae419a --- /dev/null +++ b/ex03/src/Bureaucrat.cpp @@ -0,0 +1,103 @@ +#include "Bureaucrat.hpp" +#include +#include +#include +#include + +Bureaucrat::Bureaucrat() +{ + std::cout << "Bureaucrat()" << std::endl; +} + +Bureaucrat::Bureaucrat(const Bureaucrat& src) +{ + *this = src; + std::cout << "Bureaucrat(Bureaucrat)" << std::endl; +} + +Bureaucrat::Bureaucrat(const std::string& name, int grade): + _name(name) +{ + setGrade(grade); + std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl; +} + +Bureaucrat::~Bureaucrat() +{ + std::cout << "~Bureaucrat()" << std::endl; +} + +Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src) +{ + this->_grade = src._grade; + + return *this; +} + +void Bureaucrat::setGrade(int new_grade) +{ + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + this->_grade = new_grade; +} + +const std::string& Bureaucrat::getName(void) const +{ + return this->_name; +} + +int Bureaucrat::getGrade(void) const +{ + return this->_grade; +} + +void Bureaucrat::increment() +{ + if (this->_grade == 1) + throw GradeTooHighException(); + this->_grade--; +} + +void Bureaucrat::decrement() +{ + if (this->_grade == 150) + throw GradeTooLowException(); + this->_grade++; +} + +void Bureaucrat::signForm(AForm& form) const +{ + form.beSigned(*this); +} + +void Bureaucrat::executeForm(AForm const & form) const +{ + try + { + form.execute(*this); + std::cout << this->_name << " executed " << form.getName() << std::endl; + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + } +} + + +const char* Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Too low grade"; +} + +const char* Bureaucrat::GradeTooHighException::what() const throw() +{ + return "Too high grade"; +} + +std::ostream& operator<<(std::ostream &stream, Bureaucrat &src) +{ + stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl; + return stream; +} diff --git a/ex03/src/Bureaucrat.hpp b/ex03/src/Bureaucrat.hpp new file mode 100644 index 0000000..a32bb06 --- /dev/null +++ b/ex03/src/Bureaucrat.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include +#include + +#include "AForm.hpp" +class AForm; + +class Bureaucrat +{ + private: + const std::string _name; + int _grade; + + public: + Bureaucrat(); + ~Bureaucrat(); + Bureaucrat(const Bureaucrat& src); + Bureaucrat(const std::string& name, int grade); + + Bureaucrat& operator=(const Bureaucrat& src); + + const std::string& getName() const; + int getGrade() const; + void setGrade(int new_grade); + + void increment(); + void decrement(); + void signForm(AForm& form) const; + void executeForm(AForm const & form) const; + + class GradeTooHighException: public std::exception + { + public: + const char* what() const throw(); + }; + + class GradeTooLowException: public std::exception + { + public: + const char* what() const throw(); + }; + +}; + +std::ostream& operator<<(std::ostream& stream, Bureaucrat &src); diff --git a/ex03/src/Intern.cpp b/ex03/src/Intern.cpp new file mode 100644 index 0000000..543775a --- /dev/null +++ b/ex03/src/Intern.cpp @@ -0,0 +1,56 @@ +#include "Intern.hpp" +#include "AForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include +#include + +Intern::Intern() +{ + std::cout << "Intern()" << std::endl; +} + +Intern::Intern(Intern const& src) +{ + (void) src; +} + +Intern::~Intern() +{ + std::cout << "~Intern()" << std::endl; +} + +Intern& Intern::operator=( Intern const& src ) +{ + (void) src; + return *this; +} + +AForm* Intern::makeForm( std::string const& formName, std::string const& formTarget) const +{ + static const std::string names[3] = { + "robotomy request", + "presidential pardon", + "shrubbery creation" + }; + + int name_idx; + for (name_idx = 0; name_idx < 3 && names[name_idx] != formName; ++name_idx) ; + + switch (name_idx) { + case 0: + std::cout << "Intern creates " << formName << std::endl; + return new RobotomyRequestForm(formTarget); + case 1: + std::cout << "Intern creates " << formName << std::endl; + return new PresidentialPardonForm(formTarget); + case 2: + std::cout << "Intern creates " << formName << std::endl; + return new ShrubberyCreationForm(formTarget); + default: + std::cout << "Intern can't find form named " << formName << std::endl; + return NULL; + } +} + diff --git a/ex03/src/Intern.hpp b/ex03/src/Intern.hpp new file mode 100644 index 0000000..2f7e386 --- /dev/null +++ b/ex03/src/Intern.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "AForm.hpp" + +#include + +class Intern +{ + public: + Intern(); + Intern(const Intern& src); + ~Intern(); + + Intern& operator=(const Intern& src); + + AForm* makeForm( std::string const& formName, std::string const& formTarget ) const; +}; diff --git a/ex03/src/PresidentialPardonForm.cpp b/ex03/src/PresidentialPardonForm.cpp new file mode 100644 index 0000000..998bbc3 --- /dev/null +++ b/ex03/src/PresidentialPardonForm.cpp @@ -0,0 +1,41 @@ +#include "PresidentialPardonForm.hpp" +#include "AForm.hpp" + +#include +#include + +PresidentialPardonForm::PresidentialPardonForm(): + AForm("PresidentialPardonForm", 5, 25, "") +{ + std::cout << "PresidentialPardonForm()" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm(const std::string& target): + AForm("PresidentialPardonForm", 5, 25, target) +{ + std::cout << "PresidentialPardonForm(" << target << ")" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& src): + AForm(src) +{ + std::cout << "PresidentialPardonForm(PresidentialPardonForm)" << std::endl; +} + +PresidentialPardonForm::~PresidentialPardonForm() +{ + std::cout << "PresidentialPardonForm()" << std::endl; +} + +PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& src) +{ + if (this != & src) + *this = src; + return *this; +} + +void PresidentialPardonForm::execute(const Bureaucrat &exec) const +{ + AForm::execute(exec); + std::cout << this->_target << " has been pardoned by Zaphod Beeblebrox" << std::endl; +} diff --git a/ex03/src/PresidentialPardonForm.hpp b/ex03/src/PresidentialPardonForm.hpp new file mode 100644 index 0000000..087aa88 --- /dev/null +++ b/ex03/src/PresidentialPardonForm.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +class PresidentialPardonForm: public AForm +{ + public: + PresidentialPardonForm(); + PresidentialPardonForm(const std::string& target); + PresidentialPardonForm(const PresidentialPardonForm& src); + ~PresidentialPardonForm(); + + PresidentialPardonForm& operator=(const PresidentialPardonForm& src); + + void execute(const Bureaucrat& exec) const; +}; diff --git a/ex03/src/RobotomyRequestForm.cpp b/ex03/src/RobotomyRequestForm.cpp new file mode 100644 index 0000000..8b382e0 --- /dev/null +++ b/ex03/src/RobotomyRequestForm.cpp @@ -0,0 +1,49 @@ +#include "RobotomyRequestForm.hpp" +#include "AForm.hpp" + +#include +#include + +RobotomyRequestForm::RobotomyRequestForm(): + AForm("RobotomyRequestForm", 45, 72, "") +{ + std::cout << "RobotomyRequestForm()" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm(const std::string& target): + AForm("RobotomyRequestForm", 45, 72, target) +{ + std::cout << "RobotomyRequestForm(" << target << ")" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& src): + AForm(src) +{ + std::cout << "RobotomyRequestForm(RobotomyRequestForm)" << std::endl; +} + +RobotomyRequestForm::~RobotomyRequestForm() +{ + std::cout << "~RobotomyRequestForm()" << std::endl; +} + +RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& src) +{ + if (this != & src) + *this = src; + return *this; +} + +void RobotomyRequestForm::execute(const Bureaucrat &exec) const +{ + static bool ok(false); + + AForm::execute(exec); + + std::cout << "Bzit bzit" << std::endl; + if (ok) + std::cout << this->_target << " be robotomized" << std::endl; + else + std::cout << this->_target << " robotomized failed" << std::endl; + ok = !ok; +} diff --git a/ex03/src/RobotomyRequestForm.hpp b/ex03/src/RobotomyRequestForm.hpp new file mode 100644 index 0000000..6d165a7 --- /dev/null +++ b/ex03/src/RobotomyRequestForm.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +class RobotomyRequestForm: public AForm +{ + public: + RobotomyRequestForm(); + RobotomyRequestForm(const std::string& target); + RobotomyRequestForm(const RobotomyRequestForm& src); + ~RobotomyRequestForm(); + + RobotomyRequestForm& operator=(const RobotomyRequestForm& src); + + void execute(const Bureaucrat& exec) const; +}; diff --git a/ex03/src/ShrubberyCreationForm.cpp b/ex03/src/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..3001a31 --- /dev/null +++ b/ex03/src/ShrubberyCreationForm.cpp @@ -0,0 +1,71 @@ +#include "ShrubberyCreationForm.hpp" +#include "AForm.hpp" + +#include +#include +#include + +ShrubberyCreationForm::ShrubberyCreationForm(): + AForm("shrubbery creation", 137, 145, "") +{ + std::cout << "ShrubberyCreationForm()" << std::endl; +} + +ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target): + AForm("ShrubberyCreationForm", 137, 145, target) +{ + std::cout << "ShrubberyCreationForm(" << target << ")" << std::endl; +} + +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& src): + AForm(src) +{ + std::cout << "ShrubberyCreationForm(ShrubberyCreationForm)" << std::endl; +} + +ShrubberyCreationForm::~ShrubberyCreationForm() +{ + std::cout << "~ShrubberyCreationForm()" << std::endl; +} + +ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm &src) +{ + if (this != &src) + *this = src; + return *this; +} + +void ShrubberyCreationForm::execute(const Bureaucrat &exec) const +{ + static const std::string asciiTrees = + " _-_\n" + " /~~ ~~\\\n" + " /~~ ~~\\\n" + "{ }\n" + " \\ _- -_ /\n" + " ~ \\\\ // ~\n" + "_- - | | _- _\n" + " _ - | | -_\n" + " // \\\\\n" + " _-_\n" + " /~~ ~~\\\n" + " /~~ ~~\\\n" + "{ }\n" + " \\ _- -_ /\n" + " ~ \\\\ // ~\n" + "_- - | | _- _\n" + " _ - | | -_\n" + " // \\\\\n" + ; + AForm::execute(exec); + + std::ofstream file((this->_target + "_shruberry").c_str()); + + if (!file.good()) + { + std::cout << "error: " << this->_target << "_shruberry" << "file incorrect" << std::endl; + return; + } + file << asciiTrees; + +} diff --git a/ex03/src/ShrubberyCreationForm.hpp b/ex03/src/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..ded5f64 --- /dev/null +++ b/ex03/src/ShrubberyCreationForm.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +class ShrubberyCreationForm: public AForm +{ + public: + ShrubberyCreationForm(); + ShrubberyCreationForm(const ShrubberyCreationForm& src); + ShrubberyCreationForm(const std::string& target); + ~ShrubberyCreationForm(); + + ShrubberyCreationForm& operator=(const ShrubberyCreationForm& src); + + void execute(const Bureaucrat& exec) const; +}; diff --git a/ex03/src/main.cpp b/ex03/src/main.cpp new file mode 100644 index 0000000..a717d8e --- /dev/null +++ b/ex03/src/main.cpp @@ -0,0 +1,360 @@ +#include "AForm.hpp" +#include "Intern.hpp" +#include "Bureaucrat.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "PresidentialPardonForm.hpp" +#include +#include + +int main() { + + std::cout << "-------------Bureaucrat TEST-----------" << std::endl; + std::cout << "Constructor too high grade" << std::endl; + { + try { + Bureaucrat crat("crat", 0); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "Constructor too low grade" << std::endl; + { + try { + Bureaucrat crat("crat", 151); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "increment too high" << std::endl; + { + Bureaucrat crat("crat", 1); + try { + crat.increment(); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "decrement too low" << std::endl; + { + Bureaucrat crat("crat", 150); + try { + crat.decrement(); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "setgrade too low" << std::endl; + { + Bureaucrat crat("crat", 42); + try { + crat.setGrade(151); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "setgrade too high" << std::endl; + { + Bureaucrat crat("crat", 42); + try { + crat.setGrade(0); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "increment normal" << std::endl; + { + Bureaucrat crat("crat", 42); + std::cout << "default grade: " << crat.getGrade() << std::endl; + crat.increment(); + std::cout << "after increment grade: " << crat.getGrade() << std::endl; + } + std::cout << std::endl; + + std::cout << "decrement normal" << std::endl; + { + Bureaucrat crat("crat", 42); + std::cout << "default grade: " << crat.getGrade() << std::endl; + crat.decrement(); + std::cout << "after decrement grade: " << crat.getGrade() << std::endl; + } + std::cout << std::endl; + + std::cout << "print" << std::endl; + { + Bureaucrat crat("crat", 42); + std::cout << crat; + } + std::cout << std::endl; + + std::cout << "------------- Form TEST-----------" << std::endl; + + std::cout << "print" << std::endl; + { + RobotomyRequestForm form("moi"); + std::cout << form; + } + std::cout << std::endl; + + std::cout << "------------- RobotomyRequestForm Sign TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 42); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "RobotomyRequestForm already sign" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "------------- RobotomyRequestForm TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 42); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "RobotomyRequestForm already sign" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "RobotomyRequestForm execute not signed form" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.executeForm(form); + } + std::cout << std::endl; + std::cout << "RobotomyRequestForm execute" << std::endl; + { + RobotomyRequestForm form("bozo"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + + crat.executeForm(form); + crat.executeForm(form); + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "------------- PresidentialPardonForm TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a PresidentialPardonForm" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a PresidentialPardonForm" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low PresidentialPardonForm" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "PresidentialPardonForm already sign" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "PresidentialPardonForm not signed execute" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "PresidentialPardonForm execute" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + + crat.executeForm(form); + } + std::cout << std::endl; + + + std::cout << "------------- ShrubberyCreationForm TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a ShrubberyCreationForm" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 42); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a ShrubberyCreationForm" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low ShrubberyCreationForm" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "ShrubberyCreationForm already sign" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "ShrubberyCreationForm not sign execute" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 1); + + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "ShrubberyCreationForm execute" << std::endl; + { + ShrubberyCreationForm form("bozo"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "------------- Intern TEST-----------" << std::endl; + + std::cout << "Intern" << std::endl; + { + Intern intern; + static const std::string forms[] = { + "robotomy request", + "presidential pardon", + "shrubbery creation", + "bozo" + }; + for (int i = 0; i != 4; ++i) + { + AForm* form = intern.makeForm(forms[i], "bozoman"); + if (form != NULL) + delete form; + } + } + std::cout << std::endl; +}