From cdb5125cb4236a56fa8a5d5955c068e57a901d8f Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 4 Apr 2023 15:51:36 +0300 Subject: [PATCH 01/10] =?UTF-8?q?=D0=A1=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D1=83=D1=80=D0=B0=20=20=D0=B3=D1=80=D0=B0=D1=84=D0=B0=20+=20?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D0=B0=D0=BB=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D1=82=D0=BC=D0=B0=20=D0=A4=D0=BB=D0=BE=D0=B9=D0=B4?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers.sln | 25 +++ Routers/Routers/Graph.cs | 67 ++++++++ Routers/Routers/GraphElement.cs | 14 ++ Routers/Routers/InvalidPositionException.cs | 3 + Routers/Routers/List.cs | 170 ++++++++++++++++++++ Routers/Routers/NullPointerException.cs | 3 + Routers/Routers/Program.cs | 16 ++ Routers/Routers/Routers.csproj | 10 ++ 8 files changed, 308 insertions(+) create mode 100644 Routers/Routers.sln create mode 100644 Routers/Routers/Graph.cs create mode 100644 Routers/Routers/GraphElement.cs create mode 100644 Routers/Routers/InvalidPositionException.cs create mode 100644 Routers/Routers/List.cs create mode 100644 Routers/Routers/NullPointerException.cs create mode 100644 Routers/Routers/Program.cs create mode 100644 Routers/Routers/Routers.csproj diff --git a/Routers/Routers.sln b/Routers/Routers.sln new file mode 100644 index 0000000..5f8f81e --- /dev/null +++ b/Routers/Routers.sln @@ -0,0 +1,25 @@ + +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}") = "Routers", "Routers\Routers.csproj", "{EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {18B17EA7-5909-4E96-B776-65626F45E146} + EndGlobalSection +EndGlobal diff --git a/Routers/Routers/Graph.cs b/Routers/Routers/Graph.cs new file mode 100644 index 0000000..9f9c8dc --- /dev/null +++ b/Routers/Routers/Graph.cs @@ -0,0 +1,67 @@ +using System.Reflection.Metadata; + +namespace Routers; + +public class Graph +{ + public void AddVertexWithBandwidthSize(int fromVertex, int toVertex, int bandwidthSize) + { + if (GraphByList == null || GraphByList.ListWithVertexes == null) + { + throw new NullPointerException(); + } + GraphByList.ListWithVertexes.AddAnArr(GraphByList.ListWithVertexes, fromVertex, toVertex, bandwidthSize); + } + + public void AddStandartListWithAllVertexes(int sizeVertexes) + { + GraphByList = new ListVertexes(); + GraphByList.SizeGraph = sizeVertexes; + GraphByList.ListWithVertexes = new List(); + for(int i = 1; i < sizeVertexes + 1; i++) + { + GraphByList.ListWithVertexes.AddElement(GraphByList.ListWithVertexes, i, 0); + } + } + + public void PrintGraph() + { + if (GraphByList == null || GraphByList.ListWithVertexes == null) + { + throw new NullPointerException(); + } + GraphByList.ListWithVertexes.PrintGraphByList(GraphByList.ListWithVertexes); + } + + public void SortListsInGraph() + { + ; + } + + public void FloydsAlgorithm() + { + SortListsInGraph(); + if (GraphByList == null || GraphByList.ListWithVertexes == null) + { + throw new NullPointerException(); + } + int sizeGraph = GraphByList.SizeGraph; + for(int k = 0; k < sizeGraph; ++k) + { + for(int i = 0; i < sizeGraph; ++i) + { + for(int j = 0; j < sizeGraph; ++j) + { + + } + } + } + } + + private class ListVertexes + { + public List? ListWithVertexes { get; set; } + public int SizeGraph { get; set; } + } + private ListVertexes? GraphByList { get; set; } +} diff --git a/Routers/Routers/GraphElement.cs b/Routers/Routers/GraphElement.cs new file mode 100644 index 0000000..7033e9f --- /dev/null +++ b/Routers/Routers/GraphElement.cs @@ -0,0 +1,14 @@ +namespace Routers; + +public class GraphElement +{ + public GraphElement(int vertex, int bandwidthSize) + { + Vertex = vertex; + BandwidthSize = bandwidthSize; + } + + public int Vertex { get; set; } + public List Vertexes { get; set; } + public int BandwidthSize { get; set; } +} \ No newline at end of file diff --git a/Routers/Routers/InvalidPositionException.cs b/Routers/Routers/InvalidPositionException.cs new file mode 100644 index 0000000..d66eefb --- /dev/null +++ b/Routers/Routers/InvalidPositionException.cs @@ -0,0 +1,3 @@ +namespace Routers; + +public class InvalidPositionException : Exception { } \ No newline at end of file diff --git a/Routers/Routers/List.cs b/Routers/Routers/List.cs new file mode 100644 index 0000000..e14a806 --- /dev/null +++ b/Routers/Routers/List.cs @@ -0,0 +1,170 @@ +namespace Routers; + +// Container for storing values +public class List +{ + private ListElement? Head; + private ListElement? Tail; + + // Adding element to list + public void AddElement(List list, int value, int bandwidthSize) + { + ListElement item = new ListElement(); + item.Value = new GraphElement(value, bandwidthSize); + if (list.Head == null) + { + list.Head = item; + list.Tail = item; + } + else + { + list.Tail.Next = item; + list.Tail = item; + } + } + + public void AddAnArr(List list, int firstVertex, int secondVertex, int sizeArr) + { + var walkerFirstVertex = list.Head; + while(walkerFirstVertex != null && walkerFirstVertex.Value.Vertex != firstVertex) + { + walkerFirstVertex = walkerFirstVertex.Next; + } + if (walkerFirstVertex == null) + { + throw new InvalidPositionException(); + } + if (walkerFirstVertex.Value.Vertexes == null) + { + walkerFirstVertex.Value.Vertexes = new List(); + } + AddElement(walkerFirstVertex.Value.Vertexes, secondVertex, sizeArr); + var walkerSecondVertex = list.Head; + while(walkerSecondVertex != null && walkerSecondVertex.Value.Vertex != secondVertex) + { + walkerSecondVertex = walkerSecondVertex.Next; + } + if (walkerSecondVertex == null) + { + throw new InvalidPositionException(); + } + if (walkerSecondVertex.Value.Vertexes == null) + { + walkerSecondVertex.Value.Vertexes = new List(); + } + AddElement(walkerSecondVertex.Value.Vertexes, firstVertex, sizeArr); + } + + public void PrintGraphByList(List mainVertexes) + { + var walker = mainVertexes.Head; + while (walker != null) + { + Console.Write(walker.Value.Vertex); + Console.Write(": "); + if (walker.Value.Vertexes != null) + { + var walkerAnotherVertexes = walker.Value.Vertexes.Head; + while (walkerAnotherVertexes != null) + { + Console.Write(walkerAnotherVertexes.Value.Vertex); + Console.Write("("); + Console.Write(walkerAnotherVertexes.Value.BandwidthSize); + Console.Write(") "); + walkerAnotherVertexes = walkerAnotherVertexes.Next; + } + } + Console.WriteLine(); + walker = walker.Next; + } + } + + /* + // Deleting element in list + virtual public void RemoveElement(ref int item) + { + if (Head == null || Tail == null) + { + throw new NullPointerException(); + } + if (Tail == Head) + { + item = Tail.Value; + Tail = null; + Head = null; + return; + } + --Head.SizeList; + ListElement walker = Head; + while (walker.Next.Next != null) + { + walker = walker.Next; + } + item = walker.Next.Value; + Tail = walker; + walker.Next = null; + } + + // Print all elements in list + virtual public void PrintTheElements() + { + ListElement walker = Head; + while (walker != null) + { + Console.Write(walker.Position); + Console.Write(' '); + Console.WriteLine(walker.Value); + walker = walker.Next; + } + } + + // Change value by position + virtual public void ChangeValueByPosition(int position, int newValue) + { + if (Head == null || Tail == null) + { + throw new NullPointerException(); + } + ListElement? walker = Head; + while (walker != null && walker.Position != position) + { + walker = walker.Next; + } + if (walker != null) + { + walker.Value = newValue; + } + } + + // Checks if the list is empty + virtual public bool IsEmpty() + { + return Head == null; + } + + // Returns the value by position + virtual public int ReturnValueByPosition(int Position) + { + if (Head == null || Tail == null) + { + throw new NullPointerException(); + } + var walker = Head; + while (walker != null && walker.Position != Position) + { + walker = walker.Next; + } + if (walker == null) + { + throw new InvalidPositionException(); + } + return walker.Value; + } + */ + + private class ListElement + { + public GraphElement Value { get; set; } + public ListElement Next { get; set; } + } +} \ No newline at end of file diff --git a/Routers/Routers/NullPointerException.cs b/Routers/Routers/NullPointerException.cs new file mode 100644 index 0000000..3b1ac23 --- /dev/null +++ b/Routers/Routers/NullPointerException.cs @@ -0,0 +1,3 @@ +namespace Routers; + +public class NullPointerException : Exception { } \ No newline at end of file diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs new file mode 100644 index 0000000..83ebbc2 --- /dev/null +++ b/Routers/Routers/Program.cs @@ -0,0 +1,16 @@ +namespace ListAndUniqueList; + +using Routers; +using System; +using System.Net.WebSockets; + +class Program +{ + public static void Main(string[] args) + { + var graph = new Graph(); + graph.AddStandartListWithAllVertexes(10); + graph.AddVertexWithBandwidthSize(2, 3, 54); + graph.PrintGraph(); + } +} \ No newline at end of file diff --git a/Routers/Routers/Routers.csproj b/Routers/Routers/Routers.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/Routers/Routers/Routers.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + From ffbc5bf27c141360ba51551b6c9791b1ad64f42f Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 5 Apr 2023 23:39:01 +0300 Subject: [PATCH 02/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83?= =?UTF-8?q?=D1=80=D1=8B=20=D0=B8=20=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=D0=B0=20?= =?UTF-8?q?=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers/Graph.cs | 86 +++++---- Routers/Routers/GraphElement.cs | 14 -- Routers/Routers/InvalidFileException.cs | 3 + Routers/Routers/InvalidGraphException.cs | 3 + Routers/Routers/InvalidPositionException.cs | 3 + Routers/Routers/List.cs | 170 ----------------- Routers/Routers/ListArcs.cs | 194 ++++++++++++++++++++ Routers/Routers/ListVertexes.cs | 98 ++++++++++ Routers/Routers/NullPointerException.cs | 3 + Routers/Routers/Program.cs | 6 +- Routers/Routers/Routers.cs | 150 +++++++++++++++ Routers/Routers/Routers.csproj | 4 + 12 files changed, 510 insertions(+), 224 deletions(-) delete mode 100644 Routers/Routers/GraphElement.cs create mode 100644 Routers/Routers/InvalidFileException.cs create mode 100644 Routers/Routers/InvalidGraphException.cs delete mode 100644 Routers/Routers/List.cs create mode 100644 Routers/Routers/ListArcs.cs create mode 100644 Routers/Routers/ListVertexes.cs create mode 100644 Routers/Routers/Routers.cs diff --git a/Routers/Routers/Graph.cs b/Routers/Routers/Graph.cs index 9f9c8dc..8a013ab 100644 --- a/Routers/Routers/Graph.cs +++ b/Routers/Routers/Graph.cs @@ -1,67 +1,81 @@ using System.Reflection.Metadata; +using System.Linq; +using System.Collections.Generic; namespace Routers; public class Graph { - public void AddVertexWithBandwidthSize(int fromVertex, int toVertex, int bandwidthSize) + public void WriteToFile(string filePath) { - if (GraphByList == null || GraphByList.ListWithVertexes == null) + if (IsEmpty()) { throw new NullPointerException(); } - GraphByList.ListWithVertexes.AddAnArr(GraphByList.ListWithVertexes, fromVertex, toVertex, bandwidthSize); + GraphByList.Arcs.WirteToFile(filePath); } - public void AddStandartListWithAllVertexes(int sizeVertexes) + public ListArcs ReturnListArcs() { - GraphByList = new ListVertexes(); - GraphByList.SizeGraph = sizeVertexes; - GraphByList.ListWithVertexes = new List(); - for(int i = 1; i < sizeVertexes + 1; i++) - { - GraphByList.ListWithVertexes.AddElement(GraphByList.ListWithVertexes, i, 0); - } + return !IsEmpty() ? GraphByList.Arcs : null; } - public void PrintGraph() + public ListVertexes ReturnListVertexes() { - if (GraphByList == null || GraphByList.ListWithVertexes == null) - { - throw new NullPointerException(); - } - GraphByList.ListWithVertexes.PrintGraphByList(GraphByList.ListWithVertexes); + return !IsEmpty() ? GraphByList.Vertexes : null; } - public void SortListsInGraph() + public bool IsEmpty() { - ; + return GraphByList == null || GraphByList.Arcs == null || GraphByList.Vertexes == null; } - public void FloydsAlgorithm() + public void AddArcs(int fromVertex, int toVertex, int sizeWay) { - SortListsInGraph(); - if (GraphByList == null || GraphByList.ListWithVertexes == null) + if(GraphByList == null) { - throw new NullPointerException(); + GraphByList = new GraphElement(); } - int sizeGraph = GraphByList.SizeGraph; - for(int k = 0; k < sizeGraph; ++k) + if (GraphByList.Arcs == null) { - for(int i = 0; i < sizeGraph; ++i) - { - for(int j = 0; j < sizeGraph; ++j) - { + GraphByList.Arcs = new ListArcs(); + } + GraphByList.Arcs.AddElement(fromVertex, toVertex, sizeWay); + } - } - } + public void AddVertexes(int sizeGraph) + { + if (GraphByList == null) + { + GraphByList = new GraphElement(); } + if (GraphByList.Vertexes == null) + { + GraphByList.Vertexes = new ListVertexes(); + } + GraphByList.sizeGraph = sizeGraph; + for(int i = 1; i <= sizeGraph; i++) + { + GraphByList.Vertexes.AddElement(i); + } + } + + public bool KraskalAlgorithm(Graph graph) + { + if (graph.IsEmpty()) + { + throw new NullPointerException(); + } + return graph.GraphByList.Arcs.KraskalAlgorithm(graph); } - private class ListVertexes + private class GraphElement { - public List? ListWithVertexes { get; set; } - public int SizeGraph { get; set; } + public ListVertexes? Vertexes { get; set; } + + public ListArcs? Arcs { get; set; } + + public int sizeGraph { get; set; } } - private ListVertexes? GraphByList { get; set; } -} + private GraphElement? GraphByList { get; set; } +} \ No newline at end of file diff --git a/Routers/Routers/GraphElement.cs b/Routers/Routers/GraphElement.cs deleted file mode 100644 index 7033e9f..0000000 --- a/Routers/Routers/GraphElement.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Routers; - -public class GraphElement -{ - public GraphElement(int vertex, int bandwidthSize) - { - Vertex = vertex; - BandwidthSize = bandwidthSize; - } - - public int Vertex { get; set; } - public List Vertexes { get; set; } - public int BandwidthSize { get; set; } -} \ No newline at end of file diff --git a/Routers/Routers/InvalidFileException.cs b/Routers/Routers/InvalidFileException.cs new file mode 100644 index 0000000..3dbbb8a --- /dev/null +++ b/Routers/Routers/InvalidFileException.cs @@ -0,0 +1,3 @@ +namespace Routers; + +public class InvalidFileException : Exception {} \ No newline at end of file diff --git a/Routers/Routers/InvalidGraphException.cs b/Routers/Routers/InvalidGraphException.cs new file mode 100644 index 0000000..01c6f10 --- /dev/null +++ b/Routers/Routers/InvalidGraphException.cs @@ -0,0 +1,3 @@ +namespace Routers; + +public class InvalidGraphException : Exception {} \ No newline at end of file diff --git a/Routers/Routers/InvalidPositionException.cs b/Routers/Routers/InvalidPositionException.cs index d66eefb..1eb2b91 100644 --- a/Routers/Routers/InvalidPositionException.cs +++ b/Routers/Routers/InvalidPositionException.cs @@ -1,3 +1,6 @@ namespace Routers; +/// +/// Exception for incorrect position +/// public class InvalidPositionException : Exception { } \ No newline at end of file diff --git a/Routers/Routers/List.cs b/Routers/Routers/List.cs deleted file mode 100644 index e14a806..0000000 --- a/Routers/Routers/List.cs +++ /dev/null @@ -1,170 +0,0 @@ -namespace Routers; - -// Container for storing values -public class List -{ - private ListElement? Head; - private ListElement? Tail; - - // Adding element to list - public void AddElement(List list, int value, int bandwidthSize) - { - ListElement item = new ListElement(); - item.Value = new GraphElement(value, bandwidthSize); - if (list.Head == null) - { - list.Head = item; - list.Tail = item; - } - else - { - list.Tail.Next = item; - list.Tail = item; - } - } - - public void AddAnArr(List list, int firstVertex, int secondVertex, int sizeArr) - { - var walkerFirstVertex = list.Head; - while(walkerFirstVertex != null && walkerFirstVertex.Value.Vertex != firstVertex) - { - walkerFirstVertex = walkerFirstVertex.Next; - } - if (walkerFirstVertex == null) - { - throw new InvalidPositionException(); - } - if (walkerFirstVertex.Value.Vertexes == null) - { - walkerFirstVertex.Value.Vertexes = new List(); - } - AddElement(walkerFirstVertex.Value.Vertexes, secondVertex, sizeArr); - var walkerSecondVertex = list.Head; - while(walkerSecondVertex != null && walkerSecondVertex.Value.Vertex != secondVertex) - { - walkerSecondVertex = walkerSecondVertex.Next; - } - if (walkerSecondVertex == null) - { - throw new InvalidPositionException(); - } - if (walkerSecondVertex.Value.Vertexes == null) - { - walkerSecondVertex.Value.Vertexes = new List(); - } - AddElement(walkerSecondVertex.Value.Vertexes, firstVertex, sizeArr); - } - - public void PrintGraphByList(List mainVertexes) - { - var walker = mainVertexes.Head; - while (walker != null) - { - Console.Write(walker.Value.Vertex); - Console.Write(": "); - if (walker.Value.Vertexes != null) - { - var walkerAnotherVertexes = walker.Value.Vertexes.Head; - while (walkerAnotherVertexes != null) - { - Console.Write(walkerAnotherVertexes.Value.Vertex); - Console.Write("("); - Console.Write(walkerAnotherVertexes.Value.BandwidthSize); - Console.Write(") "); - walkerAnotherVertexes = walkerAnotherVertexes.Next; - } - } - Console.WriteLine(); - walker = walker.Next; - } - } - - /* - // Deleting element in list - virtual public void RemoveElement(ref int item) - { - if (Head == null || Tail == null) - { - throw new NullPointerException(); - } - if (Tail == Head) - { - item = Tail.Value; - Tail = null; - Head = null; - return; - } - --Head.SizeList; - ListElement walker = Head; - while (walker.Next.Next != null) - { - walker = walker.Next; - } - item = walker.Next.Value; - Tail = walker; - walker.Next = null; - } - - // Print all elements in list - virtual public void PrintTheElements() - { - ListElement walker = Head; - while (walker != null) - { - Console.Write(walker.Position); - Console.Write(' '); - Console.WriteLine(walker.Value); - walker = walker.Next; - } - } - - // Change value by position - virtual public void ChangeValueByPosition(int position, int newValue) - { - if (Head == null || Tail == null) - { - throw new NullPointerException(); - } - ListElement? walker = Head; - while (walker != null && walker.Position != position) - { - walker = walker.Next; - } - if (walker != null) - { - walker.Value = newValue; - } - } - - // Checks if the list is empty - virtual public bool IsEmpty() - { - return Head == null; - } - - // Returns the value by position - virtual public int ReturnValueByPosition(int Position) - { - if (Head == null || Tail == null) - { - throw new NullPointerException(); - } - var walker = Head; - while (walker != null && walker.Position != Position) - { - walker = walker.Next; - } - if (walker == null) - { - throw new InvalidPositionException(); - } - return walker.Value; - } - */ - - private class ListElement - { - public GraphElement Value { get; set; } - public ListElement Next { get; set; } - } -} \ No newline at end of file diff --git a/Routers/Routers/ListArcs.cs b/Routers/Routers/ListArcs.cs new file mode 100644 index 0000000..fa6eaa5 --- /dev/null +++ b/Routers/Routers/ListArcs.cs @@ -0,0 +1,194 @@ +using System.Runtime.InteropServices; + +namespace Routers; + +public class ListArcs +{ + private ListElement? Head; + private ListElement? Tail; + + private bool SortByVertexOrArcs(bool sortByVertex, ListElement firstListElement, ListElement secondListElement) + { + return sortByVertex ? firstListElement.ToVertex > secondListElement.ToVertex : firstListElement.SizeWay < secondListElement.SizeWay; + } + + + // Sort List Arcs + public void SortListArcs(bool sortByVertex) + { + if (Head == null) + { + throw new NullPointerException(); + } + int size = Head.SizeList; + for(int i = 0; i < size; ++i) + { + var walker = Head; + for(int j = 0; j < size; ++j) + { + if (walker.Next != null && SortByVertexOrArcs(sortByVertex, walker, walker.Next)) + { + var item = new ListElement(walker.FromVertex, walker.ToVertex, walker.SizeWay); + walker.SizeWay = walker.Next.SizeWay; + walker.FromVertex = walker.Next.FromVertex; + walker.ToVertex = walker.Next.ToVertex; + walker.Next.SizeWay = item.SizeWay; + walker.Next.ToVertex = item.ToVertex; + walker.Next.FromVertex = item.FromVertex; + } + walker = walker.Next; + } + } + } + + private void DeleteArc(int fromVertex, int toVertex) + { + if (Head == null) + { + throw new NullPointerException(); + } + var walker = Head; + if (walker.FromVertex == fromVertex && walker.ToVertex == toVertex) + { + Head = Head.Next; + return; + } + while (walker.Next != null) + { + if (walker.Next.FromVertex == fromVertex && walker.Next.ToVertex == toVertex) + { + walker.Next = walker.Next.Next; + return; + } + walker = walker.Next; + } + } + + public bool KraskalAlgorithm(Graph graph) + { + if (graph == null || graph.IsEmpty()) + { + throw new NullPointerException(); + } + var listArcs = graph.ReturnListArcs(); + var listVertexes = graph.ReturnListVertexes(); + listArcs.SortListArcs(false); + int sizeGraph = listArcs.Head.SizeList; + var walker = listArcs.Head; + int numberPlenty = 1; + int i = 1; + while (walker != null) + { + if (walker.ToVertex != walker.FromVertex) + { + int returnedNumberPlentyFirstVertex = listVertexes.FromWichSet(walker.FromVertex); + int returnedNumberPlentySecondVertex = listVertexes.FromWichSet(walker.ToVertex); + + if (returnedNumberPlentyFirstVertex != returnedNumberPlentySecondVertex) + { + if (returnedNumberPlentyFirstVertex == 0 && returnedNumberPlentySecondVertex != 0) + { + listVertexes.ChangeOneVertexSet(walker.FromVertex, returnedNumberPlentySecondVertex); + } + else if (returnedNumberPlentyFirstVertex != 0 && returnedNumberPlentySecondVertex == 0) + { + listVertexes.ChangeOneVertexSet(walker.ToVertex, returnedNumberPlentyFirstVertex); + } + else + { + listVertexes.ChangeNumbersSet(returnedNumberPlentyFirstVertex, returnedNumberPlentySecondVertex); + } + ++i; + } + else if (returnedNumberPlentyFirstVertex == 0) + { + listVertexes.ChangeOneVertexSet(walker.FromVertex, numberPlenty); + listVertexes.ChangeOneVertexSet(walker.ToVertex, numberPlenty); + ++numberPlenty; + i += 2; + } + else + { + DeleteArc(walker.FromVertex, walker.ToVertex); + --Head.SizeList; + } + } + else + { + DeleteArc(walker.FromVertex, walker.ToVertex); + --Head.SizeList; + } + walker = walker.Next; + } + SortListArcs(true); + return !(walker == null && i <= sizeGraph); + } + + // Adding element to list + public void AddElement(int fromVertex, int toVertex, int sizeWay) + { + var item = new ListElement(fromVertex, toVertex, sizeWay); + if (Head == null) + { + Head = item; + } + else + { + Tail.Next = item; + } + Tail = item; + ++Head.SizeList; + } + + public void WirteToFile(string filePath) + { + filePath = filePath + ".new"; + StreamWriter file = new StreamWriter(filePath); + var walker = Head; + int previousMainVertex = 0; + bool isFirst = true; + while (walker != null) + { + if (walker.FromVertex != previousMainVertex) + { + if (!isFirst) + { + file.WriteLine(); + } + file.Write(walker.FromVertex); + file.Write(": "); + previousMainVertex = walker.FromVertex; + isFirst = false; + } + else + { + file.Write(", "); + } + file.Write(walker.ToVertex); + file.Write(" ("); + file.Write(walker.SizeWay); + file.Write(")"); + walker = walker.Next; + } + file.Close(); + } + + private class ListElement + { + public ListElement(int fromVertex, int toVertex, int sizeWay) + { + FromVertex = fromVertex; + ToVertex = toVertex; + SizeWay = sizeWay; + } + + public int FromVertex { get; set; } + + public int ToVertex { get; set; } + public ListElement? Next { get; set; } + + public int SizeWay { get; set; } + + public int SizeList { get; set; } + } +} diff --git a/Routers/Routers/ListVertexes.cs b/Routers/Routers/ListVertexes.cs new file mode 100644 index 0000000..787c4f2 --- /dev/null +++ b/Routers/Routers/ListVertexes.cs @@ -0,0 +1,98 @@ +namespace Routers; + +/// summary +/// Container for storing vertices and their belonging to a certain set +/// +public class ListVertexes +{ + private ListElement? Head; + private ListElement? Tail; + + /// + /// Change number set for all vertexes with previous number set in list + /// + /// Number new set for change + /// Number previous set + public void ChangeNumbersSet(int numberNewSet, int numberPreviousSet) + { + var walker = Head; + while(walker != null) + { + if (walker.InWichSet == numberPreviousSet) + { + walker.InWichSet = numberNewSet; + } + walker = walker.Next; + } + } + + /// + /// Change number set for one vertex + /// + /// The vertex where we change the set value + /// New set number + public void ChangeOneVertexSet(int vertex, int numberSet) + { + var walker = Head; + while(walker != null) + { + if (walker.Vertex == vertex) + { + walker.InWichSet = numberSet; + } + walker = walker.Next; + } + } + + /// + /// Returns the set number for a specific vertex + /// + /// The vertex for which we return + /// Return -1 if not found and the set number if found + public int FromWichSet(int vertex) + { + var walker = Head; + while(walker != null) + { + if (walker.Vertex == vertex) + { + return walker.InWichSet; + } + walker = walker.Next; + } + return -1; + } + + /// + /// Adds a new item to the list + /// + /// The number of the vertex to be added to the list + virtual public void AddElement(int vertex) + { + if (Head == null) + { + var item = new ListElement(vertex); + Head = item; + Tail = item; + } + else + { + var item = new ListElement(vertex); + Tail.Next = item; + Tail = item; + } + } + + private class ListElement + { + public ListElement(int vertex) + { + Vertex = vertex; + } + + public int Vertex { get; set; } + public ListElement? Next { get; set; } + + public int InWichSet { get; set; } + } +} diff --git a/Routers/Routers/NullPointerException.cs b/Routers/Routers/NullPointerException.cs index 3b1ac23..b1a6a39 100644 --- a/Routers/Routers/NullPointerException.cs +++ b/Routers/Routers/NullPointerException.cs @@ -1,3 +1,6 @@ namespace Routers; +/// +/// Exception for an empty pointer +/// public class NullPointerException : Exception { } \ No newline at end of file diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs index 83ebbc2..0b53354 100644 --- a/Routers/Routers/Program.cs +++ b/Routers/Routers/Program.cs @@ -8,9 +8,7 @@ class Program { public static void Main(string[] args) { - var graph = new Graph(); - graph.AddStandartListWithAllVertexes(10); - graph.AddVertexWithBandwidthSize(2, 3, 54); - graph.PrintGraph(); + var routers = new Routers(); + routers.WorkWithFile("C:\\Users\\User\\Downloads\\graph.txt"); } } \ No newline at end of file diff --git a/Routers/Routers/Routers.cs b/Routers/Routers/Routers.cs new file mode 100644 index 0000000..cff67c6 --- /dev/null +++ b/Routers/Routers/Routers.cs @@ -0,0 +1,150 @@ +namespace Routers; + +public class Routers +{ + + private void WriteToFile(Graph graph, string filePath) + { + graph.WriteToFile(filePath); + } + + public bool WorkWithFile(string filePath) + { + var file = new StreamReader(filePath); + // Проверка на файл + string line = "\0"; + int mainVertex = 0; + int anotherVertex = 0; + int sizeArc = 0; + int theBiggestVertex = 0; + var graph = new Graph(); + while (!file.EndOfStream) + { + line = file.ReadLine(); + bool isFirst = true; + for(int i = 0; i < line.Length; ++i) + { + if (isFirst && Char.IsDigit(line[i])) + { + mainVertex = line[i] - 48; + ++i; + while (Char.IsDigit(line[i])) + { + mainVertex = mainVertex * 10 + line[i] - 48; + ++i; + } + + if (line[i] != ':') + { + file.Close(); + throw new InvalidFileException(); + } + ++i; + if (line[i] != ' ') + { + file.Close(); + throw new InvalidFileException(); + } + isFirst = false; + } + else if (isFirst) + { + file.Close(); + throw new InvalidFileException(); + } + else + { + if (Char.IsDigit(line[i])) + { + anotherVertex = line[i] - 48; + ++i; + while (Char.IsDigit(line[i])) + { + anotherVertex = anotherVertex * 10 + line[i] - 48; + ++i; + } + if (line[i] != ' ') + { + file.Close(); + throw new InvalidFileException(); + } + ++i; + if (line[i] == '(') + { + ++i; + if (!Char.IsDigit(line[i])) + { + file.Close(); + throw new InvalidFileException(); + } + sizeArc = line[i] - 48; + ++i; + while (Char.IsDigit(line[i])) + { + sizeArc = sizeArc * 10 + line[i] - 48; + ++i; + } + + if (theBiggestVertex < mainVertex) + { + theBiggestVertex = mainVertex; + } + + if (theBiggestVertex < anotherVertex) + { + theBiggestVertex = anotherVertex; + } + + graph.AddArcs(mainVertex, anotherVertex, sizeArc); + + if (line[i] != ')') + { + file.Close(); + throw new InvalidFileException(); + } + ++i; + if (i < line.Length) + { + if (line[i] != ',') + { + file.Close(); + throw new InvalidFileException(); + } + ++i; + if (line[i] != ' ') + { + file.Close(); + throw new InvalidFileException(); + } + } + } + else + { + file.Close(); + throw new InvalidFileException(); + } + } + else + { + file.Close(); + throw new InvalidFileException(); + } + } + anotherVertex = 0; + sizeArc = 0; + } + mainVertex = 0; + } + file.Close(); + + graph.AddVertexes(theBiggestVertex); + if (!graph.KraskalAlgorithm(graph)) + { + return false; + } + + graph.WriteToFile(filePath); + + return true; + } +} diff --git a/Routers/Routers/Routers.csproj b/Routers/Routers/Routers.csproj index f02677b..91f3888 100644 --- a/Routers/Routers/Routers.csproj +++ b/Routers/Routers/Routers.csproj @@ -7,4 +7,8 @@ enable + + + + From a675cc4f9632386d4ac2267c8758915eba127d66 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 6 Apr 2023 00:14:42 +0300 Subject: [PATCH 03/10] =?UTF-8?q?+=D0=9A=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D0=B0=D1=80=D0=B8=D0=B8,=20=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D1=81=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers/Graph.cs | 42 ++++++++++++++++++--- Routers/Routers/InvalidFileException.cs | 3 ++ Routers/Routers/InvalidGraphException.cs | 3 -- Routers/Routers/InvalidPositionException.cs | 6 --- Routers/Routers/ListArcs.cs | 36 +++++++++++++++--- Routers/Routers/Program.cs | 17 +++++++-- Routers/Routers/Routers.cs | 15 +++++++- 7 files changed, 98 insertions(+), 24 deletions(-) delete mode 100644 Routers/Routers/InvalidGraphException.cs delete mode 100644 Routers/Routers/InvalidPositionException.cs diff --git a/Routers/Routers/Graph.cs b/Routers/Routers/Graph.cs index 8a013ab..2c9bab4 100644 --- a/Routers/Routers/Graph.cs +++ b/Routers/Routers/Graph.cs @@ -1,11 +1,15 @@ -using System.Reflection.Metadata; -using System.Linq; -using System.Collections.Generic; - -namespace Routers; +namespace Routers; +/// +/// A container consisting of two lists List Arcs, ListVertexes and its own size +/// public class Graph { + /// + /// It is used as a wrapper for writing a graph to a file + /// + /// Location of the original file + /// An empty or unfilled graph throws an exception public void WriteToFile(string filePath) { if (IsEmpty()) @@ -15,21 +19,39 @@ public void WriteToFile(string filePath) GraphByList.Arcs.WirteToFile(filePath); } + /// + /// Function to return ListArcs + /// + /// ListArcs in Graph public ListArcs ReturnListArcs() { return !IsEmpty() ? GraphByList.Arcs : null; } + /// + /// Function to return ListVertexes + /// + /// ListVertexes public ListVertexes ReturnListVertexes() { return !IsEmpty() ? GraphByList.Vertexes : null; } + /// + /// Checks that the graph and its components are filled + /// + /// Returns true if the graph or its components are filled otherwise false public bool IsEmpty() { return GraphByList == null || GraphByList.Arcs == null || GraphByList.Vertexes == null; } + /// + /// Adds paths from one vertex to another to the graph + /// + /// The vertex from which the path exits + /// The vertex that the path is included in + /// Path Size public void AddArcs(int fromVertex, int toVertex, int sizeWay) { if(GraphByList == null) @@ -43,6 +65,10 @@ public void AddArcs(int fromVertex, int toVertex, int sizeWay) GraphByList.Arcs.AddElement(fromVertex, toVertex, sizeWay); } + /// + /// Initializes the list of vertices in the graph + /// + /// The size of the future graph public void AddVertexes(int sizeGraph) { if (GraphByList == null) @@ -60,6 +86,12 @@ public void AddVertexes(int sizeGraph) } } + /// + /// The shell of the Kraskala algorithm + /// + /// Accepts the graph for which the algorithm will be applied + /// Returns true if the graph is connected and false otherwise + /// Throws an exception if the graph or its components are empty public bool KraskalAlgorithm(Graph graph) { if (graph.IsEmpty()) diff --git a/Routers/Routers/InvalidFileException.cs b/Routers/Routers/InvalidFileException.cs index 3dbbb8a..26ffa80 100644 --- a/Routers/Routers/InvalidFileException.cs +++ b/Routers/Routers/InvalidFileException.cs @@ -1,3 +1,6 @@ namespace Routers; +/// +/// Exception if there is a problem with the path to the file or there is an incorrect sequence inside the file +/// public class InvalidFileException : Exception {} \ No newline at end of file diff --git a/Routers/Routers/InvalidGraphException.cs b/Routers/Routers/InvalidGraphException.cs deleted file mode 100644 index 01c6f10..0000000 --- a/Routers/Routers/InvalidGraphException.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Routers; - -public class InvalidGraphException : Exception {} \ No newline at end of file diff --git a/Routers/Routers/InvalidPositionException.cs b/Routers/Routers/InvalidPositionException.cs deleted file mode 100644 index 1eb2b91..0000000 --- a/Routers/Routers/InvalidPositionException.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Routers; - -/// -/// Exception for incorrect position -/// -public class InvalidPositionException : Exception { } \ No newline at end of file diff --git a/Routers/Routers/ListArcs.cs b/Routers/Routers/ListArcs.cs index fa6eaa5..35cf060 100644 --- a/Routers/Routers/ListArcs.cs +++ b/Routers/Routers/ListArcs.cs @@ -1,7 +1,8 @@ -using System.Runtime.InteropServices; - -namespace Routers; +namespace Routers; +/// +/// A list of arcs consisting of a pair of vertices and arcs between them +/// public class ListArcs { private ListElement? Head; @@ -13,7 +14,11 @@ private bool SortByVertexOrArcs(bool sortByVertex, ListElement firstListElement, } - // Sort List Arcs + /// + /// Sorting by the list bubble method + /// + /// The parameter for selecting a comparison in the subsequent sorting is true if by vertices and false if by arcs + /// Throws an exception if the list is empty public void SortListArcs(bool sortByVertex) { if (Head == null) @@ -41,6 +46,12 @@ public void SortListArcs(bool sortByVertex) } } + /// + /// Removes arcs from the list + /// + /// The vertex from which the arc goes + /// The vertex to which the arc goes + /// Throws an exception if the list is empty private void DeleteArc(int fromVertex, int toVertex) { if (Head == null) @@ -64,6 +75,12 @@ private void DeleteArc(int fromVertex, int toVertex) } } + /// + /// Kraskal's algorithm for a minimal spanning tree + /// + /// The graph in which everything is stored + /// Returns false if the tree is not connected + /// Throws an exception if the graph and its components are empty public bool KraskalAlgorithm(Graph graph) { if (graph == null || graph.IsEmpty()) @@ -124,7 +141,12 @@ public bool KraskalAlgorithm(Graph graph) return !(walker == null && i <= sizeGraph); } - // Adding element to list + /// + /// Adds a new item to the list + /// + /// The vertex from which the arc originates + /// The vertex that the arc enters + /// Arc Size public void AddElement(int fromVertex, int toVertex, int sizeWay) { var item = new ListElement(fromVertex, toVertex, sizeWay); @@ -140,6 +162,10 @@ public void AddElement(int fromVertex, int toVertex, int sizeWay) ++Head.SizeList; } + /// + /// Writing to the arc list file + /// + /// The path to the file public void WirteToFile(string filePath) { filePath = filePath + ".new"; diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs index 0b53354..6fe2a16 100644 --- a/Routers/Routers/Program.cs +++ b/Routers/Routers/Program.cs @@ -1,14 +1,25 @@ namespace ListAndUniqueList; using Routers; -using System; -using System.Net.WebSockets; class Program { public static void Main(string[] args) { var routers = new Routers(); - routers.WorkWithFile("C:\\Users\\User\\Downloads\\graph.txt"); + Console.WriteLine("Enter the file path with double slashes"); + var filePath = Console.ReadLine(); + try + { + routers.WorkWithFile(filePath); + } + catch (NullPointerException) + { + Console.WriteLine("Problems with pointers, an incorrect example in the file is possible"); + } + catch(InvalidFileException) + { + Console.WriteLine("Problems with the path to the file or the contents of the file"); + } } } \ No newline at end of file diff --git a/Routers/Routers/Routers.cs b/Routers/Routers/Routers.cs index cff67c6..3cf91bb 100644 --- a/Routers/Routers/Routers.cs +++ b/Routers/Routers/Routers.cs @@ -1,17 +1,28 @@ namespace Routers; +/// +/// A class for implementing finding a spanning tree and printing its file by retrieving data from a file +/// public class Routers { - private void WriteToFile(Graph graph, string filePath) { graph.WriteToFile(filePath); } + /// + /// Works with the file containing the initial data + /// + /// The path to the file + /// Returns true if the graph is connected and false if not + /// Throws an exception if the entry in the file is uncorrected public bool WorkWithFile(string filePath) { var file = new StreamReader(filePath); - // Проверка на файл + if (file == null) + { + throw new InvalidFileException(); + } string line = "\0"; int mainVertex = 0; int anotherVertex = 0; From 5380bb2d8d674a5d2e69e9693e8c891b58598fd9 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 6 Apr 2023 21:42:20 +0300 Subject: [PATCH 04/10] =?UTF-8?q?+=D0=A2=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers.sln | 20 +++- Routers/Routers/Graph.cs | 14 +++ Routers/Routers/ListArcs.cs | 13 +- Routers/TestsForGraph/TestsForGraph.cs | 71 +++++++++++ Routers/TestsForGraph/TestsForGraph.csproj | 19 +++ Routers/TestsForGraph/Usings.cs | 1 + Routers/TestsForRouters/TestForRouters.cs | 112 ++++++++++++++++++ .../TestsForRouters/TestsForRouters.csproj | 23 ++++ Routers/TestsForRouters/Usings.cs | 1 + ...ctExpressionForMoreDifficultExpression.txt | 3 + .../TestsForRouters/fileWithFirstCorrect.txt | 1 + .../fileWithFirstCorrectExpression.txt | 0 .../fileWithInvalidFirstSymbol.txt | 1 + .../TestsForRouters/fileWithInvalidSymbol.txt | 1 + .../fileWithMoreDifficultExpression.txt | 3 + .../fileWithNotLinkedGraph.txt | 2 + .../fileWithNotLinkedGraphMoreDificult.txt | 4 + .../fileWithoutCloseingParenthesis.txt | 1 + .../fileWithoutColonAfterMainVertex.txt | 1 + ...leWithoutCommaAfterCloseingParenthesis.txt | 1 + ...leWithoutNumberAfterOpeningParenthesis.txt | 1 + .../fileWithoutOpeningParenthesis.txt | 1 + .../fileWithoutSpaceAfterColon.txt | 1 + .../fileWithoutSpaceAfterVertexNotMain.txt | 1 + Routers/TestsForTwoLists/TestsForTwoLists.cs | 81 +++++++++++++ .../TestsForTwoLists/TestsForTwoLists.csproj | 23 ++++ Routers/TestsForTwoLists/Usings.cs | 1 + 27 files changed, 394 insertions(+), 7 deletions(-) create mode 100644 Routers/TestsForGraph/TestsForGraph.cs create mode 100644 Routers/TestsForGraph/TestsForGraph.csproj create mode 100644 Routers/TestsForGraph/Usings.cs create mode 100644 Routers/TestsForRouters/TestForRouters.cs create mode 100644 Routers/TestsForRouters/TestsForRouters.csproj create mode 100644 Routers/TestsForRouters/Usings.cs create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithCorrectExpressionForMoreDifficultExpression.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrectExpression.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidFirstSymbol.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidSymbol.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraph.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraphMoreDificult.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCloseingParenthesis.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutColonAfterMainVertex.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCommaAfterCloseingParenthesis.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutNumberAfterOpeningParenthesis.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutOpeningParenthesis.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterColon.txt create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterVertexNotMain.txt create mode 100644 Routers/TestsForTwoLists/TestsForTwoLists.cs create mode 100644 Routers/TestsForTwoLists/TestsForTwoLists.csproj create mode 100644 Routers/TestsForTwoLists/Usings.cs diff --git a/Routers/Routers.sln b/Routers/Routers.sln index 5f8f81e..9a20157 100644 --- a/Routers/Routers.sln +++ b/Routers/Routers.sln @@ -3,7 +3,13 @@ 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}") = "Routers", "Routers\Routers.csproj", "{EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Routers", "Routers\Routers.csproj", "{EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestsForRouters", "TestsForRouters\TestsForRouters.csproj", "{FB3AE590-F3E6-4FBF-A49E-C733744BAC40}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsFotGraph", "TestsFotGraph\TestsFotGraph.csproj", "{F3528591-4B04-43EE-A5C8-615C35C67503}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForTwoLists", "TestsForTwoLists\TestsForTwoLists.csproj", "{9C296729-ADA4-4900-8155-A9BE6FB52711}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +21,18 @@ Global {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Debug|Any CPU.Build.0 = Debug|Any CPU {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Release|Any CPU.ActiveCfg = Release|Any CPU {EB4BA75B-C03B-4E71-B21F-37AFE12F12FA}.Release|Any CPU.Build.0 = Release|Any CPU + {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Release|Any CPU.Build.0 = Release|Any CPU + {F3528591-4B04-43EE-A5C8-615C35C67503}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F3528591-4B04-43EE-A5C8-615C35C67503}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F3528591-4B04-43EE-A5C8-615C35C67503}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F3528591-4B04-43EE-A5C8-615C35C67503}.Release|Any CPU.Build.0 = Release|Any CPU + {9C296729-ADA4-4900-8155-A9BE6FB52711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C296729-ADA4-4900-8155-A9BE6FB52711}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C296729-ADA4-4900-8155-A9BE6FB52711}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C296729-ADA4-4900-8155-A9BE6FB52711}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Routers/Routers/Graph.cs b/Routers/Routers/Graph.cs index 2c9bab4..03e165e 100644 --- a/Routers/Routers/Graph.cs +++ b/Routers/Routers/Graph.cs @@ -5,6 +5,20 @@ /// public class Graph { + /// + /// Returns graph size + /// + /// Graph size + /// If graph null throw exception + public int ReturnSizeGraph() + { + if (IsEmpty()) + { + throw new NullPointerException(); + } + return GraphByList.sizeGraph; + } + /// /// It is used as a wrapper for writing a graph to a file /// diff --git a/Routers/Routers/ListArcs.cs b/Routers/Routers/ListArcs.cs index 35cf060..be4740c 100644 --- a/Routers/Routers/ListArcs.cs +++ b/Routers/Routers/ListArcs.cs @@ -10,7 +10,7 @@ public class ListArcs private bool SortByVertexOrArcs(bool sortByVertex, ListElement firstListElement, ListElement secondListElement) { - return sortByVertex ? firstListElement.ToVertex > secondListElement.ToVertex : firstListElement.SizeWay < secondListElement.SizeWay; + return sortByVertex ? firstListElement.FromVertex > secondListElement.FromVertex : firstListElement.SizeWay < secondListElement.SizeWay; } @@ -90,10 +90,9 @@ public bool KraskalAlgorithm(Graph graph) var listArcs = graph.ReturnListArcs(); var listVertexes = graph.ReturnListVertexes(); listArcs.SortListArcs(false); - int sizeGraph = listArcs.Head.SizeList; var walker = listArcs.Head; int numberPlenty = 1; - int i = 1; + int extraPlenty = 0; while (walker != null) { if (walker.ToVertex != walker.FromVertex) @@ -106,23 +105,25 @@ public bool KraskalAlgorithm(Graph graph) if (returnedNumberPlentyFirstVertex == 0 && returnedNumberPlentySecondVertex != 0) { listVertexes.ChangeOneVertexSet(walker.FromVertex, returnedNumberPlentySecondVertex); + --extraPlenty; } else if (returnedNumberPlentyFirstVertex != 0 && returnedNumberPlentySecondVertex == 0) { listVertexes.ChangeOneVertexSet(walker.ToVertex, returnedNumberPlentyFirstVertex); + --extraPlenty; } else { listVertexes.ChangeNumbersSet(returnedNumberPlentyFirstVertex, returnedNumberPlentySecondVertex); + --extraPlenty; } - ++i; } else if (returnedNumberPlentyFirstVertex == 0) { listVertexes.ChangeOneVertexSet(walker.FromVertex, numberPlenty); listVertexes.ChangeOneVertexSet(walker.ToVertex, numberPlenty); ++numberPlenty; - i += 2; + ++extraPlenty; } else { @@ -138,7 +139,7 @@ public bool KraskalAlgorithm(Graph graph) walker = walker.Next; } SortListArcs(true); - return !(walker == null && i <= sizeGraph); + return !(walker == null && (extraPlenty != 1 && extraPlenty != 0)); } /// diff --git a/Routers/TestsForGraph/TestsForGraph.cs b/Routers/TestsForGraph/TestsForGraph.cs new file mode 100644 index 0000000..3e0a696 --- /dev/null +++ b/Routers/TestsForGraph/TestsForGraph.cs @@ -0,0 +1,71 @@ +namespace TestsFotGraph; + +using Routers; +public class Tests +{ + Graph graph; + + [SetUp] + public void Setup() + { + graph = new Graph(); + } + + [TestCaseSource(nameof(GraphForTest))] + public void EmptyGraphShouldBeEmpty(Graph graph) + { + Assert.True(graph.IsEmpty()); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldThrowExceptionAfterTryGetSizeWhileItEmpty(Graph graph) + { + Assert.Throws(() => graph.ReturnSizeGraph()); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldReturnNullAfterTryGetListArcsWhileItEmpty(Graph graph) + { + Assert.True(graph.ReturnListArcs() == null); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldReturnNullAfterTryGetListVertexesWhileItEmpty(Graph graph) + { + Assert.True(graph.ReturnListVertexes() == null); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldThrowExceptionAfterTryWriteInFileWhileGraphEmpty(Graph graph) + { + Assert.Throws(() => graph.WriteToFile("v_v")); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldThrowExceptionAfterTryUseKraskalAlgorithmWhileGraphEmpty(Graph graph) + { + Assert.Throws(() => graph.KraskalAlgorithm(graph)); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldReturnCorrectSizeAfterAddding(Graph graph) + { + graph.AddArcs(1, 2, 3); + graph.AddVertexes(2); + Assert.True(graph.ReturnSizeGraph() == 2); + } + + [TestCaseSource(nameof(GraphForTest))] + public void GraphShouldBeNotEmptyAfterAddding(Graph graph) + { + graph.AddArcs(1, 2, 3); + graph.AddVertexes(2); + Assert.False(graph.IsEmpty()); + } + + private static IEnumerable GraphForTest + => new TestCaseData[] + { + new TestCaseData(new Graph()), + }; +} \ No newline at end of file diff --git a/Routers/TestsForGraph/TestsForGraph.csproj b/Routers/TestsForGraph/TestsForGraph.csproj new file mode 100644 index 0000000..cbb7690 --- /dev/null +++ b/Routers/TestsForGraph/TestsForGraph.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + diff --git a/Routers/TestsForGraph/Usings.cs b/Routers/TestsForGraph/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/Routers/TestsForGraph/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/Routers/TestsForRouters/TestForRouters.cs b/Routers/TestsForRouters/TestForRouters.cs new file mode 100644 index 0000000..60d7490 --- /dev/null +++ b/Routers/TestsForRouters/TestForRouters.cs @@ -0,0 +1,112 @@ +namespace TestsForRouters; + +using Routers; + +public class Tests +{ + Routers routers; + + [SetUp] + public void Setup() + { + routers = new Routers(); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfThereIsAnInvalidCharacterInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidSymbol.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfFirstSymbolIsNotNumberInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidFirstSymbol.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfSpaceNotAferNotMainVertexInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterVertexNotMain.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfColonAbsentAfterMainVertexInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutColonAfterMainVertex.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfSpaceAbsentAfterColonInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterColon.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfOpeningParenthesisAbsentAfterVertexWithSpaceInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutOpeningParenthesis.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfNotNumberAfteropeningParenthesisInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutNumberAfterOpeningParenthesis.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfCloseingParenthesisAbsentInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCloseingParenthesis.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldThrowAnExceptionIfAfterCloseingParenthesisCommaAbsentInTheFile(Routers routers) + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCommaAfterCloseingParenthesis.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldWorkCorrectlyWithSimpleExpression(Routers routers) + { + bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt")); + if (!isLink) + { + Assert.Fail(); + } + string afterAlgorithm = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt.new")); + string correctResult = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt")); + Assert.True(afterAlgorithm == correctResult); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldWorkCorrectlyWithSimpleExpressionWhereGraphNotLinked(Routers routers) + { + Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraph.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldWorkCorrectlyWithDificultExpressionWhereGraphNotLinked(Routers routers) + { + Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraphMoreDificult.txt"))); + } + + [TestCaseSource(nameof(RoutersForTest))] + public void RoutersShouldWorkCorrectlyWithDificultExpression(Routers routers) + { + bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt")); + if (!isLink) + { + Assert.Fail(); + } + string afterAlgorithm = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt.new")); + string correctResult = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithCorrectExpressionForMoreDifficultExpression.txt")); + Assert.True(afterAlgorithm == correctResult); + } + + private static IEnumerable RoutersForTest + => new TestCaseData[] + { + new TestCaseData(new Routers()), + }; +} \ No newline at end of file diff --git a/Routers/TestsForRouters/TestsForRouters.csproj b/Routers/TestsForRouters/TestsForRouters.csproj new file mode 100644 index 0000000..021ce04 --- /dev/null +++ b/Routers/TestsForRouters/TestsForRouters.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/Routers/TestsForRouters/Usings.cs b/Routers/TestsForRouters/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/Routers/TestsForRouters/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithCorrectExpressionForMoreDifficultExpression.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithCorrectExpressionForMoreDifficultExpression.txt new file mode 100644 index 0000000..cbce792 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithCorrectExpressionForMoreDifficultExpression.txt @@ -0,0 +1,3 @@ +1: 3 (40) +2: 4 (45) +3: 2 (36), 5 (35) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt new file mode 100644 index 0000000..8eea4b6 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt @@ -0,0 +1 @@ +1: 2 (10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrectExpression.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrectExpression.txt new file mode 100644 index 0000000..e69de29 diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidFirstSymbol.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidFirstSymbol.txt new file mode 100644 index 0000000..7e82f4d --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidFirstSymbol.txt @@ -0,0 +1 @@ +a: 2 (10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidSymbol.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidSymbol.txt new file mode 100644 index 0000000..9918aec --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithInvalidSymbol.txt @@ -0,0 +1 @@ +1: d 2 (10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt new file mode 100644 index 0000000..3798d0f --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt @@ -0,0 +1,3 @@ +1: 2 (30), 3 (40) +2: 4 (45), 5 (15) +3: 2 (36), 5 (35) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraph.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraph.txt new file mode 100644 index 0000000..088bde7 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraph.txt @@ -0,0 +1,2 @@ +1: 2 (10) +3: 4 (100) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraphMoreDificult.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraphMoreDificult.txt new file mode 100644 index 0000000..0b7ef4e --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithNotLinkedGraphMoreDificult.txt @@ -0,0 +1,4 @@ +1: 2 (10), 3 (20) +2: 3 (30), 4 (33) +4: 5 (31) +5: 7 (23) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCloseingParenthesis.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCloseingParenthesis.txt new file mode 100644 index 0000000..3d8db74 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCloseingParenthesis.txt @@ -0,0 +1 @@ +1: 2 (10, 3 (40) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutColonAfterMainVertex.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutColonAfterMainVertex.txt new file mode 100644 index 0000000..741ec17 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutColonAfterMainVertex.txt @@ -0,0 +1 @@ +1) 2 (10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCommaAfterCloseingParenthesis.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCommaAfterCloseingParenthesis.txt new file mode 100644 index 0000000..65ecd72 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutCommaAfterCloseingParenthesis.txt @@ -0,0 +1 @@ +1: 2 (10) 3 (40) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutNumberAfterOpeningParenthesis.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutNumberAfterOpeningParenthesis.txt new file mode 100644 index 0000000..734e7c5 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutNumberAfterOpeningParenthesis.txt @@ -0,0 +1 @@ +1: 2 (a) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutOpeningParenthesis.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutOpeningParenthesis.txt new file mode 100644 index 0000000..3ee47d9 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutOpeningParenthesis.txt @@ -0,0 +1 @@ +1: 2 10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterColon.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterColon.txt new file mode 100644 index 0000000..49ee933 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterColon.txt @@ -0,0 +1 @@ +1:2 (10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterVertexNotMain.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterVertexNotMain.txt new file mode 100644 index 0000000..c444e8b --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithoutSpaceAfterVertexNotMain.txt @@ -0,0 +1 @@ +1: 2(10) \ No newline at end of file diff --git a/Routers/TestsForTwoLists/TestsForTwoLists.cs b/Routers/TestsForTwoLists/TestsForTwoLists.cs new file mode 100644 index 0000000..befb7cf --- /dev/null +++ b/Routers/TestsForTwoLists/TestsForTwoLists.cs @@ -0,0 +1,81 @@ +namespace TestsFotGraph; + +using NuGet.Frameworks; +using Routers; +public class Tests +{ + ListVertexes list; + + [SetUp] + public void Setup() + { + list = new ListVertexes(); + } + + [TestCaseSource(nameof(ListForTest))] + public void InStartNumberSetForAllVertexesShouldZero(ListVertexes list) + { + Graph graph = new Graph(); + graph.AddVertexes(3); + graph.AddArcs(1, 2, 30); + graph.AddArcs(1, 3, 20); + graph.AddArcs(2, 3, 10); + var anotherList = graph.ReturnListVertexes(); + for (int i = 1; i <= 3; ++i) + { + if (anotherList.FromWichSet(i) != 0) + { + Assert.Fail(); + } + } + Assert.True(true); + } + + [TestCaseSource(nameof(ListForTest))] + public void EmptyListVertexesShouldReturnMinusOneAfterTryToReturnNumberSetIfVertexNotInList(ListVertexes list) + { + Assert.True(list.FromWichSet(1) == -1); + } + + [TestCaseSource(nameof(ListForTest))] + public void ListVertexesShouldChangedNumberSet(ListVertexes list) + { + Graph graph = new Graph(); + graph.AddVertexes(3); + graph.AddArcs(1, 2, 3); + var anotherList = graph.ReturnListVertexes(); + anotherList.ChangeOneVertexSet(2, 10); + Assert.True(anotherList.FromWichSet(2) == 10); + } + + [TestCaseSource(nameof(ListForTest))] + public void ListVertexesShouldChangedAllVertexesNumberSet(ListVertexes list) + { + Graph graph = new Graph(); + graph.AddVertexes(3); + graph.AddArcs(1, 2, 3); + var anotherList = graph.ReturnListVertexes(); + anotherList.ChangeNumbersSet(10, 0); + for (int i = 1; i <= 3; ++i) + { + if (anotherList.FromWichSet(i) != 10) + { + Assert.Fail(); + } + } + Assert.True(true); + } + + [Test] + public void ListArcsShouldThrowExceptionIfTryToSortNullPointer() + { + var list = new ListArcs(); + Assert.Throws(() => list.SortListArcs(true)); + } + + private static IEnumerable ListForTest + => new TestCaseData[] + { + new TestCaseData(new ListVertexes()), + }; +} \ No newline at end of file diff --git a/Routers/TestsForTwoLists/TestsForTwoLists.csproj b/Routers/TestsForTwoLists/TestsForTwoLists.csproj new file mode 100644 index 0000000..021ce04 --- /dev/null +++ b/Routers/TestsForTwoLists/TestsForTwoLists.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/Routers/TestsForTwoLists/Usings.cs b/Routers/TestsForTwoLists/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/Routers/TestsForTwoLists/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From 6a2e6f112ffab7e40704869a65206e1a73320972 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 6 Apr 2023 22:07:16 +0300 Subject: [PATCH 05/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers.sln | 12 ++++----- Routers/Routers/Graph.cs | 2 +- Routers/Routers/ListArcs.cs | 5 ++-- Routers/Routers/Program.cs | 16 ++++++++++-- Routers/Routers/Routers.cs | 8 +++--- Routers/TestsForGraph/TestsForGraph.cs | 2 +- Routers/TestsForGraph/TestsForGraph.csproj | 4 +++ Routers/TestsForRouters/TestForRouters.cs | 26 +++++++++---------- .../fileWithMoreDifficultExpression.txt | 4 +-- 9 files changed, 47 insertions(+), 32 deletions(-) diff --git a/Routers/Routers.sln b/Routers/Routers.sln index 9a20157..4b2fc7f 100644 --- a/Routers/Routers.sln +++ b/Routers/Routers.sln @@ -7,10 +7,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Routers", "Routers\Routers. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestsForRouters", "TestsForRouters\TestsForRouters.csproj", "{FB3AE590-F3E6-4FBF-A49E-C733744BAC40}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsFotGraph", "TestsFotGraph\TestsFotGraph.csproj", "{F3528591-4B04-43EE-A5C8-615C35C67503}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForTwoLists", "TestsForTwoLists\TestsForTwoLists.csproj", "{9C296729-ADA4-4900-8155-A9BE6FB52711}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForGraph", "TestsForGraph\TestsForGraph.csproj", "{E8F69361-637E-4930-B8F3-30A9506B0843}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -25,14 +25,14 @@ Global {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Debug|Any CPU.Build.0 = Debug|Any CPU {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Release|Any CPU.ActiveCfg = Release|Any CPU {FB3AE590-F3E6-4FBF-A49E-C733744BAC40}.Release|Any CPU.Build.0 = Release|Any CPU - {F3528591-4B04-43EE-A5C8-615C35C67503}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F3528591-4B04-43EE-A5C8-615C35C67503}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F3528591-4B04-43EE-A5C8-615C35C67503}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F3528591-4B04-43EE-A5C8-615C35C67503}.Release|Any CPU.Build.0 = Release|Any CPU {9C296729-ADA4-4900-8155-A9BE6FB52711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9C296729-ADA4-4900-8155-A9BE6FB52711}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C296729-ADA4-4900-8155-A9BE6FB52711}.Release|Any CPU.ActiveCfg = Release|Any CPU {9C296729-ADA4-4900-8155-A9BE6FB52711}.Release|Any CPU.Build.0 = Release|Any CPU + {E8F69361-637E-4930-B8F3-30A9506B0843}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8F69361-637E-4930-B8F3-30A9506B0843}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8F69361-637E-4930-B8F3-30A9506B0843}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8F69361-637E-4930-B8F3-30A9506B0843}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Routers/Routers/Graph.cs b/Routers/Routers/Graph.cs index 03e165e..23666eb 100644 --- a/Routers/Routers/Graph.cs +++ b/Routers/Routers/Graph.cs @@ -24,7 +24,7 @@ public int ReturnSizeGraph() /// /// Location of the original file /// An empty or unfilled graph throws an exception - public void WriteToFile(string filePath) + public void WriteToFile(string filePath, string fileAfter) { if (IsEmpty()) { diff --git a/Routers/Routers/ListArcs.cs b/Routers/Routers/ListArcs.cs index be4740c..72110e7 100644 --- a/Routers/Routers/ListArcs.cs +++ b/Routers/Routers/ListArcs.cs @@ -167,10 +167,9 @@ public void AddElement(int fromVertex, int toVertex, int sizeWay) /// Writing to the arc list file /// /// The path to the file - public void WirteToFile(string filePath) + public void WirteToFile(string fileAfter) { - filePath = filePath + ".new"; - StreamWriter file = new StreamWriter(filePath); + StreamWriter file = new StreamWriter(fileAfter); var walker = Head; int previousMainVertex = 0; bool isFirst = true; diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs index 6fe2a16..50f8b5e 100644 --- a/Routers/Routers/Program.cs +++ b/Routers/Routers/Program.cs @@ -4,14 +4,16 @@ class Program { - public static void Main(string[] args) + public static int Main(string[] args) { var routers = new Routers(); Console.WriteLine("Enter the file path with double slashes"); var filePath = Console.ReadLine(); + var fileAfter = Console.ReadLine(); + bool isLinkedGraph = true; try { - routers.WorkWithFile(filePath); + isLinkedGraph = routers.WorkWithFile(filePath, fileAfter); } catch (NullPointerException) { @@ -21,5 +23,15 @@ public static void Main(string[] args) { Console.WriteLine("Problems with the path to the file or the contents of the file"); } + catch (FileNotFoundException) + { + Console.WriteLine("Problems with incorrect way to file"); + } + if (!isLinkedGraph) + { + Console.WriteLine("Graph not Linked", Console.Error); + return -1; + } + return 0; } } \ No newline at end of file diff --git a/Routers/Routers/Routers.cs b/Routers/Routers/Routers.cs index 3cf91bb..b88890a 100644 --- a/Routers/Routers/Routers.cs +++ b/Routers/Routers/Routers.cs @@ -5,9 +5,9 @@ /// public class Routers { - private void WriteToFile(Graph graph, string filePath) + private void WriteToFile(Graph graph, string filePath, string fileAfter) { - graph.WriteToFile(filePath); + graph.WriteToFile(filePath, fileAfter); } /// @@ -16,7 +16,7 @@ private void WriteToFile(Graph graph, string filePath) /// The path to the file /// Returns true if the graph is connected and false if not /// Throws an exception if the entry in the file is uncorrected - public bool WorkWithFile(string filePath) + public bool WorkWithFile(string filePath, string fileAfter) { var file = new StreamReader(filePath); if (file == null) @@ -154,7 +154,7 @@ public bool WorkWithFile(string filePath) return false; } - graph.WriteToFile(filePath); + graph.WriteToFile(filePath, fileAfter); return true; } diff --git a/Routers/TestsForGraph/TestsForGraph.cs b/Routers/TestsForGraph/TestsForGraph.cs index 3e0a696..0461238 100644 --- a/Routers/TestsForGraph/TestsForGraph.cs +++ b/Routers/TestsForGraph/TestsForGraph.cs @@ -38,7 +38,7 @@ public void GraphShouldReturnNullAfterTryGetListVertexesWhileItEmpty(Graph graph [TestCaseSource(nameof(GraphForTest))] public void GraphShouldThrowExceptionAfterTryWriteInFileWhileGraphEmpty(Graph graph) { - Assert.Throws(() => graph.WriteToFile("v_v")); + Assert.Throws(() => graph.WriteToFile("v_v", "^_^")); } [TestCaseSource(nameof(GraphForTest))] diff --git a/Routers/TestsForGraph/TestsForGraph.csproj b/Routers/TestsForGraph/TestsForGraph.csproj index cbb7690..021ce04 100644 --- a/Routers/TestsForGraph/TestsForGraph.csproj +++ b/Routers/TestsForGraph/TestsForGraph.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/Routers/TestsForRouters/TestForRouters.cs b/Routers/TestsForRouters/TestForRouters.cs index 60d7490..14fccfe 100644 --- a/Routers/TestsForRouters/TestForRouters.cs +++ b/Routers/TestsForRouters/TestForRouters.cs @@ -15,61 +15,61 @@ public void Setup() [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfThereIsAnInvalidCharacterInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidSymbol.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidSymbol.txt"), "dsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfFirstSymbolIsNotNumberInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidFirstSymbol.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidFirstSymbol.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfSpaceNotAferNotMainVertexInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterVertexNotMain.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterVertexNotMain.txt"), "dsds")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfColonAbsentAfterMainVertexInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutColonAfterMainVertex.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutColonAfterMainVertex.txt"), "sddsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfSpaceAbsentAfterColonInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterColon.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterColon.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfOpeningParenthesisAbsentAfterVertexWithSpaceInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutOpeningParenthesis.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutOpeningParenthesis.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfNotNumberAfteropeningParenthesisInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutNumberAfterOpeningParenthesis.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutNumberAfterOpeningParenthesis.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfCloseingParenthesisAbsentInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCloseingParenthesis.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCloseingParenthesis.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldThrowAnExceptionIfAfterCloseingParenthesisCommaAbsentInTheFile(Routers routers) { - Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCommaAfterCloseingParenthesis.txt"))); + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCommaAfterCloseingParenthesis.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldWorkCorrectlyWithSimpleExpression(Routers routers) { - bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt")); + bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt"), Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt.new")); if (!isLink) { Assert.Fail(); @@ -82,19 +82,19 @@ public void RoutersShouldWorkCorrectlyWithSimpleExpression(Routers routers) [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldWorkCorrectlyWithSimpleExpressionWhereGraphNotLinked(Routers routers) { - Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraph.txt"))); + Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraph.txt"), Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraph.txt.new"))); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldWorkCorrectlyWithDificultExpressionWhereGraphNotLinked(Routers routers) { - Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraphMoreDificult.txt"))); + Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraphMoreDificult.txt"), "sdsd")); } [TestCaseSource(nameof(RoutersForTest))] public void RoutersShouldWorkCorrectlyWithDificultExpression(Routers routers) { - bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt")); + bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt"), Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt.new")); if (!isLink) { Assert.Fail(); diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt index 3798d0f..cbce792 100644 --- a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt @@ -1,3 +1,3 @@ -1: 2 (30), 3 (40) -2: 4 (45), 5 (15) +1: 3 (40) +2: 4 (45) 3: 2 (36), 5 (35) \ No newline at end of file From 8d8fb0ee4bea353f0b1a25e7ba0de50d699d14d6 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 6 Apr 2023 22:08:46 +0300 Subject: [PATCH 06/10] =?UTF-8?q?+=D0=9E=D0=B1=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs index 50f8b5e..43a889c 100644 --- a/Routers/Routers/Program.cs +++ b/Routers/Routers/Program.cs @@ -9,6 +9,7 @@ public static int Main(string[] args) var routers = new Routers(); Console.WriteLine("Enter the file path with double slashes"); var filePath = Console.ReadLine(); + Console.Write("Enter file name where to write a new graph"); var fileAfter = Console.ReadLine(); bool isLinkedGraph = true; try From e826eb1b6babd89c37987fed42fa7b65149aa0e9 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 6 Apr 2023 22:12:41 +0300 Subject: [PATCH 07/10] =?UTF-8?q?Actions=20=D0=BF=D0=BE=D1=85=D0=BE=D0=B6?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=B5=20=D0=B2=D0=B8=D0=B4=D0=B8=D1=82=20=D1=87?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt.new | 1 + .../TestsForRouters/fileWithMoreDifficultExpression.txt.new | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt.new create mode 100644 Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt.new diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt.new b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt.new new file mode 100644 index 0000000..8eea4b6 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithFirstCorrect.txt.new @@ -0,0 +1 @@ +1: 2 (10) \ No newline at end of file diff --git a/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt.new b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt.new new file mode 100644 index 0000000..cbce792 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.txt.new @@ -0,0 +1,3 @@ +1: 3 (40) +2: 4 (45) +3: 2 (36), 5 (35) \ No newline at end of file From de435d6e2f3fcd79912f9095ff09b111eef0cf4a Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 03:14:59 +0300 Subject: [PATCH 08/10] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers/Graph.cs | 76 +++---- Routers/Routers/InvalidFileException.cs | 2 +- Routers/Routers/ListArcs.cs | 220 ------------------- Routers/Routers/ListVertexes.cs | 26 +-- Routers/Routers/NullPointerException.cs | 6 - Routers/Routers/Program.cs | 62 +++--- Routers/Routers/Routers.cs | 8 +- Routers/TestsForGraph/TestsForGraph.cs | 55 +++-- Routers/TestsForRouters/TestForRouters.cs | 66 +++--- Routers/TestsForTwoLists/TestsForTwoLists.cs | 48 ++-- 10 files changed, 145 insertions(+), 424 deletions(-) delete mode 100644 Routers/Routers/ListArcs.cs delete mode 100644 Routers/Routers/NullPointerException.cs diff --git a/Routers/Routers/Graph.cs b/Routers/Routers/Graph.cs index 23666eb..76e386b 100644 --- a/Routers/Routers/Graph.cs +++ b/Routers/Routers/Graph.cs @@ -1,4 +1,4 @@ -namespace Routers; +namespace RoutersByGraph; /// /// A container consisting of two lists List Arcs, ListVertexes and its own size @@ -9,15 +9,8 @@ public class Graph /// Returns graph size /// /// Graph size - /// If graph null throw exception - public int ReturnSizeGraph() - { - if (IsEmpty()) - { - throw new NullPointerException(); - } - return GraphByList.sizeGraph; - } + /// If the graph is empty throw exception + public int Size() => sizeGraph; /// /// It is used as a wrapper for writing a graph to a file @@ -26,30 +19,25 @@ public int ReturnSizeGraph() /// An empty or unfilled graph throws an exception public void WriteToFile(string filePath, string fileAfter) { - if (IsEmpty()) + if (edges == null || vertexes == null) { - throw new NullPointerException(); + throw new NullGraphOrGraphComponentsException(); } - GraphByList.Arcs.WirteToFile(filePath); + edges.WirteToFile(filePath); } /// /// Function to return ListArcs /// /// ListArcs in Graph - public ListArcs ReturnListArcs() - { - return !IsEmpty() ? GraphByList.Arcs : null; - } + public ListEdges? ReturnListArcs() => edges; + /// /// Function to return ListVertexes /// /// ListVertexes - public ListVertexes ReturnListVertexes() - { - return !IsEmpty() ? GraphByList.Vertexes : null; - } + public ListVertexes? ReturnListVertexes() => vertexes; /// /// Checks that the graph and its components are filled @@ -57,7 +45,7 @@ public ListVertexes ReturnListVertexes() /// Returns true if the graph or its components are filled otherwise false public bool IsEmpty() { - return GraphByList == null || GraphByList.Arcs == null || GraphByList.Vertexes == null; + return edges == null || vertexes == null; } /// @@ -68,35 +56,28 @@ public bool IsEmpty() /// Path Size public void AddArcs(int fromVertex, int toVertex, int sizeWay) { - if(GraphByList == null) - { - GraphByList = new GraphElement(); - } - if (GraphByList.Arcs == null) + + if (edges == null) { - GraphByList.Arcs = new ListArcs(); + edges = new ListEdges(); } - GraphByList.Arcs.AddElement(fromVertex, toVertex, sizeWay); + edges.AddElement(fromVertex, toVertex, sizeWay); } /// /// Initializes the list of vertices in the graph /// - /// The size of the future graph - public void AddVertexes(int sizeGraph) + /// The size of the future graph + public void AddVertexes(int size) { - if (GraphByList == null) - { - GraphByList = new GraphElement(); - } - if (GraphByList.Vertexes == null) + if (vertexes == null) { - GraphByList.Vertexes = new ListVertexes(); + vertexes = new ListVertexes(); } - GraphByList.sizeGraph = sizeGraph; + sizeGraph = size; for(int i = 1; i <= sizeGraph; i++) { - GraphByList.Vertexes.AddElement(i); + vertexes.AddElement(i); } } @@ -108,20 +89,17 @@ public void AddVertexes(int sizeGraph) /// Throws an exception if the graph or its components are empty public bool KraskalAlgorithm(Graph graph) { - if (graph.IsEmpty()) + if (edges == null) { - throw new NullPointerException(); + throw new NullGraphOrGraphComponentsException(); } - return graph.GraphByList.Arcs.KraskalAlgorithm(graph); + return edges.KraskalAlgorithm(graph); } - private class GraphElement - { - public ListVertexes? Vertexes { get; set; } + private ListVertexes? vertexes { get; set; } - public ListArcs? Arcs { get; set; } + private ListEdges? edges { get; set; } + + private int sizeGraph { get; set; } - public int sizeGraph { get; set; } - } - private GraphElement? GraphByList { get; set; } } \ No newline at end of file diff --git a/Routers/Routers/InvalidFileException.cs b/Routers/Routers/InvalidFileException.cs index 26ffa80..68a0557 100644 --- a/Routers/Routers/InvalidFileException.cs +++ b/Routers/Routers/InvalidFileException.cs @@ -1,4 +1,4 @@ -namespace Routers; +namespace RoutersByGraph; /// /// Exception if there is a problem with the path to the file or there is an incorrect sequence inside the file diff --git a/Routers/Routers/ListArcs.cs b/Routers/Routers/ListArcs.cs deleted file mode 100644 index 72110e7..0000000 --- a/Routers/Routers/ListArcs.cs +++ /dev/null @@ -1,220 +0,0 @@ -namespace Routers; - -/// -/// A list of arcs consisting of a pair of vertices and arcs between them -/// -public class ListArcs -{ - private ListElement? Head; - private ListElement? Tail; - - private bool SortByVertexOrArcs(bool sortByVertex, ListElement firstListElement, ListElement secondListElement) - { - return sortByVertex ? firstListElement.FromVertex > secondListElement.FromVertex : firstListElement.SizeWay < secondListElement.SizeWay; - } - - - /// - /// Sorting by the list bubble method - /// - /// The parameter for selecting a comparison in the subsequent sorting is true if by vertices and false if by arcs - /// Throws an exception if the list is empty - public void SortListArcs(bool sortByVertex) - { - if (Head == null) - { - throw new NullPointerException(); - } - int size = Head.SizeList; - for(int i = 0; i < size; ++i) - { - var walker = Head; - for(int j = 0; j < size; ++j) - { - if (walker.Next != null && SortByVertexOrArcs(sortByVertex, walker, walker.Next)) - { - var item = new ListElement(walker.FromVertex, walker.ToVertex, walker.SizeWay); - walker.SizeWay = walker.Next.SizeWay; - walker.FromVertex = walker.Next.FromVertex; - walker.ToVertex = walker.Next.ToVertex; - walker.Next.SizeWay = item.SizeWay; - walker.Next.ToVertex = item.ToVertex; - walker.Next.FromVertex = item.FromVertex; - } - walker = walker.Next; - } - } - } - - /// - /// Removes arcs from the list - /// - /// The vertex from which the arc goes - /// The vertex to which the arc goes - /// Throws an exception if the list is empty - private void DeleteArc(int fromVertex, int toVertex) - { - if (Head == null) - { - throw new NullPointerException(); - } - var walker = Head; - if (walker.FromVertex == fromVertex && walker.ToVertex == toVertex) - { - Head = Head.Next; - return; - } - while (walker.Next != null) - { - if (walker.Next.FromVertex == fromVertex && walker.Next.ToVertex == toVertex) - { - walker.Next = walker.Next.Next; - return; - } - walker = walker.Next; - } - } - - /// - /// Kraskal's algorithm for a minimal spanning tree - /// - /// The graph in which everything is stored - /// Returns false if the tree is not connected - /// Throws an exception if the graph and its components are empty - public bool KraskalAlgorithm(Graph graph) - { - if (graph == null || graph.IsEmpty()) - { - throw new NullPointerException(); - } - var listArcs = graph.ReturnListArcs(); - var listVertexes = graph.ReturnListVertexes(); - listArcs.SortListArcs(false); - var walker = listArcs.Head; - int numberPlenty = 1; - int extraPlenty = 0; - while (walker != null) - { - if (walker.ToVertex != walker.FromVertex) - { - int returnedNumberPlentyFirstVertex = listVertexes.FromWichSet(walker.FromVertex); - int returnedNumberPlentySecondVertex = listVertexes.FromWichSet(walker.ToVertex); - - if (returnedNumberPlentyFirstVertex != returnedNumberPlentySecondVertex) - { - if (returnedNumberPlentyFirstVertex == 0 && returnedNumberPlentySecondVertex != 0) - { - listVertexes.ChangeOneVertexSet(walker.FromVertex, returnedNumberPlentySecondVertex); - --extraPlenty; - } - else if (returnedNumberPlentyFirstVertex != 0 && returnedNumberPlentySecondVertex == 0) - { - listVertexes.ChangeOneVertexSet(walker.ToVertex, returnedNumberPlentyFirstVertex); - --extraPlenty; - } - else - { - listVertexes.ChangeNumbersSet(returnedNumberPlentyFirstVertex, returnedNumberPlentySecondVertex); - --extraPlenty; - } - } - else if (returnedNumberPlentyFirstVertex == 0) - { - listVertexes.ChangeOneVertexSet(walker.FromVertex, numberPlenty); - listVertexes.ChangeOneVertexSet(walker.ToVertex, numberPlenty); - ++numberPlenty; - ++extraPlenty; - } - else - { - DeleteArc(walker.FromVertex, walker.ToVertex); - --Head.SizeList; - } - } - else - { - DeleteArc(walker.FromVertex, walker.ToVertex); - --Head.SizeList; - } - walker = walker.Next; - } - SortListArcs(true); - return !(walker == null && (extraPlenty != 1 && extraPlenty != 0)); - } - - /// - /// Adds a new item to the list - /// - /// The vertex from which the arc originates - /// The vertex that the arc enters - /// Arc Size - public void AddElement(int fromVertex, int toVertex, int sizeWay) - { - var item = new ListElement(fromVertex, toVertex, sizeWay); - if (Head == null) - { - Head = item; - } - else - { - Tail.Next = item; - } - Tail = item; - ++Head.SizeList; - } - - /// - /// Writing to the arc list file - /// - /// The path to the file - public void WirteToFile(string fileAfter) - { - StreamWriter file = new StreamWriter(fileAfter); - var walker = Head; - int previousMainVertex = 0; - bool isFirst = true; - while (walker != null) - { - if (walker.FromVertex != previousMainVertex) - { - if (!isFirst) - { - file.WriteLine(); - } - file.Write(walker.FromVertex); - file.Write(": "); - previousMainVertex = walker.FromVertex; - isFirst = false; - } - else - { - file.Write(", "); - } - file.Write(walker.ToVertex); - file.Write(" ("); - file.Write(walker.SizeWay); - file.Write(")"); - walker = walker.Next; - } - file.Close(); - } - - private class ListElement - { - public ListElement(int fromVertex, int toVertex, int sizeWay) - { - FromVertex = fromVertex; - ToVertex = toVertex; - SizeWay = sizeWay; - } - - public int FromVertex { get; set; } - - public int ToVertex { get; set; } - public ListElement? Next { get; set; } - - public int SizeWay { get; set; } - - public int SizeList { get; set; } - } -} diff --git a/Routers/Routers/ListVertexes.cs b/Routers/Routers/ListVertexes.cs index 787c4f2..bb58c77 100644 --- a/Routers/Routers/ListVertexes.cs +++ b/Routers/Routers/ListVertexes.cs @@ -1,12 +1,12 @@ -namespace Routers; +namespace RoutersByGraph; /// summary /// Container for storing vertices and their belonging to a certain set /// public class ListVertexes { - private ListElement? Head; - private ListElement? Tail; + private ListElement? head; + private ListElement? tail; /// /// Change number set for all vertexes with previous number set in list @@ -15,8 +15,8 @@ public class ListVertexes /// Number previous set public void ChangeNumbersSet(int numberNewSet, int numberPreviousSet) { - var walker = Head; - while(walker != null) + var walker = head; + while (walker != null) { if (walker.InWichSet == numberPreviousSet) { @@ -33,7 +33,7 @@ public void ChangeNumbersSet(int numberNewSet, int numberPreviousSet) /// New set number public void ChangeOneVertexSet(int vertex, int numberSet) { - var walker = Head; + var walker = head; while(walker != null) { if (walker.Vertex == vertex) @@ -49,9 +49,9 @@ public void ChangeOneVertexSet(int vertex, int numberSet) /// /// The vertex for which we return /// Return -1 if not found and the set number if found - public int FromWichSet(int vertex) + public int SearchForASuitableSet(int vertex) { - var walker = Head; + var walker = head; while(walker != null) { if (walker.Vertex == vertex) @@ -69,17 +69,17 @@ public int FromWichSet(int vertex) /// The number of the vertex to be added to the list virtual public void AddElement(int vertex) { - if (Head == null) + if (head == null) { var item = new ListElement(vertex); - Head = item; - Tail = item; + head = item; + tail = item; } else { var item = new ListElement(vertex); - Tail.Next = item; - Tail = item; + tail.Next = item; + tail = item; } } diff --git a/Routers/Routers/NullPointerException.cs b/Routers/Routers/NullPointerException.cs deleted file mode 100644 index b1a6a39..0000000 --- a/Routers/Routers/NullPointerException.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Routers; - -/// -/// Exception for an empty pointer -/// -public class NullPointerException : Exception { } \ No newline at end of file diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs index 43a889c..7469f3c 100644 --- a/Routers/Routers/Program.cs +++ b/Routers/Routers/Program.cs @@ -1,38 +1,30 @@ -namespace ListAndUniqueList; +using RoutersByGraph; -using Routers; - -class Program +var routers = new Routers(); +Console.WriteLine("Enter the file path"); +var filePath = Console.ReadLine(); +Console.Write("Enter file name where to write a new graph"); +var fileAfter = Console.ReadLine(); +bool isLinkedGraph = true; +try +{ + isLinkedGraph = routers.WorkWithFile(filePath, fileAfter); +} +catch (NullGraphOrGraphComponentsException) +{ + Console.WriteLine("Problems with graph, an incorrect example in the file is possible"); +} + catch (InvalidFileException) { - public static int Main(string[] args) - { - var routers = new Routers(); - Console.WriteLine("Enter the file path with double slashes"); - var filePath = Console.ReadLine(); - Console.Write("Enter file name where to write a new graph"); - var fileAfter = Console.ReadLine(); - bool isLinkedGraph = true; - try - { - isLinkedGraph = routers.WorkWithFile(filePath, fileAfter); - } - catch (NullPointerException) - { - Console.WriteLine("Problems with pointers, an incorrect example in the file is possible"); - } - catch(InvalidFileException) - { Console.WriteLine("Problems with the path to the file or the contents of the file"); - } - catch (FileNotFoundException) - { - Console.WriteLine("Problems with incorrect way to file"); - } - if (!isLinkedGraph) - { - Console.WriteLine("Graph not Linked", Console.Error); - return -1; - } - return 0; - } -} \ No newline at end of file +} +catch (FileNotFoundException) +{ + Console.WriteLine("Problems with incorrect way to file"); +} +if (!isLinkedGraph) +{ + Console.WriteLine("Graph not Linked", Console.Error); + return -1; +} +return 0; \ No newline at end of file diff --git a/Routers/Routers/Routers.cs b/Routers/Routers/Routers.cs index b88890a..b271dcd 100644 --- a/Routers/Routers/Routers.cs +++ b/Routers/Routers/Routers.cs @@ -1,4 +1,4 @@ -namespace Routers; +namespace RoutersByGraph; /// /// A class for implementing finding a spanning tree and printing its file by retrieving data from a file @@ -18,7 +18,7 @@ private void WriteToFile(Graph graph, string filePath, string fileAfter) /// Throws an exception if the entry in the file is uncorrected public bool WorkWithFile(string filePath, string fileAfter) { - var file = new StreamReader(filePath); + using var file = new StreamReader(filePath); if (file == null) { throw new InvalidFileException(); @@ -41,7 +41,9 @@ public bool WorkWithFile(string filePath, string fileAfter) ++i; while (Char.IsDigit(line[i])) { - mainVertex = mainVertex * 10 + line[i] - 48; + int result = 0; + bool isCorrect = int.TryParse(line[i].ToString(), out result); + mainVertex = mainVertex * 10 + ++i; } diff --git a/Routers/TestsForGraph/TestsForGraph.cs b/Routers/TestsForGraph/TestsForGraph.cs index 0461238..944b95c 100644 --- a/Routers/TestsForGraph/TestsForGraph.cs +++ b/Routers/TestsForGraph/TestsForGraph.cs @@ -1,9 +1,10 @@ namespace TestsFotGraph; -using Routers; +using RoutersByGraph; + public class Tests { - Graph graph; + private Graph graph; [SetUp] public void Setup() @@ -11,61 +12,55 @@ public void Setup() graph = new Graph(); } - [TestCaseSource(nameof(GraphForTest))] - public void EmptyGraphShouldBeEmpty(Graph graph) + [Test] + public void EmptyGraphShouldBeEmpty() { Assert.True(graph.IsEmpty()); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldThrowExceptionAfterTryGetSizeWhileItEmpty(Graph graph) + [Test] + public void GraphShouldThrowExceptionAfterTryGetSizeWhileItEmpty() { - Assert.Throws(() => graph.ReturnSizeGraph()); + Assert.That(graph.Size(), Is.EqualTo(0)); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldReturnNullAfterTryGetListArcsWhileItEmpty(Graph graph) + [Test] + public void GraphShouldReturnNullAfterTryGetListArcsWhileItEmpty() { - Assert.True(graph.ReturnListArcs() == null); + Assert.That(graph.ReturnListArcs(), Is.EqualTo(null)); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldReturnNullAfterTryGetListVertexesWhileItEmpty(Graph graph) + [Test] + public void GraphShouldReturnNullAfterTryGetListVertexesWhileItEmpty() { - Assert.True(graph.ReturnListVertexes() == null); + Assert.That(graph.ReturnListVertexes(), Is.EqualTo(null)); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldThrowExceptionAfterTryWriteInFileWhileGraphEmpty(Graph graph) + [Test] + public void GraphShouldThrowExceptionAfterTryWriteInFileWhileGraphEmpty() { - Assert.Throws(() => graph.WriteToFile("v_v", "^_^")); + Assert.Throws(() => graph.WriteToFile("v_v", "^_^")); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldThrowExceptionAfterTryUseKraskalAlgorithmWhileGraphEmpty(Graph graph) + [Test] + public void GraphShouldThrowExceptionAfterTryUseKraskalAlgorithmWhileGraphEmpty() { - Assert.Throws(() => graph.KraskalAlgorithm(graph)); + Assert.Throws(() => graph.KraskalAlgorithm(graph)); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldReturnCorrectSizeAfterAddding(Graph graph) + [Test] + public void GraphShouldReturnCorrectSizeAfterAddding() { graph.AddArcs(1, 2, 3); graph.AddVertexes(2); - Assert.True(graph.ReturnSizeGraph() == 2); + Assert.That(2, Is.EqualTo(graph.Size())); } - [TestCaseSource(nameof(GraphForTest))] - public void GraphShouldBeNotEmptyAfterAddding(Graph graph) + [Test] + public void GraphShouldBeNotEmptyAfterAddding() { graph.AddArcs(1, 2, 3); graph.AddVertexes(2); Assert.False(graph.IsEmpty()); } - - private static IEnumerable GraphForTest - => new TestCaseData[] - { - new TestCaseData(new Graph()), - }; } \ No newline at end of file diff --git a/Routers/TestsForRouters/TestForRouters.cs b/Routers/TestsForRouters/TestForRouters.cs index 14fccfe..1688d04 100644 --- a/Routers/TestsForRouters/TestForRouters.cs +++ b/Routers/TestsForRouters/TestForRouters.cs @@ -1,10 +1,10 @@ namespace TestsForRouters; -using Routers; +using RoutersByGraph; public class Tests { - Routers routers; + private Routers routers; [SetUp] public void Setup() @@ -12,62 +12,62 @@ public void Setup() routers = new Routers(); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfThereIsAnInvalidCharacterInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfThereIsAnInvalidCharacterInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidSymbol.txt"), "dsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfFirstSymbolIsNotNumberInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfFirstSymbolIsNotNumberInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidFirstSymbol.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfSpaceNotAferNotMainVertexInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfSpaceNotAferNotMainVertexInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterVertexNotMain.txt"), "dsds")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfColonAbsentAfterMainVertexInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfColonAbsentAfterMainVertexInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutColonAfterMainVertex.txt"), "sddsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfSpaceAbsentAfterColonInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfSpaceAbsentAfterColonInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterColon.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfOpeningParenthesisAbsentAfterVertexWithSpaceInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfOpeningParenthesisAbsentAfterVertexWithSpaceInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutOpeningParenthesis.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfNotNumberAfteropeningParenthesisInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfNotNumberAfteropeningParenthesisInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutNumberAfterOpeningParenthesis.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfCloseingParenthesisAbsentInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfCloseingParenthesisAbsentInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCloseingParenthesis.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldThrowAnExceptionIfAfterCloseingParenthesisCommaAbsentInTheFile(Routers routers) + [Test] + public void RoutersShouldThrowAnExceptionIfAfterCloseingParenthesisCommaAbsentInTheFile() { Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCommaAfterCloseingParenthesis.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldWorkCorrectlyWithSimpleExpression(Routers routers) + [Test] + public void RoutersShouldWorkCorrectlyWithSimpleExpression() { bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt"), Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt.new")); if (!isLink) @@ -76,23 +76,23 @@ public void RoutersShouldWorkCorrectlyWithSimpleExpression(Routers routers) } string afterAlgorithm = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt.new")); string correctResult = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithFirstCorrect.txt")); - Assert.True(afterAlgorithm == correctResult); + Assert.That(correctResult, Is.EqualTo(afterAlgorithm)); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldWorkCorrectlyWithSimpleExpressionWhereGraphNotLinked(Routers routers) + [Test] + public void RoutersShouldWorkCorrectlyWithSimpleExpressionWhereGraphNotLinked() { Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraph.txt"), Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraph.txt.new"))); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldWorkCorrectlyWithDificultExpressionWhereGraphNotLinked(Routers routers) + [Test] + public void RoutersShouldWorkCorrectlyWithDificultExpressionWhereGraphNotLinked() { Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraphMoreDificult.txt"), "sdsd")); } - [TestCaseSource(nameof(RoutersForTest))] - public void RoutersShouldWorkCorrectlyWithDificultExpression(Routers routers) + [Test] + public void RoutersShouldWorkCorrectlyWithDificultExpression() { bool isLink = routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt"), Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt.new")); if (!isLink) @@ -101,12 +101,6 @@ public void RoutersShouldWorkCorrectlyWithDificultExpression(Routers routers) } string afterAlgorithm = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithMoreDifficultExpression.txt.new")); string correctResult = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithCorrectExpressionForMoreDifficultExpression.txt")); - Assert.True(afterAlgorithm == correctResult); + Assert.That(correctResult, Is.EqualTo(afterAlgorithm)); } - - private static IEnumerable RoutersForTest - => new TestCaseData[] - { - new TestCaseData(new Routers()), - }; } \ No newline at end of file diff --git a/Routers/TestsForTwoLists/TestsForTwoLists.cs b/Routers/TestsForTwoLists/TestsForTwoLists.cs index befb7cf..96e675a 100644 --- a/Routers/TestsForTwoLists/TestsForTwoLists.cs +++ b/Routers/TestsForTwoLists/TestsForTwoLists.cs @@ -1,10 +1,10 @@ namespace TestsFotGraph; -using NuGet.Frameworks; -using Routers; +using RoutersByGraph; + public class Tests { - ListVertexes list; + private ListVertexes list; [SetUp] public void Setup() @@ -12,8 +12,8 @@ public void Setup() list = new ListVertexes(); } - [TestCaseSource(nameof(ListForTest))] - public void InStartNumberSetForAllVertexesShouldZero(ListVertexes list) + [Test] + public void InStartNumberSetForAllVertexesShouldZero() { Graph graph = new Graph(); graph.AddVertexes(3); @@ -23,33 +23,29 @@ public void InStartNumberSetForAllVertexesShouldZero(ListVertexes list) var anotherList = graph.ReturnListVertexes(); for (int i = 1; i <= 3; ++i) { - if (anotherList.FromWichSet(i) != 0) - { - Assert.Fail(); - } + Assert.AreEqual(anotherList.SearchForASuitableSet(i), 0); } - Assert.True(true); } - [TestCaseSource(nameof(ListForTest))] - public void EmptyListVertexesShouldReturnMinusOneAfterTryToReturnNumberSetIfVertexNotInList(ListVertexes list) + [Test] + public void EmptyListVertexesShouldReturnMinusOneAfterTryToReturnNumberSetIfVertexNotInList() { - Assert.True(list.FromWichSet(1) == -1); + Assert.True(list.SearchForASuitableSet(1) == -1); } - [TestCaseSource(nameof(ListForTest))] - public void ListVertexesShouldChangedNumberSet(ListVertexes list) + [Test] + public void ListVertexesShouldChangedNumberSet() { Graph graph = new Graph(); graph.AddVertexes(3); graph.AddArcs(1, 2, 3); var anotherList = graph.ReturnListVertexes(); anotherList.ChangeOneVertexSet(2, 10); - Assert.True(anotherList.FromWichSet(2) == 10); + Assert.AreEqual(anotherList.SearchForASuitableSet(2), 10); } - [TestCaseSource(nameof(ListForTest))] - public void ListVertexesShouldChangedAllVertexesNumberSet(ListVertexes list) + [Test] + public void ListVertexesShouldChangedAllVertexesNumberSet() { Graph graph = new Graph(); graph.AddVertexes(3); @@ -58,24 +54,14 @@ public void ListVertexesShouldChangedAllVertexesNumberSet(ListVertexes list) anotherList.ChangeNumbersSet(10, 0); for (int i = 1; i <= 3; ++i) { - if (anotherList.FromWichSet(i) != 10) - { - Assert.Fail(); - } + Assert.AreEqual(anotherList.SearchForASuitableSet(i), 10); } - Assert.True(true); } [Test] public void ListArcsShouldThrowExceptionIfTryToSortNullPointer() { - var list = new ListArcs(); - Assert.Throws(() => list.SortListArcs(true)); + var list = new ListEdges(); + Assert.Throws< NullGraphOrGraphComponentsException>(() => list.SortListArcs(true)); } - - private static IEnumerable ListForTest - => new TestCaseData[] - { - new TestCaseData(new ListVertexes()), - }; } \ No newline at end of file From 4b2f55c4cb79eb1ec5a69488aeff6b14dc6edf10 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 03:19:24 +0300 Subject: [PATCH 09/10] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B5=D0=B2=D1=8C?= =?UTF-8?q?=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers/ListEdges.cs | 223 +++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 Routers/Routers/ListEdges.cs diff --git a/Routers/Routers/ListEdges.cs b/Routers/Routers/ListEdges.cs new file mode 100644 index 0000000..bd4f2ac --- /dev/null +++ b/Routers/Routers/ListEdges.cs @@ -0,0 +1,223 @@ +namespace RoutersByGraph; + +/// +/// A list of arcs consisting of a pair of vertices and arcs between them +/// +public class ListEdges +{ + private ListElement? head; + private ListElement? tail; + + private bool SortByVertexOrArcs(bool sortByVertex, ListElement firstListElement, ListElement secondListElement) + { + return sortByVertex ? firstListElement.FromVertex > secondListElement.FromVertex : firstListElement.SizeWay < secondListElement.SizeWay; + } + + /// + /// Sorting by the list bubble method + /// + /// The parameter for selecting a comparison in the subsequent sorting is true if by vertices and false if by arcs + /// Throws an exception if the list is empty + public void SortListArcs(bool sortByVertex) + { + if (head == null) + { + throw new NullGraphOrGraphComponentsException(); + } + int size = head.SizeListArcs; + for(int i = 0; i < size; ++i) + { + var walker = head; + for(int j = 0; j < size; ++j) + { + if (walker.Next != null && SortByVertexOrArcs(sortByVertex, walker, walker.Next)) + { + var item = new ListElement(walker.FromVertex, walker.ToVertex, walker.SizeWay); + walker.SizeWay = walker.Next.SizeWay; + walker.FromVertex = walker.Next.FromVertex; + walker.ToVertex = walker.Next.ToVertex; + walker.Next.SizeWay = item.SizeWay; + walker.Next.ToVertex = item.ToVertex; + walker.Next.FromVertex = item.FromVertex; + } + walker = walker.Next; + } + } + } + + /// + /// Removes arcs from the list + /// + /// The vertex from which the arc goes + /// The vertex to which the arc goes + /// Throws an exception if the list is empty + private void DeleteArc(int fromVertex, int toVertex) + { + if (head == null) + { + throw new NullGraphOrGraphComponentsException(); + } + var walker = head; + if (walker.FromVertex == fromVertex && walker.ToVertex == toVertex) + { + head = head.Next; + return; + } + while (walker.Next != null) + { + if (walker.Next.FromVertex == fromVertex && walker.Next.ToVertex == toVertex) + { + walker.Next = walker.Next.Next; + return; + } + walker = walker.Next; + } + } + + /// + /// Kraskal's algorithm for a minimal spanning tree + /// + /// The graph in which everything is stored + /// Returns false if the tree is not connected + /// Throws an exception if the graph and its components are empty + public bool KraskalAlgorithm(Graph graph) + { + if (graph == null || graph.IsEmpty()) + { + throw new NullGraphOrGraphComponentsException(); + } + var listArcs = graph.ReturnListArcs(); + var listVertexes = graph.ReturnListVertexes(); + if (listArcs == null || listVertexes == null || head == null) + { + throw new NullReferenceException(); + } + listArcs.SortListArcs(false); + var walker = listArcs.head; + int set = 1; + int anotherSet = 0; + while (walker != null) + { + if (walker.ToVertex != walker.FromVertex) + { + int returnedNumberPlentyFirstVertex = listVertexes.SearchForASuitableSet(walker.FromVertex); + int returnedNumberPlentySecondVertex = listVertexes.SearchForASuitableSet(walker.ToVertex); + + if (returnedNumberPlentyFirstVertex != returnedNumberPlentySecondVertex) + { + if (returnedNumberPlentyFirstVertex == 0 && returnedNumberPlentySecondVertex != 0) + { + listVertexes.ChangeOneVertexSet(walker.FromVertex, returnedNumberPlentySecondVertex); + --anotherSet; + } + else if (returnedNumberPlentyFirstVertex != 0 && returnedNumberPlentySecondVertex == 0) + { + listVertexes.ChangeOneVertexSet(walker.ToVertex, returnedNumberPlentyFirstVertex); + --anotherSet; + } + else + { + listVertexes.ChangeNumbersSet(returnedNumberPlentyFirstVertex, returnedNumberPlentySecondVertex); + --anotherSet; + } + } + else if (returnedNumberPlentyFirstVertex == 0) + { + listVertexes.ChangeOneVertexSet(walker.FromVertex, set); + listVertexes.ChangeOneVertexSet(walker.ToVertex, set); + ++set; + ++anotherSet; + } + else + { + DeleteArc(walker.FromVertex, walker.ToVertex); + --head.SizeListArcs; + } + } + else + { + DeleteArc(walker.FromVertex, walker.ToVertex); + --head.SizeListArcs; + } + walker = walker.Next; + } + SortListArcs(true); + return !(walker == null && anotherSet != 1 && anotherSet != 0); + } + + /// + /// Adds a new item to the list + /// + /// The vertex from which the arc originates + /// The vertex that the arc enters + /// Arc Size + public void AddElement(int fromVertex, int toVertex, int sizeWay) + { + var item = new ListElement(fromVertex, toVertex, sizeWay); + if (head == null) + { + head = item; + } + else + { + tail.Next = item; + } + tail = item; + ++head.SizeListArcs; + } + + /// + /// Writing to the arc list file + /// + /// The path to the file + public void WirteToFile(string fileAfter) + { + var file = new StreamWriter(fileAfter); + var walker = head; + int previousMainVertex = 0; + bool isFirst = true; + while (walker != null) + { + if (walker.FromVertex != previousMainVertex) + { + if (!isFirst) + { + file.WriteLine(); + } + file.Write(walker.FromVertex); + file.Write(": "); + previousMainVertex = walker.FromVertex; + isFirst = false; + } + else + { + file.Write(", "); + } + file.Write(walker.ToVertex); + file.Write(" ("); + file.Write(walker.SizeWay); + file.Write(")"); + walker = walker.Next; + } + file.Close(); + } + + private class ListElement + { + public ListElement(int fromVertex, int toVertex, int sizeWay) + { + FromVertex = fromVertex; + ToVertex = toVertex; + SizeWay = sizeWay; + } + + public int FromVertex { get; set; } + + public int ToVertex { get; set; } + public ListElement? Next { get; set; } + + public int SizeWay { get; set; } + + public int SizeListArcs { get; set; } + } +} From 2458990f0587a07a441c47f47dab02b3748a9fa7 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 03:24:11 +0300 Subject: [PATCH 10/10] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B5=D0=B2=D1=8C?= =?UTF-8?q?=D1=8E2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routers/Routers/NullGraphOrGraphComponentsException.cs | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Routers/Routers/NullGraphOrGraphComponentsException.cs diff --git a/Routers/Routers/NullGraphOrGraphComponentsException.cs b/Routers/Routers/NullGraphOrGraphComponentsException.cs new file mode 100644 index 0000000..7790064 --- /dev/null +++ b/Routers/Routers/NullGraphOrGraphComponentsException.cs @@ -0,0 +1,6 @@ +namespace RoutersByGraph; + +/// +/// Throws exception when graph is empty or the components of the graph are empty +/// +public class NullGraphOrGraphComponentsException : Exception { } \ No newline at end of file