public void Testing_NodeBased_Constructor() { // Test CSC382Graph_NodeBased Constructor(s) //default constructor CSC382Graph_NodeBased <int> numbersGraph = new CSC382Graph_NodeBased <int>(); //verify that the graph is not null and was created successfully as a blank graph Assert.IsNotNull(numbersGraph.Graph); //therefore the first node of the graph should be null Assert.IsNull(numbersGraph.Graph.First); //and the length of the graph should be zero Assert.AreEqual(0, numbersGraph.Graph.Count); // Test creating lots of nodes and edges Random ran = new Random(); int nodeCount = ran.Next(20, 51); for (int i = 0; i < nodeCount; i++) { numbersGraph.Insert(ran.Next()); } Assert.AreEqual(nodeCount, numbersGraph.Size()); //Test functions of the CSC382Graph_NodeBased class and affiliated classes }
public void Testing_NodeBased1() { // Test CSC382Graph_NodeBased Constructor(s) CSC382Graph_NodeBased <string> wordsGraph = new CSC382Graph_NodeBased <string>(); Assert.IsNull(wordsGraph.Graph.First); // Test creating lots of nodes and edges Random ran = new Random(); int nodeCount = ran.Next(20, 51); for (int i = 0; i < nodeCount; i++) { wordsGraph.Insert(ran.Next().ToString()); } Assert.AreEqual(nodeCount, wordsGraph.Size()); // Test functions of the CSC382Graph_NodeBased class and affiliated classes }
static public void Testing_NodeBased() { // Test CSC382Graph_NodeBased Constructor(s) CSC382Graph_Vertex <string> baseNode = new CSC382Graph_Vertex <string>("The answer is: "); CSC382Graph_NodeBased <string> stringGraph = new CSC382Graph_NodeBased <string> (baseNode); // Test creating lots of nodes string[] lines = File.ReadAllLines("num100.dat"); Console.WriteLine("Adding vertex nodes to the graph"); foreach (var line in lines) { bool exists = false; CSC382Graph_Vertex <string> temp = new CSC382Graph_Vertex <string>(line); foreach (var item in stringGraph.Graph) { //make sure there aren't duplicates in the graph if (item.GetData().Equals(temp.GetData())) { exists = true; break; } } if (!exists) { stringGraph.Insert(temp); } } // Test functions of the CSC382Graph_NodeBased class and affiliated classes //test size: should be 101 -- 1 for the baseNode, plust one for the 100 lines that are in the file we read in Console.WriteLine("The length of the string Graph is: " + stringGraph.Size()); Console.WriteLine(); CSC382Graph_Vertex <string> fortyTwo = new CSC382Graph_Vertex <string>("42"); bool found = stringGraph.FindVertex(fortyTwo) != default; Console.WriteLine("Let's try and find the node containing \"42\". "); Console.WriteLine("Did we find it? " + stringGraph.Graph.First().GetData() + found); if (found) { Console.WriteLine("Weird, \"42\" is not supposed to be in the Graph"); Console.WriteLine(); } else { Console.WriteLine("That's ok. I wasn't expecting it to be in there."); Console.WriteLine(); } //Now we find one that is in the list Console.WriteLine("How about the node containing \"47\". "); CSC382Graph_Vertex <string> fortySeven = stringGraph.FindVertex("47"); found = fortySeven != default; Console.WriteLine("Did we find it? " + stringGraph.Graph.First().GetData() + found); if (!found) { Console.WriteLine("Weird, \"47\" is supposed to be in the Graph"); Console.WriteLine(); } else { Console.WriteLine("Great!"); Console.WriteLine(); } Console.WriteLine("Let's add \"42\" to the Graph with an edge connecting the two"); stringGraph.Insert(fortyTwo); stringGraph.AddEdge(fortySeven, fortyTwo); Console.WriteLine("Is \"42\" connected to \"47\"?" + stringGraph.Graph.First().GetData() + stringGraph.IsEdge(fortySeven, fortyTwo)); Console.WriteLine(); Console.WriteLine("I know \"100\" is in the Graph. Let's see if it is connected to \"47\"."); found = fortySeven.VertexConnected(stringGraph.FindVertex("100")) != default; Console.WriteLine(stringGraph.Graph.First().GetData() + found); if (!found) { Console.WriteLine("I wander why?"); Console.WriteLine(); } else { Console.WriteLine("Great!"); Console.WriteLine(); } found = fortySeven.VertexConnected(fortyTwo) != default; Console.WriteLine("Let's verify that \"42\" got added with an edge to \"47\". " + stringGraph.Graph.First().GetData() + found); Console.WriteLine(); int edgeNum = 0; var current = stringGraph.Graph.First; while (current != null) { var compare = current.Next; while (compare != null) { if (stringGraph.IsEdge(current.Value, compare.Value)) { edgeNum++; } compare = compare.Next; } current = current.Next; } //at this point there should not be any edges except for the one between 42 and 47 Console.WriteLine("How many edges do we have in the Graph?" + stringGraph.Graph.First().GetData() + edgeNum); Console.WriteLine(); Console.WriteLine("Let's add some more edges so that all the nodes are connected"); current = stringGraph.Graph.First; while (current != null) { if (current.Next != null) { current.Value.AddEdge(current.Next.Value); current.Next.Value.AddEdge(current.Value); if (current.Next.Next != null) { current.Value.AddEdge(current.Next.Next.Value); current.Next.Next.Value.AddEdge(current.Value); } } current = current.Next; } Console.WriteLine(); edgeNum = 0; current = stringGraph.Graph.First; while (current != null) { var compare = current.Next; while (compare != null) { if (stringGraph.IsEdge(current.Value, compare.Value)) { edgeNum++; } compare = compare.Next; } current = current.Next; } //at this point there should not be any edges except for the one between 42 and 47 Console.WriteLine("How many edges do we have Now?" + stringGraph.Graph.First().GetData() + edgeNum); Console.WriteLine(); Console.WriteLine("the current graph edges are: "); current = stringGraph.Graph.First; while (current != null) { foreach (var item in current.Value.Connected_vertices) { Console.WriteLine(current.Value.GetData() + " -- " + item.GetData()); } current = current.Next; } Console.WriteLine(); }