diff --git a/Routers/Routers.sln b/Routers/Routers.sln new file mode 100644 index 0000000..4b2fc7f --- /dev/null +++ b/Routers/Routers.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +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}") = "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 + 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 + {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 + {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 + 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..76e386b --- /dev/null +++ b/Routers/Routers/Graph.cs @@ -0,0 +1,105 @@ +namespace RoutersByGraph; + +/// +/// A container consisting of two lists List Arcs, ListVertexes and its own size +/// +public class Graph +{ + /// + /// Returns graph size + /// + /// Graph size + /// If the graph is empty throw exception + public int Size() => sizeGraph; + + /// + /// 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, string fileAfter) + { + if (edges == null || vertexes == null) + { + throw new NullGraphOrGraphComponentsException(); + } + edges.WirteToFile(filePath); + } + + /// + /// Function to return ListArcs + /// + /// ListArcs in Graph + public ListEdges? ReturnListArcs() => edges; + + + /// + /// Function to return ListVertexes + /// + /// ListVertexes + public ListVertexes? ReturnListVertexes() => vertexes; + + /// + /// 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 edges == null || 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 (edges == null) + { + edges = new ListEdges(); + } + edges.AddElement(fromVertex, toVertex, sizeWay); + } + + /// + /// Initializes the list of vertices in the graph + /// + /// The size of the future graph + public void AddVertexes(int size) + { + if (vertexes == null) + { + vertexes = new ListVertexes(); + } + sizeGraph = size; + for(int i = 1; i <= sizeGraph; i++) + { + vertexes.AddElement(i); + } + } + + /// + /// 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 (edges == null) + { + throw new NullGraphOrGraphComponentsException(); + } + return edges.KraskalAlgorithm(graph); + } + + private ListVertexes? vertexes { get; set; } + + private ListEdges? edges { get; set; } + + private int sizeGraph { 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..68a0557 --- /dev/null +++ b/Routers/Routers/InvalidFileException.cs @@ -0,0 +1,6 @@ +namespace RoutersByGraph; + +/// +/// 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/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; } + } +} diff --git a/Routers/Routers/ListVertexes.cs b/Routers/Routers/ListVertexes.cs new file mode 100644 index 0000000..bb58c77 --- /dev/null +++ b/Routers/Routers/ListVertexes.cs @@ -0,0 +1,98 @@ +namespace RoutersByGraph; + +/// 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 SearchForASuitableSet(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/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 diff --git a/Routers/Routers/Program.cs b/Routers/Routers/Program.cs new file mode 100644 index 0000000..7469f3c --- /dev/null +++ b/Routers/Routers/Program.cs @@ -0,0 +1,30 @@ +using RoutersByGraph; + +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) +{ + 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 new file mode 100644 index 0000000..b271dcd --- /dev/null +++ b/Routers/Routers/Routers.cs @@ -0,0 +1,163 @@ +namespace RoutersByGraph; + +/// +/// 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, string fileAfter) + { + graph.WriteToFile(filePath, fileAfter); + } + + /// + /// 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, string fileAfter) + { + using var file = new StreamReader(filePath); + if (file == null) + { + throw new InvalidFileException(); + } + 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])) + { + int result = 0; + bool isCorrect = int.TryParse(line[i].ToString(), out result); + mainVertex = mainVertex * 10 + + ++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, fileAfter); + + return true; + } +} diff --git a/Routers/Routers/Routers.csproj b/Routers/Routers/Routers.csproj new file mode 100644 index 0000000..91f3888 --- /dev/null +++ b/Routers/Routers/Routers.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/Routers/TestsForGraph/TestsForGraph.cs b/Routers/TestsForGraph/TestsForGraph.cs new file mode 100644 index 0000000..944b95c --- /dev/null +++ b/Routers/TestsForGraph/TestsForGraph.cs @@ -0,0 +1,66 @@ +namespace TestsFotGraph; + +using RoutersByGraph; + +public class Tests +{ + private Graph graph; + + [SetUp] + public void Setup() + { + graph = new Graph(); + } + + [Test] + public void EmptyGraphShouldBeEmpty() + { + Assert.True(graph.IsEmpty()); + } + + [Test] + public void GraphShouldThrowExceptionAfterTryGetSizeWhileItEmpty() + { + Assert.That(graph.Size(), Is.EqualTo(0)); + } + + [Test] + public void GraphShouldReturnNullAfterTryGetListArcsWhileItEmpty() + { + Assert.That(graph.ReturnListArcs(), Is.EqualTo(null)); + } + + [Test] + public void GraphShouldReturnNullAfterTryGetListVertexesWhileItEmpty() + { + Assert.That(graph.ReturnListVertexes(), Is.EqualTo(null)); + } + + [Test] + public void GraphShouldThrowExceptionAfterTryWriteInFileWhileGraphEmpty() + { + Assert.Throws(() => graph.WriteToFile("v_v", "^_^")); + } + + [Test] + public void GraphShouldThrowExceptionAfterTryUseKraskalAlgorithmWhileGraphEmpty() + { + Assert.Throws(() => graph.KraskalAlgorithm(graph)); + } + + [Test] + public void GraphShouldReturnCorrectSizeAfterAddding() + { + graph.AddArcs(1, 2, 3); + graph.AddVertexes(2); + Assert.That(2, Is.EqualTo(graph.Size())); + } + + [Test] + public void GraphShouldBeNotEmptyAfterAddding() + { + graph.AddArcs(1, 2, 3); + graph.AddVertexes(2); + Assert.False(graph.IsEmpty()); + } +} \ No newline at end of file diff --git a/Routers/TestsForGraph/TestsForGraph.csproj b/Routers/TestsForGraph/TestsForGraph.csproj new file mode 100644 index 0000000..021ce04 --- /dev/null +++ b/Routers/TestsForGraph/TestsForGraph.csproj @@ -0,0 +1,23 @@ + + + + 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..1688d04 --- /dev/null +++ b/Routers/TestsForRouters/TestForRouters.cs @@ -0,0 +1,106 @@ +namespace TestsForRouters; + +using RoutersByGraph; + +public class Tests +{ + private Routers routers; + + [SetUp] + public void Setup() + { + routers = new Routers(); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfThereIsAnInvalidCharacterInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidSymbol.txt"), "dsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfFirstSymbolIsNotNumberInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithInvalidFirstSymbol.txt"), "sdsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfSpaceNotAferNotMainVertexInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterVertexNotMain.txt"), "dsds")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfColonAbsentAfterMainVertexInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutColonAfterMainVertex.txt"), "sddsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfSpaceAbsentAfterColonInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutSpaceAfterColon.txt"), "sdsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfOpeningParenthesisAbsentAfterVertexWithSpaceInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutOpeningParenthesis.txt"), "sdsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfNotNumberAfteropeningParenthesisInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutNumberAfterOpeningParenthesis.txt"), "sdsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfCloseingParenthesisAbsentInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCloseingParenthesis.txt"), "sdsd")); + } + + [Test] + public void RoutersShouldThrowAnExceptionIfAfterCloseingParenthesisCommaAbsentInTheFile() + { + Assert.Throws(() => routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithoutCommaAfterCloseingParenthesis.txt"), "sdsd")); + } + + [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) + { + 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.That(correctResult, Is.EqualTo(afterAlgorithm)); + } + + [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"))); + } + + [Test] + public void RoutersShouldWorkCorrectlyWithDificultExpressionWhereGraphNotLinked() + { + Assert.False(routers.WorkWithFile(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestsForRouters", "fileWithNotLinkedGraphMoreDificult.txt"), "sdsd")); + } + + [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) + { + 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.That(correctResult, Is.EqualTo(afterAlgorithm)); + } +} \ 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/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/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..cbce792 --- /dev/null +++ b/Routers/TestsForRouters/bin/Debug/net7.0/TestsForRouters/fileWithMoreDifficultExpression.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/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 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..96e675a --- /dev/null +++ b/Routers/TestsForTwoLists/TestsForTwoLists.cs @@ -0,0 +1,67 @@ +namespace TestsFotGraph; + +using RoutersByGraph; + +public class Tests +{ + private ListVertexes list; + + [SetUp] + public void Setup() + { + list = new ListVertexes(); + } + + [Test] + public void InStartNumberSetForAllVertexesShouldZero() + { + 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) + { + Assert.AreEqual(anotherList.SearchForASuitableSet(i), 0); + } + } + + [Test] + public void EmptyListVertexesShouldReturnMinusOneAfterTryToReturnNumberSetIfVertexNotInList() + { + Assert.True(list.SearchForASuitableSet(1) == -1); + } + + [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.AreEqual(anotherList.SearchForASuitableSet(2), 10); + } + + [Test] + public void ListVertexesShouldChangedAllVertexesNumberSet() + { + 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) + { + Assert.AreEqual(anotherList.SearchForASuitableSet(i), 10); + } + } + + [Test] + public void ListArcsShouldThrowExceptionIfTryToSortNullPointer() + { + var list = new ListEdges(); + Assert.Throws< NullGraphOrGraphComponentsException>(() => list.SortListArcs(true)); + } +} \ 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