#include "pimathmatrix.h"

#include "gtest/gtest.h"

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

TEST(PIMathMatrix_Test, constructor1) {
	PIMathMatrix<double> matrix(3, 3, 5.0);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 3));
}

TEST(PIMathMatrix_Test, constructor2) {
	PIVector<double> vector(2, 5.0);
	PIMathMatrix<PIVector<double>> matrix(3, 3, vector);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, vector, 3));
}

TEST(PIMathMatrix_Test, constructor3) {
	PIVector2D<double> vector(2, 2, 5.0);
	PIMathMatrix<double> matrix(vector);
	ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 2));
}

TEST(PIMathMatrix_Test, identity1) {
	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) {
					FAIL();
				}
			} else {
				if (matrix[i][i] != 1.0) {
					FAIL();
				}
			}
		}
	}
	SUCCEED();
}

TEST(PIMathMatrix_Test, identity2) {
	auto matrix = PIMathMatrix<double>::identity(4, 3);
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			if (matrix.element(i, j) != (i == j ? 1. : 0.)) FAIL();
		}
	}
}

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 (matrix.element(i, j) != (i == j ? 1. : 0.)) FAIL();
		}
	}
	SUCCEED();
}

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) {
			FAIL();
		}
	}
	SUCCEED();
}

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) {
			FAIL();
		}
	}
	SUCCEED();
}

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) {
			FAIL();
		}
	}
	SUCCEED();
}

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) {
			FAIL();
		}
	}
	SUCCEED();
}

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));
}
