matrixwithtest/MatrixProject.Test/MatrixTest.cs
2024-10-08 15:24:58 +03:00

77 lines
2.1 KiB
C#

using System.Numerics;
using System.Reflection;
namespace MatrixProject.Test;
public class MatrixTest
{
[InlineData(10)]
[InlineData(5)]
[InlineData(2)]
[InlineData(3)]
[Theory]
public void CheckRowsBeforeInitMatrixWithRows(int size)
{
Matrix matrix = new Matrix(size);
Assert.Equal(size, matrix.Rows);
Assert.Equal(size, matrix.Columns);
}
[Fact]
public void CheckColumnsBeforeInitMatrix()
{
Matrix matrix = new Matrix();
Assert.Equal(5, matrix.Columns);
}
[Theory]
[MemberData(nameof(testTwoDimArray))]
public void CheckFillMatrixInDefaultConstructor(int row, int column, int fillNumber, double[,] testArray) {
Matrix matrix = new Matrix(row, column, fillNumber);
Assert.True(EqualsTwoDimArray(testArray, matrix));
}
[Theory]
[MemberData(nameof(testMult))]
public void CheckMultOnScalar(int row,
int column,
int fillNumber,
int scalar,
double[,] testArray)
{
Matrix matrix = new Matrix(row, column, fillNumber);
matrix = matrix * scalar;
Assert.True(EqualsTwoDimArray(testArray, matrix));
}
public static TheoryData<int, int,int, double[,]> testTwoDimArray => new () {
{2, 2, 3, new double[,] { { 3, 3 }, { 3, 3 } } },
{2, 2, 3, new double[,] { { 3, 3 }, { 3, 3 } } },
{2, 2, 3, new double[,] { { 3, 3 }, { 3, 3 } } },
{2, 2, 3, new double[,] { { 3, 3 }, { 3, 3 } } },
{2, 2, 3, new double[,] { { 3, 3 }, { 3, 3 } } }
};
public static TheoryData<int, int, int, int, double[,]> testMult => new() {
{2, 2, 1, 4, new double[2,2]{ {4,4 }, {4,4} }}
};
private bool EqualsTwoDimArray(double[,] testMatrix, Matrix matrix) {
if(testMatrix.GetLength(0) != matrix.Rows ||
testMatrix.GetLength(1) != matrix.Columns) return false;
for (int i = 0; i < matrix.Rows; i++)
{
for (int j = 0; j < matrix.Columns; j++)
{
if (testMatrix[i, j] != matrix[i, j]) return false;
}
}
return true;
}
}