159 lines
4.3 KiB
Java
159 lines
4.3 KiB
Java
package aud.exam.prep.tree;
|
|
|
|
import aud.exam.prep.ListProvider;
|
|
import aud.exam.prep.Pointer;
|
|
import aud.exam.prep.Tests;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
import org.junit.jupiter.params.provider.ArgumentsSource;
|
|
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import static aud.exam.prep.Tests.CMP;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
public abstract class OrderedTreeProcessorTest<T> {
|
|
|
|
protected final OrderedTreeProcessor<Integer, T> processor;
|
|
|
|
protected OrderedTreeProcessorTest(OrderedTreeProcessor<Integer, T> processor) {
|
|
this.processor = processor;
|
|
}
|
|
|
|
@Test
|
|
void testThat_newEmptyTreeWorks() {
|
|
T t = processor.newEmptyTree();
|
|
|
|
assertTrue(processor.check(t, CMP));
|
|
|
|
assertEquals(-1, processor.height(t));
|
|
|
|
assertTrue(processor.isBalanced(t));
|
|
|
|
assertEquals(0, processor.numberOfNodes(t));
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
assertEquals(0, processor.numberOfNodesOnLevel(t, i));
|
|
}
|
|
|
|
assertIterableEquals(List.of(), processor.iterate(t));
|
|
}
|
|
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_insertAndIterateWork(List<Integer> list) {
|
|
T t = asTree(list);
|
|
list.sort(CMP);
|
|
|
|
assertTrue(processor.check(t, CMP));
|
|
assertIterableEquals(list, processor.iterate(t));
|
|
}
|
|
|
|
private T asTree(List<Integer> list) {
|
|
T t = processor.newEmptyTree();
|
|
|
|
for (var n : list) {
|
|
t = processor.insert(t, n, CMP);
|
|
}
|
|
|
|
return t;
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_maxWorks(List<Integer> list) {
|
|
list.add(-1);
|
|
T t = asTree(list);
|
|
|
|
Integer max = Tests.getMax(list);
|
|
assertEquals(max, processor.max(t));
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_secondMaxWorks(List<Integer> list) {
|
|
list.add(-1);
|
|
list.add(-2);
|
|
T t = asTree(list);
|
|
|
|
Integer max = Tests.getSecondMax(list);
|
|
assertEquals(max, processor.secondMax(t));
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_removeOfNonExistsIsFalseAndDoesNotModify(List<Integer> list) {
|
|
T t = asTree(list);
|
|
list.sort(CMP);
|
|
|
|
var p = new Pointer<>(t);
|
|
assertFalse(processor.remove(p, -1, CMP));
|
|
assertFalse(processor.remove(p, 999, CMP));
|
|
t = p.deref;
|
|
|
|
assertTrue(processor.check(t, CMP));
|
|
assertIterableEquals(list, processor.iterate(t));
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_removeOfExistsIsTrueAndDoesModify(List<Integer> list) {
|
|
T t = asTree(list);
|
|
list.sort(CMP);
|
|
|
|
while (!list.isEmpty()) {
|
|
var toRemove = list.get(list.size()/2);
|
|
list.remove(toRemove);
|
|
|
|
var p = new Pointer<>(t);
|
|
assertTrue(processor.remove(p, toRemove, CMP));
|
|
t = p.deref;
|
|
|
|
assertTrue(processor.check(t, CMP));
|
|
assertIterableEquals(list, processor.iterate(t));
|
|
}
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_removeAllOfNonExistsIsFalseAndDoesNotModify(List<Integer> list) {
|
|
T t = asTree(list);
|
|
list.sort(CMP);
|
|
|
|
var p = new Pointer<>(t);
|
|
assertFalse(processor.removeAll(p, -1, CMP));
|
|
assertFalse(processor.removeAll(p, 999, CMP));
|
|
t = p.deref;
|
|
|
|
assertTrue(processor.check(t, CMP));
|
|
assertIterableEquals(list, processor.iterate(t));
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void testThat_removeAllOfExistsIsTrueAndDoesModify(List<Integer> list) {
|
|
T t = asTree(list);
|
|
list.sort(CMP);
|
|
|
|
while (!list.isEmpty()) {
|
|
var toRemove = list.get(list.size()/2);
|
|
list.removeAll(Collections.singleton(toRemove));
|
|
|
|
var p = new Pointer<>(t);
|
|
assertTrue(processor.removeAll(p, toRemove, CMP));
|
|
t = p.deref;
|
|
|
|
assertTrue(processor.check(t, CMP));
|
|
assertIterableEquals(list, processor.iterate(t));
|
|
}
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@ArgumentsSource(ListProvider.class)
|
|
void printATree(List<Integer> list) {
|
|
T t = asTree(list);
|
|
processor.print(t);
|
|
}
|
|
} |