Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
namespace TestsForMatrixMultiplication;

using parallelMatrixMultiplication;

public class Tests
{
[Test]
public void MultiplyMatricesOfSizeThreeByThree()
{
var listOfValues = new List<int[]>
{
new int[3] { 1, 2, 3 },
new int[3] { 4, 5, 6 },
new int[3] { 7, 8, 9 },
};

var listOfCorrectValues = new List<int[]>
{
new int[3] { 30, 36, 42 },
new int[3] { 66, 81, 96 },
new int[3] { 102, 126, 150 },
};

var firstMatrix = Matrix.Create(3, 3, listOfValues);
var secondMatrix = Matrix.Create(3, 3, listOfValues);
var correctMatrix = Matrix.Create(3, 3, listOfCorrectValues);

var resultMatrix = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrix, correctMatrix));
Copy link

@YuriUfimtsev YuriUfimtsev Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Еще у вас в тестах не заметил сравнения результатов последовательного умножения CoherentMultiply с параллельным (который почему-то просто Multiply, лучше тогда тоже ParallelMultiply назвать).
Так что добавьте, пожалуйста, проверки в тестах на то, что другой метод умножения тот же результат дает

}

[Test]
public void MultiplyMatricesOfBigSize()
{
var listOfValuesFirstMatrix = new List<int[]> {};
var listOfValuesSecondMatrix = new List<int[]> {};
var listOfCorrectValues = new List<int[]> {};

for (int i = 0; i < 10000; i++)
{
listOfValuesFirstMatrix.Add(new int[10000]);
listOfValuesSecondMatrix.Add(new int[1]);
listOfCorrectValues.Add(new int[1]);
for (int j = 0; j < 10000; ++j)
{
listOfValuesFirstMatrix[i][j] = 1;
}
listOfValuesSecondMatrix[i][0] = 1;
listOfCorrectValues[i][0] = 10000;
}

var firstMatrix = Matrix.Create(10000, 10000, listOfValuesFirstMatrix);
var secondMatrix = Matrix.Create(10000, 1, listOfValuesSecondMatrix);
var correctMatrix = Matrix.Create(10000, 1, listOfCorrectValues);

var resultMatrix = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrix, correctMatrix));
}

[Test]
public void MultiplyMatricesOfDifferentSizes()
{
var listOfValuesFirstMatrix = new List<int[]>
{
new int[3] { 1, 2, 3 },
new int[3] { 4, 5, 6 },
new int[3] { 7, 8, 9 },
};

var listOfValuesSecondMatrix = new List<int[]>
{
new int[1] { 1 },
new int[1] { 2 },
new int[1] { 3 },
};

var listOfCorrectValues = new List<int[]>
{
new int[1] { 14 },
new int[1] { 32 },
new int[1] { 50 },
};

var firstMatrix = Matrix.Create(3, 3, listOfValuesFirstMatrix);
var secondMatrix = Matrix.Create(3, 1, listOfValuesSecondMatrix);
var correctMatrix = Matrix.Create(3, 1, listOfCorrectValues);

var resultMatrix = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrix, correctMatrix));
}

[Test]
public void MultiplyOfNotSquareMatrices()
{
var listOfValuesFirstMatrix = new List<int[]>
{
new int[5] { 1, 2, 3, 4, 5 },
new int[5] { 6, 7, 8, 9, 10 },
};

var listOfValuesSecondMatrix = new List<int[]>
{
new int[1] { 1 },
new int[1] { 2 },
new int[1] { 3 },
new int[1] { 4 },
new int[1] { 5 },
};

var listOfCorrectValues = new List<int[]>
{
new int[1] { 55 },
new int[1] { 130 },
};

var firstMatrix = Matrix.Create(2, 5, listOfValuesFirstMatrix);
var secondMatrix = Matrix.Create(5, 1, listOfValuesSecondMatrix);
var correctMatrix = Matrix.Create(2, 1, listOfCorrectValues);

var resultMatrix = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrix, correctMatrix));
}

[Test]
public void MultiplyMatricesWithNegativeNumbers()
{
var listOfValuesFirstMatrix = new List<int[]>
{
new int[5] { 1, 2, -3, 4, 5 },
new int[5] { 6, -7, 8, 9, 10 },
};

var listOfValuesSecondMatrix = new List<int[]>
{
new int[1] { 1 },
new int[1] { 2 },
new int[1] { 3 },
new int[1] { -4 },
new int[1] { 5 },
};

var listOfCorrectValues = new List<int[]>
{
new int[1] { 5 },
new int[1] { 30 },
};

var firstMatrix = Matrix.Create(2, 5, listOfValuesFirstMatrix);
var secondMatrix = Matrix.Create(5, 1, listOfValuesSecondMatrix);
var correctMatrix = Matrix.Create(2, 1, listOfCorrectValues);

var resultMatrix = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrix, correctMatrix));
}

[Test]
public void MultiplyMatricesWithZero()
{
var listOfValuesFirstMatrix = new List<int[]>
{
new int[5] { 1, 2, 0, 4, 5 },
new int[5] { 6, 0, 8, 9, 10 },
};

var listOfValuesSecondMatrix = new List<int[]>
{
new int[1] { 1 },
new int[1] { 2 },
new int[1] { 0 },
new int[1] { 0 },
new int[1] { 5 },
};

var listOfCorrectValues = new List<int[]>
{
new int[1] { 30 },
new int[1] { 56 },
};

var firstMatrix = Matrix.Create(2, 5, listOfValuesFirstMatrix);
var secondMatrix = Matrix.Create(5, 1, listOfValuesSecondMatrix);
var correctMatrix = Matrix.Create(2, 1, listOfCorrectValues);

var resultMatrix = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrix, correctMatrix));
}

[Test]
public void MultiplyMatricesWithWrongData()
{
var listOfCorrectValues = new List<int[]>
{
new int[1] { 30 },
new int[1] { 56 },
};

var correctMatrix = Matrix.Create(2, 1, listOfCorrectValues);
Assert.Throws<InvalidFileException>(() => Matrix.Multiplication(Path.Combine(TestContext.CurrentContext.TestDirectory,
"TestsForMatrix", "firstCorrectMatrix.txt"),
Path.Combine(TestContext.CurrentContext.TestDirectory,
"TestsForMatrix", "incorrectMatrix.txt"),
Path.Combine(TestContext.CurrentContext.TestDirectory,
"TestsForMatrix", "resultMatrix.txt")));
}

[Test]
public void CompareParalelAndConsistentMultiply()
{
var listOfValues = new List<int[]>
{
new int[3] { 1, 2, 3 },
new int[3] { 4, 5, 6 },
new int[3] { 7, 8, 9 },
};

var firstMatrix = Matrix.Create(3, 3, listOfValues);
var secondMatrix = Matrix.Create(3, 3, listOfValues);

var resultMatrixFromParallelMultiply = Matrix.ParallelMultiply(firstMatrix, secondMatrix);
var resultMatrixFromConsistentMultiply = Matrix.ConsistentMultiply(firstMatrix, secondMatrix);
Assert.True(Matrix.AreEquals(resultMatrixFromParallelMultiply, resultMatrixFromConsistentMultiply));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\parallelMatrixMultiplication\parallelMatrixMultiplication.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 2 3
4 5 6
5 6 7
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 2 a
3 4 5
6 7 8
31 changes: 31 additions & 0 deletions parallelMatrixMultiplication/parallelMatrixMultiplication.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "parallelMatrixMultiplication", "parallelMatrixMultiplication\parallelMatrixMultiplication.csproj", "{E4C7CC35-FD7C-4BA7-B1C5-EF752CDAA263}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForMatrixMultiplication", "TestsForMatrixMultiplication\TestsForMatrixMultiplication.csproj", "{F6D3443D-537C-44C2-9988-5B3D10652CE1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E4C7CC35-FD7C-4BA7-B1C5-EF752CDAA263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4C7CC35-FD7C-4BA7-B1C5-EF752CDAA263}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4C7CC35-FD7C-4BA7-B1C5-EF752CDAA263}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4C7CC35-FD7C-4BA7-B1C5-EF752CDAA263}.Release|Any CPU.Build.0 = Release|Any CPU
{F6D3443D-537C-44C2-9988-5B3D10652CE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6D3443D-537C-44C2-9988-5B3D10652CE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6D3443D-537C-44C2-9988-5B3D10652CE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6D3443D-537C-44C2-9988-5B3D10652CE1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4E527157-F298-4A47-B122-6710A34A14E1}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace parallelMatrixMultiplication;
/// <summary>
/// Exception for errors with file
/// </summary>
public class InvalidFileException : Exception
{
public InvalidFileException() { }
public InvalidFileException(string message) : base(message) {}
public InvalidFileException(string message, Exception inner)
: base(message, inner) {}
}
Loading