Skip to content

Commit

Permalink
Publish latest features.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed May 25, 2021
1 parent 62da9db commit c609dd8
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/Lint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import java.io.StringWriter
import java.util.Properties

group = "com.github.cs125-illinois"
version = "2021.5.4"
version = "2021.5.5"

plugins {
kotlin("jvm") version "1.5.10"
Expand Down
47 changes: 29 additions & 18 deletions src/main/kotlin/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.lang.reflect.Executable
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.lang.reflect.Parameter
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.Random
Expand Down Expand Up @@ -96,12 +97,12 @@ annotation class FixedParameters {
"@$name parameter fields must annotate parameterized collections"
}
collectionType.actualTypeArguments.first().also { itemType ->
check(itemType is ParameterizedType && itemType.rawType in parameterGroupTypes) {
"@$name parameter fields must annotate collections of types " +
parameterGroupTypes.joinToString(", ") { it.simpleName }
}
field.isAccessible = true
return itemType.actualTypeArguments
return if (itemType is ParameterizedType && itemType.rawType in parameterGroupTypes) {
itemType.actualTypeArguments
} else {
arrayOf(itemType)
}
}
}
}
Expand All @@ -126,18 +127,18 @@ annotation class RandomParameters {
}
2 -> {
check(
method.parameterTypes[0] == Int::class.java
&& method.parameterTypes[1] == Random::class.java
method.parameterTypes[0] == Int::class.java &&
method.parameterTypes[1] == Random::class.java
) { message }
}
else -> error(message)
}
check(method.returnType in parameterGroupTypes) {
"@$name parameter methods must return one of types " +
parameterGroupTypes.joinToString(", ") { it.simpleName }
}
method.isAccessible = true
return (method.genericReturnType as ParameterizedType).actualTypeArguments
return if (method.returnType in parameterGroupTypes) {
(method.genericReturnType as ParameterizedType).actualTypeArguments
} else {
arrayOf(method.genericReturnType)
}
}
}
}
Expand Down Expand Up @@ -320,6 +321,12 @@ annotation class CheckSource {

fun Method.isCheckSource() = isAnnotationPresent(CheckSource::class.java)

@Target(AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class NotNull

fun Parameter.isNotNull() = isAnnotationPresent(NotNull::class.java)

fun Field.isStatic() = Modifier.isStatic(modifiers)
fun Field.isFinal() = Modifier.isFinal(modifiers)
fun Field.isPublic() = Modifier.isPublic(modifiers)
Expand Down Expand Up @@ -593,11 +600,12 @@ fun List<*>.deepCompare(other: List<*>) = if (size != other.size) {
}

@Suppress("ReturnCount")
fun Type.parameterGroupMatches(parameters: Array<Type>) = if (this == None::class.java && parameters.isEmpty()) {
true
} else {
(this as ParameterizedType).actualTypeArguments.compareBoxed(parameters)
}
fun Type.parameterGroupMatches(parameters: Array<Type>) =
if (this == None::class.java && parameters.isEmpty()) {
true
} else {
(this as ParameterizedType).actualTypeArguments.compareBoxed(parameters)
}

// Borrowed from Kotlin core

Expand All @@ -611,7 +619,10 @@ internal fun <T> Array<out T>?.safeContentDeepToString(): String {
}

@Suppress("ComplexMethod")
private fun <T> Array<out T>.safeContentDeepToStringInternal(result: StringBuilder, processed: MutableList<Array<*>>) {
private fun <T> Array<out T>.safeContentDeepToStringInternal(
result: StringBuilder,
processed: MutableList<Array<*>>
) {
if (this in processed) {
result.append("[...]")
return
Expand Down
40 changes: 20 additions & 20 deletions src/main/kotlin/Submission.kt
Original file line number Diff line number Diff line change
Expand Up @@ -434,33 +434,33 @@ class Submission(val solution: Solution, val submission: Class<*>, private val s
sealed class SubmissionDesignError(message: String) : RuntimeException(message)
class SubmissionDesignMissingMethodError(klass: Class<*>, executable: Executable) : SubmissionDesignError(
"Submission class ${klass.name} didn't provide ${
if (executable.isStatic()) {
"static "
} else {
""
}
if (executable.isStatic()) {
"static "
} else {
""
}
}${
if (executable is Method) {
"method"
} else {
"constructor"
}
if (executable is Method) {
"method"
} else {
"constructor"
}
} ${executable.fullName()}"
)

class SubmissionDesignExtraMethodError(klass: Class<*>, executable: Executable) : SubmissionDesignError(
"Submission class ${klass.name} provided extra ${
if (executable.isStatic()) {
"static "
} else {
""
}
if (executable.isStatic()) {
"static "
} else {
""
}
}${
if (executable is Method) {
"method"
} else {
"constructor"
}
if (executable is Method) {
"method"
} else {
"constructor"
}
} ${executable.fullName()}"
)

Expand Down
46 changes: 36 additions & 10 deletions src/main/kotlin/generators/Parameters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package edu.illinois.cs.cs125.jenisol.core.generators

import edu.illinois.cs.cs125.jenisol.core.EdgeType
import edu.illinois.cs.cs125.jenisol.core.FixedParameters
import edu.illinois.cs.cs125.jenisol.core.One
import edu.illinois.cs.cs125.jenisol.core.ParameterGroup
import edu.illinois.cs.cs125.jenisol.core.RandomParameters
import edu.illinois.cs.cs125.jenisol.core.RandomType
Expand All @@ -15,6 +16,7 @@ import edu.illinois.cs.cs125.jenisol.core.asArray
import edu.illinois.cs.cs125.jenisol.core.deepCopy
import edu.illinois.cs.cs125.jenisol.core.isEdgeType
import edu.illinois.cs.cs125.jenisol.core.isFixedParameters
import edu.illinois.cs.cs125.jenisol.core.isNotNull
import edu.illinois.cs.cs125.jenisol.core.isRandomParameters
import edu.illinois.cs.cs125.jenisol.core.isRandomType
import edu.illinois.cs.cs125.jenisol.core.isSimpleType
Expand Down Expand Up @@ -306,6 +308,7 @@ interface ExecutableGenerator {
class MethodParametersGeneratorGenerator(target: Executable) {
val fixedParameters: Collection<ParameterGroup>?
val randomParameters: Method?
val notNullParameters = target.parameters.map { it.isNotNull() }

init {
val parameterTypes = target.genericParameterTypes.map { type -> type as Type }.toTypedArray()
Expand All @@ -322,13 +325,14 @@ class MethodParametersGeneratorGenerator(target: Executable) {
check(values is Collection<*>) { "@${FixedParameters.name} field does not contain a collection" }
check(values.isNotEmpty()) { "@${FixedParameters.name} field contains as empty collection" }
@Suppress("SwallowedException")
try {
val actualValues = try {
values.filterNotNull().forEach { it as ParameterGroup }
@Suppress("UNCHECKED_CAST")
values as Collection<ParameterGroup>
} catch (e: ClassCastException) {
error("@${FixedParameters.name} field does not contain a collection of parameter groups")
values.map { One(it) }
}
values.forEach {
actualValues.forEach {
val solutionParameters = it.deepCopy()
val submissionParameters = it.deepCopy()
check(solutionParameters !== submissionParameters) {
Expand All @@ -338,7 +342,7 @@ class MethodParametersGeneratorGenerator(target: Executable) {
"@${FixedParameters.name} field does not produce equal copies"
}
}
values
actualValues
}
randomParameters = target.declaringClass.declaredMethods
.filter { method -> method.isRandomParameters() }
Expand All @@ -355,15 +359,23 @@ class MethodParametersGeneratorGenerator(target: Executable) {
parametersGenerator: ParametersGeneratorGenerator?,
settings: Settings,
random: Random = Random
) = ConfiguredParametersGenerator(parametersGenerator, settings, random, fixedParameters, randomParameters)
) = ConfiguredParametersGenerator(
parametersGenerator,
settings,
random,
fixedParameters,
randomParameters,
notNullParameters
)
}

class ConfiguredParametersGenerator(
parametersGenerator: ParametersGeneratorGenerator?,
private val settings: Settings,
private val random: Random = Random,
overrideFixed: Collection<ParameterGroup>? = null,
private val overrideRandom: Method? = null
overrideFixed: Collection<ParameterGroup>?,
private val overrideRandom: Method?,
private val notNullParameters: List<Boolean>
) : ExecutableGenerator {
private val generator = if (overrideFixed != null && overrideRandom != null) {
null
Expand All @@ -390,17 +402,25 @@ class ConfiguredParametersGenerator(

override val fixed: List<Parameters> by lazy {
if (overrideFixed != null) {
overrideFixed.toFixedParameters()
overrideFixed.toFixedParameters().also { parameters ->
check(parameters.none { it.filterNotNull() }) {
"@FixedParameters list contains null values for parameters marked as @NotNull"
}
}
} else {
check(generator != null) { "Automatic parameter generator was unexpectedly null" }
generator.let {
it.simple.trim(settings.simpleCount) +
it.edge.trim(settings.edgeCount) +
it.mixed.trim(settings.mixedCount)
}.trim(settings.fixedCount)
}.filter { !it.filterNotNull() }.trim(settings.fixedCount)
}
}

private fun Parameters.filterNotNull() = solution.filterIndexed { index, any ->
notNullParameters[index] && any == null
}.isNotEmpty()

private val randomPair =
RandomGroup(random.nextLong())
private var index = 0
Expand All @@ -418,7 +438,13 @@ class ConfiguredParametersGenerator(
1 -> overrideRandom.invoke(null, random)
2 -> overrideRandom.invoke(null, complexity.level, random)
else -> error("Bad argument count for @RandomParameters")
} as ParameterGroup
}.let {
if (it is ParameterGroup) {
it
} else {
One(it)
}
}

override fun random(complexity: Complexity, runner: TestRunner): Parameters = if (overrideRandom != null) {
check(randomPair.synced) { "Random pair was out of sync before parameter generation" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.5.4
version=2021.5.5
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ class TestJavaExamples : StringSpec(
examples.java.noreceiver.randomparameternocomplexity.Correct::class.java.also {
"${it.testName()}" { it.test() }
}
examples.java.noreceiver.notnullannotation.Correct::class.java.also {
"${it.testName()}" { it.test() }
}
examples.java.noreceiver.onewithoutwrapper.Correct::class.java.also {
"${it.testName()}" { it.test() }
}
examples.java.receiver.timeouttest.Correct::class.java.also {
"${it.testName()}" {
val runnable = object : Runnable {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package examples.java.noreceiver.notnullannotation;

import edu.illinois.cs.cs125.jenisol.core.NotNull;

public class Correct {
public static int it(@NotNull String input) {
if (input == null) {
return 0;
} else {
return input.length();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package examples.java.noreceiver.notnullannotation;

public class Correct0 {
public static int it(String input) {
return input.length();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package examples.java.noreceiver.notnullannotation;

public class Incorrect0 {
public static int it(String input) {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package examples.java.noreceiver.onewithoutwrapper;

import edu.illinois.cs.cs125.jenisol.core.FixedParameters;
import edu.illinois.cs.cs125.jenisol.core.RandomParameters;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Correct {
@FixedParameters private static final List<String> FIXED = Arrays.asList(null, "8888");

@RandomParameters
private static String valueParameters(int complexity, Random random) {
if (random.nextBoolean()) {
return null;
} else {
return "";
}
}

public static boolean value(String first) {
return first.equals("8888");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package examples.java.noreceiver.onewithoutwrapper;

@SuppressWarnings("unused")
public class Incorrect0 {
@SuppressWarnings("unused")
public static boolean value(String first) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import edu.illinois.cs.cs125.jenisol.core.FixedParameters;
import edu.illinois.cs.cs125.jenisol.core.One;
import edu.illinois.cs.cs125.jenisol.core.RandomParameters;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Correct {
@FixedParameters
private static final List<One<Long>> FIXED = Arrays.asList(
new One<>(8888L)
);
@FixedParameters private static final List<One<Long>> FIXED = Arrays.asList(new One<>(8888L));

@RandomParameters
private static One<Long> valueParameters(Random random) {
Expand Down

0 comments on commit c609dd8

Please sign in to comment.