Commit 670286f4 authored by Renk, Jonas's avatar Renk, Jonas
Browse files

Refactor MyTreeTest.testConstructor()

All inputs are now generated at random.
Also all four cases are covered:

- Rec, Valid
- Rec, Invalid
- Iter, Valid
- Iter, Invalid
parent 63e2e7b5
package h05;
import h05.Utils.TestType;
import h05.Utils.*;
import h05.provider.RandomTreeProvider;
import h05.provider.TreeProvider;
import org.junit.jupiter.api.BeforeAll;
......@@ -127,35 +127,71 @@ public class MyTreeTest {
}
@ParameterizedTest
@ArgumentsSource(TreeProvider.class)
public void testConstructor(String treeString, Boolean shouldThrowException) throws ReflectiveOperationException, IOException {
boolean buildRecursively = RANDOM.nextBoolean();
StringReader reader = new StringReader(treeString);
@ArgumentsSource(RandomTreeProvider.class)
void testConstructorWithValid(String treeString) throws ReflectiveOperationException, IOException {
assertConstructorWorks(treeString, true);
assertConstructorWorks(treeString, false);
}
@ParameterizedTest
@ArgumentsSource(RandomTreeProvider.class)
void testConstructorWithInvalid(String treeString) {
treeString = invalidate(treeString);
assertConstructorThrows(treeString, true);
assertConstructorThrows(treeString, false);
}
private String invalidate(String treeString) {
var invalidator = getInvalidator();
var endOfTree = treeString.indexOf('\n');
if (endOfTree == 0) {
return invalidator + treeString;
}
var index = RANDOM.nextInt(endOfTree);
return new StringBuilder(treeString)
.insert(index, invalidator)
.toString();
}
if (shouldThrowException) {
assertThrows(BadStringOperationException.class, () -> {
try {
constructor.newInstance(reader, buildRecursively);
} catch (ReflectiveOperationException e) {
throw e.getCause();
}
}, "Constructor did not throw BadStringOperationException, called with second parameter = " + buildRecursively);
} else {
Object instance = constructor.newInstance(reader, buildRecursively);
int i = 0;
while (i < treeString.length() && (treeString.charAt(i) == '(' || treeString.charAt(i) == ')'))
i++;
assertEquals(treeString.substring(0, i), TreeProvider.treeToString(root.get(instance), true),
"Trees do not match, called constructor with second parameter = " + buildRecursively);
for (i = treeString.length() - i - 1; i > 0; i--)
assertEquals(treeString.charAt(treeString.length() - i), (char) reader.read(),
"Reader did not return correct character");
assertEquals(-1, reader.read(), "Reader should not have any characters left");
private String getInvalidator() {
var n = RANDOM.nextInt(10);
if (n < 2) {
return Utils.randomLatin(3);
}
return n % 2 == 0 ? ")" : "(";
}
private void assertConstructorWorks(String treeString, boolean buildRecursively) throws ReflectiveOperationException, IOException {
StringReader reader = new StringReader(treeString);
Object instance = constructor.newInstance(reader, buildRecursively);
int endOfTree = treeString.indexOf('\n');
assertTreeStringEqual(treeString.substring(0, endOfTree), instance);
assertRestOfReaderMatches(treeString.substring(endOfTree+1), reader);
}
private void assertRestOfReaderMatches(String rest, StringReader reader) throws IOException {
for (int i = 0; i < rest.length(); i++)
assertEquals(rest.charAt(i), reader.read(),
"Reader did not return correct character");
assertEquals(-1, reader.read(), "Reader should not have any characters left");
}
private void assertTreeStringEqual(String treeString, Object instance) throws ReflectiveOperationException {
assertEquals(treeString, TreeProvider.treeToString(root.get(instance), true),
"Trees do not match, called constructor with second parameter = " + buildRecursively);
}
private void assertConstructorThrows(String treeString, boolean buildRecursively) {
assertThrows(BadStringOperationException.class, () -> {
try {
constructor.newInstance(new StringReader(treeString), buildRecursively);
} catch (ReflectiveOperationException e) {
throw e.getCause();
}
}, "Constructor did not throw BadStringOperationException, called with second parameter = " + buildRecursively);
}
@Test
......
......@@ -12,12 +12,14 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Utils {
public static final long SEED = new Random().nextLong();
private static final boolean CHECK_FOR_UPDATES = true, CHECK_HASHES = true, AUTO_UPDATE = true;
private static final boolean CHECK_FOR_UPDATES = false, CHECK_HASHES = false, AUTO_UPDATE = false;
// -------------------------------------- //
// DO NOT CHANGE ANYTHING BELOW THIS LINE //
......@@ -166,6 +168,15 @@ public class Utils {
}
}
public static String randomLatin(int size) {
return IntStream
.generate(() -> 'A' + RANDOM.nextInt(26))
.mapToObj(n ->
String.valueOf(new char[] {(char) n}))
.limit(size)
.collect(Collectors.joining());
}
private static class Updater {
private static final String REPOSITORY_URL = "https://git.rwth-aachen.de/aud-tests/AuD-2021-" + ASSIGNMENT_ID + "-Student/-/";
......
......@@ -11,10 +11,17 @@ public class RandomTreeProvider implements ArgumentsProvider {
private static final int MAX_STREAM_SIZE = 5;
private static final int MAX_TREE_DEPTH = 5;
private static final int MAX_REST_SIZE = 5;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.generate(() -> Arguments.of(randomTreeString(MAX_TREE_DEPTH) + '\n')).limit(MAX_STREAM_SIZE);
return Stream.generate(RandomTreeProvider::randomTreeAguments).limit(MAX_STREAM_SIZE);
}
private static Arguments randomTreeAguments() {
var treeString = randomTreeString(MAX_TREE_DEPTH);
var n = Utils.RANDOM.nextInt(MAX_REST_SIZE);
return Arguments.of(treeString + '\n' + Utils.randomLatin(n));
}
public static String randomTreeString(int maxTreeDepth) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment