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; package h05;
import h05.Utils.TestType; import h05.Utils.*;
import h05.provider.RandomTreeProvider; import h05.provider.RandomTreeProvider;
import h05.provider.TreeProvider; import h05.provider.TreeProvider;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
...@@ -127,35 +127,71 @@ public class MyTreeTest { ...@@ -127,35 +127,71 @@ public class MyTreeTest {
} }
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(TreeProvider.class) @ArgumentsSource(RandomTreeProvider.class)
public void testConstructor(String treeString, Boolean shouldThrowException) throws ReflectiveOperationException, IOException { void testConstructorWithValid(String treeString) throws ReflectiveOperationException, IOException {
boolean buildRecursively = RANDOM.nextBoolean(); assertConstructorWorks(treeString, true);
StringReader reader = new StringReader(treeString); 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) { private String getInvalidator() {
assertThrows(BadStringOperationException.class, () -> { var n = RANDOM.nextInt(10);
try { if (n < 2) {
constructor.newInstance(reader, buildRecursively); return Utils.randomLatin(3);
} 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");
} }
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 @Test
......
...@@ -12,12 +12,14 @@ import java.security.MessageDigest; ...@@ -12,12 +12,14 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.time.Duration; import java.time.Duration;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Utils { public class Utils {
public static final long SEED = new Random().nextLong(); 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 // // DO NOT CHANGE ANYTHING BELOW THIS LINE //
...@@ -166,6 +168,15 @@ public class Utils { ...@@ -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 class Updater {
private static final String REPOSITORY_URL = "https://git.rwth-aachen.de/aud-tests/AuD-2021-" + ASSIGNMENT_ID + "-Student/-/"; 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 { ...@@ -11,10 +11,17 @@ public class RandomTreeProvider implements ArgumentsProvider {
private static final int MAX_STREAM_SIZE = 5; private static final int MAX_STREAM_SIZE = 5;
private static final int MAX_TREE_DEPTH = 5; private static final int MAX_TREE_DEPTH = 5;
private static final int MAX_REST_SIZE = 5;
@Override @Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { 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) { 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