Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cse332-22au/p2-public
  • cse332-22sp/p2-public
  • cse332-22su/p2-public
  • cse332-23wi/p2-public
  • cse332-23sp/p2-public
  • cse332-23su/p2-public
  • cse332-23au/p2-public
  • cse332-24wi/p2-public
  • yc888/p-2-public-yc-888
  • ajk1004/p2-public
  • gpletosu/p2-public
  • cse332-24sp/p2-public
  • samganyx/p2-public
13 results
Show changes
Showing
with 408 additions and 246 deletions
package ckpt2;
package provided;
import cse332.datastructures.containers.Item;
import cse332.datastructures.trees.BinarySearchTree.BSTNode;
......@@ -15,11 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
public class AVLTreeTests {
private AVLTree<String, Integer> init() {
return new AVLTree<>();
}
private <E extends Comparable<E>> void incCount(Dictionary<E, Integer> tree, E key) {
private <E extends Comparable<E>> void incrementValueWithKey(Dictionary<E, Integer> tree, E key) {
Integer value = tree.find(key);
if (value == null) {
tree.insert(key, 1);
......@@ -31,57 +27,52 @@ public class AVLTreeTests {
@SuppressWarnings("rawtypes")
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void checkStructure() {
public void test_insertFind_severalElements_correctStructure() {
AVLTree<Integer, Integer> tree = new AVLTree<>();
incCount(tree, 10);
incCount(tree, 14);
incCount(tree, 10);
incCount(tree, 31);
incCount(tree, 10);
incCount(tree, 13);
incCount(tree, 10);
incCount(tree, 10);
incCount(tree, 12);
incCount(tree, 10);
incCount(tree, 13);
incCount(tree, 10);
incCount(tree, 10);
incCount(tree, 11);
incCount(tree, 10);
incCount(tree, 14);
incCount(tree, 9);
incCount(tree, 8);
incCount(tree, 7);
incCount(tree, 6);
incCount(tree, 5);
incCount(tree, 4);
incCount(tree, 3);
incCount(tree, 2);
incCount(tree, 1);
incCount(tree, 0);
// {10, 14, 10, 31, 10, 13, 10, 10, 12, 10, 13, 10, 10, 11, 10, 14, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
// {10, 14, 31, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
// {10, 14, 10, 31, 10, 13, 10, 10, 12, 10, 13, 10, 10, 11, 10, 14, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 14);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 31);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 13);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 12);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 13);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 11);
// {10, 14, 31, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 14);
incrementValueWithKey(tree, 9);
incrementValueWithKey(tree, 8);
incrementValueWithKey(tree, 7);
incrementValueWithKey(tree, 6);
incrementValueWithKey(tree, 5);
incrementValueWithKey(tree, 4);
incrementValueWithKey(tree, 3);
incrementValueWithKey(tree, 2);
incrementValueWithKey(tree, 1);
incrementValueWithKey(tree, 0);
BSTNode root = getField(tree, "root");
String trueData = " [8 [4 [2 [1 [0..].] [3..]] [6 [5..] [7..]]] [12 [10 [9..] [11..]] [14 [13..] [31..]]]]";
String trueCounts = " [1 [1 [1 [1 [1..].] [1..]] [1 [1..] [1..]]] [1 [9 [1..] [1..]] [2 [2..] [1..]]]]";
// String trueData = " [10 [6 [2 [1 [0..].] [4 [3..] [5..]]] [8 [7..] [9..]]] [13 [12 [11..].] [14. [31..]]]]";
// String trueCounts = " [9 [1 [1 [1 [1..].] [1 [1..] [1..]]] [1 [1..] [1..]]] [2 [1 [1..].] [2. [1..]]]]";
// System.err.println(nestd(root));
// System.err.println(trueData);
assertEquals(trueData, nestd(root));
assertEquals(trueCounts, nestc(root));
}
@SuppressWarnings("rawtypes")
public String nestd(BSTNode root) {
if(root == null)
return ".";
return " [" + root.key + nestd(root.children[0]) + nestd(root.children[1]) + "]";
}
@SuppressWarnings("rawtypes")
public String nestc(BSTNode root) {
if(root == null)
return ".";
......@@ -90,13 +81,13 @@ public class AVLTreeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testTreeWith5Items() {
AVLTree<String, Integer> tree = init();
public void test_insertFind_fewElements_correctStructure() {
AVLTree<String, Integer> tree = new AVLTree<>();
String[] tests_struct = { "a", "b", "c", "d", "e" };
String[] tests = { "b", "d", "e", "c", "a" };
for (int i = 0; i < 5; i++) {
String str = tests[i] + "a";
incCount(tree, str);
incrementValueWithKey(tree, str);
}
int i = 0;
......@@ -110,8 +101,8 @@ public class AVLTreeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeTree() {
AVLTree<String, Integer> tree = init();
public void test_insertFind_manyElements_correctStructure() {
AVLTree<String, Integer> tree = new AVLTree<>();
int n = 1000;
// Add them
......@@ -119,7 +110,7 @@ public class AVLTreeTests {
int k = (i % n) * 37 % n;
String str = String.format("%05d", k);
for (int j = 0; j < k + 1; j ++)
incCount(tree, str);
incrementValueWithKey(tree, str);
}
// Calculate count of all values in tree
......
package ckpt2;
package provided;
import cse332.datastructures.containers.Item;
import cse332.datastructures.trees.BinarySearchTree;
import cse332.interfaces.misc.Dictionary;
import datastructures.dictionaries.ChainingHashTable;
import datastructures.dictionaries.MoveToFrontList;
......@@ -12,21 +13,24 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class HashTableTests {
public class ChainingHashTableTests {
private void incCount(Dictionary<String, Integer> list, String key) {
private void incrementValueWithKey(Dictionary<String, Integer> list, String key) {
Integer find = list.find(key);
if (find == null)
list.insert(key, 1);
else
list.insert(key, 1 + find);
list.insert(key, find + 1);
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeHashTable() {
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(MoveToFrontList::new);
public void test_insertFind_manyElements_correctStructure() {
/*
Replace BinarySearchTree with your own Dictionary implementations like MoveToFrontList or AVLTree
to test them as chains for the ChainingHashTable (highly recommended to find potential bugs)
* */
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(BinarySearchTree::new);
int n = 1000;
// Add them
......@@ -34,7 +38,7 @@ public class HashTableTests {
int k = (i % n) * 37 % n;
String str = String.format("%05d", k);
for (int j = 0; j < k + 1; j ++)
incCount(list, str);
incrementValueWithKey(list, str);
}
// Delete them all
......
package ckpt1;
package provided;
import datastructures.worklists.CircularArrayFIFOQueue;
import org.junit.jupiter.api.Test;
......@@ -11,118 +11,127 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class CircularArrayComparatorTests {
private CircularArrayFIFOQueue<String> init() {
return new CircularArrayFIFOQueue<String>(10);
}
private boolean result(int a, int b) {
return Integer.signum(a) == Integer.signum(b);
}
@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_empty_empty() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
assertTrue(result(l1.compareTo(l2), "".compareTo("")));
public void test_compareTo_empty_equalTo() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
// Checks if comparing two empty CAFQ is the same as comparing 2 empty strings.
assertEquals(Integer.signum("".compareTo("")), Integer.signum(l1.compareTo(l2)));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_ab_ab() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_compareTo_sameElements_equalTo() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("b");
l2.add("a");
l2.add("b");
assertTrue(result(l1.compareTo(l2), "ab".compareTo("ab")));
assertEquals(Integer.signum("ab".compareTo("ab")), Integer.signum(l1.compareTo(l2)));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_ab_abc() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_compareTo_differentElements_lessThan() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("b");
l2.add("a");
l2.add("b");
l2.add("c");
assertTrue(result(l1.compareTo(l2), "ab".compareTo("abc")));
assertEquals(Integer.signum("ab".compareTo("abc")), Integer.signum(l1.compareTo(l2)));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_abc_ab() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_compareTo_differentElements_greaterThan() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("b");
l1.add("c");
l2.add("a");
l2.add("b");
assertTrue(result(l1.compareTo(l2), "abc".compareTo("ab")));
assertEquals(Integer.signum("abc".compareTo("ab")), Integer.signum(l1.compareTo(l2)));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_ac_abc() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_compareTo_differentElements2_greaterThan() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("c");
l2.add("a");
l2.add("b");
l2.add("c");
assertTrue(result(l1.compareTo(l2), "ac".compareTo("abc")));
assertEquals(Integer.signum("ac".compareTo("abc")), Integer.signum(l1.compareTo(l2)));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_a_aa() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_compareTo_differentElements3_greaterThan() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l2.add("a");
l2.add("a");
assertTrue(result(l1.compareTo(l2), "a".compareTo("aa")));
assertEquals(Integer.signum("a".compareTo("aa")), Integer.signum(l1.compareTo(l2)));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_compare_transitive() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
CircularArrayFIFOQueue<String> l3 = init();
public void test_compareTo_transitive_correct() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l3 = new CircularArrayFIFOQueue<>(10);
l1.add("abc");
l2.add("def");
l3.add("efg");
assertTrue(l1.compareTo(l2) < 0 && l2.compareTo(l3) < 0 && l1.compareTo(l3) < 0) ;
assertTrue(l1.compareTo(l2) < 0, "\"abc\" should be less than \"def\"") ;
assertTrue(l1.compareTo(l3) < 0, "\"abc\" should be less than \"efg\"");
assertTrue(l2.compareTo(l3) < 0, "\"def\" should be less than \"efg\"");
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_equality_consistent_with_compare() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_compareToEquality_sameElements_sameResults() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("b");
l2.add("a");
l2.add("b");
assertEquals(l1, l2);
assertEquals(l1.compareTo(l2), 0);
assertEquals(0, l1.compareTo(l2));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_equals_doesnt_modify() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_equals_sameElements_nothingHappens() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l2.add("a");
l1.equals(l2);
assertEquals(l1.size(), 1);
assertEquals(1, l1.size());
}
}
package ckpt2;
package provided;
import datastructures.worklists.CircularArrayFIFOQueue;
import org.junit.jupiter.api.Test;
......@@ -11,15 +11,11 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class CircularArrayHashCodeTests {
private CircularArrayFIFOQueue<String> init() {
return new CircularArrayFIFOQueue<String>(10);
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void equality() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_hashCode_fewElements_equal() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
for (int i = 0; i < 3; i++) {
l1.add("a");
l2.add("a");
......@@ -29,9 +25,9 @@ public class CircularArrayHashCodeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void ineq1() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_hashCode_fewElements_notEqual() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("a");
l1.add("b");
......@@ -43,9 +39,9 @@ public class CircularArrayHashCodeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void ineq2() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_hashCode_fewElements2_notEqual() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("a");
l1.add("a");
......@@ -58,9 +54,9 @@ public class CircularArrayHashCodeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void ineq3() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_hashCode_fewElements3_notEqual() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("b");
l1.add("c");
......@@ -72,9 +68,9 @@ public class CircularArrayHashCodeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void equality_consistent_with_hashcode() {
CircularArrayFIFOQueue<String> l1 = init();
CircularArrayFIFOQueue<String> l2 = init();
public void test_equalHashCode_fewElements2_equal() {
CircularArrayFIFOQueue<String> l1 = new CircularArrayFIFOQueue<>(10);
CircularArrayFIFOQueue<String> l2 = new CircularArrayFIFOQueue<>(10);
l1.add("a");
l1.add("b");
l2.add("a");
......
package ckpt2;
package provided;
import cse332.types.AlphabeticString;
import datastructures.dictionaries.HashTrieMap;
......@@ -14,19 +14,14 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
public class HashTrieMapTests {
protected static HashTrieMap<Character, AlphabeticString, String> STUDENT;
@BeforeEach
public void init() {
STUDENT = new HashTrieMap<>(AlphabeticString.class);
}
/**
* Tests if insert, find, and findPrefix work in general.
*/
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testBasic() {
public void test_insertFindFindPrefix_fewElements_correctStructure() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
String[] words = {"dog", "doggy", "doge", "dragon", "cat", "draggin"};
String[] invalid = {"d", "cataract", "", "do"};
addAll(STUDENT, words);
......@@ -39,7 +34,8 @@ public class HashTrieMapTests {
*/
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testFindPrefixes() {
public void test_findPrefix_fewElements_correctStructure() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
String[] words = {"dog", "doggy", "doge", "dragon", "cat", "draggin"};
addAll(STUDENT, words);
......@@ -52,7 +48,8 @@ public class HashTrieMapTests {
*/
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testFindNonexistentDoesNotCrash() {
public void test_finds_nonexistentKey_doesNotCrash() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
addAll(STUDENT, "foo", "bar", "baz");
assertNull(STUDENT.find(a("orangutan")));
assertNull(STUDENT.find(a("z")));
......@@ -64,7 +61,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testFindingNullKeyCausesError() {
public void test_finds_nullKey_throwsException() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
assertThrows(IllegalArgumentException.class, () -> {
STUDENT.find(null);
});
......@@ -72,7 +70,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testFindingNullPrefixCausesError() {
public void test_findPrefix_nullKey_throwsException() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
assertThrows(IllegalArgumentException.class, () -> {
STUDENT.findPrefix(null);
});
......@@ -83,7 +82,8 @@ public class HashTrieMapTests {
*/
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInsertReplacesOldValue() {
public void test_insert_fewElements_valueReplaced() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
AlphabeticString key = a("myKey");
assertNull(STUDENT.insert(key, "foo"));
assertEquals("foo", STUDENT.insert(key, "bar"));
......@@ -92,7 +92,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInsertingNullKeyCausesError() {
public void test_insert_nullKey_throwsException() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
assertThrows(IllegalArgumentException.class, () -> {
STUDENT.insert(null, "foo");
});
......@@ -100,7 +101,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testInsertingNullValueCausesError() {
public void test_insert_nullValue_throwsException() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
assertThrows(IllegalArgumentException.class, () -> {
STUDENT.insert(a("foo"), null);
});
......@@ -108,7 +110,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testDeletingCausesError() {
public void test_delete_oneElement_throwsException() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
assertThrows(UnsupportedOperationException.class, () -> {
STUDENT.insert(a("foo"), "doo");
STUDENT.delete(a("foo"));
......@@ -117,7 +120,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testClearCausesError() {
public void test_clear_oneElement_throwsException() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
assertThrows(UnsupportedOperationException.class, () -> {
STUDENT.insert(a("foo"), "doo");
STUDENT.clear();
......@@ -126,7 +130,8 @@ public class HashTrieMapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void checkUnderlyingStructure() {
public void test_insert_fewElements_correctInternalStructure() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
STUDENT.insert(a(""), "A");
STUDENT.insert(a("foo"), "B");
STUDENT.insert(a("fez"), "C");
......@@ -150,6 +155,45 @@ public class HashTrieMapTests {
assertTrue(equals(fullExpected, getField(STUDENT, "root")));
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_insertFindFindPrefix_manyElements_correctStructure() {
HashTrieMap<Character, AlphabeticString, String> STUDENT = new HashTrieMap<>(AlphabeticString.class);
// Should contain 30 characters
char[] symbols = "abcdefghijklmnopqrstuvwxyz!@#$".toCharArray();
long i = 0;
for (char a : symbols) {
for (char b : symbols) {
for (char c : symbols) {
for (char d : symbols) {
Character[] word = new Character[]{a, b, c, d};
STUDENT.insert(new AlphabeticString(word), "" + i);
i += 1;
}
}
}
}
for (char a : symbols) {
for (char b : symbols) {
assertTrue(STUDENT.findPrefix(new AlphabeticString(new Character[]{a, b})));
}
}
i = 0;
for (char a : symbols) {
for (char b : symbols) {
for (char c : symbols) {
for (char d : symbols) {
Character[] word = new Character[]{a, b, c, d};
assertEquals("" + i, STUDENT.find(new AlphabeticString(word)));
i += 1;
}
}
}
}
}
protected static boolean equals(MockNode expected, HashTrieMap<Character, AlphabeticString, String>.HashTrieNode student) {
if (expected == null && student == null) {
return true;
......@@ -193,44 +237,6 @@ public class HashTrieMapTests {
}
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void stressTest() {
// Should contain 30 characters
char[] symbols = "abcdefghijklmnopqrstuvwxyz!@#$".toCharArray();
long i = 0;
for (char a : symbols) {
for (char b : symbols) {
for (char c : symbols) {
for (char d : symbols) {
Character[] word = new Character[]{a, b, c, d};
STUDENT.insert(new AlphabeticString(word), "" + i);
i += 1;
}
}
}
}
for (char a : symbols) {
for (char b : symbols) {
assertTrue(STUDENT.findPrefix(new AlphabeticString(new Character[]{a, b})));
}
}
i = 0;
for (char a : symbols) {
for (char b : symbols) {
for (char c : symbols) {
for (char d : symbols) {
Character[] word = new Character[]{a, b, c, d};
assertEquals("" + i, STUDENT.find(new AlphabeticString(word)));
i += 1;
}
}
}
}
}
/**
* Converts a String into an AlphabeticString
*/
......
package provided;
import cse332.types.AlphabeticString;
import datastructures.dictionaries.HashTrieSet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
public class HashTrieSetTests {
/**
* Test to check if HashTrieSet has been copied over from P1
* */
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_isHashTrieSetAvailable() {
HashTrieSet<Character, AlphabeticString> STUDENT = new HashTrieSet<>(AlphabeticString.class);
String[] words = {"dog", "doggy", "doge", "dragon", "cat", "draggin"};
String[] invalid = {"d", "cataract", "", "do"};
for (String word: words) {
STUDENT.add(a(word));
}
assertEquals(6, STUDENT.size());
for (String word: words) {
assertTrue(STUDENT.contains(a(word)));
}
for (String invalid_word: invalid) {
assertFalse(STUDENT.contains(a(invalid_word)));
}
}
private static AlphabeticString a(String s) {
return new AlphabeticString(s);
}
}
package ckpt2;
package provided;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
......@@ -12,7 +12,7 @@ public class HeapSortTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void integer_sorted() {
public void test_sort_integerSorted_correctSort() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer[] arr_sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
HeapSort.sort(arr, Integer::compareTo);
......@@ -22,7 +22,7 @@ public class HeapSortTests {
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void integer_random() {
public void test_sort_integerRandom_correctSort() {
Integer[] arr = {3, 1, 4, 5, 9, 2, 6, 7, 8};
Integer[] arr_sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9};
HeapSort.sort(arr, Integer::compareTo);
......
package ckpt1;
package provided;
import cse332.interfaces.worklists.PriorityWorkList;
import cse332.interfaces.worklists.WorkList;
......@@ -14,35 +14,30 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
public class MinFourHeapComparableTests {
private static WorkList<String> STUDENT_STR;
private static WorkList<Double> STUDENT_DOUBLE;
private static WorkList<Integer> STUDENT_INT;
private static Random RAND;
@BeforeEach
public void init() {
STUDENT_STR = new MinFourHeapComparable<>();
STUDENT_DOUBLE = new MinFourHeapComparable<>();
STUDENT_INT = new MinFourHeapComparable<>();
RAND = new Random(42);
}
private static final int SEED = 42;
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHasWork() {
public void test_hasWork_empty_noWork() {
WorkList<Integer> STUDENT_INT = new MinFourHeapComparable<>();
assertFalse(STUDENT_INT.hasWork());
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHasWorkAfterAdd() {
public void test_hasWork_oneElement_hasWork() {
WorkList<Integer> STUDENT_INT = new MinFourHeapComparable<>();
STUDENT_INT.add(1);
assertTrue(STUDENT_INT.hasWork());
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHasWorkAfterAddRemove() {
public void test_hasWork_manyElements_noWork() {
WorkList<Double> STUDENT_DOUBLE = new MinFourHeapComparable<>();
for (int i = 0; i < 1000; i++) {
STUDENT_DOUBLE.add(Math.random());
}
......@@ -53,24 +48,35 @@ public class MinFourHeapComparableTests {
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testPeekHasException() {
assertTrue(doesPeekThrowException(STUDENT_INT));
public void test_peek_empty_exceptionThrown() {
WorkList<Integer> STUDENT_INT = new MinFourHeapComparable<>();
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.peek();
});
addAndRemove(STUDENT_INT, 42, 10);
assertTrue(doesPeekThrowException(STUDENT_INT));
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.peek();
});
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testNextHasException() {
assertTrue(doesNextThrowException(STUDENT_INT));
public void test_next_empty_exceptionThrown() {
WorkList<Integer> STUDENT_INT = new MinFourHeapComparable<>();
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.next();
});
addAndRemove(STUDENT_INT, 42, 10);
assertTrue(doesNextThrowException(STUDENT_INT));
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.next();
});
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testClear() {
public void test_clear_fewElements_empty() {
WorkList<String> STUDENT_STR = new MinFourHeapComparable<>();
addAll(STUDENT_STR, new String[]{"Beware", "the", "Jabberwock", "my", "son!"});
assertTrue(STUDENT_STR.hasWork());
......@@ -79,13 +85,17 @@ public class MinFourHeapComparableTests {
STUDENT_STR.clear();
assertFalse(STUDENT_STR.hasWork());
assertEquals(0, STUDENT_STR.size());
assertTrue(doesPeekThrowException(STUDENT_STR));
assertTrue(doesNextThrowException(STUDENT_STR));
assertThrows(NoSuchElementException.class, () -> {
STUDENT_STR.peek();
});
assertThrows(NoSuchElementException.class, () -> {
STUDENT_STR.next();
});
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHeapWith5Items() {
public void test_add_fewElements_correct() {
PriorityWorkList<String> heap = new MinFourHeapComparable<>();
String[] tests = { "a", "b", "c", "d", "e" };
for (int i = 0; i < 5; i++) {
......@@ -102,7 +112,7 @@ public class MinFourHeapComparableTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testOrderingDoesNotMatter() {
public void test_addPeekNext_fewElements_correctOrdering() {
PriorityWorkList<String> ordered = new MinFourHeapComparable<>();
PriorityWorkList<String> reversed = new MinFourHeapComparable<>();
PriorityWorkList<String> random = new MinFourHeapComparable<>();
......@@ -128,7 +138,7 @@ public class MinFourHeapComparableTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeHeap() {
public void test_addNext_manyElements_correctOrdering() {
PriorityWorkList<String> heap = new MinFourHeapComparable<>();
int n = 10000;
......@@ -146,7 +156,8 @@ public class MinFourHeapComparableTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testWithCustomComparable() {
public void test_peekNextCustomComparator_manyElements_correctOrdering() {
Random RAND = new Random(SEED);
PriorityWorkList<Coordinate> student = new MinFourHeapComparable<>();
Queue<Coordinate> reference = new PriorityQueue<>();
......@@ -165,7 +176,7 @@ public class MinFourHeapComparableTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void checkStructure() {
public void test_addNext_severalElements_correctStructure() {
PriorityWorkList<Integer> heap = new MinFourHeapComparable<>();
addAll(heap, new Integer[] {10, 10, 15, 1, 17, 16, 100, 101, 102, 103, 105, 106, 107, 108});
......@@ -246,22 +257,4 @@ public class MinFourHeapComparableTests {
worklist.next();
}
}
protected static <E> boolean doesPeekThrowException(WorkList<E> worklist) {
try {
worklist.peek();
} catch (NoSuchElementException e) {
return true;
}
return false;
}
protected static <E> boolean doesNextThrowException(WorkList<E> worklist) {
try {
worklist.next();
} catch (NoSuchElementException e) {
return true;
}
return false;
}
}
package ckpt2;
package provided;
import cse332.interfaces.worklists.PriorityWorkList;
import cse332.interfaces.worklists.WorkList;
......@@ -15,35 +14,27 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
public class MinFourHeapTests {
private static Random RAND;
protected static WorkList<String> STUDENT_STR;
protected static WorkList<Double> STUDENT_DOUBLE;
protected static WorkList<Integer> STUDENT_INT;
@BeforeEach
public void init() {
STUDENT_STR = new MinFourHeap<>(String::compareTo);
STUDENT_DOUBLE = new MinFourHeap<>(Double::compareTo);
STUDENT_INT = new MinFourHeap<>(Integer::compareTo);
RAND = new Random(42);
}
private static final int SEED = 42;
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHasWork() {
public void test_hasWork_empty_noWork() {
WorkList<Integer> STUDENT_INT = new MinFourHeap<>(Integer::compareTo);
assertFalse(STUDENT_INT.hasWork());
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHasWorkAfterAdd() {
public void test_hasWork_oneElement_hasWork() {
WorkList<Integer> STUDENT_INT = new MinFourHeap<>(Integer::compareTo);
STUDENT_INT.add(1);
assertTrue(STUDENT_INT.hasWork());
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHasWorkAfterAddRemove() {
public void test_addNextHasWork_manyElements_noWork() {
WorkList<Double> STUDENT_DOUBLE = new MinFourHeap<>(Double::compareTo);
for (int i = 0; i < 1000; i++) {
STUDENT_DOUBLE.add(Math.random());
}
......@@ -54,24 +45,35 @@ public class MinFourHeapTests {
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testPeekHasException() {
assertTrue(doesPeekThrowException(STUDENT_INT));
public void test_peek_fewElements_throwsException() {
WorkList<Integer> STUDENT_INT = new MinFourHeap<>(Integer::compareTo);
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.peek();
});
addAndRemove(STUDENT_INT, 42, 10);
assertTrue(doesPeekThrowException(STUDENT_INT));
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.peek();
});
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testNextHasException() {
assertTrue(doesNextThrowException(STUDENT_INT));
public void test_next_fewElements_throwsException() {
WorkList<Integer> STUDENT_INT = new MinFourHeap<>(Integer::compareTo);
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.next();
});
addAndRemove(STUDENT_INT, 42, 10);
assertTrue(doesNextThrowException(STUDENT_INT));
assertThrows(NoSuchElementException.class, () -> {
STUDENT_INT.next();
});
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testClear() {
public void test_clear_fewElements_empty() {
WorkList<String> STUDENT_STR = new MinFourHeap<>(String::compareTo);
addAll(STUDENT_STR, new String[]{"Beware", "the", "Jabberwock", "my", "son!"});
assertTrue(STUDENT_STR.hasWork());
......@@ -80,13 +82,17 @@ public class MinFourHeapTests {
STUDENT_STR.clear();
assertFalse(STUDENT_STR.hasWork());
assertEquals(0, STUDENT_STR.size());
assertTrue(doesPeekThrowException(STUDENT_STR));
assertTrue(doesNextThrowException(STUDENT_STR));
assertThrows(NoSuchElementException.class, () -> {
STUDENT_STR.peek();
});
assertThrows(NoSuchElementException.class, () -> {
STUDENT_STR.next();
});
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHeapWith5Items() {
public void test_addNext_fewElements_correctStructure() {
PriorityWorkList<String> heap = new MinFourHeap<>(String::compareTo);
String[] tests = { "a", "b", "c", "d", "e" };
for (int i = 0; i < 5; i++) {
......@@ -103,7 +109,7 @@ public class MinFourHeapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testOrderingDoesNotMatter() {
public void test_addPeekNext_differentOrderings_correctStructure() {
PriorityWorkList<String> ordered = new MinFourHeap<>(String::compareTo);
PriorityWorkList<String> reversed = new MinFourHeap<>(String::compareTo);
PriorityWorkList<String> random = new MinFourHeap<>(String::compareTo);
......@@ -129,7 +135,7 @@ public class MinFourHeapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeHeap() {
public void test_addNext_manyElements_correctStructure() {
PriorityWorkList<String> heap = new MinFourHeap<>(String::compareTo);
int n = 10000;
......@@ -147,7 +153,8 @@ public class MinFourHeapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testWithCustomComparable() {
public void test_customComparator_manyElements_correctStructure() {
Random RAND = new Random(SEED);
PriorityWorkList<Coordinate> student = new MinFourHeap<>(Coordinate::compareTo);
Queue<Coordinate> reference = new PriorityQueue<>();
......@@ -166,7 +173,7 @@ public class MinFourHeapTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void checkStructure() {
public void test_addNext_severalElements_correctStructure() {
PriorityWorkList<Integer> heap = new MinFourHeap<>(Integer::compareTo);
addAll(heap, new Integer[] {10, 10, 15, 1, 17, 16, 100, 101, 102, 103, 105, 106, 107, 108});
......@@ -231,24 +238,6 @@ public class MinFourHeapTests {
}
}
protected static <E> boolean doesPeekThrowException(WorkList<E> worklist) {
try {
worklist.peek();
} catch (NoSuchElementException e) {
return true;
}
return false;
}
protected static <E> boolean doesNextThrowException(WorkList<E> worklist) {
try {
worklist.next();
} catch (NoSuchElementException e) {
return true;
}
return false;
}
protected <T> T getField(Object o, String fieldName) {
try {
Field field = o.getClass().getSuperclass().getDeclaredField(fieldName);
......
package ckpt1;
package provided;
import cse332.datastructures.containers.Item;
import datastructures.dictionaries.MoveToFrontList;
......@@ -16,7 +16,7 @@ public class MoveToFrontListTests {
@SuppressWarnings("unchecked")
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void checkStructure() {
public void test_insertIterator_severalElements_correctStructure() {
MoveToFrontList<Integer, Integer> list = new MoveToFrontList<Integer, Integer>();
int[] arr = {6, 5, 10, 14, 10, 31, 10, 13, 10, 10, 12, 10, 14, 10, 10, 11, 10, 14, 9, 8, 3, 2, 1, 0, 7, 4};
......@@ -37,14 +37,13 @@ public class MoveToFrontListTests {
// Compare strings to make sure we get the right one
// Can use list.toString as well, but I'm not sure if students may modify that
String mtf_correct = "[4=1, 7=1, 0=1, 1=1, 2=1, 3=1, 8=1, 9=1, 14=3, 10=9, 11=1, 12=1, 13=1, 31=1, 5=1, 6=1]";
String mtf_test = Arrays.toString(dcs);
assertEquals(mtf_correct, mtf_test);
assertEquals("[4=1, 7=1, 0=1, 1=1, 2=1, 3=1, 8=1, 9=1, 14=3, 10=9, 11=1, 12=1, 13=1, 31=1, 5=1, 6=1]", mtf_test);
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeMTFList() {
public void test_insertFind_manyElements_correctStructure() {
MoveToFrontList<String, Integer> list = new MoveToFrontList<>();
int n = 1000;
......@@ -56,7 +55,8 @@ public class MoveToFrontListTests {
for (int j = 0; j < k + 1; j ++)
list.insert(str, list.find(str) == null ? 1 : list.find(str) + 1);
}
// Delete them all
// Check the elements
int totalCount = 0;
for (Item<String, Integer> dc : list) {
assertEquals((Integer.parseInt(dc.key) + 1) * 5, dc.value.intValue());
......
package ckpt2;
package provided;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
......@@ -12,7 +12,7 @@ public class QuickSortTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void integer_sorted() {
public void test_sort_integerSorted_correctSort() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer[] arr_sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
QuickSort.sort(arr, Integer::compareTo);
......@@ -23,7 +23,7 @@ public class QuickSortTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void integer_random() {
public void test_sort_integerRandom_correctSort() {
Integer[] arr = {3, 1, 4, 5, 9, 2, 6, 7, 8};
Integer[] arr_sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9};
QuickSort.sort(arr, Integer::compareTo);
......
package ckpt2;
package provided;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
......@@ -12,25 +12,25 @@ public class TopKSortTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void integer_sorted() {
public void test_sort_integerSorted_correctSort() {
int K = 4;
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer[] arr_sorted = {7, 8, 9, 10};
TopKSort.sort(arr, K, Integer::compareTo);
for(int i = 0; i < K; i++) {
assertEquals(arr[i], arr_sorted[i]);
assertEquals(arr_sorted[i], arr[i]);
}
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void integer_random() {
public void test_sort_integerRandom_correctSort() {
int K = 4;
Integer[] arr = {3, 1, 4, 5, 9, 2, 6, 7, 8};
Integer[] arr_sorted = {6, 7, 8, 9};
TopKSort.sort(arr, K, Integer::compareTo);
for(int i = 0; i < K; i++) {
assertEquals(arr[i], arr_sorted[i]);
assertEquals(arr_sorted[i], arr[i]);
}
}
}
\ No newline at end of file
}