Skip to content
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

Вильданов Савелий #241

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
dee9824
create interface
Saveliy21 Nov 12, 2024
a1d1e33
Add CircularCloudLayotherTest
Saveliy21 Nov 12, 2024
1fb56fb
add CircularCloudLayouter
Saveliy21 Nov 12, 2024
16fb3d6
Add SpiralTest
Saveliy21 Nov 12, 2024
42f7bf7
Add class Spiral
Saveliy21 Nov 12, 2024
f86d62f
add settings
Saveliy21 Nov 12, 2024
ef1be52
refactor CircularCloudLayouter.cs
Saveliy21 Nov 15, 2024
3415138
refactor Spiral.cs
Saveliy21 Nov 15, 2024
38ceb83
refactor CircularCloudLayotherTest.cs
Saveliy21 Nov 15, 2024
4de5007
refactor SpiralTest.cs
Saveliy21 Nov 15, 2024
d20083c
refactor project Tests
Saveliy21 Nov 15, 2024
198e49c
change Spiral settings
Saveliy21 Nov 16, 2024
069d48f
create CloudGenerator.cs
Saveliy21 Nov 16, 2024
492c16e
create Program.cs
Saveliy21 Nov 16, 2024
ec49a56
Add Images
Saveliy21 Nov 16, 2024
597425e
small refactor Program.cs
Saveliy21 Nov 16, 2024
4dc529e
change Images location
Saveliy21 Nov 16, 2024
a1aab21
create class for constants
Saveliy21 Nov 16, 2024
42d0b33
refactor CloudGenerator.cs
Saveliy21 Nov 16, 2024
bbb7ffa
Add TearDown
Saveliy21 Nov 16, 2024
27c162d
refactor SpiralTest.cs
Saveliy21 Nov 16, 2024
5e7cbd6
Add CloudGeneratorTest.cs
Saveliy21 Nov 16, 2024
fdc168c
Add new class SaveImages
Saveliy21 Nov 21, 2024
801bba9
change FluentAssertions version
Saveliy21 Nov 21, 2024
2ecfb96
refactor CircularCloudLayotherTest.cs
Saveliy21 Nov 21, 2024
0119e8d
replace CloudGenerator.cs to CloudDrawer.cs
Saveliy21 Nov 21, 2024
1a2d746
replace CloudGeneratorTest to CloudDrawerTest
Saveliy21 Nov 21, 2024
083d09f
refactor Program.cs
Saveliy21 Nov 21, 2024
0a052f3
add new method to CircularCloudLayouter.cs
Saveliy21 Nov 22, 2024
b4d4356
refactor CloudDrawer.cs
Saveliy21 Nov 22, 2024
71c304c
rename class
Saveliy21 Nov 22, 2024
a045a06
refactor Program.cs
Saveliy21 Nov 22, 2024
12d47d1
refactor TagsCloudVisualizationTests.csproj
Saveliy21 Nov 22, 2024
f16502f
refactor TagsCloudVisualization.csproj
Saveliy21 Nov 22, 2024
1097f79
rename CloudDrawerTest.cs
Saveliy21 Nov 22, 2024
0ac7a7c
refactor CircularCloudLayotherTest.cs
Saveliy21 Nov 22, 2024
fb56b45
unit SaveImage.cs with CloudDrawer.cs
Saveliy21 Nov 22, 2024
d625800
refactor
Saveliy21 Nov 22, 2024
3ce2afc
small refactor
Saveliy21 Nov 22, 2024
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
41 changes: 41 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Drawing;

namespace TagsCloudVisualization;

public class CircularCloudLayouter : ICircularCloudLayouter
{
private readonly Point center;
private List<Rectangle> rectangles;
private Spiral spiral;

Choose a reason for hiding this comment

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

Эти поля тоже можно readonly сделать


public CircularCloudLayouter(Point center)
{
this.center = center;
rectangles = new List<Rectangle>();
spiral = new Spiral(center);
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
Rectangle rectangle;
if (rectangleSize.Width < 0 || rectangleSize.Height < 0)

Choose a reason for hiding this comment

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

А есть ли смысл работать с нулевой шириной или высотой?

{
throw new ArgumentException($"Rectangle size ({rectangleSize}) should be positive");
}

do
{
Point point = spiral.getNextPointOnSpiral();
point.Offset(-rectangleSize.Width / 2, -rectangleSize.Height / 2);
rectangle = new Rectangle(point, rectangleSize);
} while (rectangles.Any(r => r.IntersectsWith(rectangle)));

rectangles.Add(rectangle);
return rectangle;
}

public List<Rectangle> GetRectangles()
{
return rectangles;
}
}
8 changes: 8 additions & 0 deletions cs/TagsCloudVisualization/ICircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagsCloudVisualization;

public interface ICircularCloudLayouter
{
Rectangle PutNextRectangle(Size rectangleSize);
}
26 changes: 26 additions & 0 deletions cs/TagsCloudVisualization/Spiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Drawing;

namespace TagsCloudVisualization;

public class Spiral
{
private readonly Point center;
private readonly int angleStep;
private double angle;


public Spiral(Point center, int angleStep = 1)
{
this.center = center;
this.angleStep = angleStep;
angle = 0;
}

public Point getNextPointOnSpiral()

Choose a reason for hiding this comment

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

Публичный метод с маленькой буквы

{
angle += angleStep;
var x = (int) (center.X + angle * Math.Cos(angle));
var y = (int) (center.Y + angle * Math.Sin(angle));
return new Point(x, y);
}
}
16 changes: 16 additions & 0 deletions cs/TagsCloudVisualization/TagsCloudVisualization.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
</ItemGroup>

</Project>

Choose a reason for hiding this comment

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

Хорошей практикой считается разделять тесты и основной код на отдельные проекты, так ты не нагружаешь процесс сборки основного кода лишними зависимостями. На больших проектах с тысячами тестов это может значительно сказаться на производительности.

Давай и тут выделим под тесты отдельный проект

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Drawing;
using FluentAssertions;
using NUnit.Framework;

namespace TagsCloudVisualization.TagsCloudVisualizationTests;

public class CircularCloudLayotherTest

Choose a reason for hiding this comment

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

Было бы круто ещё написать тест на вставку ооооочень большого количества прямоугольников, проверим, что он отработает за некоторое вменяемое время

Проверок внутри не нужно будет, у Nunit есть атрибут, который завершит тест, если тот не уложится в заданное время

{
[TestCase(-2, 3, TestName = "Negative width")]
[TestCase(-2, 3, TestName = "Negative height")]

Choose a reason for hiding this comment

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

В Negative height тесте высота положительная)

[TestCase(-2, -3, TestName = "Negative weight and height")]
public void PutNextRectangle_ShouldThrowArgumentException_WithNegativeInput(int width, int height)
{
Action action = () => new CircularCloudLayouter(new Point()).PutNextRectangle(new Size(width, height));
action.Should().Throw<ArgumentException>();
}

[TestCase(0, 0, TestName = "zero width and height")]
[TestCase(2, 3, TestName = "positive width and height")]
public void PutNextRectangle_ShouldNotThrowArgumentException_WithCorrectInput(int width, int height)
{
Action action = () => new CircularCloudLayouter(new Point()).PutNextRectangle(new Size(width, height));
action.Should().NotThrow<ArgumentException>();
}

[Test]
public void PutNextRectangle_ShouldAddNewRectangle()
{
CircularCloudLayouter circularCloudLayouter = new CircularCloudLayouter(new Point());

Choose a reason for hiding this comment

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

Создание этого класса происходит во всех тестах этого класса - можно вынести в SetUp, как обязательное действие перед прогоном теста. Плюсом будет то, что код самих тестов станет читать попроще

circularCloudLayouter.PutNextRectangle(new Size(2, 2));
circularCloudLayouter.GetRectangles().Should().NotBeEmpty();
}

[Test]
public void PutNextRectangle_ShouldContainsNewRectangle()
{
CircularCloudLayouter circularCloudLayouter = new CircularCloudLayouter(new Point());
circularCloudLayouter.PutNextRectangle(new Size(2, 2));
circularCloudLayouter.GetRectangles().Should().Contain(new Rectangle(-1, -1, 2, 2));

Choose a reason for hiding this comment

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

По выполняемым в тесте действиям этот тест идентичен предыдущему, а значит и проверку из этого теста можно включить в предыдущий

}

[Test]
public void PutNextRectangle_ShouldHaveCorrectSize()

Choose a reason for hiding this comment

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

Из названия теста не совсем понятно, Size чего будет проверяться в тесте - размещенного прямоугольника или коллекции с размещенными прямоугольниками

{
CircularCloudLayouter circularCloudLayouter = new CircularCloudLayouter(new Point());
circularCloudLayouter.PutNextRectangle(new Size(2, 2));
circularCloudLayouter.PutNextRectangle(new Size(2, 2));
circularCloudLayouter.PutNextRectangle(new Size(2, 2));
circularCloudLayouter.GetRectangles().Count.Should().Be(3);
}

[Test]
public void CircularCloudLayouter_RectAngelsShouldNoIntersectsWithOthers()
{
CircularCloudLayouter circularCloudLayouter = new CircularCloudLayouter(new Point());
circularCloudLayouter.PutNextRectangle(new Size(1, 3));
circularCloudLayouter.PutNextRectangle(new Size(3, 4));
circularCloudLayouter.PutNextRectangle(new Size(4, 2));
List<Rectangle> rectangels = circularCloudLayouter.GetRectangles();
foreach (Rectangle rectangle in rectangels)
{
rectangels.Where((_, j) => j != rectangels.IndexOf(rectangle)).All(r => !r.IntersectsWith(rectangle))
.Should().BeTrue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Drawing;
using FluentAssertions;
using NUnit.Framework;

namespace TagsCloudVisualization.TagsCloudVisualizationTests;

public class SpiralTest
{
[Test]
public void getNextPointOnSpiral_ShouldReturnNextPointOnSpiral()

Choose a reason for hiding this comment

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

Давай тест на несколько размещаемых точек ещё напишем

{
Spiral spiral = new Spiral(new Point(0, 0));
Point point = new Point();
for (int i = 0; i < 2; i++)
{
point = spiral.getNextPointOnSpiral();
}

point.Should().Be(new Point(0, 1));
}
}
8 changes: 6 additions & 2 deletions cs/tdd.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BowlingGame", "BowlingGame\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "Samples\Samples.csproj", "{B5108E20-2ACF-4ED9-84FE-2A718050FC94}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudVisualization", "TagsCloudVisualization\TagsCloudVisualization.csproj", "{43089B42-005E-43E9-8E19-544E3534C010}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AD0F018A-732E-4074-8527-AB2EEC8D0BF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD0F018A-732E-4074-8527-AB2EEC8D0BF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD0F018A-732E-4074-8527-AB2EEC8D0BF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD0F018A-732E-4074-8527-AB2EEC8D0BF3}.Release|Any CPU.Build.0 = Release|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Release|Any CPU.Build.0 = Release|Any CPU
{43089B42-005E-43E9-8E19-544E3534C010}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43089B42-005E-43E9-8E19-544E3534C010}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43089B42-005E-43E9-8E19-544E3534C010}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43089B42-005E-43E9-8E19-544E3534C010}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down