matrixwithtest/MatrixProject.Test/MatrixTest.cs

88 lines
2.4 KiB
C#
Raw Normal View History

2024-10-08 13:20:27 +00:00
using MatrixProject.Exceptions;
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 13:20:27 +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 13:20:27 +00:00
2024-10-08 10:58:42 +00:00
Matrix matrix = new Matrix(row, column, fillNumber);
2024-10-08 13:20:27 +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))]
2024-10-08 13:20:27 +00:00
public void CheckMultOnScalar(int row,
int column,
int fillNumber,
int scalar,
2024-10-08 12:24:58 +00:00
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 13:20:27 +00:00
[Theory]
[MemberData(nameof(testSum))]
public void CheckSum(Matrix matrix, Matrix matrixTwo) {
Assert.Throws<AdditionException>(() => matrix += matrixTwo);
}
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 13:20:27 +00:00
public static TheoryData<Matrix, Matrix> testSum => new() {
{new Matrix(4, 3), new Matrix(3, 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;
}
}