diff --git a/build.gradle.kts b/build.gradle.kts index 5310e95..5e22551 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,11 +7,11 @@ group = "com.github.cs125-illinois" version = "2021.9.1" plugins { - kotlin("jvm") version "1.5.30" + kotlin("jvm") version "1.5.31" java `maven-publish` - id("org.jmailen.kotlinter") version "3.5.1" + id("org.jmailen.kotlinter") version "3.6.0" checkstyle id("com.github.sherter.google-java-format") version "0.9" @@ -24,11 +24,11 @@ repositories { maven("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven") } dependencies { - implementation("org.jetbrains.kotlin:kotlin-reflect:1.5.30") - implementation("io.github.classgraph:classgraph:4.8.115") + implementation("org.jetbrains.kotlin:kotlin-reflect:1.5.31") + implementation("io.github.classgraph:classgraph:4.8.116") implementation("io.github.kostaskougios:cloning:1.10.3") - testImplementation("io.kotest:kotest-runner-junit5:4.6.2") + testImplementation("io.kotest:kotest-runner-junit5:4.6.3") testImplementation("org.slf4j:slf4j-simple:1.7.32") } tasks.withType { diff --git a/src/main/kotlin/generators/Type.kt b/src/main/kotlin/generators/Type.kt index 878ff19..080020c 100644 --- a/src/main/kotlin/generators/Type.kt +++ b/src/main/kotlin/generators/Type.kt @@ -222,6 +222,12 @@ object Defaults { map.containsKey(type.actualTypeArguments[0]) ) { return { random -> ListGenerator(random, create(type.actualTypeArguments[0], random)) } + } else if ( + type.rawType == java.util.Set::class.java && + type.actualTypeArguments.size == 1 && + map.containsKey(type.actualTypeArguments[0]) + ) { + return { random -> SetGenerator(random, create(type.actualTypeArguments[0], random)) } } else if (type.rawType == java.util.Map::class.java && type.actualTypeArguments.size == 2 && map.containsKey(type.actualTypeArguments[0]) && map.containsKey(type.actualTypeArguments[1]) ) { @@ -265,6 +271,30 @@ class ListGenerator(random: Random, private val componentGenerator: TypeGenerato } } +class SetGenerator(random: Random, private val componentGenerator: TypeGenerator<*>) : + TypeGenerators(random) { + + override val simple: Set> + get() { + val simpleCases = componentGenerator.simple.mapNotNull { it.solutionCopy }.toSet() + return setOf( + setOf(), + simpleCases + ).values(ZeroComplexity) + } + + override val edge: Set> = setOf(null).values(ZeroComplexity) + + override fun random(complexity: Complexity, runner: TestRunner?): Value { + val setSize = random.nextInt(complexity.squared().coerceAtLeast(2).toInt()) + return mutableSetOf().apply { + repeat(setSize) { + add(componentGenerator.random(complexity, runner).solutionCopy!!) + } + }.value(complexity) + } +} + class MapGenerator( random: Random, private val keyGenerator: TypeGenerator<*>, diff --git a/src/main/resources/edu.illinois.cs.cs125.jenisol.core.version b/src/main/resources/edu.illinois.cs.cs125.jenisol.core.version index 7e6c050..8b454c4 100644 --- a/src/main/resources/edu.illinois.cs.cs125.jenisol.core.version +++ b/src/main/resources/edu.illinois.cs.cs125.jenisol.core.version @@ -1 +1 @@ -version=2021.9.0 \ No newline at end of file +version=2021.9.1 \ No newline at end of file diff --git a/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestGenerators.kt b/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestGenerators.kt index 52fbf8a..b1fc6bf 100644 --- a/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestGenerators.kt +++ b/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestGenerators.kt @@ -167,6 +167,14 @@ class TestGenerators : StringSpec( method.testGenerator() } } + "it should generate sets properly" { + methodNamed("testIntegerSet").also { method -> + method.invoke(null, null) + method.invoke(null, setOf()) + method.invoke(null, setOf(1, 2, 5)) + method.testGenerator() + } + } "it should generate nested arrays properly" { Defaults.create(Array>::class.java).also { generator -> (0..128).map { diff --git a/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestJavaExamples.kt b/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestJavaExamples.kt index b7eff27..db84cea 100644 --- a/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestJavaExamples.kt +++ b/src/test/java/edu/illinois/cs/cs125/jenisol/core/TestJavaExamples.kt @@ -298,7 +298,7 @@ class TestJavaExamples : StringSpec( examples.java.receiver.rejectstaticfield.Correct::class.java.also { "${it.testName()}" { it.test() } } - examples.java.receiver.withshouldcontinue.Correct::class.java.also { + examples.java.noreceiver.setsum.Correct::class.java.also { "${it.testName()}" { it.test() } } examples.java.receiver.timeouttest.Correct::class.java.also { diff --git a/src/test/java/examples/generatortesting/TestGenerators.java b/src/test/java/examples/generatortesting/TestGenerators.java index f3bf349..7e98b27 100644 --- a/src/test/java/examples/generatortesting/TestGenerators.java +++ b/src/test/java/examples/generatortesting/TestGenerators.java @@ -1,6 +1,7 @@ package examples.generatortesting; import java.util.List; +import java.util.Set; @SuppressWarnings("unused") public class TestGenerators { @@ -44,6 +45,8 @@ public static void testIntArray(int[] values) {} public static void testIntegerList(List values) {} + public static void testIntegerSet(Set values) {} + public static void testChar(char value) {} public static void testBoxedChar(Character value) {} diff --git a/src/test/java/examples/java/noreceiver/setsum/Correct.java b/src/test/java/examples/java/noreceiver/setsum/Correct.java new file mode 100644 index 0000000..228a237 --- /dev/null +++ b/src/test/java/examples/java/noreceiver/setsum/Correct.java @@ -0,0 +1,16 @@ +package examples.java.noreceiver.setsum; + +import java.util.Set; + +public class Correct { + public static int sum(Set values) { + if (values == null) { + return 0; + } + int sum = 0; + for (int value : values) { + sum += value; + } + return sum; + } +} diff --git a/src/test/java/examples/java/noreceiver/setsum/Incorrect0.java b/src/test/java/examples/java/noreceiver/setsum/Incorrect0.java new file mode 100644 index 0000000..3dfe12b --- /dev/null +++ b/src/test/java/examples/java/noreceiver/setsum/Incorrect0.java @@ -0,0 +1,16 @@ +package examples.java.noreceiver.setsum; + +import java.util.Set; + +public class Incorrect0 { + public static int sum(Set values) { + if (values == null) { + return 0; + } + int sum = 0; + for (int value : values) { + sum += value; + } + return sum - 1; + } +}