Kotlinlearncs.online LogoJava
Return to List

Test Writing: Unweighted Graph Is Undirected

Created By: Geoffrey Challen
/ Version: 2021.10.0

Create a method named isUndirected. isUndirected accepts an UnweightedGraph<*> from cs1.graphs and should return true if the graph is undirected and false otherwise.

Each node in the graph has a set of neighbors that you can retrieve representing links from that node to other nodes. If the graph is undirected, if there is a link from A to B then there is also a link from B to A. So if B is a neighbor of A, then A should also be a neighbor of B.

Note that you do not need to solve this problem recursively, since there is a concise iterative solution.

For reference, cs1.graphs.UnweightedGraph is declared somewhat like this:

And cs1.graphs.GraphNode is declared somewhat like this:

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestGraphAnalysis with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods