import java.util.function.Predicate; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; /** * Some of SamR's tests of implementations of the directory interface. */ class SamDirectoryTests { // +-----------+--------------------------------------------------- // | Constants | // +-----------+ /** * Some fruits Mostly taken from https://simple.wikipedia.org/wiki/List_of_fruits. Some are fake. */ static String[] fruits = {"Apple", "Banana", "Cherry", "Durian", "Elderberry", "Fig", "Grape", "Huckleberry", "Ice", "Jackfruit", "Kiwi", "Lime", "Melon", "Nectarine", "Orange", "Papaya", "Quince", "Raspberry", "Strawberry", "Tamarind", "Ugli", "Vfruit", "Whitefruit", "Xuit", "Yuzu", "Zuit"}; /** * Some names. Mostly taken from https://en.wikipedia.org/wiki/Unisex_name */ static String[] names = {"Addison", "Blake", "Casey", "Delaney", "Elliot", "Flynn", "Gale", "Hadley", "Ivy", "Jody", "Kelly", "Lesley", "Madison", "Nash", "Odell", "Paige", "Quinn", "Reed", "Scout", "Tatum", "U", "Valentine", "Wallis", "Xavy", "You", "Zane"}; // +---------+----------------------------------------------------- // | Globals | // +---------+ Directory dir; // +-----------+--------------------------------------------------- // | Utilities | // +-----------+ /** * Set up the directory. */ void setup() { dir = new Directory(); } // setup /** * Set a bunch of strings to the directory. */ void setValues(Directory dir, String[] values) { for (String str : values) { String screen = str.substring(0, 1); dir.set(screen, str); } // for } // setValues /** * Check that the directory contains a set of strings. */ void checkValues(String[] strings, Directory dir) throws Exception { for (String str : strings) { assertEquals(str, dir.get(str.substring(0, 1))); } // for } // checkValues /** * Check that the directory contains a set of strings that meet a particular predicate. */ void checkValues(String[] strings, Predicate pred, Directory dir) throws Exception { for (String str : strings) { String screen = str.substring(0, 1); if (pred.test(screen)) { assertEquals(str, dir.get(screen)); } else { assertThrows(java.lang.Exception.class, () -> dir.get(screen)); } // if/else } // for } // checkValues // +-------+------------------------------------------------------- // | Tests | // +-------+ @Test void testBasic() throws Exception { setup(); setValues(dir, fruits); checkValues(fruits, dir); } // testBasic() @Test void testBackwards() throws Exception { setup(); String[] stiurf = new String[fruits.length]; for (int i = 0; i < fruits.length; i++) { stiurf[i] = fruits[fruits.length - 1 - i]; } // for setValues(dir, stiurf); checkValues(fruits, dir); } // testBackwards() @Test void testRandomized() throws Exception { for (int trials = 0; trials < 5; trials++) { setup(); String[] temp = fruits.clone(); TestUtils.randomlyPermute(temp); setValues(dir, temp); checkValues(fruits, dir); } // for } // testRandomized() @Test void testReplacement() throws Exception { setup(); setValues(dir, fruits); setValues(dir, names); checkValues(names, dir); setValues(dir, fruits); checkValues(fruits, dir); String[] temp = names.clone(); TestUtils.randomlyPermute(temp); setValues(dir, temp); checkValues(names, dir); temp = fruits.clone(); TestUtils.randomlyPermute(temp); setValues(dir, temp); checkValues(fruits, dir); } // testReplacement() @Test void testRemoveScreen() throws Exception { // Note: This needs refactoring. setup(); setValues(dir, fruits); // Remove some entries TestUtils.remove(dir.screenNames(), (str) -> "FGHTUVWY".contains(str)); checkValues(fruits, (str) -> (!"FGHTUVWY".contains(str)), dir); // Add them back in setValues(dir, fruits); checkValues(fruits, dir); // Remove entries at the front (and in the middle) TestUtils.remove(dir.screenNames(), (str) -> "ABCDELMNOP".contains(str)); checkValues(fruits, (str) -> (!"ABCDELMNOP".contains(str)), dir); // Add them back in setValues(dir, fruits); checkValues(fruits, dir); // Remove entries at the end TestUtils.remove(dir.screenNames(), (str) -> "WXYZ".contains(str)); checkValues(fruits, (str) -> (!"WXYZ".contains(str)), dir); // Add them back in setValues(dir, fruits); checkValues(fruits, dir); // Multiple removes TestUtils.remove(dir.screenNames(), (str) -> "LMNOP".contains(str)); TestUtils.remove(dir.screenNames(), (str) -> "ABC".contains(str)); TestUtils.remove(dir.screenNames(), (str) -> "EFG".contains(str)); TestUtils.remove(dir.screenNames(), (str) -> "XYZ".contains(str)); checkValues(fruits, (str) -> (!"ABCEFGLMNOPXYZ".contains(str)), dir); // Add them back in setValues(dir, fruits); checkValues(fruits, dir); } // testRemoveScreen() @Test void testRemoveRealNames() throws Exception { setup(); setValues(dir, fruits); String[] screens = {"A", "B", "C", "E", "L", "O", "P", "Q", "X", "Y", "Z"}; // Update a bunch of values so that they have the same name. for (String str : screens) { dir.set(str, "REMOVE"); } // Remove them TestUtils.remove(dir.realNames(), (name) -> "REMOVE".equals(name)); // Verify that the are no longer there for (String str : screens) { assertThrows(java.lang.Exception.class, () -> dir.get(str)); } // Verify that everything else is still there checkValues(fruits, (name) -> (!"ABCELOPQXYZ".contains(name)), dir); // And that we can put everything back in setValues(dir, fruits); checkValues(fruits, dir); } // testRemoveRealNames() } // SamDirectoryTests