d07 ex02 a peu pres ok

This commit is contained in:
hugogogo
2022-03-11 01:54:16 +01:00
parent a38d7184e3
commit 9197577889
10 changed files with 379 additions and 1 deletions

View File

@@ -2,7 +2,7 @@
# define ITER_HPP
template< typename T, typename F >
void Iter(T *arr, size_t len, F & f)
void Iter(T &arr, size_t len, F & f)
{
for (size_t i = 0; i < len; i++)
f(arr[i]);

BIN
d07/ex01/iter Executable file

Binary file not shown.

82
d07/ex02/Makefile Normal file
View File

@@ -0,0 +1,82 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value \ . += append to a variable #
# VARIABLES . value . != set result of command #
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = array
#TYPE = c
TYPE = cpp
ifeq "$(TYPE)" "c"
CC = c
EXT = c
else ifeq "$(TYPE)" "cpp"
CC = c++
EXT = cpp
endif
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g
ifeq "$(TYPE)" "cpp"
CFLAGS += -std=c++98
endif
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = .
SRCS = main.cpp
D_HEADERS = headers
HEADERS = colors.h \
Array.hpp \
Iter.hpp \
Print.hpp \
Fill.hpp
D_OBJS = builds
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
ifeq "$(D_OBJS)" "."
RM_OBJS = rm -f $(OBJS)
else
RM_OBJS = rm -rf $(D_OBJS)
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
leaks: $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
clean:
$(RM_OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY : all clean fclean re

BIN
d07/ex02/array Executable file

Binary file not shown.

View File

@@ -0,0 +1,50 @@
#ifndef ARRAY_HPP
# define ARRAY_HPP
#include <iostream>
template < typename T >
class Array {
public:
Array(unsigned int n = 0u)
: _size(n)
, _arr(new T[n]) {};
~Array() { delete [] _arr; };
Array(Array const & src) : _arr(new T[0]) { *this = src; };
Array &operator=(Array const & rhs) {
if (this == &rhs)
return (*this);
delete[] _arr;
_arr = new T[rhs._size];
_size = rhs._size;
for (unsigned int it = 0; it < rhs._size && it < _size; it++)
_arr[it] = rhs._arr[it];
return (*this);
};
// seen on luke's code :
// https://isocpp.org/wiki/faq/const-correctness#const-overloading
T & operator[](size_t i) {
if (i < 0 || i >= _size)
throw ArrayOutOfRangeException();
return (_arr[i]);
};
const T & operator[](size_t i) const {
if (i < 0 || i >= _size)
throw ArrayOutOfRangeException();
return (_arr[i]);
};
size_t size() const { return (_size); };
private:
class ArrayOutOfRangeException : public std::exception {
const char *what() const throw() {
return ("out of range array index");};
};
size_t _size;
T * _arr;
};
#endif

15
d07/ex02/headers/Fill.hpp Normal file
View File

@@ -0,0 +1,15 @@
#ifndef FILL_HPP
# define FILL_HPP
# include <iostream>
# include <string>
# include "Iter.hpp"
template< typename T, typename U >
void Fill(T & arr, size_t len, U const & e) {
for (size_t i = 0; i < len; i++)
arr[i] = e;
}
#endif

11
d07/ex02/headers/Iter.hpp Normal file
View File

@@ -0,0 +1,11 @@
#ifndef ITER_HPP
# define ITER_HPP
template< typename T, typename F >
void Iter(T & arr, size_t len, F & f)
{
for (size_t i = 0; i < len; i++)
f(arr[i]);
}
#endif

View File

@@ -0,0 +1,12 @@
#ifndef PRINT_HPP
# define PRINT_HPP
# include <iostream>
# include <string>
template< typename T >
void Print(T const & e) {
std::cout << "[" << e << "]";
}
#endif

25
d07/ex02/headers/colors.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef COLORS_H
# define COLORS_H
# define GRAY "\e[0;30m"
# define RED "\e[0;31m"
# define GREEN "\e[0;32m"
# define YELLOW "\e[0;33m"
# define BLUE "\e[0;34m"
# define PURPLE "\e[0;35m"
# define CYAN "\e[0;36m"
# define WHITE "\e[0;37m"
# define B_GRAY "\e[1;30m"
# define B_RED "\e[1;31m"
# define B_GREEN "\e[1;32m"
# define B_YELLOW "\e[1;33m"
# define B_BLUE "\e[1;34m"
# define B_PURPLE "\e[1;35m"
# define B_CYAN "\e[1;36m"
# define B_WHITE "\e[1;37m"
# define RESET "\e[0m"
#endif

183
d07/ex02/main.cpp Normal file
View File

@@ -0,0 +1,183 @@
#include <iostream>
#include <string>
#include <cstdlib> // rand()
#include "colors.h"
#include "Iter.hpp"
#include "Print.hpp"
#include "Array.hpp"
#include "Fill.hpp"
#define N_TEST "7"
int main() {
srand(time(NULL));
int i = 0;
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests basic copy :" RESET "\n";
{
Array<int> a(13);
Array<int> b(a);
::Fill(a, a.size(), 0);
::Fill(b, b.size(), 0);
std::cout << "a: ";
::Iter(a, a.size(), Print<int>);
std::cout << "\n";
std::cout << "b: ";
::Iter(b, b.size(), Print<int>);
std::cout << "\n";
::Fill(a, a.size(), 2);
std::cout << B_BLUE "\na filled with 2" RESET "\n";
std::cout << "a: ";
::Iter(a, a.size(), Print<int>);
std::cout << "\n";
std::cout << "b: ";
::Iter(b, b.size(), Print<int>);
std::cout << "\n";
b = a;
::Fill(a, a.size(), 3);
std::cout << B_BLUE "\nb = a\na filled with 3" RESET "\n";
std::cout << "a: ";
::Iter(a, a.size(), Print<int>);
std::cout << "\n";
std::cout << "b: ";
::Iter(b, b.size(), Print<int>);
std::cout << "\n";
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests copy with different size:" RESET "\n";
{
Array<int> a(17);
Array<int> b(13);
::Fill(a, a.size(), 2);
::Fill(b, b.size(), 3);
std::cout << B_BLUE "\na filled with 2" RESET "\n";
std::cout << B_BLUE "\nb filled with 3" RESET "\n";
std::cout << "a: ";
::Iter(a, a.size(), Print<int>);
std::cout << "\nb: ";
::Iter(b, b.size(), Print<int>);
std::cout << "\n";
b = a;
std::cout << B_BLUE "\nb = a" RESET "\n";
std::cout << "a: ";
::Iter(a, a.size(), Print<int>);
std::cout << "\nb: ";
::Iter(b, b.size(), Print<int>);
std::cout << "\n";
::Fill(b, b.size(), 4);
std::cout << B_BLUE "\nb filled with 4" RESET "\n";
std::cout << "a: ";
::Iter(a, a.size(), Print<int>);
std::cout << "\nb: ";
::Iter(b, b.size(), Print<int>);
std::cout << "\n";
}
// tests luke
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests :" RESET "\n";
{
Array<int> a(10);
std::cout << "a.size :" << a.size() << "\n";
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests index correct :" RESET "\n";
{
unsigned int l = 162;
Array<int> arr(l);
::Fill(arr, arr.size(), 0);
try {
arr[42] = 42; }
catch(std::exception& e) {
std::cerr << e.what() << '\n'; }
std::cout << "print arr: ";
::Iter(arr, l, Print<int>);
std::cout << "\n";
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests index < 0 :" RESET "\n";
{
Array<int> arr(162);
try {
arr[-42] = 42; }
catch(std::exception& e) {
std::cerr << e.what() << '\n'; }
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests index >= array.size :" RESET "\n";
{
Array<int> arr(59);
try {
arr[arr.size()] = 42; }
catch(std::exception& e) {
std::cerr << e.what() << '\n'; }
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests :" RESET "\n";
{
#define MAX_VAL 750
Array<int> numbers(MAX_VAL);
int* mirror = new int[MAX_VAL];
for (int i = 0; i < MAX_VAL; i++) {
const int value = rand();
numbers[i] = value;
mirror[i] = value;
}
//SCOPE
{
Array<int> tmp = numbers;
Array<int> test(tmp);
}
for (int i = 0; i < MAX_VAL; i++) {
if (mirror[i] != numbers[i]) {
std::cerr << "didn't save the same value!!" << '\n';
return 1;
}
}
try {
numbers[-2] = 0;}
catch(const std::exception& e) {
std::cerr << e.what() << '\n';}
try {
numbers[MAX_VAL] = 0;}
catch(const std::exception& e) {
std::cerr << e.what() << '\n';}
for (int i = 0; i < MAX_VAL; i++) {
numbers[i] = rand();
}
delete [] mirror;
}
return 0;
}