Best Kotest code snippet using io.kotest.property.arbitrary.doubles.Arb.Companion.numericDouble
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)...
Arb.Companion.numericDouble
Using AI Code Generation
1val arbDouble = Arb . numericDouble ( 0.0 , 1.0 )2val arbFloat = Arb . numericFloat ( 0.0f , 1.0f )3val arbInt = Arb . numericInt ( 0 , 1 )4val arbLong = Arb . numericLong ( 0L , 1L )5val arbShort = Arb . numericShort ( 0 , 1 )6val arbString = Arb . string ( 1 , 10 , Charsets . UTF_8 )7val arbString = Arb . string ( 1 , 10 )8val arbString = Arb . string ( 1 , 10 , Charsets . UTF_8 )9val arbString = Arb . string ( 1 , 10 )10val arbString = Arb . string ( 1 , 10 , Charsets . UTF_8 )11val arbString = Arb . string ( 1 , 10 )12val arbString = Arb . string ( 1 , 10 , Charsets . UTF_8 )13val arbString = Arb . string ( 1 , 10 )14val arbString = Arb . string ( 1 ,
Arb.Companion.numericDouble
Using AI Code Generation
1 Arb.numericDouble(2 Arb.numericFloat(3 Arb.numericInt(4 Arb.numericLong(5 Arb.numericShort(6 Arb.numericByte(7 Arb.numericBigInteger(8 range = BigInteger.valueOf(-100)..BigInteger.valueOf(100)9 Arb.numericBigDecimal(10 range = BigDecimal.valueOf(-100)..BigDecimal.valueOf(100),11 Arb.numericBigInteger(12 range = BigInteger.valueOf(-100)..BigInteger.valueOf(100)13 Arb.numericBigInteger(14 range = BigInteger.valueOf(-100)..BigInteger.valueOf(100)15 Arb.numericBigInteger(16 range = BigInteger.valueOf(-100)..BigInteger.valueOf(100)
Arb.Companion.numericDouble
Using AI Code Generation
1val arb = Arb.numericDouble()2val property = forAll(arb) { a ->3Math.exp(a) >= 04}5property.check()6}7}8Data Type Method String Arb.string() Int Arb.int() Long Arb.long() Double Arb.double() Float Arb.float() Boolean Arb.bool() Byte Arb.byte() Char Arb.char() Short Arb.short() BigInteger Arb.bigInt() BigDecimal Arb.bigDec() Enum Arb.enum() List Arb.list() Set Arb.set() Array Arb.array() Map Arb.map() Tuple Arb.tuple() String Arb.string() Int Arb.int() Long Arb.long() Double Arb.double() Float Arb.float() Boolean Arb.bool() Byte Arb.byte() Char Arb.char() Short Arb.short() BigInteger Arb.bigInt() BigDecimal Arb.bigDec() Enum Arb.enum() List Arb.list() Set Arb.set() Array Arb.array() Map Arb.map() Tuple Arb.tuple()9import io.kotest.core.spec.style.StringSpec10import io.kotest.property.Arb11import io.kotest.property.arbitrary.int12import io.kotest.property.forAll13class PropertyExample : StringSpec({14"property test" {15val arb = Arb.int()16val property = forAll(arb) { a ->17}18property.check()19}20})
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!!