Skip to content

Commit

Permalink
Add set generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Sep 19, 2021
1 parent 719aedc commit 3457820
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 7 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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<JavaCompile> {
Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/generators/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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])
) {
Expand Down Expand Up @@ -265,6 +271,30 @@ class ListGenerator(random: Random, private val componentGenerator: TypeGenerato
}
}

class SetGenerator(random: Random, private val componentGenerator: TypeGenerator<*>) :
TypeGenerators<Any>(random) {

override val simple: Set<Value<Any>>
get() {
val simpleCases = componentGenerator.simple.mapNotNull { it.solutionCopy }.toSet()
return setOf(
setOf(),
simpleCases
).values(ZeroComplexity)
}

override val edge: Set<Value<Any?>> = setOf<Any?>(null).values(ZeroComplexity)

override fun random(complexity: Complexity, runner: TestRunner?): Value<Any> {
val setSize = random.nextInt(complexity.squared().coerceAtLeast(2).toInt())
return mutableSetOf<Any>().apply {
repeat(setSize) {
add(componentGenerator.random(complexity, runner).solutionCopy!!)
}
}.value(complexity)
}
}

class MapGenerator(
random: Random,
private val keyGenerator: TypeGenerator<*>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.9.0
version=2021.9.1
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>())
method.invoke(null, setOf(1, 2, 5))
method.testGenerator()
}
}
"it should generate nested arrays properly" {
Defaults.create(Array<Array<IntArray>>::class.java).also { generator ->
(0..128).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/examples/generatortesting/TestGenerators.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples.generatortesting;

import java.util.List;
import java.util.Set;

@SuppressWarnings("unused")
public class TestGenerators {
Expand Down Expand Up @@ -44,6 +45,8 @@ public static void testIntArray(int[] values) {}

public static void testIntegerList(List<Integer> values) {}

public static void testIntegerSet(Set<Integer> values) {}

public static void testChar(char value) {}

public static void testBoxedChar(Character value) {}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/examples/java/noreceiver/setsum/Correct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package examples.java.noreceiver.setsum;

import java.util.Set;

public class Correct {
public static int sum(Set<Integer> values) {
if (values == null) {
return 0;
}
int sum = 0;
for (int value : values) {
sum += value;
}
return sum;
}
}
16 changes: 16 additions & 0 deletions src/test/java/examples/java/noreceiver/setsum/Incorrect0.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package examples.java.noreceiver.setsum;

import java.util.Set;

public class Incorrect0 {
public static int sum(Set<Integer> values) {
if (values == null) {
return 0;
}
int sum = 0;
for (int value : values) {
sum += value;
}
return sum - 1;
}
}

0 comments on commit 3457820

Please sign in to comment.