diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..7f950fe --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,26 @@ +CXX := c++ +CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g +SRCDIR := src +OBJDIR := obj +NAME := ex01 + +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/ex01/Span.cpp b/ex01/src/Span.cpp similarity index 60% rename from ex01/Span.cpp rename to ex01/src/Span.cpp index 65aa2d2..408dda9 100644 --- a/ex01/Span.cpp +++ b/ex01/src/Span.cpp @@ -27,18 +27,21 @@ Span& Span::operator=(const Span& src) int Span::shortestSpan() const { - return *std::min_element(_vector.begin(), _vector.end()); + if (this->_vector.size() == 0) + throw std::exception(); + return *std::min_element(this->_vector.begin(), this->_vector.end()); } int Span::longestSpan() const { - return *std::max_element(_vector.begin(), _vector.end()); + if (this->_vector.size() == 0) + throw std::exception(); + return *std::max_element(this->_vector.begin(), this->_vector.end()); } void Span::addNumber(const int nb) { if (this->_vector.size() == this->_size) throw std::exception(); - this->_vector[this->_size] = nb; - this->_size++; + this->_vector.push_back(nb); } \ No newline at end of file diff --git a/ex01/Span.hpp b/ex01/src/Span.hpp similarity index 100% rename from ex01/Span.hpp rename to ex01/src/Span.hpp diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..3386d8e --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,98 @@ +#include "Span.hpp" +#include + +int main() +{ + std::cout << "Test too many numbers 1: "; + { + Span span(0); + try + { + span.addNumber(1); + std::cout << "failed !" << std::endl; + } + catch(const std::exception& e) + { + std::cout << "ok" << std::endl; + } + } + std::cout << "Test too many numbers 2: "; + { + Span span(3); + span.addNumber(1); + span.addNumber(2); + span.addNumber(3); + try + { + span.addNumber(4); + std::cout << "failed !" << std::endl; + } + catch(const std::exception& e) + { + std::cout << "ok" << std::endl; + } + } + std::cout << "Test shortest empty span: "; + { + Span span(0); + try + { + span.shortestSpan(); + std::cout << "failed !" << std::endl; + } + catch(const std::exception& e) + { + std::cout << "ok" << std::endl; + } + } + std::cout << "Test shortest: "; + { + Span span(10); + span.addNumber(0); + span.addNumber(1); + span.addNumber(2); + span.addNumber(3); + span.addNumber(4); + if (span.shortestSpan() == 0) + std::cout << "ok" << std::endl; + else + std::cout << "failed !" << std::endl; + } + std::cout << "Test longest empty span: "; + { + Span span(0); + try + { + span.longestSpan(); + std::cout << "failed !" << std::endl; + } + catch(const std::exception& e) + { + std::cout << "ok" << std::endl; + } + } + std::cout << "Test longest: "; + { + Span span(10); + span.addNumber(0); + span.addNumber(1); + span.addNumber(2); + span.addNumber(3); + span.addNumber(4); + if (span.longestSpan() == 4) + std::cout << "ok" << std::endl; + else + std::cout << "failed !" << std::endl; + } + std::cout << "Test subject" << std::endl; + { + Span sp = Span(5); + sp.addNumber(6); + sp.addNumber(3); + sp.addNumber(17); + sp.addNumber(9); + sp.addNumber(11); + std::cout << sp.shortestSpan() << std::endl; + std::cout << sp.longestSpan() << std::endl; + } +} \ No newline at end of file