-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9eeff2
commit d7bed78
Showing
6 changed files
with
207 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package net.benwoodworth.knbt.test.generators | ||
|
||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.list | ||
import io.kotest.property.arbitrary.map | ||
import net.benwoodworth.knbt.NbtByteArray | ||
import net.benwoodworth.knbt.NbtIntArray | ||
import net.benwoodworth.knbt.NbtLongArray | ||
|
||
fun Arb.Companion.nbtByteArray(): Arb<NbtByteArray> = Arb | ||
.list(Arb.nbtByte()) | ||
.map { list -> | ||
NbtByteArray(list.map { it.value }) | ||
} | ||
|
||
fun Arb.Companion.nbtIntArray(): Arb<NbtIntArray> = Arb | ||
.list(Arb.nbtInt()) | ||
.map { list -> | ||
NbtIntArray(list.map { it.value }) | ||
} | ||
|
||
fun Arb.Companion.nbtLongArray(): Arb<NbtLongArray> = Arb | ||
.list(Arb.nbtLong()) | ||
.map { list -> | ||
NbtLongArray(list.map { it.value }) | ||
} |
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,18 @@ | ||
package net.benwoodworth.knbt.test.generators | ||
|
||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.map | ||
import io.kotest.property.arbitrary.of | ||
import net.benwoodworth.knbt.NbtCompound | ||
|
||
fun Arb.Companion.nbtCompound(maxNesting: Int = 2): Arb<NbtCompound> = | ||
if (maxNesting <= 0) { | ||
Arb.of(NbtCompound(emptyMap())) | ||
} else { | ||
Arb | ||
.map( | ||
Arb.nbtString().map { it.value }, | ||
Arb.nbtTag(maxNesting - 1) | ||
) | ||
.map { NbtCompound(it) } | ||
} |
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,78 @@ | ||
package net.benwoodworth.knbt.test.generators | ||
|
||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.choice | ||
import io.kotest.property.arbitrary.list | ||
import io.kotest.property.arbitrary.map | ||
import io.kotest.property.arbitrary.of | ||
import net.benwoodworth.knbt.* | ||
|
||
@OptIn(UnsafeNbtApi::class) | ||
private fun <T : NbtTag> Arb.Companion.nbtList(e: Arb<T>): Arb<NbtList<T>> = Arb | ||
.list(e) | ||
.map { NbtList(it) } | ||
|
||
|
||
fun Arb.Companion.nbtList(maxNesting: Int = 2): Arb<NbtList<*>> = | ||
if (maxNesting <= 0) { | ||
Arb.nbtListOfNothing() | ||
} else { | ||
Arb.choice( | ||
Arb.nbtListOfNothing(), | ||
Arb.nbtListOfBytes(), | ||
Arb.nbtListOfShorts(), | ||
Arb.nbtListOfInts(), | ||
Arb.nbtListOfLongs(), | ||
Arb.nbtListOfFloats(), | ||
Arb.nbtListOfDoubles(), | ||
Arb.nbtListOfStrings(), | ||
Arb.nbtListOfByteArrays(), | ||
Arb.nbtListOfIntArrays(), | ||
Arb.nbtListOfLongArrays(), | ||
Arb.nbtListOfLists(maxNesting), | ||
Arb.nbtListOfCompounds(maxNesting), | ||
) | ||
} | ||
|
||
|
||
fun Arb.Companion.nbtListOfNothing(): Arb<NbtList<Nothing>> = | ||
Arb.of(NbtList(emptyList())) | ||
|
||
fun Arb.Companion.nbtListOfBytes(): Arb<NbtList<NbtByte>> = | ||
Arb.nbtList(Arb.nbtByte()) | ||
|
||
fun Arb.Companion.nbtListOfShorts(): Arb<NbtList<NbtShort>> = | ||
Arb.nbtList(Arb.nbtShort()) | ||
|
||
fun Arb.Companion.nbtListOfInts(): Arb<NbtList<NbtInt>> = | ||
Arb.nbtList(Arb.nbtInt()) | ||
|
||
fun Arb.Companion.nbtListOfLongs(): Arb<NbtList<NbtLong>> = | ||
Arb.nbtList(Arb.nbtLong()) | ||
|
||
fun Arb.Companion.nbtListOfFloats(): Arb<NbtList<NbtFloat>> = | ||
Arb.nbtList(Arb.nbtFloat()) | ||
|
||
fun Arb.Companion.nbtListOfDoubles(): Arb<NbtList<NbtDouble>> = | ||
Arb.nbtList(Arb.nbtDouble()) | ||
|
||
fun Arb.Companion.nbtListOfStrings(): Arb<NbtList<NbtString>> = | ||
Arb.nbtList(Arb.nbtString()) | ||
|
||
|
||
fun Arb.Companion.nbtListOfByteArrays(): Arb<NbtList<NbtByteArray>> = | ||
Arb.nbtList(Arb.nbtByteArray()) | ||
|
||
fun Arb.Companion.nbtListOfIntArrays(): Arb<NbtList<NbtIntArray>> = | ||
Arb.nbtList(Arb.nbtIntArray()) | ||
|
||
fun Arb.Companion.nbtListOfLongArrays(): Arb<NbtList<NbtLongArray>> = | ||
Arb.nbtList(Arb.nbtLongArray()) | ||
|
||
|
||
fun Arb.Companion.nbtListOfLists(maxNesting: Int = 2): Arb<NbtList<NbtList<*>>> = | ||
Arb.nbtList(Arb.nbtList(maxNesting - 1)) | ||
|
||
|
||
private fun Arb.Companion.nbtListOfCompounds(maxNesting: Int = 2): Arb<NbtList<NbtCompound>> = | ||
Arb.nbtList(Arb.nbtCompound(maxNesting - 1)) |
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,51 @@ | ||
package net.benwoodworth.knbt.test.generators | ||
|
||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.* | ||
import net.benwoodworth.knbt.* | ||
|
||
fun Arb.Companion.nbtByte(): Arb<NbtByte> = | ||
byte().map(::NbtByte) | ||
|
||
fun Arb.Companion.nbtShort(): Arb<NbtShort> = | ||
short().map(::NbtShort) | ||
|
||
fun Arb.Companion.nbtInt(): Arb<NbtInt> = | ||
int().map(::NbtInt) | ||
|
||
fun Arb.Companion.nbtLong(): Arb<NbtLong> = | ||
long().map(::NbtLong) | ||
|
||
|
||
private val floatNaNs = Arb | ||
.choice( | ||
Arb.uInt(0x7F80_0001u..0x7FBF_FFFFu), // Signaling NaNs | ||
Arb.uInt(0xFF80_0001u..0xFFBF_FFFFu), // Signaling NaNs | ||
Arb.uInt(0x7FC0_0000u..0x7FFF_FFFFu), // Quiet NaNs | ||
Arb.uInt(0xFFC0_0000u..0xFFFF_FFFFu), // Quiet NaNs | ||
) | ||
.map { Float.fromBits(it.toInt()) } | ||
|
||
fun Arb.Companion.nbtFloat(): Arb<NbtFloat> = Arb | ||
.choose( | ||
9 to float(), | ||
1 to floatNaNs | ||
) | ||
.map { NbtFloat(it) } | ||
|
||
|
||
private val doubleNaNs = Arb | ||
.choice( | ||
Arb.uLong(0x7FF0_0000_0000_0001uL..0x7FF7_FFFF_FFFF_FFFFuL), // Signaling NaNs | ||
Arb.uLong(0xFFF0_0000_0000_0001uL..0xFFF7_FFFF_FFFF_FFFFuL), // Signaling NaNs | ||
Arb.uLong(0x7FF8_0000_0000_0000uL..0x7FFF_FFFF_FFFF_FFFFuL), // Quiet NaNs | ||
Arb.uLong(0xFFF8_0000_0000_0000uL..0xFFFF_FFFF_FFFF_FFFFuL), // Quiet NaNs | ||
) | ||
.map { Double.fromBits(it.toLong()) } | ||
|
||
fun Arb.Companion.nbtDouble(): Arb<NbtDouble> = Arb | ||
.choose( | ||
9 to double(), | ||
1 to doubleNaNs | ||
) | ||
.map { NbtDouble(it) } |
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,14 @@ | ||
package net.benwoodworth.knbt.test.generators | ||
|
||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.* | ||
import net.benwoodworth.knbt.NbtString | ||
|
||
private val specialCodepoints = Arb.choose( | ||
10 to Codepoint.ascii(), | ||
1 to Arb.codepoints(), | ||
) | ||
|
||
fun Arb.Companion.nbtString(): Arb<NbtString> = Arb | ||
.string(0..20, specialCodepoints) | ||
.map { NbtString(it) } |
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,20 @@ | ||
package net.benwoodworth.knbt.test.generators | ||
|
||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.choice | ||
import net.benwoodworth.knbt.NbtTag | ||
|
||
fun Arb.Companion.nbtTag(maxNesting: Int = 2): Arb<NbtTag> = Arb | ||
.choice( | ||
Arb.nbtByte(), | ||
Arb.nbtShort(), | ||
Arb.nbtInt(), | ||
Arb.nbtLong(), | ||
Arb.nbtFloat(), | ||
Arb.nbtDouble(), | ||
Arb.nbtList(maxNesting), | ||
Arb.nbtCompound(maxNesting), | ||
Arb.nbtByteArray(), | ||
Arb.nbtIntArray(), | ||
Arb.nbtLongArray(), | ||
) |