add not working main

This commit is contained in:
starnakin 2023-09-26 17:46:08 +02:00
parent bbb87d690d
commit 9a6ffd0e48
4 changed files with 131 additions and 4 deletions

26
ex01/Makefile Normal file
View File

@ -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

View File

@ -27,18 +27,21 @@ Span& Span::operator=(const Span& src)
int Span::shortestSpan() const 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 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) void Span::addNumber(const int nb)
{ {
if (this->_vector.size() == this->_size) if (this->_vector.size() == this->_size)
throw std::exception(); throw std::exception();
this->_vector[this->_size] = nb; this->_vector.push_back(nb);
this->_size++;
} }

98
ex01/src/main.cpp Normal file
View File

@ -0,0 +1,98 @@
#include "Span.hpp"
#include <iostream>
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;
}
}