Skip to content
Snippets Groups Projects
Commit e37a285e authored by anniemao's avatar anniemao
Browse files

Add separate MinFourHeapComparable

parent 4205a8c8
No related branches found
No related tags found
No related merge requests found
...@@ -3,15 +3,17 @@ package datastructures.worklists; ...@@ -3,15 +3,17 @@ package datastructures.worklists;
import cse332.exceptions.NotYetImplementedException; import cse332.exceptions.NotYetImplementedException;
import cse332.interfaces.worklists.PriorityWorkList; import cse332.interfaces.worklists.PriorityWorkList;
import java.util.Comparator;
/** /**
* See cse332/interfaces/worklists/PriorityWorkList.java * See cse332/interfaces/worklists/PriorityWorkList.java
* for method specifications. * for method specifications.
*/ */
public class MinFourHeap<E extends Comparable<E>> extends PriorityWorkList<E> { public class MinFourHeap<E> extends PriorityWorkList<E> {
/* Do not change the name of this field; the tests rely on it to work correctly. */ /* Do not change the name of this field; the tests rely on it to work correctly. */
private E[] data; private E[] data;
public MinFourHeap() { public MinFourHeap(Comparator<E> c) {
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
......
package datastructures.worklists;
import cse332.exceptions.NotYetImplementedException;
import cse332.interfaces.worklists.PriorityWorkList;
/**
* See cse332/interfaces/worklists/PriorityWorkList.java
* for method specifications.
*/
public class MinFourHeapComparable<E extends Comparable<E>> extends PriorityWorkList<E> {
/* Do not change the name of this field; the tests rely on it to work correctly. */
private E[] data;
public MinFourHeapComparable() {
throw new NotYetImplementedException();
}
@Override
public boolean hasWork() {
throw new NotYetImplementedException();
}
@Override
public void add(E work) {
throw new NotYetImplementedException();
}
@Override
public E peek() {
throw new NotYetImplementedException();
}
@Override
public E next() {
throw new NotYetImplementedException();
}
@Override
public int size() {
throw new NotYetImplementedException();
}
@Override
public void clear() {
throw new NotYetImplementedException();
}
}
package tests.gitlab.ckpt1; package tests.gitlab.ckpt1;
import datastructures.worklists.MinFourHeap; import datastructures.worklists.MinFourHeap;
import datastructures.worklists.MinFourHeapComparable;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
@RunWith(Suite.class) @RunWith(Suite.class)
@Suite.SuiteClasses({ @Suite.SuiteClasses({
MinFourHeap.class, MinFourHeapComparable.class,
MoveToFrontListTests.class, MoveToFrontListTests.class,
CircularArrayComparatorTests.class CircularArrayComparatorTests.class
}) })
......
package tests.gitlab.ckpt1;
import cse332.interfaces.worklists.PriorityWorkList;
import datastructures.worklists.MinFourHeapComparable;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import static org.junit.Assert.*;
public class MinFourHeapComparableTests extends WorklistGradingTests {
private static Random RAND;
@Before
public void init() {
STUDENT_STR = new MinFourHeapComparable<>();
STUDENT_DOUBLE = new MinFourHeapComparable<>();
STUDENT_INT = new MinFourHeapComparable<>();
RAND = new Random(42);
}
@Test(timeout = 3000)
public void testHeapWith5Items() {
PriorityWorkList<String> heap = new MinFourHeapComparable<>();
String[] tests = { "a", "b", "c", "d", "e" };
for (int i = 0; i < 5; i++) {
String str = tests[i] + "a";
heap.add(str);
}
for (int i = 0; i < 5; i++) {
String str_heap = heap.next();
String str = (char) ('a' + i) + "a";
assertEquals(str, str_heap);
}
}
@Test(timeout = 3000)
public void testOrderingDoesNotMatter() {
PriorityWorkList<String> ordered = new MinFourHeapComparable<>();
PriorityWorkList<String> reversed = new MinFourHeapComparable<>();
PriorityWorkList<String> random = new MinFourHeapComparable<>();
addAll(ordered, new String[]{"a", "b", "c", "d", "e"});
addAll(reversed, new String[]{"e", "d", "c", "b", "a"});
addAll(random, new String[]{"d", "b", "c", "e", "a"});
assertTrue(isSame("a", ordered.peek(), reversed.peek(), random.peek()));
assertTrue(isSame("a", ordered.next(), reversed.next(), random.next()));
assertTrue(isSame("b", ordered.next(), reversed.next(), random.next()));
addAll(ordered, new String[] {"a", "a", "b", "c", "z"});
addAll(reversed, new String[] {"z", "c", "b", "a", "a"});
addAll(random, new String[] {"c", "z", "a", "b", "a"});
String[] expected = new String[] {"a", "a", "b", "c", "c", "d", "e", "z"};
for (String e : expected) {
assertTrue(isSame(e, ordered.peek(), reversed.peek(), random.peek()));
assertTrue(isSame(e, ordered.next(), reversed.next(), random.next()));
}
}
private boolean isSame(String... args) {
String first = args[0];
for (String arg : args) {
if (!first.equals(arg)) {
return false;
}
}
return true;
}
@Test(timeout = 3000)
public void testHugeHeap() {
PriorityWorkList<String> heap = new MinFourHeapComparable<>();
int n = 10000;
// Add them
for (int i = 0; i < n; i++) {
String str = String.format("%05d", i * 37 % n);
heap.add(str);
}
// Delete them all
for (int i = 0; i < n; i++) {
String s = heap.next();
assertEquals(i , Integer.parseInt(s));
}
}
@Test(timeout = 3000)
public void testWithCustomComparable() {
PriorityWorkList<Coordinate> student = new MinFourHeapComparable<>();
Queue<Coordinate> reference = new PriorityQueue<>();
for (int i = 0; i < 10000; i++) {
Coordinate coord = new Coordinate(RAND.nextInt(10000) - 5000, RAND.nextInt(10000) - 5000);
student.add(coord);
reference.add(coord);
}
assertEquals(reference.size(), student.size());
while (!reference.isEmpty()) {
assertEquals(reference.peek() , student.peek());
assertEquals(reference.remove() , student.next());
}
}
public static class Coordinate implements Comparable<Coordinate> {
private int x;
private int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
// What exactly this comparable method is doing is somewhat arbitrary.
public int compareTo(Coordinate other) {
if (this.x != other.x) {
return this.x - other.x;
} else {
return this.y - other.y;
}
}
}
@Test(timeout = 3000)
public void checkStructure() {
PriorityWorkList<Integer> heap = new MinFourHeapComparable<>();
addAll(heap, new Integer[] {10, 10, 15, 1, 17, 16, 100, 101, 102, 103, 105, 106, 107, 108});
Object[] heapData = getField(heap, "data");
String heapStr = Arrays.toString(heapData);
String heapExp = "[1, 10, 15, 10, 17, 16, 100, 101, 102, 103, 105, 106, 107, 108";
heap.next();
heap.next();
heap.next();
Object[] heapData2 = getField(heap, "data");
String heapStr2 = Arrays.toString(heapData2);
String heapExp2 = "[15, 16, 103, 107, 17, 108, 100, 101, 102, 106, 105,";
assertTrue(heapStr.contains(heapExp));
assertTrue(heapStr2.contains(heapExp2));
}
protected <T> T getField(Object o, String fieldName) {
try {
Field field = o.getClass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
Object f = field.get(o);
return (T) f;
} catch (Exception var6) {
try {
Field field = o.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object f = field.get(o);
return (T) f;
} catch (Exception var5) {
return null;
}
}
}
}
...@@ -12,7 +12,8 @@ import org.junit.runners.Suite; ...@@ -12,7 +12,8 @@ import org.junit.runners.Suite;
QuickSortTests.class, QuickSortTests.class,
TopKSortTests.class, TopKSortTests.class,
HeapSortTests.class, HeapSortTests.class,
HashTrieMapTests.class HashTrieMapTests.class,
MinFourHeapTests.class
}) })
public class Ckpt2Tests { public class Ckpt2Tests {
......
package tests.gitlab.ckpt1; package tests.gitlab.ckpt2;
import cse332.interfaces.worklists.PriorityWorkList; import cse332.interfaces.worklists.PriorityWorkList;
import datastructures.worklists.MinFourHeap; import datastructures.worklists.MinFourHeap;
...@@ -19,15 +20,15 @@ public class MinFourHeapTests extends WorklistGradingTests { ...@@ -19,15 +20,15 @@ public class MinFourHeapTests extends WorklistGradingTests {
@Before @Before
public void init() { public void init() {
STUDENT_STR = new MinFourHeap<>(); STUDENT_STR = new MinFourHeap<>(String::compareTo);
STUDENT_DOUBLE = new MinFourHeap<>(); STUDENT_DOUBLE = new MinFourHeap<>(Double::compareTo);
STUDENT_INT = new MinFourHeap<>(); STUDENT_INT = new MinFourHeap<>(Integer::compareTo);
RAND = new Random(42); RAND = new Random(42);
} }
@Test(timeout = 3000) @Test(timeout = 3000)
public void testHeapWith5Items() { public void testHeapWith5Items() {
PriorityWorkList<String> heap = new MinFourHeap<>(); PriorityWorkList<String> heap = new MinFourHeap<>(String::compareTo);
String[] tests = { "a", "b", "c", "d", "e" }; String[] tests = { "a", "b", "c", "d", "e" };
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
String str = tests[i] + "a"; String str = tests[i] + "a";
...@@ -43,9 +44,9 @@ public class MinFourHeapTests extends WorklistGradingTests { ...@@ -43,9 +44,9 @@ public class MinFourHeapTests extends WorklistGradingTests {
@Test(timeout = 3000) @Test(timeout = 3000)
public void testOrderingDoesNotMatter() { public void testOrderingDoesNotMatter() {
PriorityWorkList<String> ordered = new MinFourHeap<>(); PriorityWorkList<String> ordered = new MinFourHeap<>(String::compareTo);
PriorityWorkList<String> reversed = new MinFourHeap<>(); PriorityWorkList<String> reversed = new MinFourHeap<>(String::compareTo);
PriorityWorkList<String> random = new MinFourHeap<>(); PriorityWorkList<String> random = new MinFourHeap<>(String::compareTo);
addAll(ordered, new String[]{"a", "b", "c", "d", "e"}); addAll(ordered, new String[]{"a", "b", "c", "d", "e"});
addAll(reversed, new String[]{"e", "d", "c", "b", "a"}); addAll(reversed, new String[]{"e", "d", "c", "b", "a"});
...@@ -78,7 +79,7 @@ public class MinFourHeapTests extends WorklistGradingTests { ...@@ -78,7 +79,7 @@ public class MinFourHeapTests extends WorklistGradingTests {
@Test(timeout = 3000) @Test(timeout = 3000)
public void testHugeHeap() { public void testHugeHeap() {
PriorityWorkList<String> heap = new MinFourHeap<>(); PriorityWorkList<String> heap = new MinFourHeap<>(String::compareTo);
int n = 10000; int n = 10000;
// Add them // Add them
...@@ -95,7 +96,7 @@ public class MinFourHeapTests extends WorklistGradingTests { ...@@ -95,7 +96,7 @@ public class MinFourHeapTests extends WorklistGradingTests {
@Test(timeout = 3000) @Test(timeout = 3000)
public void testWithCustomComparable() { public void testWithCustomComparable() {
PriorityWorkList<Coordinate> student = new MinFourHeap<>(); PriorityWorkList<Coordinate> student = new MinFourHeap<>(Coordinate::compareTo);
Queue<Coordinate> reference = new PriorityQueue<>(); Queue<Coordinate> reference = new PriorityQueue<>();
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
...@@ -132,7 +133,7 @@ public class MinFourHeapTests extends WorklistGradingTests { ...@@ -132,7 +133,7 @@ public class MinFourHeapTests extends WorklistGradingTests {
@Test(timeout = 3000) @Test(timeout = 3000)
public void checkStructure() { public void checkStructure() {
PriorityWorkList<Integer> heap = new MinFourHeap<>(); 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}); addAll(heap, new Integer[] {10, 10, 15, 1, 17, 16, 100, 101, 102, 103, 105, 106, 107, 108});
Object[] heapData = getField(heap, "data"); Object[] heapData = getField(heap, "data");
...@@ -169,3 +170,4 @@ public class MinFourHeapTests extends WorklistGradingTests { ...@@ -169,3 +170,4 @@ public class MinFourHeapTests extends WorklistGradingTests {
} }
} }
} }
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