// TRAII_25_X4_test.java SJ import fi.uef.cs.tra.DiGraph; import fi.uef.cs.tra.Vertex; import java.util.*; public class TRAII_25_X4_test { static TRAII_25_X4 myClass = new TRAII_25_X4_skeleton(); // ^^^^ own id here static Random rnd = new Random(); public static void main(String[] args) { int treeSize = 10; if (args.length > 0) treeSize = Integer.parseInt(args[0]); int randomSeed = 42; if (args.length > 1) randomSeed = Integer.parseInt(args[1]); rnd.setSeed(randomSeed); int print = 5; if (args.length > 2) print = Integer.parseInt(args[2]); boolean ok1 = true; ok1 &= testRootNodes(0, 0, print); ok1 &= testRootNodes(0, 1, print); ok1 &= testRootNodes(1, 2, print); ok1 &= testRootNodes(2, 0, print); ok1 &= testRootNodes(0, 2, print); ok1 &= testRootNodes(treeSize, treeSize, print); ok1 &= testRootNodes(treeSize, treeSize, print); rnd.setSeed(System.currentTimeMillis()); int nTest = 200; int k; int errors = 0; for (k = 0; k < nTest; k++) { if (!testRootNodes(rnd.nextInt(k+1), rnd.nextInt(k+1), 0)) errors++; if (errors > 30) break; } if (errors > 0) ok1 = false; System.out.println("\nOut of " + k + " random root tests " + (k - errors) + " correct."); if (ok1) System.out.println("\nAll root tests gave correct answer."); else System.out.println("\nSome root tests had errors."); rnd.setSeed(randomSeed); boolean ok2 = true; ok2 &= testTreeCount(0, 0, print, false); ok2 &= testTreeCount(0, 1, print, false); ok2 &= testTreeCount(1, 2, print, false); ok2 &= testTreeCount(2, 0, print, true); ok2 &= testTreeCount(0, 2, print, false); ok2 &= testTreeCount(3, 3, print, true); ok2 &= testTreeCount(5, 5, print, false); ok2 &= testTreeCount(treeSize, treeSize, print, false); ok2 &= testTreeCount(treeSize, treeSize, print, true); rnd.setSeed(System.currentTimeMillis()); nTest = 200; k = 0; errors = 0; while (k < nTest) { k++; if (!testTreeCount(rnd.nextInt(k+1), rnd.nextInt(k+1), 0, true)) errors++; if (errors > 30) break; } if (errors > 0) ok2 = false; System.out.println("\nOut of " + k + " random tree count tests " + (k - errors) + " correct."); if (ok2) System.out.println("\nAll tree count tests gave correct answer."); else System.out.println("\nSome tree count tests had errors."); if (ok1 && ok2) System.out.println("\nAll tests gave correct answer."); else System.out.println("\nSome tests had errors."); } // main() static DiGraph makeInput(int trees, int others, Set roots, boolean shuffle) { DiGraph G = new DiGraph(); // here vertex names are given so that trees and others are easy to differ // DO NOT EVEN THINK ABOUT DISTINGUISHING THE TREES BASED ON VERTEX NAMES // OR VERTEX ORDER for (int i = 0; i < trees; i++) { Vertex root = G.addVertex("P" + i + "."); roots.add(root); GraphMaker.growTree(G, root, rnd, rnd.nextInt(trees)); } for (int i = 0; i < others; i++) { Vertex node = G.addVertex("M" + i + "."); roots.add(node); GraphMaker.makeNonTree(G, node, rnd, rnd.nextInt(others)+2, rnd.nextInt(others)+5, rnd.nextBoolean(), roots); } if (shuffle) return GraphMaker.shuffleCopy(G); // take new version of GraphMaker from Moodle else return G; } static boolean testRootNodes(int trees, int others, int print) { if (print > 0) System.out.println("\nRoot test, trees=" + trees + " others="+others); Set reference = new HashSet<>(); DiGraph G = makeInput(trees, others, reference, false); if (print > 4 && G.size() < 30) { System.out.println("Input:"); System.out.print(GraphMaker.toString(G, 1)); } if (print > 3 && reference.size() < 30) { System.out.println("Expected roots: " + reference); } Set juuret = myClass.rootVertics(G); if (print > 2 && reference.size() < 30) { System.out.println("Result roots: " + juuret); } if (reference.equals(juuret)) { if (print > 0) System.out.println("Root test ok"); return true; } else { System.out.println("Root set does not match expected."); return false; } } static boolean testTreeCount(int trees, int others, int print, boolean shuffle) { if (print > 0) System.out.println("\nTree count test, trees=" + trees + " others="+others); Set reference = new HashSet<>(); DiGraph G = makeInput(trees, others, reference, shuffle); if (print > 4 && G.size() < 20) { System.out.println("Input:"); System.out.print(GraphMaker.toString(G, 1)); } int result = myClass.treeCount(G); if (print > 1) { System.out.println("Result: " + result + ", expected result: " + trees); } if (result == trees) { if (print > 0) System.out.println("Tree count test ok"); return true; } else { System.out.println("Tree count is not expected: " + result + ", expected result: " + trees); if (print > 2 && G.size() < 50) { System.out.println("Input was:"); System.out.print(GraphMaker.toString(G, 1)); } return false; } } }