Verified Commit 940b9152 authored by Daniel Mangold's avatar Daniel Mangold
Browse files

Refactored configurations

parent f5974f26
package h05;
import java.util.Map;
import java.util.Random;
/**
* This class contains constants that are used as settings for the tests.
* They will not be overwritten when an update is downloaded
*/
@SuppressWarnings("JavadocReference")
public class Config {
/**
* Seed that is used for initialization of Random object, set to fixed value for (hopefully) reproducible tests / results
* @see Utils#RANDOM
*/
public static final long SEED = new Random().nextLong();
/**
* Settings for the updater / installer <br>
* Set the values of these constants to {@code true} or {@code false} respectively, if you want or don't want to...
* <ul>
* <li>{@code CHECK_FOR_UPDATES} - use the updater / installer</li>
* <li>{@code CHECK_HASHES} - compare the hashes of local files with the ones in the repository</li>
* <li>{@code AUTO_UPDATE} - let the updater / installer download files from the repository and overwrite the local files automatically</li>
* </ul>
* @see Utils.Updater
*/
public static final boolean CHECK_FOR_UPDATES = true, CHECK_HASHES = true, AUTO_UPDATE = true;
/**
* Determines which build variant should be used in MyTree.
* More specifically, what value to pass as the second parameter of the constructor.
* If the value is {@code null} then the constructor will be called randomly with either {@code true} or {@code false}
*/
public static final Boolean BUILD_VARIANT = null;
/**
* Allows customization of the number of test runs for a parameterized test method <br>
* To override the number of runs add an entry consisting of the fully qualified class name +
* '#' + the method signature mapped to an integer value (example below).
* If the method is not in this map, a default value of 5 is used
*/
public static final Map<String, Integer> NUMBER_OF_TEST_RUNS = Map.of(
"h05.MyTreeTest#testIsIsomorphic(String)", 5
);
/**
* Defines the maximum depth of a randomly generated parentheses term
* @see h05.provider.RandomTreeProvider
*/
public static final int MAX_TREE_DEPTH = 5;
}
......@@ -19,10 +19,12 @@ 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 java.lang.reflect.Modifier.*;
@SuppressWarnings("ConstantConditions")
@TestType(CLASS)
public class MyTreeTest {
......@@ -128,7 +130,7 @@ public class MyTreeTest {
@ParameterizedTest
@ArgumentsSource(TreeProvider.class)
public void testConstructor(String treeString, Boolean shouldThrowException) throws ReflectiveOperationException, IOException {
boolean buildRecursively = RANDOM.nextBoolean();
boolean buildRecursively = BUILD_VARIANT == null ? RANDOM.nextBoolean() : BUILD_VARIANT;
StringReader reader = new StringReader(treeString);
if (shouldThrowException) {
......@@ -161,7 +163,7 @@ public class MyTreeTest {
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");
......@@ -169,14 +171,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");
......@@ -184,7 +189,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");
......
......@@ -13,15 +13,13 @@ import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.*;
public class Utils {
public static final long SEED = new Random().nextLong();
import static h05.Config.*;
private static final boolean CHECK_FOR_UPDATES = true, CHECK_HASHES = true, AUTO_UPDATE = true;
public class Utils {
// -------------------------------------- //
// DO NOT CHANGE ANYTHING BELOW THIS LINE //
// -------------------------------------- //
// ----------------------------------- //
// DO NOT CHANGE ANYTHING IN THIS FILE //
// ----------------------------------- //
public static final Random RANDOM = new Random(SEED);
public static final Map<Class<?>, Boolean> CLASS_CORRECT = new HashMap<>();
......@@ -34,10 +32,26 @@ public class Utils {
);
static {
if (CHECK_FOR_UPDATES)
Updater.checkForUpdates();
try {
getClassForName("h05.Config");
if (CHECK_FOR_UPDATES)
Updater.checkForUpdates();
System.out.println("Seed: " + SEED);
System.out.println("Seed: " + SEED);
} catch (TestAbortedException e) {
System.out.println("Configurations could not be found, they must be downloaded from the repository. " +
"They will automatically be downloaded if AUTO_UPDATE is true. Please re-run the tests.");
if (AUTO_UPDATE)
try {
Updater.updateLocal("src/test/h05/Config.java");
} catch (IOException | InterruptedException ee) {
ee.printStackTrace();
}
System.exit(0);
}
}
/**
......@@ -175,8 +189,8 @@ public class Utils {
* if so:
*
* <ul>
* <li>{@link Utils#CHECK_HASHES} = {@code true}: only compares the MD5 hashes of all files</li>
* <li>{@link Utils#AUTO_UPDATE} = {@code true}: compares the MD5 hashes of all files and
* <li>{@link Config#CHECK_HASHES} = {@code true}: only compares the MD5 hashes of all files</li>
* <li>{@link Config#AUTO_UPDATE} = {@code true}: compares the MD5 hashes of all files and
* re-downloads those who do not match</li>
* </ul>
*
......
......@@ -7,17 +7,19 @@ import org.junit.jupiter.params.provider.ArgumentsProvider;
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;
public class RandomTreeProvider implements ArgumentsProvider {
@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(() -> Arguments.of(randomTreeString(MAX_TREE_DEPTH) + '\n'))
.limit(NUMBER_OF_TEST_RUNS.getOrDefault(
context.getRequiredTestMethod().getDeclaringClass().getName() + "#" + context.getDisplayName(),
5));
}
public static String randomTreeString(int maxTreeDepth) {
private static String randomTreeString(int maxTreeDepth) {
double x = Utils.RANDOM.nextDouble();
if (x < 0.1 || maxTreeDepth == 0)
......
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