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

Рушкова Ольга2 #261

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8cae611
AddNearestFinder
Nov 13, 2024
09cf278
Add CircularLayer
Nov 13, 2024
9bbe39a
Add CircularCloudLayoter
Nov 13, 2024
dfbc811
Add Direction
Nov 13, 2024
9f88c15
Fix calculation radius
Nov 14, 2024
ce6db8c
Add move to next sector when find position without intersection
Nov 14, 2024
517ccb0
Add intersections and small fix
Nov 14, 2024
3761068
Refactoring: replace validation in layoter, fix tests
Nov 14, 2024
4ec2ddc
Remove usless saving
Nov 15, 2024
fe8c9fe
Refactoring: add RectangleStorage
Nov 15, 2024
d661efd
Refactoring: add using storage
Nov 15, 2024
e4ca17d
Fix: changing rectangle should change rectangle in storage
Nov 15, 2024
8dff187
Add move closer to center
Nov 15, 2024
6f70c9d
Fix radius recalculate and intersection multiplier, add visualisation
Nov 15, 2024
a4217c3
Small fix
Nov 15, 2024
051cb3a
Refactoring: remove dublicate tests
Nov 15, 2024
9879db0
Refactoring And Fix infinite cicle when find position for new rectangle
Nov 17, 2024
badf496
Add results for random generated data
Nov 17, 2024
23112ec
fix filepath
Nov 17, 2024
80508bb
Add third task
Nov 17, 2024
3eb403a
Fix visualizer and add .csproj
Nov 20, 2024
cb4cb27
Fix naming
Nov 20, 2024
9d33c50
Replace DrawGrid to extensions
Nov 20, 2024
d4550a0
Remove storage
Nov 21, 2024
067800a
Fix intelisense and add compressor
Nov 21, 2024
4dfb06b
new realisation for circleLayer
Nov 21, 2024
09726a2
Fix static finder and remove useless tests
Dec 16, 2024
9fd115b
Create project for tests
Dec 16, 2024
adc2df3
Fix circle layer
Dec 16, 2024
066293b
Refactoring
Dec 16, 2024
0f532ed
Change compress algorithm
Dec 16, 2024
c8f2b68
Fix naming
Dec 17, 2024
175d3ca
Delete comment
Dec 17, 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
4 changes: 2 additions & 2 deletions cs/BowlingGame/BowlingGame.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RootNamespace>BowlingGame</RootNamespace>
<LangVersion>7.3</LangVersion>
<LangVersion>11</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions cs/TagCloudTests/BruteForceNearestFinderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using NUnit.Framework;
using FluentAssertions;
using System.Drawing;
using TagsCloudVisualization;

namespace TagCloudTests
{
public class BruteForceNearestFinderTests
{
[Test]
public void FindNearest_ShouldReturnNull_OnEmptyRectangles()
{
var rectangleForFind = new Rectangle(new Point(5, 7), new Size(4, 2));

BruteForceNearestFinder.FindNearestByDirection(rectangleForFind, Direction.Top, []).Should().BeNull();
}

[TestCase(4, 10, Direction.Top)]
[TestCase(2, 7, Direction.Top, true)]
[TestCase(2, 7, Direction.Right)]
[TestCase(0, 0, Direction.Right, true)]
[TestCase(0, 0, Direction.Bottom, true)]
[TestCase(7, 4, Direction.Bottom)]
[TestCase(10, 11, Direction.Left)]
[TestCase(7, 4, Direction.Left, true)]
public void FindNearest_ShouldReturnNearestRectangleByDirection_ForArgumentRectangle(int x, int y, Direction direction, bool isFirstNearest = false)
{
var addedRectangle1 = new Rectangle(new Point(2, 2), new Size(3, 4));
var addedRectangle2 = new Rectangle(new Point(5, 7), new Size(4, 2));
var rectangleForFind = new Rectangle(new Point(x, y), new Size(2, 1));
var rectangles = new List<Rectangle> { addedRectangle1, addedRectangle2 };

var nearest = BruteForceNearestFinder.FindNearestByDirection(rectangleForFind, direction, rectangles);

nearest.Should().Be(isFirstNearest ? addedRectangle1 : addedRectangle2);
}
}
}
44 changes: 44 additions & 0 deletions cs/TagCloudTests/CirclePositionDistributorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Drawing;
using FluentAssertions;
using NUnit.Framework;
using TagsCloudVisualization;

namespace TagCloudTests;

public class CirclePositionDistributorTests
{
private CirclePositionDistributor currentLayer;

[SetUp]
public void SetUp()
{
var center = new Point(5, 5);
currentLayer = new CirclePositionDistributor(center);
}

[Test]
public void GetNextRectanglePosition_ShouldTryPutRectangleOnCircle()
{
var firstRectangleLocation = currentLayer.GetNextPosition();
var secondRectangleLocation = currentLayer.GetNextPosition();

var radius = currentLayer.Center.CalculateDistanceBetween(firstRectangleLocation);

radius.Should().Be(currentLayer.Center.CalculateDistanceBetween(secondRectangleLocation));
}

[Test]
public void GetNextRectanglePosition_MustExpandCircle_WhenTryPutRectangleOnFullCircle()
{
var firstPoint = currentLayer.GetNextPosition();
for (int i = 0; i < 360; i++)
{
currentLayer.GetNextPosition();
}

var finalPoint = currentLayer.GetNextPosition();

finalPoint.CalculateDistanceBetween(currentLayer.Center).Should()
.BeGreaterThan(firstPoint.CalculateDistanceBetween(currentLayer.Center));
}
}
113 changes: 113 additions & 0 deletions cs/TagCloudTests/CircularCloudLayouterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using TagsCloudVisualization;

namespace TagCloudTests;

public class CircularCloudLayouterTests
{
private CircularCloudLayouter layouter;
private Point defaultCenter;
private List<Rectangle> storage;

public static IEnumerable<TestCaseData> IntersectionTestsData
{
get
{
yield return new TestCaseData(new Size[]
{
new(1, 1), new(1, 1), new(1, 1), new(400, 400),
new(1, 4), new(6, 6)
})
.SetName("WhenAddedSmallRectanglesWithOneVeryBig");
yield return new TestCaseData(new Size[]
{
new(100, 100), new(123, 121), new(100, 100), new(400, 400),
new(100, 400), new(600, 128)
})
.SetName("WhenAddedBigRectangles");
yield return new TestCaseData(new Size[] { new(4, 4), new(4, 4), new(4, 4), new(4, 4), new(4, 4) })
.SetName("WhenAddedRectanglesHasSameSize");
}
}

[SetUp]
public void SetUp()
{
defaultCenter = new Point(5, 5);
storage = [];
layouter = CircularCloudLayouter.CreateLayouterWithStartRectangles(defaultCenter, storage);
}

[TearDown]
public void TearDown()
{
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
{
SaveImageOnFail();
}
}

private void SaveImageOnFail()
{
var visualizer = new CircularCloudVisualizer(storage);
var pathFile = Path.Combine(Directory.GetCurrentDirectory(), TestContext.CurrentContext.Test.Name);
visualizer.CreateImage(pathFile);
TestContext.Out.WriteLine($"Tag cloud visualization saved to file {pathFile}");
}

[TestCase(0, 4, TestName = "WhenWidthZero")]
[TestCase(3, 0, TestName = "WhenHeightZero")]
[TestCase(-3, 4, TestName = "WhenWidthIsNegative")]
[TestCase(3, -4, TestName = "WhenHeightNegative")]
[TestCase(-3, -4, TestName = "WhenWidthAndHeightNegative")]
[TestCase(0, 0, TestName = "WhenWidthAndHeightIsZero")]
public void Insert_ShouldThrow(int width, int height)
{
var inCorrectSize = new Size(width, height);

Action act = () => layouter.PutNextRectangle(inCorrectSize);

act.Should().Throw<ArgumentException>();
}

[TestCaseSource(nameof(IntersectionTestsData))]
public void PutRectangleOnCircleWithoutIntersection_ShouldPutRectangleWithoutIntersection(Size[] sizes)
{
layouter = InsertFewRectangles(layouter, sizes, storage);
var last = layouter.PutRectangleWithoutIntersection(new Size(3, 3));

storage.Where(addedRectangle => addedRectangle.IntersectsWith(last)).Should().BeEmpty();
}

private static CircularCloudLayouter InsertFewRectangles(CircularCloudLayouter layouter, Size[] sizes,
List<Rectangle> storage)
{
for (var i = 0; i < sizes.Length; i++)
{
var forInsertion = layouter.PutNextRectangle(sizes[i]);
storage.Add(forInsertion);
}

return layouter;
}

[Test]
public void PutNextRectangle_ShouldTryMoveRectangleCloserToCenter_WhenItPossible()
{
var firstRectangleSize = new Size(6, 4);
var secondRectangleSize = new Size(4, 4);

var first = layouter.PutNextRectangle(firstRectangleSize);
var second = layouter.PutNextRectangle(secondRectangleSize);
var hasEqualBound = first.Right == second.Left || first.Top == second.Bottom
|| second.Right == first.Left || second.Top == first.Bottom;

first.IntersectsWith(second).Should().BeFalse();
hasEqualBound.Should().BeTrue();
}
}
43 changes: 43 additions & 0 deletions cs/TagCloudTests/CircularCloudLayouterVisualizationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using NUnit.Framework;
using TagsCloudVisualization;

namespace TagCloudTests;

public class CircularCloudLayouterVisualizationTests
{
private Point center;
private CircularCloudVisualizer visualizer;

[SetUp]
public void SetUp()
{
center = new Point(750, 750);
visualizer = new CircularCloudVisualizer(GenerateRectangles(center, 100, 30, 100));
}

[Test]
public void GenerateImage()
{
visualizer.CreateImage();
}

[Test]
public void GenerateImageWithSaveEveryStep()
{
visualizer.CreateImage(withSaveSteps: true);
}

private static List<Rectangle> GenerateRectangles(Point center, int maxSize, int minSize, int count)
{
var rnd = new Random();
var storage = new List<Rectangle>();
var layouter = CircularCloudLayouter.CreateLayouterWithStartRectangles(center, storage);
for (var i = 0; i < count; i++) layouter.PutNextRectangle(new Size(rnd.Next(minSize, maxSize),
rnd.Next(minSize, maxSize)));

return storage;
}
}
77 changes: 77 additions & 0 deletions cs/TagCloudTests/RTreeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using NUnit.Framework;
using FluentAssertions;

namespace TagsCloudVisualization.Tests
{
public class RTreeTests
{
[Test]
public void RTree_ShouldBeEmpty_AfterCreation()
{
var tree = new RTree();

tree.GetRectanglesForNode().Should().BeEmpty();
}

[Test]
public void Insert_ShouldAddRectangleToNode_AfterAdd()
{
var tree = new RTree();
var addedRectangle = new Rectangle(new Point(2, 2), new Size(3, 4));

tree.Insert(addedRectangle);

tree.GetRectanglesForNode().Should().HaveCount(1).And.Contain(addedRectangle);
}

[Test]
public void RTreeNodeBound_ShouldBeEqualFirstRectangle_AfterAdd()
{
var tree = new RTree();
var addedRectangle = new Rectangle(new Point(2, 2), new Size(3, 4));
var expectedBound = addedRectangle;


tree.Insert(addedRectangle);

tree.NodeBound.Should().Be(expectedBound);

}

[Test]
public void Insert_ShouldUpdateNodeBounds_AfterAddRectangle()
{
var tree = new RTree();
var addedRectangle1 = new Rectangle(new Point(2, 2), new Size(3, 4));
var addedRectangle2 = new Rectangle(new Point(5, 7), new Size(4, 2));
var expectedBound = new Rectangle(addedRectangle1.Location, new Size(7, 7));


tree.Insert(addedRectangle1);
tree.Insert(addedRectangle2);

tree.NodeBound.Should().Be(expectedBound);

}


private RTree InsertManyRectangles(int rectanglesCount, RTree tree, Rectangle first)
{
var prev = first;
var deltaCoor = Math.Max(first.Width, first.Height);
for (var _ = 0; _ < rectanglesCount; _++)
{
var newR = new Rectangle(new Point(prev.X + deltaCoor, prev.Y + deltaCoor), prev.Size);
tree.Insert(newR);
prev = newR;
}

return tree;
}
}
}
34 changes: 34 additions & 0 deletions cs/TagCloudTests/TagCloudTests.csproj
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>
Loading