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
58 changes: 58 additions & 0 deletions test2/test2.Test/QueueTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using NUnit.Framework;


namespace test2.Test
{
public class Tests
{
private Queue queue;

[SetUp]
public void Setup()
{
queue = new Queue();
queue.Enqueue(1, 1);
queue.Enqueue(9, 9);
queue.Enqueue(2, 2);
queue.Enqueue(5, 5);
queue.Enqueue(3, 3);
queue.Enqueue(4, 4);
queue.Enqueue(5, 10);
queue.Enqueue(7, 10);
queue.Enqueue(10, 10);
queue.Enqueue(7, 7);
queue.Enqueue(8, 8);
queue.Enqueue(6, 6);
}

[Test]
public void EnqueueTest()
{
var queueNew = new Queue();
Assert.IsTrue(queueNew.Empty());
queueNew.Enqueue(1, 4);
Assert.IsFalse(queueNew.Empty());
}

[Test]
public void DequeueTest()
{
int[] result1 = new int[4]{ 5, 7, 10, 9 };
var functionResult = queue.Dequeue();
Assert.AreEqual(result1[0], functionResult);
functionResult = queue.Dequeue();
Assert.AreEqual(result1[1], functionResult);
functionResult = queue.Dequeue();
Assert.AreEqual(result1[2], functionResult);
functionResult = queue.Dequeue();
Assert.AreEqual(result1[3], functionResult);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Честно говоря, не очень понятно, зачем-таки result. Можно было просто проверить на равенство 5, 7 и т.д.

}

[Test]
public void DequeueExceptionTest()
{
var queueException = new Queue();
Assert.Throws<QueueException>(() => queueException.Dequeue());
}
}
}
19 changes: 19 additions & 0 deletions test2/test2.Test/test2.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\test2\test2.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions test2/test2.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 16
VisualStudioVersion = 16.0.31205.134
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test2", "test2\test2.csproj", "{020B075D-FF1A-41BD-90FF-1A545F520156}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test2.Test", "test2.Test\test2.Test.csproj", "{65AEFBC3-3090-41B6-B4C3-F17B836DC26B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{020B075D-FF1A-41BD-90FF-1A545F520156}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{020B075D-FF1A-41BD-90FF-1A545F520156}.Debug|Any CPU.Build.0 = Debug|Any CPU
{020B075D-FF1A-41BD-90FF-1A545F520156}.Release|Any CPU.ActiveCfg = Release|Any CPU
{020B075D-FF1A-41BD-90FF-1A545F520156}.Release|Any CPU.Build.0 = Release|Any CPU
{65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65AEFBC3-3090-41B6-B4C3-F17B836DC26B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {29C9CA88-0E80-4B80-A756-321F9F4FBCC9}
EndGlobalSection
EndGlobal
32 changes: 32 additions & 0 deletions test2/test2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace test2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Пространства имён в .NET именуются с заглавной

{
class Program
{
static void Main()
{
var queue = new Queue();
queue.Enqueue(1, 1);
queue.Enqueue(9, 9);
queue.Enqueue(2, 2);
queue.Enqueue(5, 5);
queue.Enqueue(3, 3);
queue.Enqueue(4, 4);
queue.Enqueue(5, 10);
queue.Enqueue(7, 10);
queue.Enqueue(10, 10);
queue.Enqueue(7, 7);
queue.Enqueue(8, 8);
queue.Enqueue(6, 6);
var result = queue.Dequeue();
Console.WriteLine(result);
result = queue.Dequeue();
Console.WriteLine(result);
result = queue.Dequeue();
Console.WriteLine(result);
result = queue.Dequeue();
Console.WriteLine(result);
}
}
}
115 changes: 115 additions & 0 deletions test2/test2/Queue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;

namespace test2
{
/// <summary>
/// queue with priority
/// </summary>
public class Queue
{
/// <summary>
/// node on queue
/// </summary>
private class Node
{
public int Priority { get; set; }

public int Value { get; set; }

public Node Next { get; set; }
Comment on lines +15 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

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

Действительно ли им всем нужны public-сеттеры? Ведь если значение попадает в очередь, оно уже не меняется, да и приоритет его тоже.

public Node(int priority, int value)
{
Value = value;
Priority = priority;
Next = null;
}
}

private Node head;

private Node tail;

public Queue()
=> head = null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Он и так null, тем более что tail Вы не инициализируете :)



/// <summary>
/// add to queue
/// </summary>
public void Enqueue(int value, int priority)
{
if (head == null)
{
head = new Node(priority, value);
head.Next = null;
tail = head;
return;
}
var node = tail;
var newNode = new Node(priority, value);
tail = newNode;
node.Next = tail;
}

private Node runner;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Лучше все поля и все методы объявлять рядом, не перемешивая, тем более что конкретно у этой штуки нет ни одной причины быть полем (она всегда инициализируется в методе перед использованием, следовательно состояние объекта на самом деле не хранит, следовательно могла бы быть локальной переменной).


private int GetMaxPriority()
{
int priority = int.MinValue;
runner = head;
while (runner != null)
{
if (runner.Priority > priority)
{
priority = runner.Priority;
runner = runner.Next;
continue;
}
runner = runner.Next;
}
return priority;
}

/// <summary>
/// function remove from queue
/// </summary>
/// <returns>value's array with max priority</returns>
Copy link
Collaborator

Choose a reason for hiding this comment

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

"Массив значения", что? :)

public int Dequeue()
{
if (Empty())
{
throw new QueueException("Queue is Empty!");
}
int maxPriority = GetMaxPriority();
runner = head;
int result = 0;
Node previosNode = null;
while (runner.Next != null)
{
if (runner.Next.Priority == maxPriority)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Гм. Если самый большой приоритет прячется в голове очереди, что будет? Мы же его пропустим тут?

{
result = runner.Next.Value;;
runner.Next = runner.Next.Next;
previosNode = runner;
break;
}
runner = runner.Next;
}
if (runner.Priority == maxPriority && runner.Next == null)
{
result = runner.Next.Value;
Copy link
Collaborator

Choose a reason for hiding this comment

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

runner.Next == null же, иначе мы бы сюда не попали. runner.Next.Value --- это NullReferenceException

runner = null;
tail = previosNode;
}
return result;
}

/// <summary>
/// is queue empty?
/// </summary>
/// <returns>true - queue is empty</returns>
public bool Empty()
=> head == null;

}
}
20 changes: 20 additions & 0 deletions test2/test2/QueueException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test2
{
public class QueueException : Exception
Copy link
Collaborator

Choose a reason for hiding this comment

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

Тоже нужны комментарии

{
public QueueException()
{
}

public QueueException(string message)
: base(message)
{
}
}
}
8 changes: 8 additions & 0 deletions test2/test2/test2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>