Verified Commit fe3b78a1 authored by Daniel Mangold's avatar Daniel Mangold
Browse files

Optimized RandomTreeProvider

parents 005f9ccc 946f405c
......@@ -3,7 +3,7 @@ src/test/java/h05/Utils.java afb4a4a21023642a53304ab9bbf
src/test/java/h05/Assertions.java 02349c33cf60fea1b77b0c457bf1fad8
src/test/java/h05/ListItemTest.java d746f290bee1694c5b79e77538c86fa5
src/test/java/h05/MyTreeNodeTest.java d2de48e7fa2087a08fc6d81fdb273064
src/test/java/h05/MyTreeTest.java 0518763edafad0433db866dd6e0ef743
src/test/java/h05/MyTreeTest.java 5a77bf969645a08aa50966f4d5cb9319
src/test/java/h05/MyParenthesesTreeIteratorTest.java 311cd428d3c3c82069f28905bd5831e7
src/test/java/h05/provider/TreeProvider.java 955510b43e5a8b60e0d92aebbdf1b6a9
src/test/java/h05/provider/RandomTreeProvider.java 61e0086e05bde7f9faa60d570000804d
\ No newline at end of file
src/test/java/h05/provider/RandomTreeProvider.java 4f610933595546a166e106ad9929b290
\ No newline at end of file
### Version 1.0
Initial tests
\ No newline at end of file
Initial tests \
Addendum: Optimized RandomTreeProvider
\ No newline at end of file
......@@ -182,7 +182,7 @@ public class MyTreeTest {
assertTrue((Boolean) isIsomorphic.invoke(instance1, instance2), "Trees are isomorphic");
assertTrue((Boolean) isIsomorphic.invoke(instance2, instance1), "Trees are isomorphic");
String nonIsomorphicTreeString = RandomTreeProvider.randomTreeString(3, 2);
String nonIsomorphicTreeString = "(((((())))))";
Object nonIsomorphicInstance = constructor.newInstance(new StringReader(nonIsomorphicTreeString), false);
assertFalse((Boolean) isIsomorphic.invoke(instance1, nonIsomorphicInstance), "Trees are not isomorphic");
......
......@@ -10,36 +10,21 @@ import java.util.stream.Stream;
public class RandomTreeProvider implements ArgumentsProvider {
private static final int MAX_STREAM_SIZE = 5;
private static final int MAX_TREE_DEPTH = 3, MAX_NODES = 10;
private static final int MAX_TREE_DEPTH = 5;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.generate(() -> Arguments.of(randomTreeString(MAX_NODES, MAX_TREE_DEPTH))).limit(MAX_STREAM_SIZE);
return Stream.generate(() -> Arguments.of(randomTreeString(MAX_TREE_DEPTH))).limit(MAX_STREAM_SIZE);
}
public static String randomTreeString(int maxNodes, int maxTreeDepth) {
int unclosedParentheses = 0, numberOfNodes = 0;
StringBuilder builder = new StringBuilder();
while (numberOfNodes < maxNodes)
if (Utils.RANDOM.nextBoolean() && unclosedParentheses < maxTreeDepth) {
builder.append('(');
unclosedParentheses++;
numberOfNodes++;
} else {
if (unclosedParentheses > 0) {
builder.append(')');
unclosedParentheses--;
} else {
builder.append('(');
unclosedParentheses++;
numberOfNodes++;
}
}
for (; unclosedParentheses > 0; unclosedParentheses--)
builder.append(')');
return builder.toString();
public static String randomTreeString(int maxTreeDepth) {
double x = Utils.RANDOM.nextDouble();
if (x < 0.1 || maxTreeDepth == 0)
return "";
else if (x < 0.4)
return "(" + randomTreeString(maxTreeDepth - 1) + ")";
return randomTreeString(maxTreeDepth - 1) + randomTreeString(maxTreeDepth - 1);
}
}
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