Skip to content
Snippets Groups Projects
Commit a4c94f05 authored by Nathan Lipiarski's avatar Nathan Lipiarski
Browse files

Diet refactoring to have better style

parent 0f30f6a7
No related branches found
No related tags found
No related merge requests found
......@@ -53,7 +53,7 @@ public class MoveToFrontListTests {
// Delete them all
int totalCount = 0;
for (Item<String, Integer> dc : list) {
assertTrue ((Integer.parseInt(dc.key) + 1) * 5 == dc.value);
assertEquals((Integer.parseInt(dc.key) + 1) * 5, dc.value.intValue());
totalCount += dc.value;
}
......@@ -61,6 +61,6 @@ public class MoveToFrontListTests {
assertEquals(totalCount, (n * (n + 1)) / 2 * 5);
assertEquals(list.size(), n);
assertNotNull(list.find("00851"));
assertTrue(list.find("00851") == 4260);
assertEquals(list.find("00851").intValue(), 4260);
}
}
......@@ -41,10 +41,10 @@ public class NGramToNextChoicesMapTests {
}
for (int i = 0; i < ngrams.length; i++) {
Item<String, Integer>[] items = map.getCountsAfter(ngrams[i]);
assertTrue(items.length == 1);
assertEquals(1, items.length);
Item<String, Integer> item = items[0];
assertTrue(item.key.equals(words[i]));
assertTrue(item.value.equals(1));
assertEquals(words[i], item.key);
assertEquals(1, item.value.intValue());
}
}
......@@ -76,16 +76,14 @@ public class NGramToNextChoicesMapTests {
for (int i = 0; i < ngrams.length; i++) {
Item<String, Integer>[] items = map.getCountsAfter(ngrams[i]);
String[] answer = words[i];
assertTrue(items.length == answer.length);
assertEquals(answer.length, items.length);
String[] itemsWithoutCounts = new String[items.length];
for (int j = 0; j < answer.length; j++) {
assertTrue(items[j].value.equals(1));
assertEquals(1, items[j].value.intValue());
itemsWithoutCounts[j] = items[j].key;
}
Arrays.sort(itemsWithoutCounts);
for (int j = 0; j < answer.length; j++) {
assertTrue(itemsWithoutCounts[j].equals(answer[j]));
}
assertArrayEquals(answer, itemsWithoutCounts);
}
}
......@@ -104,7 +102,7 @@ public class NGramToNextChoicesMapTests {
}
Item<String, Integer>[] items = map.getCountsAfter(new NGram(new String[] { "yo" }));
assertNotNull(items);
assertTrue(items.length == 0);
assertEquals(0, items.length);
}
@SuppressWarnings("unchecked")
......@@ -133,53 +131,53 @@ public class NGramToNextChoicesMapTests {
// corrlates with words and ngrams above
// Note that words after are in sorted order, not in order of array in words
Map<NGram, Item<String, Integer>[]> answers = new TreeMap<>();
answers.put(ngrams[0], (Item<String, Integer>[]) new Item[3]);
answers.get(ngrams[0])[0] = new Item<String, Integer>("bip", 1);
answers.get(ngrams[0])[1] = new Item<String, Integer>("boop", 1);
answers.get(ngrams[0])[2] = new Item<String, Integer>("bop", 3);
answers.put(ngrams[1], (Item<String, Integer>[]) new Item[2]);
answers.get(ngrams[1])[0] = new Item<String, Integer>("fum", 1);
answers.get(ngrams[1])[1] = new Item<String, Integer>("giants", 2);
answers.put(ngrams[2], (Item<String, Integer>[]) new Item[3]);
answers.get(ngrams[2])[0] = new Item<String, Integer>("ago", 2);
answers.get(ngrams[2])[1] = new Item<String, Integer>("seven", 1);
answers.get(ngrams[2])[2] = new Item<String, Integer>("years", 2);
answers.put(ngrams[3], (Item<String, Integer>[]) new Item[1]);
answers.get(ngrams[3])[0] = new Item<String, Integer>("throw", 5);
answers.put(ngrams[4], (Item<String, Integer>[]) new Item[3]);
answers.get(ngrams[4])[0] = new Item<String, Integer>("do", 2);
answers.get(ngrams[4])[1] = new Item<String, Integer>("for", 2);
answers.get(ngrams[4])[2] = new Item<String, Integer>("while", 2);
answers.put(ngrams[0], (Item<String, Integer>[]) new Item[]{
new Item<>("bip", 1),
new Item<>("boop", 1),
new Item<>("bop", 3)
});
answers.put(ngrams[1], (Item<String, Integer>[]) new Item[]{
new Item<>("fum", 1),
new Item<>("giants", 2)
});
answers.put(ngrams[2], (Item<String, Integer>[]) new Item[]{
new Item<>("ago", 2),
new Item<>("seven", 1),
new Item<>("years", 2)
});
answers.put(ngrams[3], (Item<String, Integer>[]) new Item[]{
new Item<>("throw", 5)
});
answers.put(ngrams[4], (Item<String, Integer>[]) new Item[]{
new Item<>("do", 2),
new Item<>("for", 2),
new Item<>("while", 2)
});
// Adds nGrams and words after to student's NGramToNextChoicesMap
for (int i = 0; i < ngrams.length; i++) {
for (int j = 0; j < words[i].length; j++) {
map.seenWordAfterNGram(ngrams[i], words[i][j]);
}
}
// checks to see if getCountsAfter returns correctly
for (int i = 0; i < ngrams.length; i++) {
NGram ngram = ngrams[i];
Item<String, Integer>[] results = map.getCountsAfter(ngram);
Arrays.sort(results, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Item<String, Integer> r1 = (Item<String, Integer>)o1;
Item<String, Integer> r2 = (Item<String, Integer>)o2;
return r1.key.compareTo(r2.key);
}
});
Arrays.sort(results, Comparator.comparing(r -> r.key));
Item<String, Integer>[] expected = answers.get(ngram);
// checks for correct number of unique words after
assertTrue(results.length == expected.length);
assertEquals(expected.length, results.length);
for (int j = 0; j < expected.length; j++) {
// checks if correct word after via sorted words
assertTrue(expected[j].key.equals(results[j].key));
assertEquals(expected[j].key, results[j].key);
// checks if correct count for given word after
assertTrue(expected[j].value.equals(results[j].value));
assertEquals(expected[j].value, results[j].value);
}
}
}
......
......@@ -13,9 +13,7 @@ import static org.junit.Assert.*;
public class AVLTreeTests {
private AVLTree<String, Integer> init() {
AVLTree<String, Integer> tree = new AVLTree<>();
return tree;
return new AVLTree<>();
}
private <E extends Comparable<E>> void incCount(Dictionary<E, Integer> tree, E key) {
......@@ -121,7 +119,7 @@ public class AVLTreeTests {
// Delete them all
int totalCount = 0;
for (Item<String, Integer> dc : tree) {
assertTrue((Integer.parseInt(dc.key) + 1) * 5 == dc.value);
assertEquals((Integer.parseInt(dc.key) + 1) * 5, dc.value.intValue());
totalCount += dc.value;
}
......@@ -129,7 +127,7 @@ public class AVLTreeTests {
assertEquals(totalCount, (n * (n + 1)) / 2 * 5);
assertEquals(tree.size(), n);
assertNotNull(tree.find("00851"));
assertTrue(tree.find("00851") == 4260);
assertEquals(4260, (int) tree.find("00851"));
}
private void definalize(Field field) {
......@@ -137,7 +135,7 @@ public class AVLTreeTests {
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
} catch (Exception e) {}
} catch (Exception ignored) {}
}
/**
......
......@@ -19,7 +19,6 @@ public class CircularArrayHashCodeTests {
l2.add("a");
}
assertEquals(l1.hashCode(), l2.hashCode());
assertTrue(l1.hashCode() == l2.hashCode());
}
@Test(timeout = 3000)
......
......@@ -11,7 +11,8 @@ import org.junit.runners.Suite;
CircularArrayHashCodeTests.class,
QuickSortTests.class,
TopKSortTests.class,
HeapSortTests.class
HeapSortTests.class,
HashTrieMapTests.class
})
public class Ckpt2Tests {
......
......@@ -20,7 +20,7 @@ public class HashTableTests {
@Test(timeout = 3000)
public void testHugeHashTable() {
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(() -> new MoveToFrontList<>());
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(MoveToFrontList::new);
int n = 1000;
......@@ -35,12 +35,12 @@ public class HashTableTests {
// Delete them all
int totalCount = 0;
for (Item<String, Integer> dc : list) {
assertTrue ((Integer.parseInt(dc.key) + 1) * 5 == dc.value);
assertEquals((Integer.parseInt(dc.key) + 1) * 5, (int) dc.value);
totalCount += dc.value;
}
assertEquals(totalCount, (n * (n + 1)) / 2 * 5);
assertEquals(list.size(), n);
assertNotNull(list.find("00851") );
assertTrue(list.find("00851") == 4260);
assertNotNull(list.find("00851"));
assertEquals(4260, (int) list.find("00851"));
}
}
......@@ -48,28 +48,22 @@ public class HashTrieMapTests {
@Test(timeout = 3000)
public void testFindNonexistentDoesNotCrash() {
addAll(STUDENT, "foo", "bar", "baz");
assertTrue(STUDENT.find(a("orangutan")) == null);
assertTrue(STUDENT.find(a("z")) == null);
assertTrue(STUDENT.find(a("ba")) == null);
assertTrue(STUDENT.find(a("bazz")) == null);
assertTrue(!STUDENT.findPrefix(a("boor")));
assertTrue(!STUDENT.findPrefix(a("z")) );
assertNull(STUDENT.find(a("orangutan")));
assertNull(STUDENT.find(a("z")));
assertNull(STUDENT.find(a("ba")));
assertNull(STUDENT.find(a("bazz")));
assertFalse(STUDENT.findPrefix(a("boor")));
assertFalse(STUDENT.findPrefix(a("z")) );
}
@Test(timeout = 3000)
public void testFindingNullEntriesCausesError() {
try {
STUDENT.find(null);
assertTrue(false);
} catch (IllegalArgumentException ex) {
// Do nothing
}
try {
STUDENT.findPrefix(null);
assertTrue(false);
} catch (IllegalArgumentException ex) {
// Do nothing
}
@Test(timeout = 3000, expected = IllegalArgumentException.class)
public void testFindingNullKeyCausesError() {
STUDENT.find(null);
}
@Test(timeout = 3000, expected = IllegalArgumentException.class)
public void testFindingNullPrefixCausesError() {
STUDENT.findPrefix(null);
}
/**
......@@ -78,48 +72,31 @@ public class HashTrieMapTests {
@Test(timeout = 3000)
public void testInsertReplacesOldValue() {
AlphabeticString key = a("myKey");
assertTrue(STUDENT.insert(key, "foo") == null);
assertTrue(STUDENT.insert(key, "bar").equals("foo"));
assertTrue(STUDENT.insert(key, "baz").equals("bar"));
assertNull(STUDENT.insert(key, "foo"));
assertEquals("foo", STUDENT.insert(key, "bar"));
assertEquals("bar", STUDENT.insert(key, "baz"));
}
@Test(timeout = 3000)
public void testInsertingNullEntriesCausesError() {
try {
STUDENT.insert(null, "foo");
assertTrue(false);
} catch (IllegalArgumentException ex) {
// Do nothing
}
@Test(timeout = 3000, expected = IllegalArgumentException.class)
public void testInsertingNullKeyCausesError() {
STUDENT.insert(null, "foo");
}
try {
STUDENT.insert(a("foo"), null);
assertTrue(false);
} catch (IllegalArgumentException ex) {
// Do nothing
}
@Test(timeout = 3000, expected = IllegalArgumentException.class)
public void testInsertingNullValueCausesError() {
STUDENT.insert(a("foo"), null);
}
@Test(timeout = 3000)
@Test(timeout = 3000, expected=UnsupportedOperationException.class)
public void testDeletingCausesError() {
STUDENT.insert(a("foo"), "doo");
try {
STUDENT.delete(a("foo"));
assertTrue(false);
} catch (UnsupportedOperationException ex) {
// do nothing, should throw exception
}
STUDENT.delete(a("foo"));
}
@Test(timeout = 3000)
@Test(timeout = 3000, expected=UnsupportedOperationException.class)
public void testClearCausesError() {
STUDENT.insert(a("foo"), "doo");
try {
STUDENT.clear();
assertTrue(false);
} catch (UnsupportedOperationException ex) {
// do nothing, should throw exception
}
STUDENT.clear();
}
@Test(timeout = 3000)
......@@ -223,7 +200,7 @@ public class HashTrieMapTests {
for (char c : symbols) {
for (char d : symbols) {
Character[] word = new Character[]{a, b, c, d};
assertTrue(STUDENT.find(new AlphabeticString(word)).equals("" + i));
assertEquals("" + i, STUDENT.find(new AlphabeticString(word)));
i += 1;
}
}
......
......@@ -2,6 +2,9 @@ package tests.gitlab.ckpt2;
import p2.sorts.HeapSort;
import org.junit.Test;
import java.util.Comparator;
import static org.junit.Assert.*;
public class HeapSortTests {
......@@ -10,7 +13,7 @@ public class HeapSortTests {
public void integer_sorted() {
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, (i1, i2) -> i1.compareTo(i2));
HeapSort.sort(arr, Integer::compareTo);
for(int i = 0; i < arr.length; i++) {
assertEquals(arr[i], arr_sorted[i]);
}
......@@ -19,7 +22,7 @@ public class HeapSortTests {
public void integer_random() {
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, (i1, i2) -> i1.compareTo(i2));
HeapSort.sort(arr, Integer::compareTo);
for(int i = 0; i < arr.length; i++) {
assertEquals(arr[i], arr_sorted[i]);
}
......
......@@ -12,12 +12,7 @@ public class QuickSortTests {
public void integer_sorted() {
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, new Comparator<Integer>() {
@Override
public int compare(Integer e1, Integer e2) {
return e1.compareTo(e2);
}
});
QuickSort.sort(arr, Integer::compareTo);
for(int i = 0; i < arr.length; i++) {
assertEquals(arr[i], arr_sorted[i]);
}
......@@ -27,12 +22,7 @@ public class QuickSortTests {
public void integer_random() {
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, new Comparator<Integer>() {
@Override
public int compare(Integer e1, Integer e2) {
return e1.compareTo(e2);
}
});
QuickSort.sort(arr, Integer::compareTo);
for(int i = 0; i < arr.length; i++) {
assertEquals(arr[i], arr_sorted[i]);
}
......
......@@ -13,12 +13,7 @@ public class TopKSortTests {
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, new Comparator<Integer>() {
@Override
public int compare(Integer e1, Integer e2) {
return e1.compareTo(e2);
}
});
TopKSort.sort(arr, K, Integer::compareTo);
for(int i = 0; i < K; i++) {
assertEquals(arr[i], arr_sorted[i]);
}
......@@ -29,12 +24,7 @@ public class TopKSortTests {
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, new Comparator<Integer>() {
@Override
public int compare(Integer e1, Integer e2) {
return e1.compareTo(e2);
}
});
TopKSort.sort(arr, K, Integer::compareTo);
for(int i = 0; i < K; i++) {
assertEquals(arr[i], arr_sorted[i]);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment