This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add character generator (#215)
- Loading branch information
Showing
5 changed files
with
235 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rator/stdlib/src/commonMain/kotlin/com/github/jcornaz/kwik/generator/stdlib/Characters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.jcornaz.kwik.generator.stdlib | ||
|
||
import com.github.jcornaz.kwik.generator.api.Generator | ||
import com.github.jcornaz.kwik.generator.api.withSamples | ||
import kotlin.random.Random | ||
|
||
fun Generator.Companion.characters( | ||
charset: Set<Char> = CharSets.printable, | ||
exclude: Set<Char> = emptySet() | ||
): Generator<Char> { | ||
val characters = (charset - exclude).toList() | ||
require(characters.isNotEmpty()) | ||
|
||
val samples = mutableListOf<Char>() | ||
|
||
if (' ' in characters) | ||
samples += ' ' | ||
|
||
return Generator { rng: Random -> characters.random(rng) }.withSamples(samples) | ||
} | ||
|
||
/** | ||
* Common set of character to be used with generators | ||
*/ | ||
object CharSets { | ||
|
||
/** All printable characters */ | ||
@Suppress("MagicNumber") | ||
val printable: Set<Char> = (32..127).mapTo(HashSet()) { it.toChar() } | ||
|
||
/** Numeric characters (0-9) */ | ||
val numeric: Set<Char> = ('0'..'9').toHashSet() | ||
|
||
/** Lowercase alphabetic characters */ | ||
val alphaLowerCase: Set<Char> = ('a'..'z').toHashSet() | ||
|
||
/** Uppercase alphabetic characters */ | ||
val alphaUpperCase: Set<Char> = ('A'..'Z').toHashSet() | ||
|
||
/** Alphabetic characters (lower and upper cases)*/ | ||
val alpha: Set<Char> = alphaLowerCase + alphaUpperCase | ||
|
||
/** | ||
* Alphabetic and numeric characters | ||
* | ||
* Equivalent of [alpha] + [numeric] | ||
*/ | ||
val alphaNum: Set<Char> = alpha + numeric | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ator/stdlib/src/commonTest/kotlin/com/github/jcornaz/kwik/generator/stdlib/CharSetTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.jcornaz.kwik.generator.stdlib | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class CharSetTest { | ||
|
||
@Test | ||
fun provideNumericCharacters() { | ||
assertEquals("0123456789".toSet(), CharSets.numeric) | ||
} | ||
|
||
@Test | ||
fun provideLowerCaseAlphabeticCharacters() { | ||
assertEquals("abcdefghijklmnopqrstuvwxyz".toSet(), CharSets.alphaLowerCase) | ||
} | ||
|
||
@Test | ||
fun provideUpperCaseAlphabeticCharacters() { | ||
assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toSet(), CharSets.alphaUpperCase) | ||
} | ||
|
||
@Test | ||
fun alphaContainsAllLowerCaseCharacters() { | ||
CharSets.alphaLowerCase.forEach { | ||
assertTrue(it in CharSets.alpha) | ||
} | ||
} | ||
|
||
@Test | ||
fun alphaContainsAllUpperCaseCharacters() { | ||
CharSets.alphaUpperCase.forEach { | ||
assertTrue(it in CharSets.alpha) | ||
} | ||
} | ||
|
||
@Test | ||
fun alphaDoesNotContainsCharactersOtherThanLowerAndUpperCaseAlpha() { | ||
(Char.MIN_VALUE..Char.MAX_VALUE).asSequence() | ||
.filterNot { it in CharSets.alphaLowerCase } | ||
.filterNot { it in CharSets.alphaUpperCase } | ||
.forEach { | ||
assertFalse(it in CharSets.alpha ) | ||
} | ||
} | ||
|
||
@Test | ||
fun alphaNumContainsAllAlphaCharacters() { | ||
CharSets.alpha.forEach { | ||
assertTrue(it in CharSets.alphaNum) | ||
} | ||
} | ||
|
||
@Test | ||
fun alphaNumContainsAllNumericCharacters() { | ||
CharSets.numeric.forEach { | ||
assertTrue(it in CharSets.alphaNum) | ||
} | ||
} | ||
|
||
@Test | ||
fun alphaNulDoesNotContainsCharactersOtherThenAlphaAndNums() { | ||
(Char.MIN_VALUE..Char.MAX_VALUE).asSequence() | ||
.filterNot { it in CharSets.alpha } | ||
.filterNot { it in CharSets.numeric } | ||
.forEach { | ||
assertFalse(it in CharSets.alphaNum ) | ||
} | ||
} | ||
|
||
@Test | ||
fun printableCharacterIsSuperSetOfAlphaNum() { | ||
CharSets.alphaNum.forEach { | ||
assertTrue(it in CharSets.printable) | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
.../src/commonTest/kotlin/com/github/jcornaz/kwik/generator/stdlib/CharacterGeneratorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.jcornaz.kwik.generator.stdlib | ||
|
||
import com.github.jcornaz.kwik.generator.api.Generator | ||
import com.github.jcornaz.kwik.generator.api.randomSequence | ||
import com.github.jcornaz.kwik.generator.test.AbstractGeneratorTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertTrue | ||
|
||
class CharacterGeneratorTest : AbstractGeneratorTest() { | ||
|
||
override val generator: Generator<Char> = Generator.characters() | ||
|
||
@Test | ||
fun generateSpace() { | ||
assertTrue(generator.randomSequence(0).take(5).any { it == ' ' }) | ||
} | ||
|
||
@Test | ||
fun failIfEmptyCharSet() { | ||
assertFailsWith<IllegalArgumentException> { Generator.characters(charset = emptySet()) } | ||
} | ||
|
||
@Test | ||
fun failIfEntireCharsetExcluded() { | ||
assertFailsWith<IllegalArgumentException> { | ||
Generator.characters( | ||
charset = CharSets.printable, | ||
exclude = CharSets.printable | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun generateDifferentValues() { | ||
val values = mutableSetOf<Char>() | ||
|
||
Generator.characters(CharSets.alphaNum) | ||
.randomSequence(0) | ||
.take(200) | ||
.forEach { | ||
values += it | ||
} | ||
|
||
assertTrue { values.size > 10 } | ||
} | ||
|
||
@Test | ||
fun dontGenerateExcludedChars() { | ||
val values = Generator.characters(exclude = setOf('a', 'b', 'c')) | ||
.randomSequence(0) | ||
.take(100) | ||
|
||
assertTrue(values.none { it == 'a' || it == 'b' || it == 'c' }) | ||
} | ||
} |