Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions FirstKrFirstTry/FirstKrFirstTry.sln
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
6 changes: 6 additions & 0 deletions FirstKrFirstTry/FirstKrFirstTry/EmtpyQueueException.cs
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 {}
10 changes: 10 additions & 0 deletions FirstKrFirstTry/FirstKrFirstTry/FirstKrFirstTry.csproj
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>
9 changes: 9 additions & 0 deletions FirstKrFirstTry/FirstKrFirstTry/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace PriorityQueue;

public class Program

Choose a reason for hiding this comment

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

Это не надо сразу по двум причинам:

  • Можно было сделать очередь с приоритетами библиотекой (<OutputType>Library</OutputType> в .csproj), и тогда точка входа была бы не нужна вовсе.
  • Если бы точка входа была нужна, её лучше было реализовать с помощью top-level statements, без всяких class Program и Main

{
public static void Main()
{

}
}
84 changes: 84 additions & 0 deletions FirstKrFirstTry/FirstKrFirstTry/Queue.cs
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;

Choose a reason for hiding this comment

The 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)
{

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

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

nullability-анализ недоволен, сразу минус балл :)

{
item.Next = Head;
Head = item;

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

Заказывали свойство Empty, получили метод IsEmpty(). В реальной жизни у кого-то не собрался бы проект, и кто-то получил бы неустойку за ошибку в протоколе.


private class QueueElement
{
public QueueElement(int element, int priority){

Choose a reason for hiding this comment

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

Suggested change
public QueueElement(int element, int priority){
public QueueElement(int element, int priority) {

Element = element;
Priority = priority;
}


Choose a reason for hiding this comment

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

Лишняя пустая строчка

public int Element { get; set; }

public QueueElement Next{ get; set; }

Choose a reason for hiding this comment

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

Suggested change
public QueueElement Next{ get; set; }
public QueueElement Next { get; set; }


public int Priority { get; set; }

}
}
64 changes: 64 additions & 0 deletions FirstKrFirstTry/TestsForQueue/TestsForQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace TestsForQueue;

using PriorityQueue;

public class Tests
{
Queue queue;

Choose a reason for hiding this comment

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

Suggested change
Queue queue;
private Queue queue;


[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);

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

Это не нужно, есть же поле queue и SetUp, который его инициализирует. Сейчас получается, что это поле вообще не используется.

}

Choose a reason for hiding this comment

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

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

23 changes: 23 additions & 0 deletions FirstKrFirstTry/TestsForQueue/TestsForQueue.csproj
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>
1 change: 1 addition & 0 deletions FirstKrFirstTry/TestsForQueue/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;