-
Notifications
You must be signed in to change notification settings - Fork 0
First kr first try #14
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.4.33403.182 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirstKrFirstTry", "FirstKrFirstTry\FirstKrFirstTry.csproj", "{A3FCBF78-047E-4041-8702-06D2A789B8B3}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForQueue", "TestsForQueue\TestsForQueue.csproj", "{7E21A81B-A989-4A58-912F-B3ADA4467362}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {A3FCBF78-047E-4041-8702-06D2A789B8B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {A3FCBF78-047E-4041-8702-06D2A789B8B3}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {A3FCBF78-047E-4041-8702-06D2A789B8B3}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {A3FCBF78-047E-4041-8702-06D2A789B8B3}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {7E21A81B-A989-4A58-912F-B3ADA4467362}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {7E21A81B-A989-4A58-912F-B3ADA4467362}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {7E21A81B-A989-4A58-912F-B3ADA4467362}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {7E21A81B-A989-4A58-912F-B3ADA4467362}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {0DA28CAF-6796-472A-AF42-4830E4DF9DF0} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace PriorityQueue; | ||
|
|
||
| /// <summary> | ||
| /// An exception related to an empty queue | ||
| /// </summary> | ||
| public class EmtpyQueueException : Exception {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace PriorityQueue; | ||
|
|
||
| public class Program | ||
| { | ||
| public static void Main() | ||
| { | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||
| namespace PriorityQueue; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Queue with priority | ||||||
| /// </summary> | ||||||
| public class Queue | ||||||
| { | ||||||
| private QueueElement? Head; | ||||||
|
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. Поля именуются со строчной |
||||||
|
|
||||||
| /// <summary> | ||||||
| /// Add element in queue by priority | ||||||
| /// </summary> | ||||||
| /// <param name="element">Adding element</param> | ||||||
| public void Enqueue(int element, int priority) | ||||||
| { | ||||||
|
|
||||||
|
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. Лишняя пустая строчка, обычно после |
||||||
| if (IsEmpty()) | ||||||
| { | ||||||
| Head = new QueueElement(element, priority); | ||||||
| return; | ||||||
| } | ||||||
| var item = new QueueElement(element, priority); | ||||||
| if (Head.Priority < priority) | ||||||
|
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. nullability-анализ недоволен, сразу минус балл :) |
||||||
| { | ||||||
| item.Next = Head; | ||||||
| Head = item; | ||||||
|
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. Может, имеет смысл тут сделать return |
||||||
| } | ||||||
| var walker = Head; | ||||||
| while (walker.Next != null) | ||||||
| { | ||||||
| if (walker.Next.Priority < priority) | ||||||
| { | ||||||
| var data = walker.Next; | ||||||
| walker.Next = item; | ||||||
| item.Next = data; | ||||||
| return; | ||||||
| } | ||||||
| walker = walker.Next; | ||||||
| } | ||||||
| walker.Next = item; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Deleting first element with the highest priority | ||||||
| /// </summary> | ||||||
| /// <returns>Element with the highest priority</returns> | ||||||
| /// <exception cref="NullReferenceException">Exception if queue empty</exception> | ||||||
| public int Dequeue() | ||||||
| { | ||||||
| if (IsEmpty()) | ||||||
| { | ||||||
| throw new EmtpyQueueException(); | ||||||
| } | ||||||
| var walker = Head.Next; | ||||||
| var data = Head.Element; | ||||||
| Head = walker; | ||||||
| return data; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Checking if queue is null | ||||||
| /// </summary> | ||||||
| /// <returns>Returns true if queue null</returns> | ||||||
| public bool IsEmpty() | ||||||
| { | ||||||
| return Head == null; | ||||||
| } | ||||||
|
Comment on lines
+64
to
+67
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. Заказывали свойство Empty, получили метод IsEmpty(). В реальной жизни у кого-то не собрался бы проект, и кто-то получил бы неустойку за ошибку в протоколе. |
||||||
|
|
||||||
| private class QueueElement | ||||||
| { | ||||||
| public QueueElement(int element, int priority){ | ||||||
|
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.
Suggested change
|
||||||
| Element = element; | ||||||
| Priority = priority; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
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. Лишняя пустая строчка |
||||||
| public int Element { get; set; } | ||||||
|
|
||||||
| public QueueElement Next{ get; set; } | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| public int Priority { get; set; } | ||||||
|
|
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||
| namespace TestsForQueue; | ||||||
|
|
||||||
| using PriorityQueue; | ||||||
|
|
||||||
| public class Tests | ||||||
| { | ||||||
| Queue queue; | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| [SetUp] | ||||||
| public void Setup() | ||||||
| { | ||||||
| queue = new Queue(); | ||||||
| } | ||||||
|
|
||||||
| [TestCaseSource(nameof(QueueForTest))] | ||||||
| public void QueueShouldBeEmptyWhenCreated(Queue queue) | ||||||
| { | ||||||
| Assert.True(queue.IsEmpty()); | ||||||
| } | ||||||
|
|
||||||
| [TestCaseSource(nameof(QueueForTest))] | ||||||
| public void QueueShouldThrowExceptionWhenDeletingFromAnEmptyQueue(Queue queue) | ||||||
| { | ||||||
| Assert.Throws<EmtpyQueueException>(() => queue.Dequeue()); | ||||||
| } | ||||||
|
|
||||||
| [TestCaseSource(nameof(QueueForTest))] | ||||||
| public void QueueShouldNotBeEmptyWhenAddedSomething(Queue queue) | ||||||
| { | ||||||
| queue.Enqueue(12, 1); | ||||||
| Assert.False(queue.IsEmpty()); | ||||||
| } | ||||||
|
|
||||||
| [TestCaseSource(nameof(QueueForTest))] | ||||||
| public void QueueShouldReturnValueWhenAddedSomething(Queue queue) | ||||||
| { | ||||||
| queue.Enqueue(12, 1); | ||||||
| Assert.True(queue.Dequeue() == 12); | ||||||
|
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. Assert.AreEqual или Assert.That |
||||||
| } | ||||||
|
|
||||||
| [TestCaseSource(nameof(QueueForTest))] | ||||||
| public void QueueShouldReturnValueWithTheHighestPriorityWhenAddedSomething(Queue queue) | ||||||
| { | ||||||
| queue.Enqueue(11, 1); | ||||||
| queue.Enqueue(16, 2); | ||||||
| queue.Enqueue(12, 1); | ||||||
| Assert.True(queue.Dequeue() == 16); | ||||||
| } | ||||||
|
|
||||||
| [TestCaseSource(nameof(QueueForTest))] | ||||||
| public void QueueShouldReturnFirstValueWhenPriorityTheSame(Queue queue) | ||||||
| { | ||||||
| queue.Enqueue(15, 1); | ||||||
| queue.Enqueue(13, 2); | ||||||
| queue.Enqueue(14, 2); | ||||||
| Assert.True(queue.Dequeue() == 13); | ||||||
| } | ||||||
|
|
||||||
| private static IEnumerable<TestCaseData> QueueForTest | ||||||
| => new TestCaseData[] | ||||||
| { | ||||||
| new TestCaseData(new Queue()), | ||||||
| }; | ||||||
|
Comment on lines
+59
to
+63
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. Это не нужно, есть же поле queue и SetUp, который его инициализирует. Сейчас получается, что это поле вообще не используется. |
||||||
| } | ||||||
|
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,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
| <PackageReference Include="NUnit" Version="3.13.3" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="3.3.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\FirstKrFirstTry\FirstKrFirstTry.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| global using NUnit.Framework; |
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.
Это не надо сразу по двум причинам:
<OutputType>Library</OutputType>в .csproj), и тогда точка входа была бы не нужна вовсе.