Last active
March 26, 2018 17:51
-
-
Save AbhinavMadahar/7fc0ef54217865dc1499b327818d5cea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can set the strictness of the test suite by passing "loose", "normal", | |
// or "severe" as the first command-line argument to this Test class. | |
// If no level is passed, it defaults to NORMAL. | |
// | |
// LOOSE => always warn i.e. don't throw an exception to stop testing on first error | |
// NORMAL => throw an exception when a requirement is failed and a warning when a suggestion is found | |
// SEVERE => always throw an exception, treating warnings as errors | |
enum Strictness { | |
LOOSE, NORMAL, SEVERE | |
} | |
public class Test { | |
private static Strictness strictness; | |
// for throwing errors | |
private static void expect(Object a, Object b) { | |
// '^' is xor | |
if (a == null ^ b == null || (a != null && b != null && !a.equals(b))) { | |
if (strictness == Strictness.NORMAL || strictness == Strictness.SEVERE) { | |
throw new RuntimeException("ERROR: Expected " + a + " to equal " + b); | |
} else if (strictness == Strictness.LOOSE) { | |
System.err.println("WARNING: Expected " + a + " to equal " + b); | |
} | |
} | |
} | |
// for throwing warnings | |
private static void prefer(Object a, Object b) { | |
// '^' is xor | |
if (a == null ^ b == null || (a != null && b != null && !a.equals(b))) { | |
if (strictness == Strictness.SEVERE) { | |
throw new RuntimeException("ERROR: Expected " + a + " to equal " + b); | |
} else if (strictness == Strictness.NORMAL || strictness == Strictness.LOOSE) { | |
System.err.println("WARNING: Expected " + a + " to equal " + b); | |
} | |
} | |
} | |
private static void testLSEGetKeyword() throws RuntimeException { | |
System.out.println("TESTING LittleSearchEngine.getKeyword"); | |
final LittleSearchEngine lse = new LittleSearchEngine(); | |
// we individually insert noise words instead of loading them from a file | |
// to maintain simplicity | |
lse.noiseWords.add("through"); | |
prefer(lse.getKeyword(""), null); | |
expect(lse.getKeyword("abhinav"), "abhinav"); | |
expect(lse.getKeyword("abhinav."), "abhinav"); | |
expect(lse.getKeyword("abhinav.!"), "abhinav"); | |
expect(lse.getKeyword("abhinav,!?!!,,...!"), "abhinav"); | |
expect(lse.getKeyword("Abhinav"), "abhinav"); | |
expect(lse.getKeyword("Abhinav,"), "abhinav"); | |
expect(lse.getKeyword("Abhinav,!?!!,,...!"), "abhinav"); | |
expect(lse.getKeyword("distance."), "distance"); | |
expect(lse.getKeyword("equi-distant"), null); | |
expect(lse.getKeyword("Rabbit"), "rabbit"); | |
expect(lse.getKeyword("Through"), null); | |
expect(lse.getKeyword("we're"), null); | |
expect(lse.getKeyword("World..."), "world"); | |
expect(lse.getKeyword("World?!"), "world"); | |
expect(lse.getKeyword("What,ever"), null); | |
prefer(lse.getKeyword("heyyy hi"), null); | |
prefer(lse.getKeyword("print;"), "print"); | |
} | |
public static void main(String[] args) { | |
// determine the strictness based on the first passed argument | |
strictness = Strictness.NORMAL; | |
if (args.length > 0) { | |
switch (args[0]) { | |
case "loose": | |
strictness = Strictness.LOOSE; | |
break; | |
case "normal": | |
strictness = Strictness.NORMAL; | |
break; | |
case "severe": | |
strictness = Strictness.SEVERE; | |
break; | |
default: | |
throw new IllegalArgumentException("Cannot understand severity level " + args[0]); | |
} | |
} | |
System.out.println("Using severity level " + (args.length > 0 ? args[0] : "normal")); | |
System.out.println("TESTING LittleSearchEngine"); | |
testLSEGetKeyword(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment