Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Hedtke, Moritz
AuD 2021 H05 Tests
Commits
940b9152
Verified
Commit
940b9152
authored
Jun 12, 2021
by
Daniel Mangold
Browse files
Refactored configurations
parent
f5974f26
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/test/java/h05/Config.java
0 → 100644
View file @
940b9152
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
;
}
src/test/java/h05/MyTreeTest.java
View file @
940b9152
...
...
@@ -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"
);
...
...
src/test/java/h05/Utils.java
View file @
940b9152
...
...
@@ -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
LIN
E //
// -----------------------------------
---
//
// ----------------------------------- //
// DO NOT CHANGE ANYTHING
IN
THIS
FIL
E //
// ----------------------------------- //
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>
*
...
...
src/test/java/h05/provider/RandomTreeProvider.java
View file @
940b9152
...
...
@@ -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
));
}
p
ublic
static
String
randomTreeString
(
int
maxTreeDepth
)
{
p
rivate
static
String
randomTreeString
(
int
maxTreeDepth
)
{
double
x
=
Utils
.
RANDOM
.
nextDouble
();
if
(
x
<
0.1
||
maxTreeDepth
==
0
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment