Skip to content

Commit

Permalink
simplify private tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKlenner committed Jun 26, 2023
1 parent 4adedfa commit 4be8e71
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 352 deletions.
27 changes: 15 additions & 12 deletions src/graderPrivate/java/p2/FindInsertionPositionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testNoChildNoSplitting(@Property("tree") List<Object> tree,
@Property("insertionSize") int insertionSize,
@Property("expectedIndices") List<Integer> expectedIndices) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree, true);
}

@ParameterizedTest
Expand All @@ -40,7 +40,7 @@ public void testWithChildNoSplitting(@Property("tree") List<Object> tree,
@Property("insertionSize") int insertionSize,
@Property("expectedIndices") List<Integer> expectedIndices) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree, true);
}

@ParameterizedTest
Expand All @@ -52,7 +52,7 @@ public void testWithKeySplitting(@Property("tree") List<Object> tree,
@Property("expectedIndices") List<Integer> expectedIndices,
@Property("expectedTree") List<Object> expectedTree) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree, true);
}

@ParameterizedTest
Expand All @@ -64,15 +64,16 @@ public void testWithLeafSplitting(@Property("tree") List<Object> tree,
@Property("expectedIndices") List<Integer> expectedIndices,
@Property("expectedTree") List<Object> expectedTree) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree, false);
}

private void testFindInsertionPosition(List<Object> tree,
int degree,
int start,
int insertionSize,
List<Integer> expectedIndices,
List<Object> expected) throws Throwable {
List<Object> expected,
boolean simple) throws Throwable {

Context.Builder<?> context = contextBuilder()
.subject("BtrfsFile.findInsertionPosition()")
Expand Down Expand Up @@ -100,14 +101,16 @@ actualTree, new IndexedNodeLinkedList(null, getRoot(actualTree), 0), start, inse
IndexedNodeLinkedList expectedIndexedNode = createIndexedNode(null, getRoot(actualTree), new ArrayList<>(expectedIndices));
updateChildLengths(createIndexedNode(null, getRoot(expectedTree), new ArrayList<>(expectedIndices)), insertionSize);

assertTreeEquals(context.build(), "The tree is not correct", getRoot(expectedTree),
getRoot(actualTree), expectedFileAndStorage.storage(), actualFileAndStorage.storage());

assertEquals(expectedTree.getSize(), actualTree.getSize(), context.build(),
TR -> "The size of the tree should not change");
if (simple) {
assertTreeEqualsSimple(context.build(), "The tree is not correct", getRoot(expectedTree),
getRoot(actualTree), expectedFileAndStorage.storage(), actualFileAndStorage.storage());
} else {
assertTreeEquals(context.build(), "The tree is not correct", getRoot(expectedTree),
getRoot(actualTree), expectedFileAndStorage.storage(), actualFileAndStorage.storage());
assertIndexedNodeLinkedListEquals(context.build(), expectedIndexedNode, actualIndexedNode,
actualFileAndStorage.storage());
}

assertIndexedNodeLinkedListEquals(context.build(), expectedIndexedNode, actualIndexedNode,
actualFileAndStorage.storage());
}

private void updateChildLengths(IndexedNodeLinkedList indexedNode, int insertionSize) {
Expand Down
35 changes: 16 additions & 19 deletions src/graderPrivate/java/p2/FindInsertionPositionTestsPublic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
import static org.tudalgo.algoutils.tutor.general.assertions.Assertions2.assertEquals;
import static org.tudalgo.algoutils.tutor.general.assertions.Assertions2.callObject;
import static org.tudalgo.algoutils.tutor.general.assertions.Assertions2.contextBuilder;
import static p2.TreeUtil.FileAndStorage;
import static p2.TreeUtil.assertIndexedNodeLinkedListEquals;
import static p2.TreeUtil.assertTreeEquals;
import static p2.TreeUtil.constructTree;
import static p2.TreeUtil.getRoot;
import static p2.TreeUtil.treeToString;
import static p2.TreeUtil.*;

@TestForSubmission
public class FindInsertionPositionTestsPublic {
Expand All @@ -36,7 +31,7 @@ public void testNoChildNoSplitting(@Property("tree") List<Object> tree,
@Property("insertionSize") int insertionSize,
@Property("expectedIndices") List<Integer> expectedIndices) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree, true);
}

@ParameterizedTest
Expand All @@ -47,7 +42,7 @@ public void testWithChildNoSplitting(@Property("tree") List<Object> tree,
@Property("insertionSize") int insertionSize,
@Property("expectedIndices") List<Integer> expectedIndices) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, tree, true);
}

@ParameterizedTest
Expand All @@ -59,7 +54,7 @@ public void testWithKeySplitting(@Property("tree") List<Object> tree,
@Property("expectedIndices") List<Integer> expectedIndices,
@Property("expectedTree") List<Object> expectedTree) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree, true);
}

@ParameterizedTest
Expand All @@ -71,15 +66,16 @@ public void testWithLeafSplitting(@Property("tree") List<Object> tree,
@Property("expectedIndices") List<Integer> expectedIndices,
@Property("expectedTree") List<Object> expectedTree) throws Throwable {

testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree);
testFindInsertionPosition(tree, degree, start, insertionSize, expectedIndices, expectedTree, false);
}

private void testFindInsertionPosition(List<Object> tree,
int degree,
int start,
int insertionSize,
List<Integer> expectedIndices,
List<Object> expected) throws Throwable {
List<Object> expected,
boolean simple) throws Throwable {

Context.Builder<?> context = contextBuilder()
.subject("BtrfsFile.findInsertionPosition()")
Expand All @@ -105,14 +101,15 @@ actualTree, new IndexedNodeLinkedList(null, getRoot(actualTree), 0), start, inse
IndexedNodeLinkedList expectedIndexedNode = createIndexedNode(null, getRoot(actualTree), new ArrayList<>(expectedIndices));
updateChildLengths(createIndexedNode(null, getRoot(expectedTree), new ArrayList<>(expectedIndices)), insertionSize);

assertTreeEquals(context.build(), "The tree is not correct", getRoot(expectedTree),
getRoot(actualTree), expectedFileAndStorage.storage(), actualFileAndStorage.storage());

assertEquals(expectedTree.getSize(), actualTree.getSize(), context.build(),
TR -> "The size of the tree should not change");

assertIndexedNodeLinkedListEquals(context.build(), expectedIndexedNode, actualIndexedNode,
actualFileAndStorage.storage());
if (simple) {
assertTreeEqualsSimple(context.build(), "The tree is not correct", getRoot(expectedTree),
getRoot(actualTree), expectedFileAndStorage.storage(), actualFileAndStorage.storage());
} else {
assertTreeEquals(context.build(), "The tree is not correct", getRoot(expectedTree),
getRoot(actualTree), expectedFileAndStorage.storage(), actualFileAndStorage.storage());
assertIndexedNodeLinkedListEquals(context.build(), expectedIndexedNode, actualIndexedNode,
actualFileAndStorage.storage());
}
}

private void updateChildLengths(IndexedNodeLinkedList indexedNode, int insertionSize) {
Expand Down
21 changes: 14 additions & 7 deletions src/graderPrivate/java/p2/InsertTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testEnoughSpace(@Property("tree") List<Object> tree,
@Property("intervalsToInsert") List<String> intervalsToInsert,
@Property("index") int index,
@Property("expected") List<Object> expected) throws Throwable {
testInsert(tree, degree, intervalsToInsert, index, expected);
testInsert(tree, degree, intervalsToInsert, index, expected, true);
}

@ParameterizedTest
Expand All @@ -38,7 +38,7 @@ public void testNotEnoughSpaceSameNode(@Property("tree") List<Object> tree,
@Property("intervalsToInsert") List<String> intervalsToInsert,
@Property("index") int index,
@Property("expected") List<Object> expected) throws Throwable {
testInsert(tree, degree, intervalsToInsert, index, expected);
testInsert(tree, degree, intervalsToInsert, index, expected, true);
}

@ParameterizedTest
Expand All @@ -48,14 +48,15 @@ public void testNotEnoughSpaceNewNode(@Property("tree") List<Object> tree,
@Property("intervalsToInsert") List<String> intervalsToInsert,
@Property("index") int index,
@Property("expected") List<Object> expected) throws Throwable {
testInsert(tree, degree, intervalsToInsert, index, expected);
testInsert(tree, degree, intervalsToInsert, index, expected, false);
}

private void testInsert(List<Object> tree,
int degree,
List<String> intervalsToInsert,
int index,
List<Object> expected) throws Throwable {
List<Object> expected,
boolean simple) throws Throwable {

Context.Builder<?> context = contextBuilder()
.subject("BtrfsFile.insert()")
Expand Down Expand Up @@ -84,9 +85,15 @@ private void testInsert(List<Object> tree,
call(() -> callInsert(actualTree, new ArrayList<>(intervals), new IndexedNodeLinkedList(null, getRoot(actualTree), index)), context.build(),
TR -> "BtrfsFile.insert() should not throw an exception.");

assertTreeEquals(context.build(), "The tree is not correct.",
getRoot(expectedTree), getRoot(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
if (simple) {
assertTreeEqualsSimple(context.build(), "The tree is not correct.",
getRoot(expectedTree), getRoot(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
} else {
assertTreeEquals(context.build(), "The tree is not correct.",
getRoot(expectedTree), getRoot(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
}
}

private void callInsert(BtrfsFile tree, List<Interval> intervals, IndexedNodeLinkedList indexedLeaf) throws Exception {
Expand Down
21 changes: 14 additions & 7 deletions src/graderPrivate/java/p2/InsertTestsPublic.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testEnoughSpace(@Property("tree") List<Object> tree,
@Property("intervalsToInsert") List<String> intervalsToInsert,
@Property("index") int index,
@Property("expected") List<Object> expected) throws Throwable {
testInsert(tree, degree, intervalsToInsert, index, expected);
testInsert(tree, degree, intervalsToInsert, index, expected, true);
}

@ParameterizedTest
Expand All @@ -39,7 +39,7 @@ public void testNotEnoughSpaceSameNode(@Property("tree") List<Object> tree,
@Property("intervalsToInsert") List<String> intervalsToInsert,
@Property("index") int index,
@Property("expected") List<Object> expected) throws Throwable {
testInsert(tree, degree, intervalsToInsert, index, expected);
testInsert(tree, degree, intervalsToInsert, index, expected, true);
}

@ParameterizedTest
Expand All @@ -49,14 +49,15 @@ public void testNotEnoughSpaceNewNode(@Property("tree") List<Object> tree,
@Property("intervalsToInsert") List<String> intervalsToInsert,
@Property("index") int index,
@Property("expected") List<Object> expected) throws Throwable {
testInsert(tree, degree, intervalsToInsert, index, expected);
testInsert(tree, degree, intervalsToInsert, index, expected, false);
}

private void testInsert(List<Object> tree,
int degree,
List<String> intervalsToInsert,
int index,
List<Object> expected) throws Throwable {
List<Object> expected,
boolean simple) throws Throwable {

Context.Builder<?> context = contextBuilder()
.subject("BtrfsFile.insert()")
Expand All @@ -83,9 +84,15 @@ private void testInsert(List<Object> tree,
call(() -> callInsert(actualTree, new ArrayList<>(intervals), new IndexedNodeLinkedList(null, getRoot(actualTree), index)), context.build(),
TR -> "BtrfsFile.insert() should not throw an exception.");

assertTreeEquals(context.build(), "The tree is not correct.",
getRoot(expectedTree), getRoot(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
if (simple) {
assertTreeEqualsSimple(context.build(), "The tree is not correct.",
getRoot(expectedTree), getRoot(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
} else {
assertTreeEquals(context.build(), "The tree is not correct.",
getRoot(expectedTree), getRoot(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
}
}

private void callInsert(BtrfsFile tree, List<Interval> intervals, IndexedNodeLinkedList indexedLeaf) throws Exception {
Expand Down
49 changes: 29 additions & 20 deletions src/graderPrivate/java/p2/MergeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,35 @@ private void testMerge(List<Object> tree,

context.add("actual tree", treeToString(actualTree, actualFileAndStorage.storage()));

assertTreeEquals(context.build(), "The tree is not correct.",
testedNodeFunction.apply(expectedTree), testedNodeFunction.apply(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());

assertEquals(expectedTree.getSize(), actualTree.getSize(), context.build(),
TR -> "The size of the tree should not change");

assertEquals(expectedParentIndex, indexedRoot.index, context.build(),
TR -> "The index of the parent should not change");
assertEquals(root, indexedRoot.node, context.build(),
TR -> "The node of the parent should not change");
assertEquals(null, indexedRoot.parent, context.build(),
TR -> "The parent of the parent should not change");

assertEquals(expectedChildIndex, indexedChild.index, context.build(),
TR -> "The index of the child is not correct");
assertEquals(root.children[expectedParentIndex], indexedChild.node, context.build(),
TR -> "The node of the child is not correct");
assertEquals(indexedRoot, indexedChild.parent, context.build(),
TR -> "The parent of the child should not change");
if (right) {

assertTreeEqualsSimple(context.build(), "The tree is not correct.",
testedNodeFunction.apply(expectedTree), testedNodeFunction.apply(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());
} else {
assertTreeEquals(context.build(), "The tree is not correct.",
testedNodeFunction.apply(expectedTree), testedNodeFunction.apply(actualTree),
expectedFileAndStorage.storage(), actualFileAndStorage.storage());

assertEquals(expectedTree.getSize(), actualTree.getSize(), context.build(),
TR -> "The size of the tree should not change");

assertEquals(expectedParentIndex, indexedRoot.index, context.build(),
TR -> "The index of the parent should not change");
assertEquals(root, indexedRoot.node, context.build(),
TR -> "The node of the parent should not change");
assertEquals(null, indexedRoot.parent, context.build(),
TR -> "The parent of the parent should not change");

assertEquals(expectedChildIndex, indexedChild.index, context.build(),
TR -> "The index of the child is not correct");
assertEquals(root.children[expectedParentIndex], indexedChild.node, context.build(),
TR -> "The node of the child is not correct");
assertEquals(indexedRoot, indexedChild.parent, context.build(),
TR -> "The parent of the child should not change");
}


}

private void callMergeRight(BtrfsFile tree, IndexedNodeLinkedList indexedNode) throws Exception {
Expand Down
Loading

0 comments on commit 4be8e71

Please sign in to comment.