132 lines
3.5 KiB
C++
132 lines
3.5 KiB
C++
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
#include "Array.hpp"
|
|
|
|
#define MAX_VAL 750
|
|
// int main(int, char**)
|
|
// {
|
|
// Array<int> numbers(MAX_VAL);
|
|
// int* mirror = new int[MAX_VAL];
|
|
// srand(time(NULL));
|
|
// for (int i = 0; i < MAX_VAL; i++)
|
|
// {
|
|
// const int value = rand();
|
|
// numbers[i] = value;
|
|
// mirror[i] = value;
|
|
// }
|
|
// {
|
|
// 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!!" << std::endl;
|
|
// 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;
|
|
// }
|
|
|
|
int main(int, char**)
|
|
{
|
|
std::cout << "=== Test 1: Creation d'un Array ===" << std::endl;
|
|
Array<int> numbers(MAX_VAL);
|
|
std::cout << "Array cree avec " << numbers.size() << " elements" << std::endl;
|
|
|
|
int* mirror = new int[MAX_VAL];
|
|
srand(time(NULL));
|
|
|
|
std::cout << "\n=== Test 2: Remplissage de l'Array ===" << std::endl;
|
|
for (int i = 0; i < MAX_VAL; i++)
|
|
{
|
|
const int value = rand();
|
|
numbers[i] = value;
|
|
mirror[i] = value;
|
|
}
|
|
std::cout << "Array rempli avec " << MAX_VAL << " valeurs aleatoires" << std::endl;
|
|
|
|
std::cout << "\n=== Test 3: Copy Constructor et Destructeur ===" << std::endl;
|
|
{
|
|
std::cout << "Creation de copies dans un scope..." << std::endl;
|
|
Array<int> tmp = numbers;
|
|
Array<int> test(tmp);
|
|
std::cout << "Copies creees" << std::endl;
|
|
}
|
|
std::cout << "Sortie du scope, destructeurs appeles" << std::endl;
|
|
|
|
std::cout << "\n=== Test 4: Verification que les donnees n'ont pas change ===" << std::endl;
|
|
bool ok = true;
|
|
for (int i = 0; i < MAX_VAL; i++)
|
|
{
|
|
if (mirror[i] != numbers[i])
|
|
{
|
|
std::cerr << "didn't save the same value!!" << std::endl;
|
|
ok = false;
|
|
break;
|
|
}
|
|
}
|
|
if (ok)
|
|
std::cout << "OK: Les donnees sont intactes apres les copies" << std::endl;
|
|
|
|
std::cout << "\n=== Test 5: Exception - Index negatif ===" << std::endl;
|
|
try
|
|
{
|
|
std::cout << "Tentative d'acces avec index -2..." << std::endl;
|
|
numbers[-2] = 0;
|
|
}
|
|
catch(const std::exception& e)
|
|
{
|
|
std::cout << "Exception capturee: " << e.what() << std::endl;
|
|
}
|
|
|
|
std::cout << "\n=== Test 6: Exception - Index hors limites ===" << std::endl;
|
|
try
|
|
{
|
|
std::cout << "Tentative d'acces avec index " << MAX_VAL << "..." << std::endl;
|
|
numbers[MAX_VAL] = 0;
|
|
}
|
|
catch(const std::exception& e)
|
|
{
|
|
std::cout << "Exception capturee: " << e.what() << std::endl;
|
|
}
|
|
|
|
std::cout << "\n=== Test 7: Assignment Operator ===" << std::endl;
|
|
Array<int> backup(5);
|
|
std::cout << "Backup cree avec 5 elements" << std::endl;
|
|
backup = numbers;
|
|
std::cout << "Assignment: backup = numbers (nouveau size: " << backup.size() << ")" << std::endl;
|
|
for (int i = 0; i < MAX_VAL; i++)
|
|
{
|
|
numbers[i] = rand();
|
|
}
|
|
delete [] mirror;
|
|
|
|
return 0;
|
|
}
|