Library 5

added integer tests
This commit is contained in:
Alessandro Ferro 2021-06-04 22:16:34 +02:00
parent f5eb9cda23
commit 81ac5d9538
6 changed files with 421 additions and 79 deletions

View File

@ -1,7 +1,7 @@
#! /bin/bash
g++ -g -o main \
g++-10 -g -o main \
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \

View File

@ -293,20 +293,12 @@ void MatrixCSR<Data>::debug(){
tmp = tmp->next;
}
std::cout << "R VECTOR:" << '\n';
std::cout << "\nR VECTOR:" << '\n';
for(ulong i=0; i<R.Size();++i){
std::cout << R[i] << '\n';
}
std::cout<<std::endl;
std::cout<<std::endl;
//// print
// for(int i=0; i<rows; ++i){
// for(int j=0; j<columns ; ++j){
// std::cout<<(*this)(i,j)<<" ";
// }
// std::cout<<std::endl;
// }
}
/* ************************************************************************** */

View File

@ -17,7 +17,7 @@ template <typename Data>
class MatrixCSR : virtual public List<std::pair<Data,ulong>>,
virtual public Matrix<Data>{ // Must extend Matrix<Data>
public: //CAMBIARE A PROTETTO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
protected:
using Matrix<Data>::rows;
using Matrix<Data>::columns;
@ -35,47 +35,23 @@ public: //CAMBIARE A PROTETTO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
| next
*/
//public:
public:
// Default constructor
MatrixCSR();
/* ************************************************************************ */
// Specific constructors
MatrixCSR(const ulong, const ulong); // A matrix of some specified dimension
/* ************************************************************************ */
// Copy constructor
MatrixCSR(const MatrixCSR&);
// Move constructor
MatrixCSR(MatrixCSR&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~MatrixCSR();
/* ************************************************************************ */
// Copy assignment
MatrixCSR& operator=(const MatrixCSR<Data>&);
// Move assignment
MatrixCSR& operator=(MatrixCSR<Data>&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const MatrixCSR&) const noexcept;
bool operator!=(const MatrixCSR&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from Matrix)
void RowResize(const ulong&) override; // Override Matrix member
void ColumnResize(const ulong&) override; // Override Matrix member
@ -84,10 +60,6 @@ public: //CAMBIARE A PROTETTO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Data& operator()(const ulong, const ulong) override; // Override Matrix member (mutable access to the element; throw out_of_range when out of range)
const Data& operator()(const ulong, const ulong) const override; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present)
/* ************************************************************************ */
// Specific member functions (inherited from Container)
void Clear() override; // Override Container member
/* ************************************************************************ */
@ -96,11 +68,6 @@ public: //CAMBIARE A PROTETTO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
virtual void MapPreOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override MappableContainer member
virtual void MapPostOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
virtual void FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
virtual void FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member

View File

@ -19,14 +19,14 @@ using namespace std;
void lasdtest() {
cout << endl << "~*~#~*~ Welcome to the LASD Test Suite ~*~#~*~ " << endl;
// testSimpleExercise1();
// testFullExercise1();
// testSimpleExercise2();
// testFullExercise2();
// testSimpleExercise3();
// testFullExercise3();
// testSimpleExercise4();
// testFullExercise4();
testSimpleExercise1();
testFullExercise1();
testSimpleExercise2();
testFullExercise2();
testSimpleExercise3();
testFullExercise3();
testSimpleExercise4();
testFullExercise4();
testSimpleExercise5();
testFullExercise5();
cout << endl << "Goodbye!" << endl;

View File

@ -1,28 +1,13 @@
#include "test.hpp"
#include"../matrix/matrix.hpp"
#include"../matrix/csr/matrixcsr.hpp"
#include"../matrix/vec/matrixvec.hpp"
#include "../zlasdtest/test.hpp"
#include <random>
#include <iostream>
using namespace std;
using namespace lasd;
void ciao (MatrixCSR<long>& a, MatrixCSR<long>& b)
{
std::cout << "A" << '\n';
MatrixCSR<long> c(std::move(a));
std::cout << "B" << '\n';
a=std::move(b);
a.debug();
std::cout << "C" << '\n';
b=std::move(c);
}
void menu(){
DataType chosenDataType;
Implementation chosenImplementation;
unsigned short int choice;
do{
@ -35,7 +20,364 @@ void menu(){
}while(choice!=1 && choice!=2);
if(choice==1){
lasdtest();
}else{
}else if(choice==2){
chosenImplementation = ChooseImplementation();
chosenDataType = ChooseDataType();
UseChosenType(chosenImplementation, chosenDataType);
}
}
DataType ChooseDataType(){
unsigned short int choice;
do{
std::cout<<"\nChoose a data type:"<<std::endl;
std::cout<<" 1. Integer"<<std::endl;
std::cout<<" 2. Float"<<std::endl;
std::cout<<" 3. String"<<std::endl;
cout<<endl<<" -> ";
std::cin>>std::ws;
std::cin>>choice;
}while(!(choice>0 && choice<4));
if(choice==1)
return DataType::integer;
else if(choice==2)
return DataType::ffloat;
else if(choice==3)
return DataType::sstring;
}
Implementation ChooseImplementation(){
unsigned short int choice;
do{
std::cout<<"\nChoose an implementation for the binary tree:"<<std::endl;
std::cout<<" 1. Vector"<<std::endl;
std::cout<<" 2. YALE format (Compressed Sparse Row) "<<std::endl;
cout<<endl<<" -> ";
std::cin>>std::ws;
std::cin>>choice;
}while(!(choice>0 && choice<3));
if(choice==1)
return Implementation::vector;
else if(choice==2)
return Implementation::yale;
}
void UseChosenType(Implementation chosenImplementation, DataType chosenDataType){
if(chosenImplementation == Implementation::vector){
if(chosenDataType == DataType::integer){
MatrixVec<int> mtx;
mtx = GenerateIntegerMat(mtx);
IntegerFunctions(mtx);
}else if(chosenDataType == DataType::ffloat){
MatrixVec<float> mtx;
mtx = GenerateFloatMat(mtx);
//FloatFunctions(mtx);
}else if(chosenDataType == DataType::sstring){
MatrixVec<string> mtx;
mtx = GenerateStringsMat(mtx);
//StringFunctions(mtx);
}
}else if(chosenImplementation == Implementation::yale){
if(chosenDataType == DataType::integer){
MatrixCSR<int> mtx;
mtx = GenerateIntegerMat(mtx);
IntegerFunctions(mtx);
}else if(chosenDataType == DataType::ffloat){
MatrixCSR<float> mtx;
mtx = GenerateFloatMat(mtx);
// FloatFunctions(mtx);
}else if(chosenDataType == DataType::sstring){
MatrixCSR<string> mtx;
mtx = GenerateStringsMat(mtx);
//StringFunctions(mtx);
}
}
}
template <typename T>
void IntegerFunctions(T& mtx){
unsigned short int choice;
do{
std::cout<<std::endl<<std::endl;
std::cout<<"Choose one of the following options:"<<std::endl;
std::cout<<" 1. Print elements"<<std::endl;
std::cout<<" 2. Check existence of an element "<<std::endl;
std::cout<<" 3. Product of integers less than 'n'"<<std::endl;
std::cout<<" 4. Double all elements"<<std::endl;
std::cout<<" 5. Insert in a position"<<std::endl;
std::cout<<" 6. Read from a position"<<std::endl;
std::cout<<" 7. Resize"<<std::endl;
std::cout<<" 8. Go back"<<std::endl;
std::cout<<" 9. Quit"<<std::endl;
cout<<endl<<" -> ";
std::cin>>std::ws;
std::cin>>choice;
std::cout<<std::endl;
switch(choice){
case 1:
PrintElements(mtx);
break;
case 2:
CheckExistence(mtx);
break;
case 3:
ProductsElementsLessThan(mtx);
break;
case 4:
Double(mtx);
break;
case 5:
Insert(mtx);
break;
case 6:
Read(mtx);
break;
case 7:
Resize(mtx);
break;
case 8:
menu();
break;
}
}while(choice!=8 && choice!=9);
}
template <typename T>
void Resize(T& mtx){
ulong rows, columns;
cout<<" The current dimension of the matrix is " << mtx.RowNumber() << " x " << mtx.ColumnNumber() << endl;
cout<<" New dimension: \n";
cout<<" Rows: ";
cin>>rows;
cout<<" Columns: ";
cin>>columns;
mtx.RowResize(rows);
mtx.ColumnResize(columns);
cout<<" Done.";
}
template <typename T>
void Read(const T& mtx){
ulong row,column;
cout<<" Insert the position of the element to read: \n";
cout<<" Row: ";
cin>>row;
cout<<" Column: ";
cin>>column;
try{
cout<<" The element in this position is: "<<mtx(row,column);
}catch(out_of_range exc){
cout<<" "<<exc.what();
}catch(length_error exc2){
cout<<" "<<exc2.what();
}
}
template <typename T>
void Insert(T& mtx){
ulong row,column;
cout<<" Insert the position of the element to insert: \n";
cout<<" Row: ";
cin>>row;
cout<<" Column: ";
cin>>column;
cout<<" Insert the element: ";
try{
cin>>mtx(row,column);
}catch(out_of_range exc){
cout<<exc.what();
}
}
template <typename T>
void Double(T& mtx){
void (*fun)(int&, void*) = MultiplyAnElement;
int par = 2;
mtx.MapPreOrder(fun,(void*)&par);
cout<<"Done.\n";
}
void MultiplyAnElement(int& data, void* par){
data *= *(int*)par;
}
template <typename T>
void ProductsElementsLessThan(T& mtx){
int n, acc=1;
void (*func)(const int&, const void*, void*) = AccumulateProduct;
cout<<"Multipy all elements of the matrix whose value is less than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&acc);
cout<<"\nThe result is "<<acc<<endl<<endl;
}
void AccumulateProduct(const int& data, const void* par, void* acc){
if(data < (*(int*)par)){
*(int*)acc *= data;
}
}
template <template <typename...> class Matrix, typename Data>
void CheckExistence(Matrix<Data>& mtx){
Data elementToLookFor;
cout<<"\n\nCheck existence in the matrix of: ";
cin>>ws;
cin>>elementToLookFor;
cout<<"The element " << ( (mtx.FoldableContainer<Data>::Exists(elementToLookFor))? "does" : "does not") << " exist\n\n";
}
template <template <typename...> class Matrix, typename Data>
void PrintElements(Matrix<Data>& mtx){
void (*PrinterFunction) (Data&, void*) = PrintSingleElement;
cout<<"Pre order:\n";
mtx.MapPreOrder(PrinterFunction, nullptr);
cout<<endl<<endl;
cout<<"Post order:\n";
mtx.MapPostOrder(PrinterFunction, nullptr);
cout<<endl<<endl;
cout<<"Standard matrix-fashion (" << mtx.RowNumber() << " x " << mtx.ColumnNumber() << "):\n";
for(ulong i=0 ; i<mtx.RowNumber() ; ++i){
for(ulong j=0 ; j<mtx.ColumnNumber() ; ++j){
cout<<"\t";
if(mtx.ExistsCell(i,j))
cout<<mtx(i,j);
else
cout<<"-";
}
cout<<endl;
}
}
template <typename Data>
void PrintSingleElement(Data& data, void* _){
std::cout << data << " ";
}
template <typename T>
void Print(T& mat){
cout<<endl<<endl;
cout<<"Rows: "<<mat.RowNumber()<<endl<<"Columns: "<<mat.ColumnNumber()<<endl<<"Size: "<<mat.Size()<<endl<<endl;
for(ulong i=0; i<mat.RowNumber() ; ++i){
for(ulong j=0 ; j<mat.ColumnNumber() ; ++j){
cout<<"\t"<<mat(i,j);
}
cout<<endl;
}
}
/* ----- generator functions ----- */
template <typename T>
T GenerateIntegerMat(T& mat){
default_random_engine gen(random_device{}());
uniform_int_distribution<int> random_dimension(1,20); // generates the dimensions of the matrix
uniform_int_distribution<int> dist(0,100); // generates values inside the matrix
ulong n_columns = random_dimension(gen);
ulong n_rows = random_dimension(gen);
T matrix(n_rows, n_columns);
uniform_int_distribution<int> n_elements_to_insert(0,(n_columns*n_rows)); // generates how many elements to insert
ulong elements_to_insert = n_elements_to_insert(gen);
ulong row,column;
for(ulong i=1 ; i<=elements_to_insert ; ++i){
do{
row = random_dimension(gen)-1;
}while(row >= n_rows);
do{
column = random_dimension(gen)-1;
}while(column >= n_columns);
matrix(row,column) = dist(gen);
}
return matrix;
}
template <typename T>
T GenerateFloatMat(T& mat){
default_random_engine gen(random_device{}());
uniform_int_distribution<int> random_dimension(1,20);
uniform_real_distribution<double> dist(0,9);
ulong n_columns = random_dimension(gen);
ulong n_rows = random_dimension(gen);
T matrix(n_rows, n_columns);
uniform_int_distribution<int> n_elements_to_insert(0,(n_columns*n_rows));
ulong elements_to_insert = n_elements_to_insert(gen);
ulong row,column;
for(ulong i=1 ; i<=elements_to_insert ; ++i){
do{
row = random_dimension(gen)-1;
}while(row >= n_rows);
do{
column = random_dimension(gen)-1;
}while(column >= n_columns);
matrix(row,column) = dist(gen);
}
return matrix;
}
template <typename T>
T GenerateStringsMat(T& mat){
default_random_engine gen(random_device{}());
uniform_int_distribution<int> random_dimension(1,20);
uniform_int_distribution<int> dist(1,5);
ulong n_columns = random_dimension(gen);
ulong n_rows = random_dimension(gen);
T matrix(n_rows, n_columns);
uniform_int_distribution<int> n_elements_to_insert(0,(n_columns*n_rows));
ulong elements_to_insert = n_elements_to_insert(gen);
ulong row,column;
for(ulong i=1 ; i<=elements_to_insert ; ++i){
do{
row = random_dimension(gen)-1;
}while(row >= n_rows);
do{
column = random_dimension(gen)-1;
}while(column >= n_columns);
matrix(row,column) = generateRandomString(dist(gen));
}
return matrix;
}
string generateRandomString(ulong dim){
default_random_engine gen(random_device{}());
uniform_int_distribution<char> character('a','z');
char newString[dim+1];
for(int i=0;i<dim;i++){
newString[i]=character(gen);
}
newString[dim]='\0';
return newString;
}

View File

@ -2,9 +2,50 @@
#ifndef MYTEST_HPP
#define MYTEST_HPP
/* ************************************************************************** */
#include"../matrix/matrix.hpp"
#include"../matrix/csr/matrixcsr.hpp"
#include"../matrix/vec/matrixvec.hpp"
#include "../zlasdtest/test.hpp"
using namespace std;
using namespace lasd;
enum class DataType{integer,ffloat,sstring};
enum class Implementation{vector,yale};
void menu();
/* ************************************************************************** */
template <typename T>
void Print(T&);
template <typename T>
void IntegerFunctions(T&);
template <template <typename...> class Matrix, typename Data>
void PrintElements(Matrix<Data>& mtx);
template <typename Data>
void PrintSingleElement(Data&, void*);
template <template <typename...> class Matrix, typename Data>
void CheckExistence(Matrix<Data>&);
template <typename T>
void ProductsElementsLessThan(T&);
void AccumulateProduct(const int&, const void*, void*);
template <typename T>
void Double(T&);
void MultiplyAnElement(int&, void*);
template <typename T>
void Insert(T&);
template <typename T>
void Read(const T&);
template <typename T>
void Resize(T& mtx);
/* ----- generator functions ----- */
DataType ChooseDataType(); //choose data type
Implementation ChooseImplementation();
void UseChosenType(Implementation, DataType);
template <typename T>
T GenerateIntegerMat(T&);
template <typename T>
T GenerateFloatMat(T&);
template <typename T>
T GenerateStringsMat(T&);
std::string generateRandomString(ulong);
#endif