Best Kotest code snippet using io.kotest.property.arbitrary.floats.shrink
Arb.kt
Source:Arb.kt
...41}42@PublishedApi43internal fun <A, B> arrayChunkGenerator(44 arb: Arb<A>,45 shrinker: Shrinker<B>,46 range: IntRange = 0..10,47 build: (values: List<A>, offset: Int, length: Int) -> B48): Arb<B> {49 check(!range.isEmpty())50 check(range.first >= 0)51 val edgecases =52 arb.edgecases().map { a -> build(listOf(a), 0, 1) } + build(emptyList(), 0, 0)53 return arb(edgecases, shrinker) {54 val genIter = arb.generate(it).iterator()55 sequence {56 while (true) {57 val targetSize = it.random.nextInt(range)58 val list = ArrayList<A>(targetSize)59 while (list.size < targetSize && genIter.hasNext()) {60 list.add(genIter.next().value)61 }62 val offset = (0..list.size).random(it.random)63 val length = (0..(list.size - offset)).random(it.random)64 yield(build(list, offset, length))65 }66 }67 }68}69class ChunkShrinker<A> : Shrinker<Chunk<A>> {70 override fun shrink(value: Chunk<A>): List<Chunk<A>> =71 if (value.isEmpty()) emptyList()72 else listOf(73 Chunk.empty(),74 value.takeLast(1),75 value.take(value.size() / 3),76 value.take(value.size() / 2),77 value.take(value.size() * 2 / 3),78 value.dropLast(1)79 )80}81inline fun <reified A> Arb.Companion.chunk(arb: Arb<A>): Arb<Chunk<A>> =82 object : Arb<Chunk<A>>() {83 override fun edgecases(): List<Chunk<A>> =84 listOf(Chunk.empty<A>()) + arb.edgecases().map { Chunk(it) }...
floats.kt
Source:floats.kt
...5import kotlin.math.absoluteValue6private val numericEdgeCases = listOf(-1.0F, -Float.MIN_VALUE, -0.0F, 0.0F, Float.MIN_VALUE, 1.0F)7private val nonFiniteEdgeCases = listOf(Float.NEGATIVE_INFINITY, Float.NaN, Float.POSITIVE_INFINITY)8object FloatShrinker : Shrinker<Float> {9 override fun shrink(value: Float): List<Float> =10 if (value == 0f || !value.isFinite() || value.absoluteValue < 10 * Float.MIN_VALUE)11 emptyList()12 else13 listOfNotNull(DoubleShrinker.shrink(value.toString())?.toFloat())14}15/**16 * Returns an [Arb] that produces [Float]s from [min] to [max] (inclusive).17 * The edge cases are [Float.NEGATIVE_INFINITY], [min], -1.0, -[Float.MIN_VALUE], -0.0, 0.0, [Float.MIN_VALUE], 1.0,18 * [max], [Float.POSITIVE_INFINITY] and [Float.NaN] which are only included if they are in the provided range.19 *20 * @see numericFloat to only produce numeric [Float]s21 */22fun Arb.Companion.float(min: Float = -Float.MAX_VALUE, max: Float = Float.MAX_VALUE): Arb<Float> = float(min..max)23/**24 * Returns an [Arb] that produces [Float]s in [range].25 * The edge cases are [Float.NEGATIVE_INFINITY], [ClosedFloatingPointRange.start], -1.0, -[Float.MIN_VALUE], -0.0,26 * 0.0, [Float.MIN_VALUE], 1.0, [ClosedFloatingPointRange.endInclusive], [Float.POSITIVE_INFINITY] and [Float.NaN] which27 * are only included if they are in the provided range....
shrink
Using AI Code Generation
1) : Arbitrary<Float> {2 override fun edgecases(): List<Float> = emptyList()3 override fun sample(rs: RandomSource): Sample<Float> {4 val value = rs.random.nextFloat(range.start, range.endInclusive)5 return Sample(value, shrinker.shrink(value))6 }7}8) : Arbitrary<Int> {9 override fun edgecases(): List<Int> = emptyList()10 override fun sample(rs: RandomSource): Sample<Int> {11 val value = rs.random.nextInt(range)12 return Sample(value, shrinker.shrink(value))13 }14}15) : Arbitrary<Long> {16 override fun edgecases(): List<Long> = emptyList()17 override fun sample(rs: RandomSource): Sample<Long> {18 val value = rs.random.nextLong(range)19 return Sample(value, shrinker.shrink(value))20 }21}22) : Arbitrary<Short> {23 override fun edgecases(): List<Short> = emptyList()24 override fun sample(rs: RandomSource): Sample<Short> {25 val value = rs.random.nextInt(range).toShort()26 return Sample(value, shrinker.shrink(value))27 }28}29) : Arbitrary<String> {30 override fun edgecases(): List<String> = emptyList()31 override fun sample(rs: RandomSource): Sample<String> {32 val value = rs.random.nextString(range, charRange, allowedChars)33 return Sample(value, shrinker.shrink(value))34 }35}
shrink
Using AI Code Generation
1 val shrinker = Floats.shrinker(0.0f, 100.0f)2 val shrinker = Ints.shrinker(0, 100)3class Foo(val i: Int) {4 fun bar() = i + 15 }6 class FooTest : WordSpec() {7 init {8 "Foo.bar" should {9 "always return 1 more than the input" {10 forAll<Int> { i ->11 Foo(i).bar() == i + 112 }13 }14 }15 }16 }
shrink
Using AI Code Generation
1val floatArb = Arb.floats(0.0f, 1.0f)2val shrinkable = floatArb.shrink(0.5f)3shrinkable.shrinks().first().value shouldBe 0.25f4shrinkable.shrinks().first().shrinks().first().value shouldBe 0.125f5shrinkable.shrinks().first().shrinks().first().shrinks().first().value shouldBe 0.0625f6shrinkable.shrinks().first().shrinks().first().shrinks().first().shrinks().first().value shouldBe 0.03125f7shrinkable.shrinks().first().shrinks().first().shrinks().first().shrinks().first().shrinks().first().value shouldBe 0.015625f8shrinkable.shrinks().first().shrinks().first().shrinks().first().shrinks().first().shrinks().first().shrinks().first().value shouldBe 0.0078125f9shrinkable.shrinks().first().shrinks().first().shrinks
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!