Commit f0fa626c authored by Daniel Mangold's avatar Daniel Mangold
Browse files

Update src/test/java/h05/MyTreeTest.java,...

Update src/test/java/h05/MyTreeTest.java, src/test/java/h05/MyParenthesesTreeIteratorTest.java, src/test/java/h05/provider/RandomTreeProvider.java files
Deleted src/test/java/h05/provider/TreeProvider.java
parent 670286f4
......@@ -13,7 +13,6 @@ import static h05.Assertions.*;
import static h05.Utils.TestType.Type.CLASS;
import static h05.Utils.*;
import static java.lang.reflect.Modifier.*;
import static org.junit.jupiter.api.Assertions.*;
@TestType(CLASS)
public class MyParenthesesTreeIteratorTest {
......@@ -52,6 +51,7 @@ public class MyParenthesesTreeIteratorTest {
public void testNext(String treeString) throws ReflectiveOperationException {
requireTest(MyTreeTest.class.getDeclaredMethod("testIterator"));
treeString = treeString.substring(0, treeString.indexOf('\n') + 1);
Iterator<Character> iterator = MyTreeTest.getIterator(treeString);
StringBuilder builder = new StringBuilder();
......@@ -75,6 +75,7 @@ public class MyParenthesesTreeIteratorTest {
requireTest(MyTreeTest.class.getDeclaredMethod("testIterator"));
requireTest(MyParenthesesTreeIteratorTest.class.getDeclaredMethod("testNext", String.class), "()\n");
treeString = treeString.substring(0, treeString.indexOf('\n') + 1);
Iterator<Character> iterator = MyTreeTest.getIterator(treeString);
for (int i = 0; i < treeString.length() - 1; i++, iterator.next())
......
package h05;
import h05.Utils.*;
import h05.provider.RandomTreeProvider;
import h05.provider.TreeProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
......@@ -19,11 +17,13 @@ import java.util.Iterator;
import java.util.Stack;
import static h05.Assertions.*;
import static h05.Config.BUILD_VARIANT;
import static h05.Utils.*;
import static h05.Utils.TestType.Type.CLASS;
import static h05.provider.RandomTreeProvider.treeToString;
import static java.lang.reflect.Modifier.*;
import static org.junit.jupiter.api.Assertions.*;
@SuppressWarnings("ConstantConditions")
@TestType(CLASS)
public class MyTreeTest {
......@@ -129,76 +129,45 @@ public class MyTreeTest {
@ParameterizedTest
@ArgumentsSource(RandomTreeProvider.class)
void testConstructorWithValid(String treeString) throws ReflectiveOperationException, IOException {
assertConstructorWorks(treeString, true);
assertConstructorWorks(treeString, false);
boolean recursive = BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT;
StringReader reader = new StringReader(treeString);
Object instance = constructor.newInstance(reader, recursive);
int endOfTree = treeString.indexOf('\n');
assertEquals(treeString.substring(0, endOfTree), treeToString(root.get(instance), true),
"Trees do not match, called constructor with second parameter = " + recursive);
assertRestOfReaderMatches(treeString.substring(endOfTree + 1), reader);
}
@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;
}
boolean recursive = BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT;
var index = RANDOM.nextInt(endOfTree);
return new StringBuilder(treeString)
.insert(index, invalidator)
.toString();
}
private String getInvalidator() {
var n = RANDOM.nextInt(10);
if (n < 2) {
return Utils.randomLatin(3);
}
return n % 2 == 0 ? ")" : "(";
}
assertThrows(BadStringOperationException.class, () -> {
try {
String invalidTreeString = treeString.replaceFirst(
"\n", RANDOM.nextBoolean() ? "" : RANDOM.nextBoolean() ? "(\n" : ")\n");
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);
constructor.newInstance(new StringReader(invalidTreeString), recursive);
} catch (ReflectiveOperationException e) {
throw e.getCause();
}
}, "Constructor did not throw BadStringOperationException, called with second parameter = " + recursive);
}
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(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
public void testIterator() throws ReflectiveOperationException {
requireTest(MyParenthesesTreeIteratorTest.class);
Object instance = constructor.newInstance(new StringReader("\n"), false);
Object instance = constructor.newInstance(new StringReader("\n"), BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT);
assertEquals(MyParenthesesTreeIteratorTest.myParenthesesTreeIteratorClass, iterator.invoke(instance).getClass(),
"Returned object does not have type MyParenthesesTreeIterator");
......@@ -206,14 +175,17 @@ public class MyTreeTest {
@SuppressWarnings("unchecked")
public static Iterator<Character> getIterator(String treeString) throws ReflectiveOperationException {
return (Iterator<Character>) iterator.invoke(constructor.newInstance(new StringReader(treeString), false));
return (Iterator<Character>) iterator.invoke(constructor.newInstance(new StringReader(treeString),
BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT));
}
@ParameterizedTest
@ArgumentsSource(RandomTreeProvider.class)
public void testIsIsomorphic(String treeString) throws ReflectiveOperationException {
Object instance1 = constructor.newInstance(new StringReader(treeString), false),
instance2 = constructor.newInstance(new StringReader(treeString), false);
Object instance1 = constructor.newInstance(new StringReader(treeString),
BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT),
instance2 = constructor.newInstance(new StringReader(treeString),
BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT);
assertTrue((Boolean) isIsomorphic.invoke(instance1, instance2), "Trees are isomorphic");
assertTrue((Boolean) isIsomorphic.invoke(instance2, instance1), "Trees are isomorphic");
......@@ -221,7 +193,8 @@ public class MyTreeTest {
assertTrue((Boolean) isIsomorphic.invoke(instance2, instance2), "Trees are isomorphic");
String nonIsomorphicTreeString = "(((((())))))\n";
Object nonIsomorphicInstance = constructor.newInstance(new StringReader(nonIsomorphicTreeString), false);
Object nonIsomorphicInstance = constructor.newInstance(new StringReader(nonIsomorphicTreeString),
BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT);
assertFalse((Boolean) isIsomorphic.invoke(instance1, nonIsomorphicInstance), "Trees are not isomorphic");
assertFalse((Boolean) isIsomorphic.invoke(nonIsomorphicInstance, instance1), "Trees are not isomorphic");
......
package h05.provider;
import h05.ListItemTest;
import h05.MyTreeNodeTest;
import h05.Utils;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.StringJoiner;
import java.util.stream.Stream;
public class RandomTreeProvider implements ArgumentsProvider {
import static h05.Config.*;
private static final int MAX_STREAM_SIZE = 5;
private static final int MAX_TREE_DEPTH = 5;
private static final int MAX_REST_SIZE = 5;
public class RandomTreeProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.generate(RandomTreeProvider::randomTreeAguments).limit(MAX_STREAM_SIZE);
return Stream.generate(RandomTreeProvider::randomTreeArguments)
.limit(NUMBER_OF_TEST_RUNS.getOrDefault(
context.getRequiredTestMethod().getDeclaringClass().getName() + "#" + context.getDisplayName(),
5));
}
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));
private static Arguments randomTreeArguments() {
String treeString = randomTreeString(MAX_TREE_DEPTH);
return Arguments.of(treeString + '\n' + (char) ('A' + Utils.RANDOM.nextInt(26)));
}
public static String randomTreeString(int maxTreeDepth) {
private static String randomTreeString(int maxTreeDepth) {
double x = Utils.RANDOM.nextDouble();
if (x < 0.1 || maxTreeDepth == 0)
......@@ -34,4 +38,13 @@ public class RandomTreeProvider implements ArgumentsProvider {
return randomTreeString(maxTreeDepth - 1) + randomTreeString(maxTreeDepth - 1);
}
public static String treeToString(Object root, boolean isRoot) throws ReflectiveOperationException {
StringJoiner joiner = new StringJoiner("");
for (Object p = MyTreeNodeTest.successors.get(root); p != null; p = ListItemTest.next.get(p))
joiner.add(treeToString(ListItemTest.key.get(p), false));
return isRoot ? joiner.toString() : "(" + joiner + ")";
}
}
package h05.provider;
import h05.ListItemTest;
import h05.MyTreeNodeTest;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.StringJoiner;
import java.util.stream.Stream;
public class TreeProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("()()\n", false),
Arguments.of("(())(()())\n", false),
Arguments.of("\n", false),
Arguments.of("()\n()", false),
Arguments.of("()\nabc", false),
Arguments.of("", true),
Arguments.of("abc\n", true),
Arguments.of("(()))\n", true),
Arguments.of(")()()\n", true),
Arguments.of("(((()()))\n", true)
);
}
public static String treeToString(Object root, boolean isRoot) throws ReflectiveOperationException {
StringJoiner joiner = new StringJoiner("");
for (Object p = MyTreeNodeTest.successors.get(root); p != null; p = ListItemTest.next.get(p))
joiner.add(treeToString(ListItemTest.key.get(p), false));
return isRoot ? joiner.toString() : "(" + joiner + ")";
}
}
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