-
Notifications
You must be signed in to change notification settings - Fork 307
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
base: master
Are you sure you want to change the base?
Вильданов Савелий #241
Changes from 6 commits
dee9824
a1d1e33
1fb56fb
16fb3d6
42f7bf7
f86d62f
ef1be52
3415138
38ceb83
4de5007
d20083c
198e49c
069d48f
492c16e
ec49a56
597425e
4dc529e
a1aab21
42d0b33
bbb7ffa
27c162d
5e7cbd6
fdc168c
801bba9
2ecfb96
0119e8d
1a2d746
083d09f
0a052f3
b4d4356
71c304c
a045a06
12d47d1
f16502f
1097f79
0ac7a7c
fb56b45
d625800
3ce2afc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
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) | ||
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. А есть ли смысл работать с нулевой шириной или высотой? |
||
{ | ||
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; | ||
} | ||
} |
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); | ||
} |
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() | ||
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. Публичный метод с маленькой буквы |
||
{ | ||
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); | ||
} | ||
} |
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> |
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. Хорошей практикой считается разделять тесты и основной код на отдельные проекты, так ты не нагружаешь процесс сборки основного кода лишними зависимостями. На больших проектах с тысячами тестов это может значительно сказаться на производительности. Давай и тут выделим под тесты отдельный проект |
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 | ||
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. Было бы круто ещё написать тест на вставку ооооочень большого количества прямоугольников, проверим, что он отработает за некоторое вменяемое время Проверок внутри не нужно будет, у Nunit есть атрибут, который завершит тест, если тот не уложится в заданное время |
||
{ | ||
[TestCase(-2, 3, TestName = "Negative width")] | ||
[TestCase(-2, 3, TestName = "Negative height")] | ||
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. В |
||
[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()); | ||
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. Создание этого класса происходит во всех тестах этого класса - можно вынести в |
||
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)); | ||
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. По выполняемым в тесте действиям этот тест идентичен предыдущему, а значит и проверку из этого теста можно включить в предыдущий |
||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_ShouldHaveCorrectSize() | ||
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. Из названия теста не совсем понятно, |
||
{ | ||
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() | ||
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. Давай тест на несколько размещаемых точек ещё напишем |
||
{ | ||
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)); | ||
} | ||
} |
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.
Эти поля тоже можно readonly сделать