cpp04 add test

This commit is contained in:
Alexandre POMMIER 2022-08-03 20:01:32 +02:00
parent 69a9058170
commit 1a546db3d8
14 changed files with 129 additions and 109 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:05:20 by apommier #+# #+# */ /* Created: 2022/07/14 02:05:20 by apommier #+# #+# */
/* Updated: 2022/07/17 19:02:35 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:54:00 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/15 04:37:19 by apommier #+# #+# */ /* Created: 2022/07/15 04:37:19 by apommier #+# #+# */
/* Updated: 2022/07/18 09:17:44 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:37:29 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,9 +33,8 @@ Brain &Brain::operator=(const Brain& rhs)
std::cout << "Brain assignement operator called" << std::endl; std::cout << "Brain assignement operator called" << std::endl;
if (this != &rhs) if (this != &rhs)
{ {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++)
this->_ideas[i] = rhs._ideas[i]; this->_ideas[i] = rhs._ideas[i];
}
} }
return (*this); return (*this);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:37 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:37 by apommier #+# #+# */
/* Updated: 2022/08/03 13:20:30 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:13:07 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,12 +16,13 @@ Cat::Cat() : Animal()
{ {
std::cout << "Cat Default constructor called" << std::endl; std::cout << "Cat Default constructor called" << std::endl;
this->type = "Cat"; this->type = "Cat";
this->_Brain = new Brain; this->_Brain = new Brain();
} }
Cat::Cat(const Cat& copy) : Animal(copy) Cat::Cat(const Cat& copy) : Animal(copy)
{ {
std::cout << "Cat Copy constructor called" << std::endl; std::cout << "Cat Copy constructor called" << std::endl;
this->_Brain = new Brain();
*this = copy; *this = copy;
} }
@ -43,9 +44,9 @@ Cat &Cat::operator=(const Cat& rhs)
{ {
this->type = rhs.getType(); this->type = rhs.getType();
delete this->_Brain; delete this->_Brain;
this->_Brain = new Brain(*rhs._Brain); this->_Brain = new Brain(*rhs.getBrain());
} }
return (*this); return (*this);
} }
Animal &Cat::operator=( const Animal &rhs) Animal &Cat::operator=( const Animal &rhs)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:41 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:41 by apommier #+# #+# */
/* Updated: 2022/08/03 13:20:17 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:11:57 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,12 +16,13 @@ Dog::Dog() : Animal()
{ {
std::cout << "Dog Default constructor called" << std::endl; std::cout << "Dog Default constructor called" << std::endl;
this->type = "Dog"; this->type = "Dog";
this->_Brain = new Brain; this->_Brain = new Brain();
} }
Dog::Dog(const Dog& copy) : Animal(copy) Dog::Dog(const Dog& copy) : Animal(copy)
{ {
std::cout << "Dog Copy constructor called" << std::endl; std::cout << "Dog Copy constructor called" << std::endl;
this->_Brain = new Brain();
*this = copy; *this = copy;
} }
@ -43,7 +44,7 @@ Dog &Dog::operator=(const Dog& rhs)
{ {
this->type = rhs.getType(); this->type = rhs.getType();
delete this->_Brain; delete this->_Brain;
this->_Brain = new Brain(*rhs._Brain); this->_Brain = new Brain(*rhs.getBrain());
} }
return (*this); return (*this);
} }

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/07/14 02:04:13 by apommier #+# #+# # # Created: 2022/07/14 02:04:13 by apommier #+# #+# #
# Updated: 2022/07/22 07:48:18 by apommier ### ########.fr # # Updated: 2022/08/03 18:35:07 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -21,7 +21,7 @@ SRCS = main.cpp\
OBJS = ${SRCS:.cpp=.o} OBJS = ${SRCS:.cpp=.o}
CC = c++ CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98 CFLAGS = -Wall -Wextra -Werror -std=c++98 -g
RM = rm -rf RM = rm -rf
.cpp.o: .cpp.o:

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:04:10 by apommier #+# #+# */ /* Created: 2022/07/14 02:04:10 by apommier #+# #+# */
/* Updated: 2022/08/03 13:44:10 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:33:10 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,8 +19,18 @@
void copyTest() void copyTest()
{ {
std::cout << std::endl; std::cout << std::endl;
Dog first; std::cout << "test to know sort of copy\n";
Dog cpy = first;
Dog firstDog;
std::cout << std::endl;
Dog cpyDog = firstDog;
std::cout << &firstDog << "----" << &cpyDog <<std::endl;
std::cout << std::endl;
Cat first;
std::cout << std::endl;
Cat cpy = first;
std::cout << &first << "----" << &cpy<<std::endl;
std::cout << std::endl; std::cout << std::endl;
} }
@ -49,42 +59,35 @@ int main()
} }
std::cout << Animals[i]->getType() << std::endl; std::cout << Animals[i]->getType() << std::endl;
} }
std::cout << std::endl;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
std::cout << i + 1 << "- "<< Animals[i]->getBrain() << std::endl; std::cout << i + 1 << "- "<< Animals[i]->getBrain() << std::endl;
} }
//copyTest();
copyTest();
std::cout << "End of copy test\n\n";
std::cout << "Ideas of a cat" << std::endl;
std::cout << Animals[1]->getBrain()->getIdeas()[0] << std::endl;
std::cout << Animals[1]->getBrain()->getIdeas()[1] << std::endl;
std::cout << Animals[1]->getBrain()->getIdeas()[2] << std::endl;
std::cout << "\nIdeas of a dog" << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl;
std::cout << std::endl;
*Animals[6] = *Animals[1];
std::cout << std::endl << "Ideas of the previous dog after copy" << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl;
std::cout << std::endl;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
delete Animals[i]; delete Animals[i];
} }
// int main( void )
// {
// Animal *animals[10];
// Brain *brain;
// for (int i = 0; i < 10; i++)
// {
// if (i < 5)
// animals[i] = new Cat();
// else
// animals[i] = new Dog();
// std::cout << animals[i]->getType() << std::endl;
// }
// brain = animals[7]->getBrain();
// brain->setIdeas(0, "I'm hungry");
// brain->setIdeas(1, "That's a strange idea I'm having");
// brain->setIdeas(2, "Ball!!!!!");
// brain->setIdeas(3, "Squirrel!!!!!");
// std::cout << std::endl << animals[7]->getBrain()->getIdeas()[0] << std::endl;
// std::cout << animals[7]->getBrain()->getIdeas()[2] << std::endl;
// *(animals[5]) = *(animals[7]);
// std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl;
// std::cout << "Test of copy\n";
// animals[7]->getBrain()->setIdeas(2, "Squirrel!!!!!");
// std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl << std::endl;
// for (int i = 0; i < 10; i++)
// delete animals[i];
// }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:05:22 by apommier #+# #+# */ /* Created: 2022/07/14 02:05:22 by apommier #+# #+# */
/* Updated: 2022/07/17 18:36:09 by apommier ### ########.fr */ /* Updated: 2022/08/03 13:21:28 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ Animal::Animal()
Animal::Animal(const Animal& copy) Animal::Animal(const Animal& copy)
{ {
std::cout << "Animal Copy constructor called" << std::endl; std::cout << "Animal Copy constructor called" << std::endl;
this->type = copy.getType(); *this = copy;
} }
Animal::~Animal() Animal::~Animal()

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:05:20 by apommier #+# #+# */ /* Created: 2022/07/14 02:05:20 by apommier #+# #+# */
/* Updated: 2022/07/18 18:07:34 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:52:23 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/15 04:37:19 by apommier #+# #+# */ /* Created: 2022/07/15 04:37:19 by apommier #+# #+# */
/* Updated: 2022/07/18 09:17:44 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:37:29 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,9 +33,8 @@ Brain &Brain::operator=(const Brain& rhs)
std::cout << "Brain assignement operator called" << std::endl; std::cout << "Brain assignement operator called" << std::endl;
if (this != &rhs) if (this != &rhs)
{ {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++)
this->_ideas[i] = rhs._ideas[i]; this->_ideas[i] = rhs._ideas[i];
}
} }
return (*this); return (*this);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:37 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:37 by apommier #+# #+# */
/* Updated: 2022/07/17 19:03:24 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:13:07 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,14 +16,14 @@ Cat::Cat() : Animal()
{ {
std::cout << "Cat Default constructor called" << std::endl; std::cout << "Cat Default constructor called" << std::endl;
this->type = "Cat"; this->type = "Cat";
this->_Brain = new Brain; this->_Brain = new Brain();
} }
Cat::Cat(const Cat& copy) : Animal(copy) Cat::Cat(const Cat& copy) : Animal(copy)
{ {
std::cout << "Cat Copy constructor called" << std::endl; std::cout << "Cat Copy constructor called" << std::endl;
this->type = copy.getType(); this->_Brain = new Brain();
//this->_Brain = copy.getType(); *this = copy;
} }
Cat::~Cat() Cat::~Cat()
@ -44,9 +44,9 @@ Cat &Cat::operator=(const Cat& rhs)
{ {
this->type = rhs.getType(); this->type = rhs.getType();
delete this->_Brain; delete this->_Brain;
this->_Brain = new Brain(*rhs._Brain); this->_Brain = new Brain(*rhs.getBrain());
} }
return (*this); return (*this);
} }
Animal &Cat::operator=( const Animal &rhs) Animal &Cat::operator=( const Animal &rhs)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:39 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:39 by apommier #+# #+# */
/* Updated: 2022/07/17 18:39:59 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:50:44 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:41 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:41 by apommier #+# #+# */
/* Updated: 2022/07/17 19:03:52 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:11:57 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,13 +16,14 @@ Dog::Dog() : Animal()
{ {
std::cout << "Dog Default constructor called" << std::endl; std::cout << "Dog Default constructor called" << std::endl;
this->type = "Dog"; this->type = "Dog";
this->_Brain = new Brain; this->_Brain = new Brain();
} }
Dog::Dog(const Dog& copy) : Animal(copy) Dog::Dog(const Dog& copy) : Animal(copy)
{ {
std::cout << "Dog Copy constructor called" << std::endl; std::cout << "Dog Copy constructor called" << std::endl;
this->type = copy.getType(); this->_Brain = new Brain();
*this = copy;
} }
Dog::~Dog() Dog::~Dog()
@ -43,7 +44,7 @@ Dog &Dog::operator=(const Dog& rhs)
{ {
this->type = rhs.getType(); this->type = rhs.getType();
delete this->_Brain; delete this->_Brain;
this->_Brain = new Brain(*rhs._Brain); this->_Brain = new Brain(*rhs.getBrain());
} }
return (*this); return (*this);
} }

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/07/14 02:04:13 by apommier #+# #+# # # Created: 2022/07/14 02:04:13 by apommier #+# #+# #
# Updated: 2022/07/22 07:48:18 by apommier ### ########.fr # # Updated: 2022/08/03 18:35:07 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -21,7 +21,7 @@ SRCS = main.cpp\
OBJS = ${SRCS:.cpp=.o} OBJS = ${SRCS:.cpp=.o}
CC = c++ CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98 CFLAGS = -Wall -Wextra -Werror -std=c++98 -g
RM = rm -rf RM = rm -rf
.cpp.o: .cpp.o:

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:04:10 by apommier #+# #+# */ /* Created: 2022/07/14 02:04:10 by apommier #+# #+# */
/* Updated: 2022/07/18 20:29:04 by apommier ### ########.fr */ /* Updated: 2022/08/03 19:45:13 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,64 +16,80 @@
#include "WrongAnimal.hpp" #include "WrongAnimal.hpp"
#include "WrongCat.hpp" #include "WrongCat.hpp"
/*int main() void copyTest()
{
std::cout << std::endl;
std::cout << "test to know sort of copy\n";
Dog firstDog;
std::cout << std::endl;
Dog cpyDog = firstDog;
std::cout << &firstDog << "----" << &cpyDog <<std::endl;
std::cout << std::endl;
Cat first;
std::cout << std::endl;
Cat cpy = first;
std::cout << &first << "----" << &cpy<<std::endl;
std::cout << std::endl;
}
int main()
{ {
Animal *Animals[10]; Animal *Animals[10];
Brain *OneBrain; Brain *oneBrain;
//Animal test;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (i < 5) if (i < 5)
{ {
Animals[i] = new Cat; Animals[i] = new Cat;
Animals[i]->getBrain()->setgetIdeas(0) "Sleep"); oneBrain = Animals[i]->getBrain();
Animals[i]->getBrain()->setgetIdeas(1) "Hate"); oneBrain->setIdeas(0, "Sleep");
Animals[i]->getBrain()->setgetIdeas(2) "Sleep"); oneBrain->setIdeas(1, "Hate");
oneBrain->setIdeas(2, "Sleep");
} }
else else
{ {
Animals[i] = new Dog; Animals[i] = new Dog;
Animals[i]->getBrain()->setgetIdeas(0) "Sleep"); oneBrain = Animals[i]->getBrain();
Animals[i]->getBrain()->setgetIdeas(1) "Play"); oneBrain->setIdeas(0, "Sleep");
Animals[i]->getBrain()->setgetIdeas(2) "Ate"); oneBrain->setIdeas(1, "Play");
oneBrain->setIdeas(2, "Ate");
} }
std::cout << Animals[i]->getType() << std::endl; std::cout << Animals[i]->getType() << std::endl;
} }
std::cout << std::endl;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
std::cout << Animals[i]->getBrain() << std::endl; std::cout << i + 1 << "- "<< Animals[i]->getBrain() << std::endl;
} }
copyTest();
std::cout << "End of copy test\n\n";
std::cout << "Ideas of a cat" << std::endl;
std::cout << Animals[1]->getBrain()->getIdeas()[0] << std::endl;
std::cout << Animals[1]->getBrain()->getIdeas()[1] << std::endl;
std::cout << Animals[1]->getBrain()->getIdeas()[2] << std::endl;
std::cout << "\nIdeas of a dog" << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl;
std::cout << std::endl;
*Animals[6] = *Animals[1];
std::cout << std::endl << "Ideas of the previous dog after copy" << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl;
std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl;
std::cout << std::endl;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
delete Animals[i]; delete Animals[i];
}*/
int main( void )
{
Animal *animals[10];
Brain *brain;
for (int i = 0; i < 10; i++)
{
if (i < 5)
animals[i] = new Dog();
else
animals[i] = new Cat();
std::cout << animals[i]->getType() << std::endl;
}
brain = animals[7]->getBrain();
brain->setIdeas(0, "I'm hungry");
brain->setIdeas(1, "That's a strange idea I'm having");
brain->setIdeas(2, "Ball!!!!!");
brain->setIdeas(3, "Squirrel!!!!!");
std::cout << std::endl << animals[7]->getBrain()->getIdeas()[0] << std::endl;
std::cout << animals[7]->getBrain()->getIdeas()[2] << std::endl;
*(animals[5]) = *(animals[7]);
std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl;
std::cout << "Test of copy\n";
animals[7]->getBrain()->setIdeas(2, "Squirrel!!!!!");
std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl << std::endl;
for (int i = 0; i < 10; i++)
delete animals[i];
} }