Best Kotest code snippet using io.kotest.property.arbitrary.doubles.Arb.Companion.positiveDoubles
doubles.kt
Source:doubles.kt
1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import io.kotest.property.Gen4import io.kotest.property.Shrinker5import kotlin.math.absoluteValue6private val numericEdgeCases = listOf(-1.0, -Double.MIN_VALUE, -0.0, 0.0, Double.MIN_VALUE, 1.0)7private val nonFiniteEdgeCases = listOf(Double.NEGATIVE_INFINITY, Double.NaN, Double.POSITIVE_INFINITY)8object DoubleShrinker : Shrinker<Double> {9 private val pattern = Regex("""([+-]|)([0-9]*)(\.[0-9]*|)(e[+-]?[0-9]+|)""", RegexOption.IGNORE_CASE)10 override fun shrink(value: Double): List<Double> =11 if (value == 0.0 || !value.isFinite() || value.absoluteValue < 10 * Double.MIN_VALUE)12 emptyList()13 else14 listOfNotNull(shrink(value.toString())?.toDouble())15 fun shrink(value: String): String? {16 val matches = pattern.matchEntire(value) ?: return null17 val parts = matches.groupValues.drop(1)18 val (signPart, intPart, fracPart_, expPart) = parts19 val fracPart = fracPart_.trimEnd { it == '0' }20 val numberPart = if (fracPart.isNotEmpty() && fracPart.last().isDigit()) {21 "$intPart${fracPart.dropLast(1)}"22 } else {23 val length = intPart.length24 val index = intPart.indexOfLast { it != '0' }.let { if (it == -1) length else it }25 if (index == 0) {26 return null27 }28 val head = intPart.take(index)29 val tail = intPart.takeLast(length - index - 1)30 "${head}0$tail"31 }32 return "$signPart$numberPart$expPart"33 }34}35/**36 * Returns an [Arb] that produces [Double]s from [min] to [max] (inclusive).37 * The edge cases are [Double.NEGATIVE_INFINITY], [min], -1.0, -[Double.MIN_VALUE], -0.0, 0.0, [Double.MIN_VALUE], 1.0,38 * [max], [Double.POSITIVE_INFINITY] and [Double.NaN].39 *40 * Only values in the provided range are included.41 *42 * @see numericDouble to only produce numeric [Double]s43 */44fun Arb.Companion.double(45 min: Double = -Double.MAX_VALUE,46 max: Double = Double.MAX_VALUE47): Arb<Double> = double(min..max)48/**49 * Returns an [Arb] that produces [Double]s in [range].50 * The edge cases are [Double.NEGATIVE_INFINITY], [ClosedFloatingPointRange.start], -1.0, -[Double.MIN_VALUE], -0.0,51 * 0.0, [Double.MIN_VALUE], 1.0, [ClosedFloatingPointRange.endInclusive], [Double.POSITIVE_INFINITY] and [Double.NaN]52 * which are only included if they are in the provided range.53 */54fun Arb.Companion.double(55 range: ClosedFloatingPointRange<Double> = -Double.MAX_VALUE..Double.MAX_VALUE56): Arb<Double> = arbitrary(57 (numericEdgeCases.filter { it in range } + nonFiniteEdgeCases + listOf(range.start, range.endInclusive)).distinct(),58 DoubleShrinker59) {60 it.random.nextDouble(range.start, range.endInclusive)61}62/**63 * Returns an [Arb] that produces positive [Double]s from [Double.MIN_VALUE] to [max] (inclusive).64 * The edge cases are [Double.MIN_VALUE], 1.0, [max] and [Double.POSITIVE_INFINITY] which are only included if they are65 * in the provided range.66 */67fun Arb.Companion.positiveDouble(max: Double = Double.MAX_VALUE): Arb<Double> = double(Double.MIN_VALUE, max)68@Deprecated("use positiveDouble. Deprecated in 5.0.", ReplaceWith("positiveDouble()"))69fun Arb.Companion.positiveDoubles(): Arb<Double> = positiveDouble()70/**71 * Returns an [Arb] that produces negative [Double]s from [min] to -[Double.MIN_VALUE] (inclusive).72 * The edge cases are [Double.NEGATIVE_INFINITY], [min], -1.0 and -[Double.MIN_VALUE] which are only included if they73 * are in the provided range.74 */75fun Arb.Companion.negativeDouble(min: Double = -Double.MAX_VALUE): Arb<Double> = double(min, -Double.MIN_VALUE)76@Deprecated("use negativeDouble. Deprecated in 5.0.", ReplaceWith("negativeDouble()"))77fun Arb.Companion.negativeDoubles(): Arb<Double> = negativeDouble()78/**79 * Returns an [Arb] that produces numeric [Double]s from [min] to [max] (inclusive).80 * The edge cases are [min], -1.0, -[Double.MIN_VALUE], -0.0, 0.0, [Double.MIN_VALUE], 1.0 and [max] which are only81 * included if they are in the provided range.82 *83 * @see double to also have non-numeric [Double]s as edge cases.84 */85fun Arb.Companion.numericDouble(86 min: Double = -Double.MAX_VALUE,87 max: Double = Double.MAX_VALUE88): Arb<Double> = arbitrary(89 (numericEdgeCases.filter { it in (min..max) } + listOf(min, max)).distinct(), DoubleShrinker90) { it.random.nextDouble(min, max) }91@Deprecated("use numericDouble. Deprecated in 5.0.", ReplaceWith("numericDouble(from, to)"))92fun Arb.Companion.numericDoubles(from: Double = -Double.MAX_VALUE, to: Double = Double.MAX_VALUE): Arb<Double> =93 numericDouble(from, to)94/**95 * Returns an [Arb] that produces [DoubleArray]s where [length] produces the length of the arrays and96 * [content] produces the content of the arrays.97 */98fun Arb.Companion.doubleArray(length: Gen<Int>, content: Arb<Double>): Arb<DoubleArray> =99 toPrimitiveArray(length, content, Collection<Double>::toDoubleArray)...
Generators.kt
Source:Generators.kt
1import io.kotest.property.Arb2import io.kotest.property.Gen3import io.kotest.property.Shrinker4import io.kotest.property.arbitrary.*5import io.kotest.property.exhaustive.exhaustive6import org.lwjgl.glfw.GLFW7import kotlin.random.nextInt8fun Arb.Companion.primitiveModel(): Arb<Model> = Arb.bind(9 Arb.numericDoubles(-1.0, 1.0),10 Arb.numericDoubles(-1.0, 1.0),11 Arb.positiveDoubles(),12 Arb.positiveDoubles(),13 Arb.positiveDoubles(),14 Arb.quadtree(1),15) { offsetX, offsetY, zoom, windowWidth, windowHeight, leaf ->16 Model(17 offset = Vec2.screen(offsetX, offsetY),18 zoom = zoom,19 windowSize = Vec2.screen(windowWidth, windowHeight),20 palette = PaletteModel(1.0, 1.0),21 currentColour = Colour.white,22 world = leaf,23 )24}25fun Arb.Companion.fullModel(depth: Int): Arb<Model> = Arb.bind(26 Arb.primitiveModel(),27 Arb.quadtree(depth),28) { model, tree -> model.copy(world = tree) }29val zeroToOne = Arb.numericDoubles(0.0, 1.0)30fun Arb.Companion.colour(): Arb<Colour> = Arb.bind(31 zeroToOne, zeroToOne, zeroToOne, zeroToOne,32) { h, s, l, a -> Colour(h, s, l, a) }33fun Arb.Companion.quadtree(depth: Int): Arb<Quadtree> = arbitrary(TreeShrinker) { rs ->34 when (depth) {35 1 -> Leaf(Arb.colour().sample(rs).value)36 else -> {37 val deep = rs.random.nextInt(1..4)38 val children = Arb39 .quadtree(depth - 1)40 .many(4)41 .mapIndexed { i, deepArb ->42 val arb = if (i != deep && rs.random.nextDouble() < 0.5) Arb.quadtree(1)43 else deepArb44 arb.sample(rs).value45 }46 Node(children.toTypedArray())47 }48 }49}50object TreeShrinker : Shrinker<Quadtree> {51 override fun shrink(value: Quadtree): List<Quadtree> = when (value) {52 is Leaf -> listOf()53 is Node -> value.children.asList()54 }55}56fun GLFWAction.Companion.gen(): Gen<GLFWAction> = exhaustive(GLFWAction.values().asList())57fun CursorEvent.Companion.arb(): Arb<CursorEvent> = Arb.bind(58 Arb.positiveDoubles(), Arb.positiveDoubles(),59) { x, y -> CursorEvent(Vec2.screen(x, y)) }60fun KeyEvent.Companion.arb(): Arb<KeyEvent> = Arb.bind(61 Arb.int(0..1000), GLFWAction.gen()62) { key, action -> KeyEvent(key, GLFW.glfwGetKeyScancode(key), action, 0) }63fun MouseEvent.Companion.arb(): Arb<MouseEvent> = Arb.bind(64 Arb.int(0..5), GLFWAction.gen(),65) { button, action -> MouseEvent(button, action, 0) }66fun ScrollEvent.Companion.arb(): Arb<ScrollEvent> = Arb.bind(67 Arb.numericDoubles(-2.0, 2.0), Arb.numericDoubles(-2.0, 2.0),68) { vert, hor -> ScrollEvent(vert, hor) }69fun ResizeEvent.Companion.arb(): Arb<ResizeEvent> = Arb.bind(70 Arb.int(0..4000), Arb.int(0..4000),71) { w, h -> ResizeEvent(w, h) }72fun Arb.Companion.event(): Arb<Event> = Arb.choose(73 100 to CursorEvent.arb(),74 1 to KeyEvent.arb(),75 20 to MouseEvent.arb(),76 5 to ScrollEvent.arb(),77 1 to ResizeEvent.arb(),78)...
Arb.Companion.positiveDoubles
Using AI Code Generation
1 Arb.positiveDoubles(0.0, 1.0)2 Arb.negativeDoubles(0.0, 1.0)3 Arb.doubles(0.0, 1.0)4 Arb.doubles(0.0, 1.0)5 Arb.doubles(0.0, 1.0)6 Arb.doubles(0.0, 1.0)7 Arb.doubles(0.0, 1.0)8 Arb.doubles(0.0, 1.0)9 Arb.doubles(0.0, 1.0)10 Arb.doubles(0.0, 1.0)11 Arb.doubles(0.0, 1.0)12 Arb.doubles(0.0, 1.0)13 Arb.doubles(0.0, 1.0)14 Arb.doubles(0.0, 1
Arb.Companion.positiveDoubles
Using AI Code Generation
1 fun `test positive doubles`() {2 Arb.positiveDoubles().checkAll {3 it should beGreaterThan(0.0)4 }5 }6 fun `test positive floats`() {7 Arb.positiveFloats().checkAll {8 it should beGreaterThan(0.0f)9 }10 }11 fun `test positive ints`() {12 Arb.positiveInts().checkAll {13 it should beGreaterThan(0)14 }15 }16 fun `test positive longs`() {17 Arb.positiveLongs().checkAll {18 it should beGreaterThan(0L)19 }20 }21 fun `test positive shorts`() {22 Arb.positiveShorts().checkAll {23 it should beGreaterThan(0)24 }25 }26 fun `test positive uints`() {27 Arb.positiveUInts().checkAll {28 it should beGreaterThan(0u)29 }30 }31 fun `test positive ulongs`() {32 Arb.positiveULongs().checkAll {33 it should beGreaterThan(0uL)34 }35 }36 fun `test positive ushorts`() {37 Arb.positiveUShorts().checkAll {38 it should beGreaterThan(0u)39 }40 }41 fun `test positive values`() {
Arb.Companion.positiveDoubles
Using AI Code Generation
1 val arbPositiveDouble = Arb.positiveDoubles()2 forAll(arbPositiveDouble) { value ->3 }4 val arbPositiveInt = Arb.positiveInts()5 forAll(arbPositiveInt) { value ->6 }7 val arbPositiveLong = Arb.positiveLongs()8 forAll(arbPositiveLong) { value ->9 }10 val arbPositiveShort = Arb.positiveShorts()11 forAll(arbPositiveShort) { value ->12 }13 val arbPositiveFloat = Arb.positiveFloats()14 forAll(arbPositiveFloat) { value ->15 }16 val arbPositiveDouble = Arb.positiveDoubles()17 forAll(arbPositiveDouble) { value ->18 }19 val arbPositiveInt = Arb.positiveInts()20 forAll(arbPositiveInt) { value ->21 }22 val arbPositiveLong = Arb.positiveLongs()23 forAll(arbPositiveLong) { value ->24 }25 val arbPositiveShort = Arb.positiveShorts()26 forAll(arbPositiveShort) { value ->27 }
Arb.Companion.positiveDoubles
Using AI Code Generation
1val config = PropertyTestingConfig(iterations = 1000, threads = 1, seed = 1L, invocations = 1)2property("property to test") {3forAll(config, arb) { a ->4}5}6val config = PropertyTestingConfig(iterations = 1000, threads = 1, seed = 1L, invocations = 1)7property("property to test") {8forAll(config, arb) { a ->9}10}11val config = PropertyTestingConfig(iterations = 1000, threads = 1, seed = 1L, invocations = 1)12property("property to test") {13forAll(config, arb) { a ->14}15}16val config = PropertyTestingConfig(iterations = 1000, threads = 1, seed = 1L, invocations = 1)17property("property to test") {18forAll(config, arb) { a ->19}20}21val config = PropertyTestingConfig(iterations = 1000, threads = 1, seed = 1L, invocations = 1)22property("property to test") {23forAll(config, arb) { a
Arb.Companion.positiveDoubles
Using AI Code Generation
1 }2 fun `test with positive doubles`(){3 }4I have the following imports in the file:5 import io.kotest.core.spec.style.FunSpec6 import io.kotest.matchers.shouldBe7 import io.kotest.property.Arb8 import io.kotest.property.arbitrary.doubles9 import io.kotest.property.checkAll10 import io.kotest.property.arbitrary.positiveDoubles
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!!