matrixwithtest/MatrixProject.Test/MatrixTest.cs

77 lines
2.1 KiB
C#
Raw Normal View History

2024-10-08 12:24:58 +00:00
using System.Numerics;
2024-10-08 10:58:42 +00:00
using System.Reflection;
2024-10-08 10:42:11 +00:00
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();
2024-10-08 12:24:58 +00:00
2024-10-08 10:42:11 +00:00
Assert.Equal(5, matrix.Columns);
}
2024-10-08 10:58:42 +00:00
[Theory]
[MemberData(nameof(testTwoDimArray))]
public void CheckFillMatrixInDefaultConstructor(int row, int column, int fillNumber, double[,] testArray) {
2024-10-08 10:42:11 +00:00
2024-10-08 10:58:42 +00:00
Matrix matrix = new Matrix(row, column, fillNumber);
2024-10-08 10:42:11 +00:00
2024-10-08 10:58:42 +00:00
Assert.True(EqualsTwoDimArray(testArray, matrix));
}
2024-10-08 12:24:58 +00:00
[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));
}
2024-10-08 10:42:11 +00:00
2024-10-08 10:58:42 +00:00
public static TheoryData<int, int,int, double[,]> testTwoDimArray => new () {
2024-10-08 12:24:58 +00:00
{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 } } },
2024-10-08 10:58:42 +00:00
{2, 2, 3, new double[,] { { 3, 3 }, { 3, 3 } } }
};
2024-10-08 12:24:58 +00:00
public static TheoryData<int, int, int, int, double[,]> testMult => new() {
2024-10-08 10:42:11 +00:00
2024-10-08 12:24:58 +00:00
{2, 2, 1, 4, new double[2,2]{ {4,4 }, {4,4} }}
};
2024-10-08 10:42:11 +00:00
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;
}
}