Skip to content

Commit

Permalink
Merge pull request #471 from FWDekker/files-and-seeds
Browse files Browse the repository at this point in the history
Files and seeds
  • Loading branch information
FWDekker authored Sep 20, 2023
2 parents 285a161 + 317fff4 commit c402aff
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main/kotlin/com/fwdekker/randomness/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.fwdekker.randomness
import com.fwdekker.randomness.template.Template
import com.fwdekker.randomness.template.TemplateList
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.SettingsCategory
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.util.xmlb.annotations.Transient
Expand Down Expand Up @@ -50,8 +51,12 @@ data class Settings(var templateList: TemplateList = TemplateList()) : State() {
* The actual user's actual stored actually-serialized settings (actually).
*/
@JBState(
name = "com.fwdekker.randomness.PersistentSettings",
storages = [Storage("\$APP_CONFIG\$/randomness-beta.xml")],
name = "Randomness",
storages = [
Storage("\$APP_CONFIG\$/randomness.xml", deprecated = true),
Storage("\$APP_CONFIG\$/randomness-beta.xml", exportable = true),
],
category = SettingsCategory.PLUGINS,
)
class PersistentSettings : PersistentStateComponent<Settings> {
private val settings = Settings()
Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/com/fwdekker/randomness/template/Template.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.fwdekker.randomness.uuid.UuidScheme
import com.fwdekker.randomness.word.WordScheme
import com.intellij.ui.Gray
import com.intellij.util.xmlb.annotations.XCollection
import kotlin.random.Random


/**
Expand Down Expand Up @@ -76,9 +77,13 @@ data class Template(
* The schemes are first all given a reference to the same [random] before each generating [count] random strings.
* These results are then concatenated into the output.
*/
override fun generateUndecoratedStrings(count: Int) =
schemes.onEach { it.random = random }.map { it.generateStrings(count) }
override fun generateUndecoratedStrings(count: Int): List<String> {
val seed = random.nextInt()

return schemes.onEach { it.random = Random(seed + it.uuid.hashCode()) }
.map { it.generateStrings(count) }
.let { data -> (0 until count).map { i -> data.joinToString("") { it[i] } } }
}


override fun doValidate() =
Expand Down
45 changes: 45 additions & 0 deletions src/test/kotlin/com/fwdekker/randomness/template/TemplateTest.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.fwdekker.randomness.template

import com.fwdekker.randomness.CapitalizationMode
import com.fwdekker.randomness.DataGenerationException
import com.fwdekker.randomness.Settings
import com.fwdekker.randomness.array.ArrayDecorator
import com.fwdekker.randomness.integer.IntegerScheme
import com.fwdekker.randomness.stateDeepCopyTestFactory
import com.fwdekker.randomness.string.StringScheme
import com.fwdekker.randomness.testhelpers.DummyScheme
import com.fwdekker.randomness.testhelpers.shouldValidateAsBundle
import com.fwdekker.randomness.word.WordScheme
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.NamedTag
import io.kotest.core.spec.style.FunSpec
import io.kotest.data.row
import io.kotest.datatest.withData
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeSameInstanceAs
import kotlin.random.Random


/**
Expand Down Expand Up @@ -99,6 +104,46 @@ object TemplateTest : FunSpec({

shouldThrow<DataGenerationException> { template.generateStrings() }
}

context("rng") {
test("returns the same given the same seed") {
val template = Template(schemes = mutableListOf(StringScheme("[a-zA-Z]{10}"), IntegerScheme()))

val outputs1 = template.also { it.random = Random(1) }.generateStrings(5)
val outputs2 = template.also { it.random = Random(1) }.generateStrings(5)

outputs1 shouldContainExactly outputs2
}

test("sets the rng of a scheme independent of scheme order") {
val schemeA = IntegerScheme()
val glue = StringScheme("-")
val schemeB = IntegerScheme()

val output1 = Template(schemes = mutableListOf(schemeA, glue, schemeB))
.also { it.random = Random(1) }
.generateStrings()[0]
val output2 = Template(schemes = mutableListOf(schemeB, glue, schemeA))
.also { it.random = Random(1) }
.generateStrings()[0]

output1.split("-").reversed() shouldContainExactly output2.split("-")
}

test("sets the rng independent of the rng of other schemes") {
val prefix = WordScheme(words = listOf("pre"))
val scheme = IntegerScheme()
val postfix = WordScheme(words = listOf("post"))
val template = Template(schemes = mutableListOf(prefix, scheme, postfix))

val output1 = template.also { it.random = Random(1) }.generateStrings()[0]
prefix.capitalization = CapitalizationMode.RANDOM
postfix.capitalization = CapitalizationMode.RANDOM
val output2 = template.also { it.random = Random(1) }.generateStrings()[0]

output1.drop(3).dropLast(4) shouldBe output2.drop(3).dropLast(4)
}
}
}

context("doValidate") {
Expand Down

0 comments on commit c402aff

Please sign in to comment.