Skip to content

Commit c402aff

Browse files
authored
Merge pull request #471 from FWDekker/files-and-seeds
Files and seeds
2 parents 285a161 + 317fff4 commit c402aff

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/main/kotlin/com/fwdekker/randomness/Settings.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.fwdekker.randomness
33
import com.fwdekker.randomness.template.Template
44
import com.fwdekker.randomness.template.TemplateList
55
import com.intellij.openapi.components.PersistentStateComponent
6+
import com.intellij.openapi.components.SettingsCategory
67
import com.intellij.openapi.components.Storage
78
import com.intellij.openapi.components.service
89
import com.intellij.util.xmlb.annotations.Transient
@@ -50,8 +51,12 @@ data class Settings(var templateList: TemplateList = TemplateList()) : State() {
5051
* The actual user's actual stored actually-serialized settings (actually).
5152
*/
5253
@JBState(
53-
name = "com.fwdekker.randomness.PersistentSettings",
54-
storages = [Storage("\$APP_CONFIG\$/randomness-beta.xml")],
54+
name = "Randomness",
55+
storages = [
56+
Storage("\$APP_CONFIG\$/randomness.xml", deprecated = true),
57+
Storage("\$APP_CONFIG\$/randomness-beta.xml", exportable = true),
58+
],
59+
category = SettingsCategory.PLUGINS,
5560
)
5661
class PersistentSettings : PersistentStateComponent<Settings> {
5762
private val settings = Settings()

src/main/kotlin/com/fwdekker/randomness/template/Template.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.fwdekker.randomness.uuid.UuidScheme
1515
import com.fwdekker.randomness.word.WordScheme
1616
import com.intellij.ui.Gray
1717
import com.intellij.util.xmlb.annotations.XCollection
18+
import kotlin.random.Random
1819

1920

2021
/**
@@ -76,9 +77,13 @@ data class Template(
7677
* The schemes are first all given a reference to the same [random] before each generating [count] random strings.
7778
* These results are then concatenated into the output.
7879
*/
79-
override fun generateUndecoratedStrings(count: Int) =
80-
schemes.onEach { it.random = random }.map { it.generateStrings(count) }
80+
override fun generateUndecoratedStrings(count: Int): List<String> {
81+
val seed = random.nextInt()
82+
83+
return schemes.onEach { it.random = Random(seed + it.uuid.hashCode()) }
84+
.map { it.generateStrings(count) }
8185
.let { data -> (0 until count).map { i -> data.joinToString("") { it[i] } } }
86+
}
8287

8388

8489
override fun doValidate() =

src/test/kotlin/com/fwdekker/randomness/template/TemplateTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package com.fwdekker.randomness.template
22

3+
import com.fwdekker.randomness.CapitalizationMode
34
import com.fwdekker.randomness.DataGenerationException
45
import com.fwdekker.randomness.Settings
56
import com.fwdekker.randomness.array.ArrayDecorator
67
import com.fwdekker.randomness.integer.IntegerScheme
78
import com.fwdekker.randomness.stateDeepCopyTestFactory
9+
import com.fwdekker.randomness.string.StringScheme
810
import com.fwdekker.randomness.testhelpers.DummyScheme
911
import com.fwdekker.randomness.testhelpers.shouldValidateAsBundle
12+
import com.fwdekker.randomness.word.WordScheme
1013
import io.kotest.assertions.throwables.shouldThrow
1114
import io.kotest.core.NamedTag
1215
import io.kotest.core.spec.style.FunSpec
1316
import io.kotest.data.row
1417
import io.kotest.datatest.withData
18+
import io.kotest.matchers.collections.shouldContainExactly
1519
import io.kotest.matchers.ints.shouldBeGreaterThan
1620
import io.kotest.matchers.shouldBe
1721
import io.kotest.matchers.types.shouldBeSameInstanceAs
22+
import kotlin.random.Random
1823

1924

2025
/**
@@ -99,6 +104,46 @@ object TemplateTest : FunSpec({
99104

100105
shouldThrow<DataGenerationException> { template.generateStrings() }
101106
}
107+
108+
context("rng") {
109+
test("returns the same given the same seed") {
110+
val template = Template(schemes = mutableListOf(StringScheme("[a-zA-Z]{10}"), IntegerScheme()))
111+
112+
val outputs1 = template.also { it.random = Random(1) }.generateStrings(5)
113+
val outputs2 = template.also { it.random = Random(1) }.generateStrings(5)
114+
115+
outputs1 shouldContainExactly outputs2
116+
}
117+
118+
test("sets the rng of a scheme independent of scheme order") {
119+
val schemeA = IntegerScheme()
120+
val glue = StringScheme("-")
121+
val schemeB = IntegerScheme()
122+
123+
val output1 = Template(schemes = mutableListOf(schemeA, glue, schemeB))
124+
.also { it.random = Random(1) }
125+
.generateStrings()[0]
126+
val output2 = Template(schemes = mutableListOf(schemeB, glue, schemeA))
127+
.also { it.random = Random(1) }
128+
.generateStrings()[0]
129+
130+
output1.split("-").reversed() shouldContainExactly output2.split("-")
131+
}
132+
133+
test("sets the rng independent of the rng of other schemes") {
134+
val prefix = WordScheme(words = listOf("pre"))
135+
val scheme = IntegerScheme()
136+
val postfix = WordScheme(words = listOf("post"))
137+
val template = Template(schemes = mutableListOf(prefix, scheme, postfix))
138+
139+
val output1 = template.also { it.random = Random(1) }.generateStrings()[0]
140+
prefix.capitalization = CapitalizationMode.RANDOM
141+
postfix.capitalization = CapitalizationMode.RANDOM
142+
val output2 = template.also { it.random = Random(1) }.generateStrings()[0]
143+
144+
output1.drop(3).dropLast(4) shouldBe output2.drop(3).dropLast(4)
145+
}
146+
}
102147
}
103148

104149
context("doValidate") {

0 commit comments

Comments
 (0)