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

Зиновьева Милана #244

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
13 changes: 9 additions & 4 deletions cs/Samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Copy link

Choose a reason for hiding this comment

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

не используемые зависимости стоит удалить

</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions cs/TagsCloudVisualization/CloudTags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Drawing;

namespace Layouter;

class CircularCloudLayouter
{
private Spiral spiral;
private Point centercloud;

private List<Rectangle> rectangles;

public Point GetCenter => centercloud;
Copy link

Choose a reason for hiding this comment

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

опечатка


public List<Rectangle> GetRectangles => rectangles;

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

public Rectangle PutNextRectangle(Size rectangleSize){
if (rectangleSize.IsEmpty){
throw new ArgumentNullException("rectangle is empty");
}
if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0){
throw new ArgumentOutOfRangeException("side less or equal zero");
}
Rectangle tempRectangle;
do{
Point nextPoint = spiral.GetNextPoint();
tempRectangle = new Rectangle(nextPoint, rectangleSize);
}
while(IsRectangleIntersect(tempRectangle));
rectangles.Add(tempRectangle);
return tempRectangle;
}

private bool IsRectangleIntersect(Rectangle rectangleChecked){
foreach(Rectangle rectangle in rectangles){
if (rectangleChecked.IntersectsWith(rectangle))
return true;
}
return false;
Copy link

Choose a reason for hiding this comment

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

Предлагаю переписать на linq

}
}
32 changes: 32 additions & 0 deletions cs/TagsCloudVisualization/Spiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Drawing;

namespace Layouter;

class Spiral
Copy link

Choose a reason for hiding this comment

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

Название не передает сути

Copy link

Choose a reason for hiding this comment

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

Стоит поправить

{
private readonly Point startPoint;
private readonly double radiusStep;
private readonly double angleStep;
private double currentAngle;

public Spiral(Point startPoint, double radiusStep = 1)
{
if (radiusStep <= 0) throw new ArgumentOutOfRangeException(nameof(radiusStep), "radius step must be positive");

this.angleStep = Math.PI / 180;
this.startPoint = startPoint;
this.radiusStep = radiusStep;
this.currentAngle = 0;
}

public Point GetNextPoint()
{
var radius = radiusStep * currentAngle;

var x = (int)(startPoint.X + radius * Math.Cos(currentAngle));
var y = (int)(startPoint.Y + radius * Math.Sin(currentAngle));
currentAngle += angleStep;

return new Point(x, y);
}
}
30 changes: 30 additions & 0 deletions cs/TagsCloudVisualization/TagsCloudVisualization.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Copy link

Choose a reason for hiding this comment

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

Выдели тесты в отдельный проект

</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions cs/TagsCloudVisualization/TestsSpiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using NUnit.Framework;
using FluentAssertions;
using System.Drawing;

namespace Layouter;

public class TestsSpiral
{

Spiral currentSpiral;
[SetUp]
public void SetUp(){
currentSpiral = new Spiral(new Point(0, 0));
}

[Test]
public void Spiral_ThrowingWhenRadiusNonPositive(){
Action action = new Action(() => new Spiral(new Point(0,0), -9));
action.Should().Throw<ArgumentOutOfRangeException>().Which.Message.Should().Contain("radius step must be positive");
}

[Test]
public void GetNextPoint_CenterPointSetting(){
currentSpiral.GetNextPoint().Should().Be(new Point(0, 0));
}

[Test]
public void GetNextPoint_SeveralPointSetting(){
for (int i = 0; i < 180; i++)
{
currentSpiral.GetNextPoint();
}
currentSpiral.GetNextPoint().Should().Be(new Point((int)(-Math.PI), 0));
}

}
130 changes: 130 additions & 0 deletions cs/TagsCloudVisualization/TestsTagsCloudVisualization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using NUnit.Framework;
using FluentAssertions;
using System.Drawing;

namespace Layouter;

public class TestsCloudVisualization
{
private CircularCloudLayouter circularCloudLayouter;
[SetUp]
public void SetUp(){
circularCloudLayouter = new CircularCloudLayouter(new Point(0, 0));
}

[Test]
public void CircularCloudLayouter_SettingCenter()
{
CircularCloudLayouter circularLayouter = new CircularCloudLayouter(new Point(2, 4));
circularLayouter.GetCenter.X.Should().Be(2);
circularLayouter.GetCenter.Y.Should().Be(4);
}

[Test]
public void CircularCloudLayouter_StartWithoutExceptions(){
Action action = new Action(() => new CircularCloudLayouter(new Point(2, 6)));
action.Should().NotThrow();
}

[Test]
public void PutNextRectangle_ThrowingWhenLengthsNegative(){
Action action = new Action(() => circularCloudLayouter.PutNextRectangle(new Size(-1, -1)));
action.Should().Throw<ArgumentOutOfRangeException>().Which.Message.Should().Contain("side less or equal zero");
}

[Test]
public void PutNextRectangle_ThrowingWhenRectangleEmpty(){
Action action = new Action(() => circularCloudLayouter.PutNextRectangle(Size.Empty));
action.Should().Throw<ArgumentNullException>().Which.Message.Should().Contain("rectangle is empty");
}

[Test]
public void CircularCloudLayouter_RectanglesEmptyAfterInitialization(){
circularCloudLayouter.GetRectangles.Should().BeEmpty();
}

[Test]
public void PutNextRectangle_PutFirstRectangle(){
Size rectangleSize = new Size(3, 7);
Rectangle expectedRectangle = new Rectangle(new Point(0, 0), rectangleSize);
circularCloudLayouter.PutNextRectangle(rectangleSize);

circularCloudLayouter.GetRectangles.Count().Should().Be(1);
circularCloudLayouter.GetRectangles.Should().Contain(expectedRectangle);
Copy link

Choose a reason for hiding this comment

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

collection.Should().ContainSingle()
https://fluentassertions.com/collections/

}

[Test]
public void PutNextRectangle_PutSeveralRectangles(){
Size rectangleSize = new Size(3, 7);
for(int i = 0;i < 20;i++){
Copy link

Choose a reason for hiding this comment

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

Стоит использовать автоформатирование кода. Если используешь райдер, можно настроить автоформатирование при коммите

circularCloudLayouter.PutNextRectangle(rectangleSize);
}
circularCloudLayouter.GetRectangles.Count().Should().Be(20);
Copy link

Choose a reason for hiding this comment

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

collection.Should().HaveCount()
https://fluentassertions.com/collections/

circularCloudLayouter.GetRectangles.Should().AllBeOfType(typeof(Rectangle));
}

[Test]
public void PutNextRectangle_LessThanAreaOfCircle()
Copy link

Choose a reason for hiding this comment

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

Наоборот быть и не может, тест не имеет смысла

{
var rectanglesSizes = new List<Size>
{
new Size(10, 5),
new Size(8, 8),
new Size(12, 3),
new Size(6, 10)
};

foreach (var size in rectanglesSizes)
{
circularCloudLayouter.PutNextRectangle(size);
}

int totalRectangleArea = circularCloudLayouter.GetRectangles
.Sum(rect => rect.Width * rect.Height);

int maxRadiusFromCenter = (int)circularCloudLayouter.GetRectangles
.Max(rect => GetMaxDistanceToCorner(rect, circularCloudLayouter.GetCenter));

int circleArea = (int)(Math.PI * Math.Pow(maxRadiusFromCenter, 2));
totalRectangleArea.Should().BeLessThan(circleArea);
}

private double GetMaxDistanceToCorner(Rectangle rect, Point center)
{
var corners = new[]
{
new Point(rect.Left, rect.Top),
new Point(rect.Right, rect.Top),
new Point(rect.Left, rect.Bottom),
new Point(rect.Right, rect.Bottom)
};
return corners.Max(corner => Distance(center, corner));
}

private double Distance(Point p1, Point p2)
{
return (int)Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
}

[Test]
public void PutNextRectangle_SeveralRectanglesDontIntersect(){
var rectanglesSizes = new List<Size>
{
new Size(10, 5),
new Size(8, 8),
new Size(12, 3),
new Size(6, 10)
};

foreach (var size in rectanglesSizes)
{
circularCloudLayouter.PutNextRectangle(size);
}

List<Rectangle> rectanglesTemp = circularCloudLayouter.GetRectangles;
for (int i = 0; i < rectanglesTemp.Count; i++){
for (int j = i + 1; j < rectanglesTemp.Count; j++)
rectanglesTemp[i].IntersectsWith(rectanglesTemp[j]).Should().BeFalse();
}
}
}
6 changes: 6 additions & 0 deletions cs/tdd.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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", "{8CBDE389-D9A9-454C-AD5E-7D04BBE79D71}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{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
{8CBDE389-D9A9-454C-AD5E-7D04BBE79D71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CBDE389-D9A9-454C-AD5E-7D04BBE79D71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CBDE389-D9A9-454C-AD5E-7D04BBE79D71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CBDE389-D9A9-454C-AD5E-7D04BBE79D71}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down