-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ватлин Алексей #193
Open
Alex-Vay
wants to merge
5
commits into
kontur-courses:master
Choose a base branch
from
Alex-Vay:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ватлин Алексей #193
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using FluentAssertions; | ||
using System.Diagnostics; | ||
using TagsCloudVisualization; | ||
using TagsCloudVisualization.FileReaders; | ||
using TagsCloudVisualization.FileReaders.Processors; | ||
using TagsCloudVisualization.Settings; | ||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
[TestFixture] | ||
public class BoringWordsFilterTests | ||
{ | ||
private string path = "./../../../TestData/text.txt"; | ||
private TxtFileReader reader; | ||
private BoringWordsFilter filter; | ||
|
||
[SetUp] | ||
public void Init() | ||
{ | ||
filter = new BoringWordsFilter(); | ||
var fileReaderSettings = new TxtFileReaderSettings(path); | ||
reader = new TxtFileReader(fileReaderSettings); | ||
} | ||
|
||
[Test] | ||
public void BoringWordFilter_FilterText_ShouldExcludeAllBoringWords() | ||
{ | ||
var text = reader.ReadLines(); | ||
var filtered = text.Then(filter.ProcessText); | ||
|
||
filtered.Then(r => r.Should().BeEquivalentTo("Привет", "файл", "должен", "обрабатываться", "корректно")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А для чего мы дальше Result тянем сюда? |
||
} | ||
|
||
[Test] | ||
public void BoringWordFilter_AddBoringPartOfSpeech_ShouldExcludeAllBoringWords() | ||
{ | ||
var text = reader.ReadLines(); | ||
filter.AddBoringPartOfSpeech("S"); | ||
var filtered = text.Then(filter.ProcessText); | ||
|
||
filtered.Then(r => r.Should().BeEquivalentTo("должен", "обрабатываться", "корректно")); | ||
} | ||
|
||
[Test] | ||
public void BoringWordFilter_AddBoringPartOfSpeech_ShouldExcludeBoringWord() | ||
{ | ||
var text = reader.ReadLines(); | ||
filter.AddBoringPartOfSpeech("должен"); | ||
var filtered = text.Then(filter.ProcessText); | ||
|
||
filtered.Then(r => r.Should().BeEquivalentTo("Привет", "файл", "обрабатываться", "корректно")); | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
TagCloudVisualizationTests/CircularCloudLayouterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using FluentAssertions; | ||
using System.Drawing; | ||
using TagsCloudVisualization.CloudLayouter; | ||
using TagsCloudVisualization.Visualizers; | ||
using TagsCloudVisualization.CloudLayouter.PointsGenerators; | ||
using TagsCloudVisualization.FileReaders.Processors; | ||
using TagsCloudVisualization.FileReaders; | ||
using TagsCloudVisualization.Visualizers.ImageColoring; | ||
using TagsCloudVisualization.CloudLayouter.CloudGenerators; | ||
using TagsCloudVisualization.Settings; | ||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
[TestFixture, NonParallelizable] | ||
public class CircularCloudLayouterTests | ||
{ | ||
private CircularCloudLayouter cloudLayouter; | ||
private const int imageWidth = 2000; | ||
private const int imageHeight = 2000; | ||
private Point center; | ||
private CloudGenerator cloudGenerator; | ||
|
||
[SetUp] | ||
public void Init() | ||
{ | ||
center = new Point(imageWidth / 2, imageHeight / 2); | ||
var pointGeneratorSettings = new SpiralPointsGeneratorSettings(center, 0.1, 0.1); | ||
var pointGenerator = new SpiralPointsGenerator(pointGeneratorSettings); | ||
cloudLayouter = new CircularCloudLayouter(pointGenerator); | ||
|
||
var fileReaderSettings = new TxtFileReaderSettings("./../../../TestData/text.txt"); | ||
var fileReader = new TxtFileReader(fileReaderSettings); | ||
|
||
var imageSaverSettings = new ImageSaveSettings("test", "png", null); | ||
var imageSaver = new ImageSaver(imageSaverSettings); | ||
|
||
var imageSettings = new ImageSettings( | ||
new Size(imageWidth, imageHeight), new FontFamily("Calibri"), | ||
new BlackColoring(), new RandomColoring()); | ||
var imageGenerator = new BitmapCreator(imageSettings, cloudLayouter); | ||
List<ITextProcessor> processors = [new LowercaseTransformer(), new BoringWordsFilter()]; | ||
cloudGenerator = new CloudGenerator(imageSaver, fileReader, imageGenerator, processors); | ||
} | ||
|
||
[TestCase(0, 1, TestName = "WhenWidthIsZero")] | ||
[TestCase(1, 0, TestName = "WhenHeightIsZero")] | ||
[TestCase(-1, 1, TestName = "WhenWidthIsNegative")] | ||
[TestCase(1, -1, TestName = "WhenHeightIsNegative")] | ||
public void PutNextRectangle_ShouldThrowArgumentException(int width, int height) | ||
{ | ||
var size = new Size(width, height); | ||
|
||
var action = () => cloudLayouter.PutNextRectangle(size); | ||
|
||
action.Should().Throw<ArgumentException>(); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_FirstRectangle_ShouldBeInCenter() | ||
{ | ||
var rectangleSize = new Size(10, 10); | ||
var expectedRectangle = new Rectangle( | ||
center.X - rectangleSize.Width / 2, | ||
center.Y - rectangleSize.Height / 2, | ||
rectangleSize.Width, | ||
rectangleSize.Height | ||
); | ||
|
||
var actualRectangle = cloudLayouter.PutNextRectangle(rectangleSize); | ||
|
||
actualRectangle.Should().BeEquivalentTo(expectedRectangle); | ||
} | ||
|
||
[Test, Parallelizable(ParallelScope.Self)] | ||
[Repeat(10)] | ||
public void PutNextRectangle_Rectangles_ShouldNotHaveIntersects() => | ||
AreRectanglesHaveIntersects(cloudLayouter.GeneratedRectangles).Should().BeFalse(); | ||
|
||
[Test] | ||
[Repeat(10)] | ||
public void PutNextRectangle_CloudCenterMust_ShouldBeInLayoterCenter() | ||
{ | ||
var expectedDiscrepancy = 10; | ||
|
||
cloudGenerator.GenerateTagCloud(); | ||
|
||
var actualCenter = GetCenterOfAllRectangles(cloudLayouter.GeneratedRectangles); | ||
actualCenter.X.Should().BeInRange(center.X - expectedDiscrepancy, center.X + expectedDiscrepancy); | ||
actualCenter.Y.Should().BeInRange(center.Y - expectedDiscrepancy, center.Y + expectedDiscrepancy); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_RectanglesDensity_ShouldBeMax() | ||
{ | ||
var expectedDensity = 0.3; | ||
cloudGenerator.GenerateTagCloud(); | ||
var rectangles = cloudLayouter.GeneratedRectangles; | ||
|
||
var rectanglesArea = rectangles.Sum(rect => rect.Width * rect.Height); | ||
|
||
var radius = GetMaxDistanceBetweenRectangleAndCenter(rectangles); | ||
var circleArea = Math.PI * radius * radius; | ||
var density = rectanglesArea / circleArea; | ||
density.Should().BeGreaterThanOrEqualTo(expectedDensity); | ||
} | ||
|
||
private Point GetCenterOfAllRectangles(List<Rectangle> rectangles) | ||
{ | ||
var top = rectangles.Max(r => r.Top); | ||
var right = rectangles.Max(r => r.Right); | ||
var bottom = rectangles.Min(r => r.Bottom); | ||
var left = rectangles.Min(r => r.Left); | ||
var x = left + (right - left) / 2; | ||
var y = bottom + (top - bottom) / 2; | ||
return new(x, y); | ||
} | ||
|
||
private double GetMaxDistanceBetweenRectangleAndCenter(List<Rectangle> rectangles) | ||
{ | ||
var center = GetCenterOfAllRectangles(rectangles); | ||
double maxDistance = -1; | ||
foreach (var rectangle in rectangles) | ||
{ | ||
var corners = new Point[4] | ||
{ | ||
new(rectangle.Top, rectangle.Left), | ||
new(rectangle.Bottom, rectangle.Left), | ||
new(rectangle.Top, rectangle.Right), | ||
new(rectangle.Bottom, rectangle.Right) | ||
}; | ||
var distance = corners.Max(p => GetDistanceBetweenPoints(p, center)); | ||
maxDistance = Math.Max(maxDistance, distance); | ||
} | ||
return maxDistance; | ||
} | ||
|
||
private static bool AreRectanglesHaveIntersects(List<Rectangle> rectangles) | ||
{ | ||
for (var i = 0; i < rectangles.Count; i++) | ||
for (var j = i + 1; j < rectangles.Count; j++) | ||
if (rectangles[i].IntersectsWith(rectangles[j])) | ||
return true; | ||
return false; | ||
} | ||
|
||
private static double GetDistanceBetweenPoints(Point point1, Point point2) | ||
=> Math.Sqrt(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Drawing; | ||
using FluentAssertions; | ||
using TagsCloudVisualization.FileReaders; | ||
using TagsCloudVisualization.CloudLayouter; | ||
using TagsCloudVisualization.FileReaders.Processors; | ||
using TagsCloudVisualization.Visualizers; | ||
using TagsCloudVisualization.CloudLayouter.PointsGenerators; | ||
using TagsCloudVisualization.Visualizers.ImageColoring; | ||
using TagsCloudVisualization.CloudLayouter.CloudGenerators; | ||
using TagsCloudVisualization.Settings; | ||
|
||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
[TestFixture] | ||
public class CloudGeneratorTest | ||
{ | ||
[Test] | ||
public void CloudGenerator_GenerateTagCloud_ShouldGenerateFile() | ||
{ | ||
var generator = InitGenerator(); | ||
|
||
var savePath = Path.Combine(Directory.GetCurrentDirectory(), "test.png"); | ||
|
||
File.Exists(savePath).Should().BeTrue(); | ||
} | ||
|
||
private static CloudGenerator InitGenerator() | ||
{ | ||
var fileReaderSettings = new TxtFileReaderSettings("./../../../TestData/text.txt"); | ||
var fileReader = new TxtFileReader(fileReaderSettings); | ||
var imageSaverSettings = new ImageSaveSettings("test", "png", null); | ||
var imageSaver = new ImageSaver(imageSaverSettings); | ||
var pointGeneratorSettings = new SpiralPointsGeneratorSettings(new Point(1000, 1000), 0.1, 0.1); | ||
var pointGenerator = new SpiralPointsGenerator(pointGeneratorSettings); | ||
var imageSettings = new ImageSettings( | ||
new Size(2000, 2000), new FontFamily("Calibri"), | ||
new BlackColoring(), new RandomColoring()); | ||
|
||
var layouter = new CircularCloudLayouter(pointGenerator); | ||
var imageCreator = new BitmapCreator(imageSettings, layouter); | ||
List<ITextProcessor> processors = [new LowercaseTransformer(), new BoringWordsFilter()]; | ||
|
||
return new CloudGenerator(imageSaver, fileReader, imageCreator, processors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using FluentAssertions; | ||
using TagsCloudVisualization; | ||
using TagsCloudVisualization.FileReaders; | ||
using TagsCloudVisualization.Settings; | ||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
public class CsvFileReaderTests | ||
{ | ||
[Test] | ||
public void ReadLines_ShouldCorrect_WhenReadWordsFromFile() | ||
{ | ||
var fileReaderSettings = new CsvFileReaderSettings("./../../../TestData/text.csv"); | ||
var reader = new CsvFileReader(fileReaderSettings); | ||
|
||
var result = reader.ReadLines(); | ||
|
||
result.Then(r => r.Should().BeEquivalentTo("Всем", "Привет", "Этот", "файл", "должен", "обрабатываться", "корректно")); | ||
} | ||
|
||
[Test] | ||
public void ReadLines_ShouldThrow_WhenFileDoesNotExists() | ||
{ | ||
var fileReaderSettings = new CsvFileReaderSettings("text ttt ty"); | ||
var reader = new CsvFileReader(fileReaderSettings); | ||
|
||
reader.ReadLines().OnFail(err => err.Should() | ||
.BeEquivalentTo("File not found")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FluentAssertions; | ||
using System.Text; | ||
using TagsCloudVisualization; | ||
using TagsCloudVisualization.FileReaders; | ||
using TagsCloudVisualization.FileReaders.Processors; | ||
using TagsCloudVisualization.Settings; | ||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
[TestFixture] | ||
public class LowercaseProcessorTests | ||
{ | ||
private readonly LowercaseTransformer processor = new(); | ||
|
||
[Test] | ||
public void LowercaseProcessor_ProcessText_ShouldLowercaseAllWords() | ||
{ | ||
var fileReaderSettings = new TxtFileReaderSettings("./../../../TestData/text.txt"); | ||
var reader = new TxtFileReader(fileReaderSettings); | ||
|
||
var text = reader.ReadLines(); | ||
var processed = text.Then(processor.ProcessText); | ||
|
||
processed.Then(r => r.Should().BeEquivalentTo("всем", "привет", "этот", "файл", "должен", "обрабатываться", "корректно")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using FluentAssertions; | ||
using System.Drawing; | ||
using TagsCloudVisualization.CloudLayouter.PointsGenerators; | ||
using TagsCloudVisualization.Settings; | ||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
[TestFixture, Parallelizable(ParallelScope.All)] | ||
public class SpiralPointsGeneratorTests | ||
{ | ||
[TestCase(0, 1, TestName = "WhenStepIsZero")] | ||
[TestCase(1, 0, TestName = "WhenAngleOffsetIsZero")] | ||
public void Constructor_ShouldThrowArgumentException(double step, double angleOffset) | ||
{ | ||
var pointGeneratorSettings = new SpiralPointsGeneratorSettings(new Point(0, 0), step, angleOffset); | ||
|
||
var act = () => new SpiralPointsGenerator(pointGeneratorSettings); | ||
|
||
act.Should().Throw<ArgumentException>(); | ||
} | ||
|
||
[TestCaseSource(nameof(GeneratePointsTestCases))] | ||
public void GetNextPointPosition_ShouldReturnCorrectPoint(double step, double angleOffset, int pointNumber, Point expectedPoint) | ||
{ | ||
var pointGeneratorSettings = new SpiralPointsGeneratorSettings(new Point(0, 0), step, angleOffset); | ||
var pointsGenerator = new SpiralPointsGenerator(pointGeneratorSettings); | ||
|
||
var actualPoint = pointsGenerator.GetNextPointPosition(); | ||
for (var i = 0; i < pointNumber - 1; i++) | ||
actualPoint = pointsGenerator.GetNextPointPosition(); | ||
|
||
actualPoint.Should().Be(expectedPoint); | ||
} | ||
|
||
public static TestCaseData[] GeneratePointsTestCases = | ||
{ | ||
new TestCaseData(0.1, 0.1, 1, new Point(0, 0)), | ||
new TestCaseData(1, 1, 1, new Point(0, 0)), | ||
new TestCaseData(1, 1, 3, new Point(0, 1)), | ||
new TestCaseData(1, 1, 5, new Point(-2, -3)), | ||
new TestCaseData(3, 1, 3, new Point(-2, 5)), | ||
new TestCaseData(3, 1, 5, new Point(-7, -9)), | ||
new TestCaseData(5, 1, 3, new Point(-4, 9)), | ||
new TestCaseData(5, 1, 5, new Point(-13, -15)), | ||
}; | ||
} |
34 changes: 34 additions & 0 deletions
34
TagCloudVisualizationTests/TagCloudVisualizationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="NUnit" Version="4.3.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="4.4.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TagsCloudVisualization\TagsCloudVisualization.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Выстави файл в CopyAlways и будет он у тебя всегда рядом со сборкой