#include "gtest/gtest.h"
#include "pimathmatrix.h"

bool cmpSquareMatrixWithValue(PIMathMatrix<double> matrix, double val, int num) {
	bool b = true;
	for(int i = 0; i < num; i++) {
		for(int j = 0; j < num; j++) {
			if(matrix.element(i, j) != val) {
				b = false;
			}
		}
	}
	return b;
}

TEST(PIMathMatrix_Test, identity) {
	auto matrix = PIMathMatrix<double>::identity(3, 3);
	for(int i = 0; i < 3; i++) {
		for(int j = 0; j < 3; j++) {
			if(i != j) {
				if(matrix[i][j] != 0.0){
					ASSERT_TRUE(false);
				}
			}
			else {
				if(matrix[i][i] != 1.0){
					ASSERT_TRUE(false);
				}
			}
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrixT_Test, element) {
	auto matrix = PIMathMatrix<double>::identity(3, 3);
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if(i != j){
				if(matrix[i][j] != 0.0){
					ASSERT_TRUE(false);
				}
			}
			else {
				if(matrix.element(i,i) != 1.0) {
					ASSERT_TRUE(false);
				}
			}
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, matrixRow) {
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	auto matrix = PIMathMatrix<double>::matrixRow(vector);
	for(uint i = 0; i < vector.size(); i++) {
		if(matrix[0][i] != 3.0) {
			ASSERT_TRUE(false);
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, matrixCol) {
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	auto matrix = PIMathMatrix<double>::matrixCol(vector);
	for(uint i = 0; i < vector.size(); i++) {
		if(matrix[i][0] != 3.0) {
			ASSERT_TRUE(false);
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, setCol) {
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	auto matrix = PIMathMatrix<double>::matrixCol(vector);
	vector.fill(10.0);
	matrix.setCol(0, vector);
	for(uint i = 0; i < vector.size(); i++) {
		if(matrix[i][0] != 10.0) {
			ASSERT_TRUE(false);
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, setRow) {
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	auto matrix = PIMathMatrix<double>::matrixRow(vector);
	vector.fill(10.0);
	matrix.setRow(0, vector);
	for(uint i = 0; i < vector.size(); i++) {
		if(matrix[0][i] != 10.0) {
			ASSERT_TRUE(false);
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, swapCols) {
	PIMathMatrix<double> origMatr;
	PIMathMatrix<double> matrix1;
	PIMathVector<double> vector;
	uint i1 = 0; uint i2 = 1;
	double a1[3], a2[3], a3[3];
	double b1[3], b2[3], b3[3];
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix1 = origMatr.identity(3, 3);
	matrix1.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix1.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix1.setCol(2, vector);
	for(int i = 0; i < 3; i++) {
		a1[i] = matrix1.element(i, 0);
		a2[i] = matrix1.element(i, 1);
		a3[i] = matrix1.element(i, 2);
	}
	matrix1.swapCols(i1, i2);
	for(int i = 0; i < 3; i++) {
		b1[i] = matrix1.element(i, 0);
		b2[i] = matrix1.element(i, 1);
		b3[i] = matrix1.element(i, 2);
	}
	ASSERT_TRUE((memcmp(a1, b2, sizeof(b1)) == 0) && (memcmp(a2, b1, sizeof(b1)) == 0) && (memcmp(a3, b3, sizeof(b1)) == 0));
}

TEST(PIMathMatrix_Test, swapRows) {
	PIMathMatrix<double> origMatr;
	PIMathMatrix<double> matrix1;
	PIMathVector<double> vector;
	uint i1 = 0; uint i2 = 1;
	double a1[3], a2[3], a3[3];
	double b1[3], b2[3], b3[3];
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix1 = origMatr.identity(3, 3);
	matrix1.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix1.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix1.setCol(2, vector);
	for(int i = 0; i < 3; i++) {
		a1[i] = matrix1.element(0, i);
		a2[i] = matrix1.element(1, i);
		a3[i] = matrix1.element(2, i);
	}
	matrix1.swapRows(i1, i2);
	for(int i = 0; i < 3; i++) {
		b1[i] = matrix1.element(0, i);
		b2[i] = matrix1.element(1, i);
		b3[i] = matrix1.element(2, i);
	}
	ASSERT_TRUE((memcmp(a1, b2, sizeof(b1)) == 0) && (memcmp(a2, b1, sizeof(b1)) == 0) && (memcmp(a3, b3, sizeof(b1)) == 0));
}
TEST(PIMathMatrix_Test, fill) {
	PIMathMatrix<double> matrix(3, 3, 5.0);
	matrix.fill(7.0);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 7.0, 3));
}

TEST(PIMathMatrix_Test, isSquareTrue) {
	PIMathMatrix<double> matrix(3, 3, 1.0);
	ASSERT_TRUE(matrix.isSquare());
}

TEST(PIMathMatrix_Test, isSquareFalse) {
	PIMathMatrix<double> matrix(2, 4, 1.0);
	ASSERT_FALSE(matrix.isSquare());
}

TEST(PIMathMatrix_Test, isIdentityTrue) {
	auto matrix = PIMathMatrix<double>::identity(3, 3);
	ASSERT_TRUE(matrix.isIdentity());
}

TEST(PIMathMatrix_Test, isIdentityFalse) {
	PIMathMatrix<double> matrix(3, 3, 5.0);
	ASSERT_FALSE(matrix.isIdentity());
}


TEST(PIMathMatrix_Test, isNullTrue) {
	PIMathMatrix<double> matrix(3, 3, 0.0);
	ASSERT_TRUE(matrix.isNull());
}

TEST(PIMathMatrix_Test, isNullFalse) {
	PIMathMatrix<double> matrix(3, 3, 5.0);
	ASSERT_FALSE(matrix.isNull());
}

TEST(PIMathMatrix_Test, isValidTrue) {
	PIMathMatrix<double> matrix(3, 3, 1.62);
	ASSERT_TRUE(matrix.isValid());
}

TEST(PIMathMatrix_Test, isValidFalse) {
	PIMathMatrix<double> matrix;
	ASSERT_FALSE(matrix.isValid());
}

TEST(PIMathMatrix_Test, operator_Assignment) {
	PIMathMatrix<double> matrix1(3, 3, 5.72);
	PIMathMatrix<double> matrix2(3, 3, 7.12);
	matrix1 = matrix2;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 7.12, 3));
}

TEST(PIMathMatrix_Test, operator_EqualTrue) {
	PIMathMatrix<double> matrix1(2, 2, 2.0);
	PIMathMatrix<double> matrix2(2, 2, 2.0);
	matrix1.element(0, 0) = 5.1;
	matrix1.element(0, 1) = 1.21;
	matrix1.element(1, 1) = 0.671;
	matrix1.element(1, 0) = 2.623;
	matrix2.element(0, 0) = 5.1;
	matrix2.element(0, 1) = 1.21;
	matrix2.element(1, 1) = 0.671;
	matrix2.element(1, 0) = 2.623;
	ASSERT_TRUE(matrix1 == matrix2);
}

TEST(PIMathMatrix_Test, operator_EqualFalse) {
	PIMathMatrix<double> matrix1(2, 2, 2.0);
	PIMathMatrix<double> matrix2(2, 2, 2.0);
	matrix1.element(0, 0) = 5.1;
	matrix1.element(0, 1) = 1.21;
	matrix1.element(1, 1) = 0.671;
	matrix1.element(1, 0) = 2.623;
	matrix2.element(0, 0) = 5.1;
	matrix2.element(0, 1) = 1.21;
	matrix2.element(1, 1) = 665.671;
	matrix2.element(1, 0) = 2.623;
	ASSERT_FALSE(matrix1 == matrix2);
}

TEST(PIMathMatrix_Test, operator_Not_EqualTrue) {
	PIMathMatrix<double> matrix1(2, 2, 2.0);
	PIMathMatrix<double> matrix2(2, 2, 2.0);
	matrix1.element(0, 0) = 5.1;
	matrix1.element(0, 1) = 1.21;
	matrix1.element(1, 1) = 0.671;
	matrix1.element(1, 0) = 2.623;
	matrix2.element(0, 0) = 5.1;
	matrix2.element(0, 1) = 1.21;
	matrix2.element(1, 1) = 665.671;
	matrix2.element(1, 0) = 2.623;
	ASSERT_TRUE(matrix1 != matrix2);
}

TEST(PIMathMatrix_Test, operator_Not_EqualFalse) {
	PIMathMatrix<double> matrix1(2, 2, 2.0);
	PIMathMatrix<double> matrix2(2, 2, 2.0);
	matrix1.element(0, 0) = 5.1;
	matrix1.element(0, 1) = 1.21;
	matrix1.element(1, 1) = 0.671;
	matrix1.element(1, 0) = 2.623;
	matrix2.element(0, 0) = 5.1;
	matrix2.element(0, 1) = 1.21;
	matrix2.element(1, 1) = 0.671;
	matrix2.element(1, 0) = 2.623;
	ASSERT_FALSE(matrix1 != matrix2);
}

TEST(PIMathMatrix_Test, operator_Addition_Aassignment) {
	PIMathMatrix<double> matrix1(3, 3, 6.72);
	PIMathMatrix<double> matrix2(3, 3, 1.0);
	matrix1 += matrix2;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 7.72, 3));
}

TEST(PIMathMatrix_Test, operator_Subtraction_Assignment) {
	PIMathMatrix<double> matrix1(3, 3, 1.0);
	PIMathMatrix<double> matrix2(3, 3, 6.72);
	matrix1 -= matrix2;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, -5.72, 3));
}

TEST(PIMathMatrix_Test, operator_Multiplication_Assignment) {
	PIMathMatrix<double> matrix1(3, 3, 6.72);
	matrix1 *= 2.0;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 13.44, 3));
}

TEST(PIMathMatrix_Test, operator_Division_Assignment) {
	PIMathMatrix<double> matrix1(3, 3, 6.72);
	matrix1 /= 2.0;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 3.36, 3));
}

TEST(PIMathMatrix_Test, operator_Addition) {
	PIMathMatrix<double> matrix1(3, 3, 6.72);
	PIMathMatrix<double> matrix2(3, 3, 8.28);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 + matrix2, 15.0, 3));
}

TEST(PIMathMatrix_Test, operator_Subtraction) {
	PIMathMatrix<double> matrix1(3, 3, 6.0);
	PIMathMatrix<double> matrix2(3, 3, 5.0);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 - matrix2, 1.0, 3));
}

TEST(PIMathMatrix_Test, operator_Multiplication) {
	PIMathMatrix<double> matrix1(3, 3, 6.72);
	PIMathMatrix<double> matrix2(3, 3, 5.0);
	matrix2 = matrix1*4.0;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix2, 26.88, 3));
}
TEST(PIMathMatrix_Test, operator_Division) {
	PIMathMatrix<double> matrix1(3, 3, 6.72);
	PIMathMatrix<double> matrix2(3, 3, 5.0);
	matrix2 = matrix1/4.0;
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix2, 1.68, 3));
}

TEST(PIMathMatrix_Test, determinantIfSquare) {
	double d;
	double i = 59.0;
	PIMathMatrix<double> matrix(3, 3, 0.0);
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix.setCol(2, vector);
	d = matrix.determinant();
	ASSERT_DOUBLE_EQ(d, i);
}

TEST(PIMathMatrix_Test, trace) {
	PIMathMatrix<double> matrix(3, 3, 0.0);
	double t;
	double i = 9.0;
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix.setCol(2, vector);
	t = matrix.trace();
	ASSERT_DOUBLE_EQ(t, i);
}

TEST(PIMathMatrix_Test, toUpperTriangular) {
	PIMathMatrix<double> matrix(3, 3, 0.0);
	double d1, d2 = 1;
	int i;
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix.setCol(2, vector);
	d1 = matrix.determinant();
	matrix.toUpperTriangular();
	for(i = 0; i < 3; i++)
	{
		d2 = d2 * matrix.element(i, i);
	}
	ASSERT_DOUBLE_EQ(d1, d2);
}

TEST(PIMathMatrix_Test, invert) {
	double d1, d2;
	PIMathMatrix<double> matrix1(3, 3, 0.0);
	PIMathMatrix<double> matrix2(3, 3, 0.0);
	PIMathMatrix<double> matrix3(3, 3, 0.0);
	PIMathMatrix<double> matrix4(3, 3, 0.0);
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix1.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix1.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix1.setCol(2, vector);
	d1 = matrix1.determinant();
	matrix2 = matrix1;
	matrix2.invert();
	d2 = matrix2.determinant();
	matrix4.invert();
	ASSERT_TRUE((matrix3 == matrix4) && (round((1/d1)*10000)/10000 == round(d2*10000)/10000));
}

TEST(PIMathMatrix_Test, inverted) {
	double d1, d2;
	PIMathMatrix<double> matrix1(3, 3, 0.0);
	PIMathMatrix<double> matrix2(3, 3, 0.0);
	PIMathMatrix<double> matrix3(3, 3, 0.0);
	PIMathMatrix<double> matrix4(3, 3, 0.0);
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix1.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix1.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix1.setCol(2, vector);
	d1 = matrix1.determinant();
	matrix2 = matrix1;
	matrix1 = matrix2.invert();
	d2 = matrix1.determinant();
	matrix3 = matrix4.invert();
	ASSERT_TRUE((matrix3 == matrix4) && (round((1/d1)*10000)/10000 == round(d2*10000)/10000));
}

TEST(PIMathMatrix_Test, transposed) {
	PIMathMatrix<double> origMatr;
	double d1, d2;
	PIMathMatrix<double> matrix1;
	PIMathMatrix<double> matrix2;
	PIMathMatrix<double> matrix3;
	PIMathVector<double> vector;
	vector.resize(3, 3.0);
	vector[0] = 3.0;
	vector[1] = 6.0;
	vector[2] = 8.0;
	matrix1 = origMatr.identity(3, 3);
	matrix1.setCol(0, vector);
	vector[0] = 2.0;
	vector[1] = 1.0;
	vector[2] = 4.0;
	matrix1.setCol(1, vector);
	vector[0] = 6.0;
	vector[1] = 2.0;
	vector[2] = 5.0;
	matrix1.setCol(2, vector);
	d1 = matrix1.determinant();
	matrix2 = matrix1.transposed();
	d2 = matrix2.determinant();
	matrix3 = matrix2.transposed();
	ASSERT_TRUE((d1 == d2) && (matrix1 == matrix3));
}

TEST(PIMathMatrix_Test, matrixMultiplication) {
	PIMathMatrix<double> matrix1(2, 2, 1.5);
	PIMathMatrix<double> matrix2(2, 2, 2.5);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 * matrix2, 7.5, 2));
}

TEST(PIMathMatrix_Test, matrixAndVectorMultiplication) {
	PIMathMatrix<double> matrix1(2, 2, 1.5);
	PIMathVector<double> vector;
	vector.resize(2, 2.5);
	for(uint i = 0; i < 2; i++) {
		if((matrix1 * vector)[i] != 7.5) {
			ASSERT_TRUE(false);
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, vectorAndMatrixMultiplication) {
	PIMathMatrix<double> matrix1(2, 2, 1.5);
	PIMathVector<double> vector;
	vector.resize(2, 2.5);
	for(uint i = 0; i < 2; i++) {
		if((vector * matrix1)[i] != 7.5) {
			ASSERT_TRUE(false);
		}
	}
	ASSERT_TRUE(true);
}

TEST(PIMathMatrix_Test, valAndMatrixMultiplication) {
	PIMathMatrix<double> matrix1(3, 3, 1.5);
	ASSERT_TRUE(cmpSquareMatrixWithValue(25.0*matrix1, 37.5, 3));
}

TEST(PIMathMatrix_Test, hermitian) {
	complex<double> val;
	complex<double> res;
	val.imag(1.0);
	val.real(1.0);
	PIMathMatrix<complex<double>> matrix(3, 3, val);
	res.imag(-1.0);
	res.real(1.0);
	auto matr = hermitian(matrix);
	for(uint i = 0; i < 3; i++) {
		for(uint j = 0; j < 3; j++) {
			if(matr.element(i, j) != res) {
				ASSERT_TRUE(false);
			}
		}
	}
	ASSERT_TRUE(true);
}
