#include "Pile.h"
#include "Err.h"
#include "Commun.h"
#include <math.h>

void Pile::Test(int k){
	int i;
	cout << "Pile::Test : ";
	for(i=0;i<k;i++) TestRand();
	for(i=0;i<k;i++) TestRandT();
	for(i=0;i<k;i++) TestMuer();
	for(i=0;i<k;i++) TestCopy();
	for(i=0;i<k;i++) TestEgale();
	for(i=0;i<k;i++) TestEmpile();
	for(i=0;i<k;i++) TestDepile();
	for(i=0;i<k;i++) TestIndex();
	for(i=0;i<k;i++) TestSupp();
	for(i=0;i<k;i++) TestTriA();
	for(i=0;i<k;i++) TestSuppDoublon();
	for(i=0;i<k;i++) TestSuppPaire();
	for(i=0;i<k;i++) TestTriAD();
	for(i=0;i<k;i++) TestTriAP();
	cout <<"OK"<<endl;
}
void Pile::TestRand(){
	const int b_= 100;
	
	int t=lrand(2,b_);
	Pile A(t); 
	A.n=t; 
	A.Rand();
	A.TriAD(); 
	EW("Pile::TestRand : il y a des doubles", A.n==t);
}
void Pile::TestRandT(){
	const int b_= 100;

	Pile A;  
	A.Rand(b_);
	int n1=A.n;
	A.TriAD(); 
	EW("Pile::TestRandT : il y a des doubles", A.n==n1);
}
void Pile::TestMuer(){
	const int b_= 100;
	Pile A;  
	A.Rand(b_);
	A[0] = 123;
	A.Muer(lrand(1,b_));
	EW("Pile::TestMuer", A[0]==123);
}
void Pile::TestCopy(){
	Pile A1,A2,B1;	
	A1.Rand(100);
	A2.Rand(100);
	B1.Rand(100);
	A1=A2;
	Pile B2(B1);
	EI("Pile::TestCopy",A1==A2 & B1==B2);
}
void Pile::TestEgale(){
	Pile A,B;
	for(int i=0;i<20;i++){
		int u=lrand();
		A.Empile(u);
		B.Empile(u);
	}
	EI("Pile::TestEgale",A==B);
}
void Pile::TestEmpile(){
	Pile A;
	A.Rand(100);
	Pile B(A);
	int  j=lrand(0,100);
	int i;
	int e;
	e=lrand();
	for(i=0;i<j;i++) A.Empile(e);
	Pile C(A);
	for(i=0;i<j;i++){	
		int u; 
		u = C.Depile();
		EI("Pile::TestEmpile",u==e);
	}
	EI("Pile::TestEmpile",C==B);
}
void Pile::TestDepile(){
	int i;
	Pile A;
	A.Rand(100);
	Pile A2=A;
	Pile B;
	int K=A.n;
	for(i=0;i<K;i++) B.Empile(A.Depile());
	Pile C;
	K=B.n;
	for(i=0;i<K;i++) C.Empile(B.Depile());
	EI("Pile::TestDepile",C==A2);
}
void Pile::TestIndex(){
	Pile A;
	A.Rand(100);
	A.Empile(rand());
	A.TriA();
	A.SuppDoublon();
	int i = lrand(0,A.n-1);
	EI("Pile::TestIndex",A.Index(A.o[i]) == i);
}
void Pile::TestSupp(){
	Pile A;
	A.Rand(100);
	A.Empile(rand());
	int K = A.n;
	for (int i=0;i<K;i++)
	{	int j = lrand(0,A.n-1);
		A.Supp(A.o[j]);
	}
	EI("Pile::TestSupp",A.n==0);
}
void Pile::TestTriA(){
	Pile M; 
	M.Rand(100);
	Pile A(M);
	A.Qsort();
	M.TriA();
	EI("Pile::TestSuppPaire",A==M);
}
void Pile::TestSuppDoublon(){
	const int n = 50;
	Pile A;
	for(int i=0;i<n;i++) A.Empile(i);
	A.Empile(12);
	A.Empile(7);
	A.Empile(21);
	A.Empile(5);
	A.Empile(21);
	A.Empile(3);
	A.Empile(7);
	int n1=A.n;
	A.TriA();
	A.SuppDoublon();
	EI("TestSuppDoublon",A.n == n1-7);
}
void Pile::TestSuppPaire(){
	const int n = 50;
	Pile A;
	for(int i=0;i<n;i++) A.Empile(i);
	A.Empile(12);
	A.Empile(7);
	A.Empile(12);
	A.Empile(5);
	A.Empile(5);
	A.Empile(12);
	A.Empile(7);
	int n1=A.n;
	A.TriA();
	A.SuppPaire();
	EI("TestSuppPaire",A.n == n1-8);
}
void Pile::TestTriAD(){
	Pile M; 
	M.Rand(100);
	Pile A(M);
	A.Qsort();
	A.SuppDoublon();
	M.TriAD();
	EI("Pile::TestTriAD",A==M);
}
void Pile::TestTriAP(){
	Pile M; 
	M.Rand(100);
	Pile A(M);
	A.Qsort();
	A.SuppPaire();
	M.TriAD();
	EI("Pile::TestTriAP",A==M);
}
