Best Kotest code snippet using io.kotest.property.arbitrary.doubles.Arb.Companion.numericDoubles
FzyDoubleEqualityTest.kt
Source:FzyDoubleEqualityTest.kt
1package com.xrpn.order.fuzzy2import io.kotest.core.spec.style.FunSpec3import io.kotest.matchers.shouldBe4import io.kotest.property.Arb5import io.kotest.property.checkAll6import com.xrpn.order.fuzzy.FzyDouble.Companion.fzyEqual7import com.xrpn.order.fuzzy.FzyDouble.Companion.fzyIsZero8import com.xrpn.order.fuzzy.FzyDouble.Companion.asFzyDouble9import com.xrpn.order.fuzzy.FzyDouble.Companion.fzyIsUnity10import io.kotest.assertions.throwables.shouldThrow11import io.kotest.property.arbitrary.double12import io.kotest.property.arbitrary.numericDoubles13class FzyDoubleEqualityTest : FunSpec({14 beforeTest {15 }16 test("pathology") {17 Double.NaN.fzyEqual(1.0) shouldBe false18 Double.NaN.fzyEqual(Double.NaN) shouldBe false19 Double.NaN.asFzyDouble().isZero() shouldBe false20 Double.POSITIVE_INFINITY.fzyEqual(Double.POSITIVE_INFINITY) shouldBe true21 Double.NEGATIVE_INFINITY.fzyEqual(Double.POSITIVE_INFINITY) shouldBe false22 Double.POSITIVE_INFINITY.fzyEqual(Double.NEGATIVE_INFINITY) shouldBe false23 Double.NEGATIVE_INFINITY.fzyEqual(Double.NEGATIVE_INFINITY) shouldBe true24 FzyDouble(Double.NaN).equals(Double.NaN) shouldBe false25 (FzyDouble(Double.NaN) == FzyDouble(Double.NaN)) shouldBe false26 FzyDouble(Double.POSITIVE_INFINITY).equals(Double.POSITIVE_INFINITY) shouldBe true27 FzyDouble(Double.NEGATIVE_INFINITY).equals(Double.POSITIVE_INFINITY) shouldBe false28 FzyDouble(Double.POSITIVE_INFINITY).equals(Double.NEGATIVE_INFINITY) shouldBe false29 FzyDouble(Double.NEGATIVE_INFINITY).equals(Double.NEGATIVE_INFINITY) shouldBe true30 Double.MAX_VALUE.fzyEqual(Double.MAX_VALUE) shouldBe true31 }32 test("zeroes") {33 val fd0 = FzyDouble(0.0)34 val fd1 = FzyDouble(Double.MIN_VALUE)35 val fd2 = FzyDouble(-Double.MIN_VALUE)36 val fd3 = FzyDouble(1.0)37 fd0.isZero() shouldBe true38 fd1.isZero() shouldBe true39 fd2.isZero() shouldBe true40 fd3.isZero() shouldBe false41 Double.MIN_VALUE.fzyIsZero() shouldBe true42 0.0.fzyIsZero() shouldBe true43 (-Double.MIN_VALUE).fzyIsZero() shouldBe true44 1.0.fzyIsZero() shouldBe false45 Double.NaN.fzyIsZero() shouldBe false46 Double.POSITIVE_INFINITY.fzyIsZero() shouldBe false47 Double.NEGATIVE_INFINITY.fzyIsZero() shouldBe false48 }49 test("unity") {50 val fd0 = FzyDouble(1.0)51 val fd00 = FzyDouble(1.0, doubleEps, true)52 val fd1 = FzyDouble(1.0+defaultDoubleTol)53 val fd2 = FzyDouble(1.0-defaultDoubleTol)54 val fd3 = FzyDouble(1.0+defaultDoubleTol, 2.0*defaultDoubleTol)55 val fd4 = FzyDouble(1.0-defaultDoubleTol, 2.0*defaultDoubleTol)56 fd0.isUnity() shouldBe true57 fd00.isUnity() shouldBe true58 fd1.isUnity() shouldBe false59 fd2.isUnity() shouldBe false60 fd3.isUnity() shouldBe true61 fd4.isUnity() shouldBe true62 1.0.fzyIsUnity() shouldBe true63 (1.0+defaultDoubleTol).fzyIsUnity() shouldBe false64 (1.0-defaultDoubleTol).fzyIsUnity() shouldBe false65 Double.NaN.fzyIsUnity() shouldBe false66 Double.NEGATIVE_INFINITY.fzyIsUnity() shouldBe false67 Double.POSITIVE_INFINITY.fzyIsUnity() shouldBe false68 }69 test("throw if defeated"){70 shouldThrow<IllegalArgumentException> {71 FzyDouble(1.0, doubleEps)72 }73 shouldThrow<IllegalArgumentException> {74 FzyDouble(1.0, -0.0004)75 }76 }77 test("bounds") {78 (FzyDouble(Double.MAX_VALUE) == FzyDouble(Double.MAX_VALUE)) shouldBe true79 (FzyDouble(-Double.MAX_VALUE) == FzyDouble(-Double.MAX_VALUE)) shouldBe true80 FzyDouble(Double.MAX_VALUE).equals(Double.MAX_VALUE) shouldBe true81 FzyDouble(-Double.MAX_VALUE).equals(-Double.MAX_VALUE) shouldBe true82 (FzyDouble(Double.MIN_VALUE) == FzyDouble(Double.MIN_VALUE)) shouldBe true83 (FzyDouble(Double.MIN_VALUE) == FzyDouble(-Double.MIN_VALUE)) shouldBe true84 (FzyDouble(-Double.MIN_VALUE) == FzyDouble(-Double.MIN_VALUE)) shouldBe true85 FzyDouble(Double.MIN_VALUE).equals(Double.MIN_VALUE) shouldBe true86 FzyDouble(Double.MIN_VALUE).equals(-Double.MIN_VALUE) shouldBe true87 FzyDouble(-Double.MIN_VALUE).equals(-Double.MIN_VALUE) shouldBe true88 Double.MAX_VALUE.fzyEqual(Double.MAX_VALUE) shouldBe true89 (-Double.MAX_VALUE).fzyEqual(-Double.MAX_VALUE) shouldBe true90 Double.MAX_VALUE.fzyEqual(FzyDouble(Double.MAX_VALUE)) shouldBe true91 (-Double.MAX_VALUE).fzyEqual(FzyDouble(-Double.MAX_VALUE)) shouldBe true92 Double.MIN_VALUE.fzyEqual(Double.MIN_VALUE) shouldBe true93 Double.MIN_VALUE.fzyEqual(-Double.MIN_VALUE) shouldBe true94 (-Double.MIN_VALUE).fzyEqual(-Double.MIN_VALUE) shouldBe true95 Double.MIN_VALUE.fzyEqual(FzyDouble(Double.MIN_VALUE)) shouldBe true96 Double.MIN_VALUE.fzyEqual(FzyDouble(-Double.MIN_VALUE)) shouldBe true97 (-Double.MIN_VALUE).fzyEqual(FzyDouble(-Double.MIN_VALUE)) shouldBe true98 }99 test("underflow") {100 (FzyDouble(Double.MAX_VALUE) == FzyDouble(1E-20)) shouldBe false101 (FzyDouble(-Double.MAX_VALUE) == FzyDouble(-1E-20)) shouldBe false102 FzyDouble(Double.MAX_VALUE).equals(1E-20) shouldBe false103 FzyDouble(-Double.MAX_VALUE).equals(-1E-20) shouldBe false104 }105 test("overflow") {106 (FzyDouble(1E-20) == FzyDouble(Double.MAX_VALUE)) shouldBe false107 (FzyDouble(-1E-20) == FzyDouble(-Double.MAX_VALUE)) shouldBe false108 FzyDouble(1E-20).equals(Double.MAX_VALUE) shouldBe false109 FzyDouble(-1E-20).equals(-Double.MAX_VALUE) shouldBe false110 }111 test("same zeroes") {112 val fd0a = FzyDouble.zero()113 val fd0b = FzyDouble.zero()114 val fd00 = FzyDouble.zero(defaultDoubleTol*2.0)115 val fd1 = FzyDouble.unity()116 FzyDoubleEquality.isSameZeroes(fd0a, fd0a) shouldBe true117 FzyDoubleEquality.isSameZeroes(fd0a, fd0b) shouldBe true118 FzyDoubleEquality.isSameZeroes(fd0b, fd0a) shouldBe true119 FzyDoubleEquality.isSameZeroes(fd0a, fd00) shouldBe false120 FzyDoubleEquality.isSameZeroes(fd00, fd0a) shouldBe false121 FzyDoubleEquality.isSameZeroes(fd0a, fd1) shouldBe false122 FzyDoubleEquality.isSameZeroes(fd1, fd0a) shouldBe false123 }124 test("double properties, reflexive") {125 checkAll(Arb.double(), Arb.numericDoubles()) { f1, f2 ->126 val ff1a = FzyDouble(f1)127 val ff1b = FzyDouble(f1)128 val ff2a = FzyDouble(f2)129 val ff2b = FzyDouble(f2)130 ff1a.equals(f1) shouldBe !f1.isNaN()131 ff1b.equals(f1) shouldBe !f1.isNaN()132 ff2a.equals(f2) shouldBe true133 ff2b.equals(f2) shouldBe true134 f1.fzyEqual(f1) shouldBe !f1.isNaN()135 f1.fzyEqual(ff1a) shouldBe !f1.isNaN()136 f1.fzyEqual(ff1b) shouldBe !f1.isNaN()137 ff1a.equals(f1) shouldBe !f1.isNaN()138 ff1b.equals(f1) shouldBe !f1.isNaN()139 (ff1a == ff1b) shouldBe !f1.isNaN()140 (ff1b == ff1a) shouldBe !f1.isNaN()141 ff2a.equals(f2) shouldBe true142 ff2b.equals(f2) shouldBe true143 (ff2a == ff2b) shouldBe true144 (ff2b == ff2a) shouldBe true145 }146 }147 test("double properties, symmetric") {148 checkAll(Arb.double(), Arb.numericDoubles()) { f1, f2 ->149 val ff1 = FzyDouble(f1)150 val f1d = f1 / (1.0 + (defaultDoubleTol /2.0))151 val ff1d = FzyDouble(f1d)152 val ff2 = FzyDouble(f2)153 val f2d = f2 / (1.0 + (defaultDoubleTol /2.0))154 val ff2d = FzyDouble(f2d)155 (!ff1.equals(f1d) && f1.fzyIsZero()) shouldBe false156 (f1.fzyIsZero() && ff1 != ff1d) shouldBe false157 f1.fzyEqual(f1d) shouldBe !f1.isNaN()158 f1.fzyEqual(ff1d) shouldBe !f1.isNaN()159 f1d.fzyEqual(f1) shouldBe !f1.isNaN()160 f1d.fzyEqual(ff1) shouldBe !f1.isNaN()161 ff1.equals(f1d) shouldBe !f1.isNaN()162 (ff1d == ff1) shouldBe !f1.isNaN()163 ff1d.equals(f1) shouldBe !f1.isNaN()164 ff1.equals(f1d) shouldBe !f1.isNaN()165 (!ff2.equals(f2d) && f2.fzyIsZero()) shouldBe false166 (f2.fzyIsZero() && ff2 != ff2d) shouldBe false167 f2.fzyEqual(f2d) shouldBe true168 f2.fzyEqual(ff2d) shouldBe true169 f2d.fzyEqual(f2) shouldBe true170 f2d.fzyEqual(ff2) shouldBe true171 ff2.equals(f2d) shouldBe true172 (ff2d == ff2) shouldBe true173 ff2d.equals(f2) shouldBe true174 ff2.equals(f2d) shouldBe true175 }176 }177 test("double properties, transitive") {178 checkAll(Arb.double(), Arb.numericDoubles()) { f1, f2 ->179 val ff1 = FzyDouble(f1)180 val f1a = f1 / (1.0 + (defaultDoubleTol /1.7))181 val ff1a = FzyDouble(f1a)182 val f1b = f1 / (1.0 + (defaultDoubleTol /1.3))183 val ff1b = FzyDouble(f1b)184 val ff2 = FzyDouble(f2)185 val f2a = f2 / (1.0 + (defaultDoubleTol /1.9))186 val ff2a = FzyDouble(f2a)187 val f2b = f2 / (1.0 + (defaultDoubleTol /1.5))188 val ff2b = FzyDouble(f2b)189 (!ff1.equals(f1a) && f1.fzyIsZero()) shouldBe false190 (f1.fzyIsZero() && ff1 != ff1a) shouldBe false191 (!ff1.equals(f1b) && f1.fzyIsZero()) shouldBe false192 (f1.fzyIsZero() && ff1 != ff1b) shouldBe false193 // f1 == f1a, f1a == f1b => f1 == f1b194 f1.fzyEqual(f1a) shouldBe !f1.isNaN()195 f1.fzyEqual(ff1a) shouldBe !f1.isNaN()196 f1a.fzyEqual(f1b) shouldBe !f1.isNaN()197 f1a.fzyEqual(ff1b) shouldBe !f1.isNaN()198 f1b.fzyEqual(f1) shouldBe !f1.isNaN()199 f1b.fzyEqual(ff1) shouldBe !f1.isNaN()200 ff1.equals(f1a) shouldBe !f1.isNaN()201 (ff1 == ff1a) shouldBe !f1.isNaN()202 ff1a.equals(f1b) shouldBe !f1.isNaN()203 (ff1a == ff1b) shouldBe !f1.isNaN()204 ff1b.equals(f1) shouldBe !f1.isNaN()205 (ff1b == ff1) shouldBe !f1.isNaN()206 (!ff2.equals(f2a) && f2.fzyIsZero()) shouldBe false207 (f2.fzyIsZero() && ff2 != ff2a) shouldBe false208 (!ff2.equals(f2b) && f2.fzyIsZero()) shouldBe false209 (f2.fzyIsZero() && ff2 != ff2b) shouldBe false210 // f2 == f2a, f2a == f2b => f2 == f2b211 f2.fzyEqual(f2a) shouldBe true212 f2.fzyEqual(ff2a) shouldBe true213 f2a.fzyEqual(f2b) shouldBe true214 f2a.fzyEqual(ff2b) shouldBe true215 f2b.fzyEqual(f2) shouldBe true216 f2b.fzyEqual(ff2) shouldBe true217 ff2.equals(f2a) shouldBe true218 (ff2 == ff2a) shouldBe true219 ff2a.equals(f2b) shouldBe true220 (ff2a == ff2b) shouldBe true221 ff2b.equals(f2) shouldBe true222 (ff2b == ff2) shouldBe true223 }224 }225})...
FzyDoubleTest.kt
Source:FzyDoubleTest.kt
1package com.xrpn.order.fuzzy2import com.xrpn.order.fuzzy.FzyDouble.Companion.asFzyDouble3import com.xrpn.order.fuzzy.FzyDouble.Companion.fzyEqual4import com.xrpn.order.fuzzy.FzyDouble.Companion.unity5import com.xrpn.order.fuzzy.FzyDouble.Companion.zero6import com.xrpn.order.fuzzy.FzyDoubleEquality.fzyEqual7import io.kotest.core.spec.style.FunSpec8import io.kotest.matchers.shouldBe9import io.kotest.property.*10import io.kotest.property.arbitrary.arbitrary11import io.kotest.property.arbitrary.double12import io.kotest.property.arbitrary.numericDoubles13class FzyDoubleTest : FunSpec({14 val alterDoubleTol = 2.0 * defaultDoubleTol15 val fzyDoubleDefaultArb: Arb<FzyDouble> = arbitrary { rs: RandomSource ->16 FzyDouble(rs.random.nextDouble())17 }18 val fzyDoubleAlterArb: Arb<FzyDouble> = arbitrary { rs: RandomSource ->19 FzyDouble(rs.random.nextDouble(), alterDoubleTol)20 }21 beforeTest {}22 beforeEach {}23 test("equals default") {24 checkAll(fzyDoubleDefaultArb) { fzd1 ->25 val tt = fzd1.tol26 val halfTt = fzd1.tol / 2.027 val twiceTt = fzd1.tol * 2.028 (fzd1 == fzd1) shouldBe true29 val aux0 = FzyDouble(fzd1.qty, fzd1.tol)30 (fzd1 == aux0) shouldBe true31 val aux1 = FzyDouble(fzd1.qty+fzd1.qty*halfTt)32 (fzd1.qty == aux1.qty) shouldBe false33 (fzd1 == aux1) shouldBe true34 (fzd1 == FzyDouble(fzd1.qty, twiceTt)) shouldBe false35 fzd1.equals(fzd1.qty) shouldBe true36 fzd1.equals(fzd1.qty+tt) shouldBe false37 fzd1.equals(fzd1.qty-tt) shouldBe false38 fzd1.equals(fzd1.qty.toFloat()) shouldBe false39 fzd1.equals(null) shouldBe false40 fzd1.equals("") shouldBe false41 }42 }43 test("equals alter") {44 checkAll(fzyDoubleAlterArb) { fzda1 ->45 val tt = fzda1.tol46 val halfTt = fzda1.tol / 2.047 val twiceTt = fzda1.tol * 2.048 (fzda1 == fzda1) shouldBe true49 val aux0 = FzyDouble(fzda1.qty, fzda1.tol)50 (fzda1 == aux0) shouldBe true51 val aux1 = FzyDouble(fzda1.qty+fzda1.qty*halfTt,alterDoubleTol)52 (fzda1.qty == aux1.qty) shouldBe false53 (fzda1 == aux1) shouldBe true54 (fzda1 == FzyDouble(fzda1.qty, twiceTt)) shouldBe false55 fzda1.equals(fzda1.qty) shouldBe true56 fzda1.equals(fzda1.qty+tt) shouldBe false57 fzda1.equals(fzda1.qty-tt) shouldBe false58 fzda1.equals(fzda1.qty.toFloat()) shouldBe false59 fzda1.equals(null) shouldBe false60 fzda1.equals("") shouldBe false61 }62 }63 test("equals mixed") {64 checkAll(fzyDoubleDefaultArb, fzyDoubleAlterArb) { fzd1, fzda1 ->65 fzd1.equals(fzda1) shouldBe false66 fzda1.equals(fzda1.qty) shouldBe true67 }68 }69 test("equal (zeroes)") {70 zero().equal(zero()) shouldBe true71 zero().equal(unity()) shouldBe false72 unity().equal(zero()) shouldBe false73 doubleEps.fzyEqual(zero()) shouldBe true74 zero().equals(doubleEps) shouldBe true75 }76 test("hashCode") {77 forAll<FzyDouble, FzyDouble>(PropTestConfig(iterations = 10),fzyDoubleDefaultArb,fzyDoubleDefaultArb) { fzd1, fzd2 ->78 fzd1.hashCode() == fzd1.hashCode()79 && fzd2.hashCode() == fzd2.hashCode()80 && fzd1.hashCode() != fzd2.hashCode()81 }82 }83 test("isZero") {84 FzyDouble.zero().isZero() shouldBe true85 FzyDouble.zero(alterDoubleTol).isZero() shouldBe true86 }87 test("isUnity") {88 FzyDouble.unity().isUnity() shouldBe true89 FzyDouble.unity(alterDoubleTol).isUnity() shouldBe true90 }91 test("Double.fzyEqual") {92 checkAll(Arb.numericDoubles()) { nd1 ->93 val nf1fzy = nd1.asFzyDouble()94 nd1.fzyEqual(nf1fzy) shouldBe true95 nd1.fzyEqual(nd1.asFzyDouble(alterDoubleTol)) shouldBe true96 nd1.fzyEqual(nd1, defaultDoubleTol) shouldBe true97 nd1.fzyEqual(nd1, alterDoubleTol) shouldBe true98 val tol = nf1fzy.tol99 val aux = if (nd1 == 0.0) 1.0 + tol * 20.0 else nd1 + nd1 * tol * 20.0100 val nf2 = if (nd1 != aux) aux else nd1 + 1.0101 (nd1 != nf2) shouldBe true102 nd1.fzyEqual(nf2.asFzyDouble()) shouldBe false103 nd1.fzyEqual(nf2, defaultDoubleTol) shouldBe false104 nd1.fzyEqual(nf2, alterDoubleTol) shouldBe false105 }106 }107 test("Double.fzyEqual qty pathology") {108 checkAll(Arb.double()) { nf1 ->109 val nf1fzy = nf1.asFzyDouble()110 nf1.fzyEqual(nf1fzy) shouldBe !nf1.isNaN()111 nf1.fzyEqual(nf1.asFzyDouble(alterDoubleTol)) shouldBe !nf1.isNaN()112 nf1.fzyEqual(nf1, defaultDoubleTol) shouldBe !nf1.isNaN()113 nf1.fzyEqual(nf1, alterDoubleTol) shouldBe !nf1.isNaN()114 nf1fzy.fzyEqual(nf1) shouldBe !nf1.isNaN()115 nf1fzy.fzyEqual(nf1fzy) shouldBe !nf1.isNaN()116 }117 }118 test("Double.fzyEqual tol pathology") {119 checkAll(Arb.numericDoubles(), Arb.double()) { nfQty, nfTol ->120 val tol = Math.abs(nfTol)121 val nf1fzy = FzyDouble(nfQty, tol, defeatOk = true)122 nfQty.fzyEqual(nf1fzy) shouldBe !tol.isNaN()123 nfQty.fzyEqual(nfQty.asFzyDouble(alterDoubleTol)) shouldBe true124 nfQty.fzyEqual(nfQty, tol) shouldBe !tol.isNaN()125 nf1fzy.fzyEqual(nfQty) shouldBe !tol.isNaN()126 nf1fzy.fzyEqual(nf1fzy) shouldBe !tol.isNaN()127 }128 }129})...
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)...
DistanceSpec.kt
Source:DistanceSpec.kt
1package io.mehow.ruler2import io.kotest.assertions.throwables.shouldNotThrowAny3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.DescribeSpec5import io.kotest.matchers.comparables.shouldBeEqualComparingTo6import io.kotest.matchers.shouldBe7import io.kotest.property.Arb8import io.kotest.property.arbitrary.filterNot9import io.kotest.property.arbitrary.long10import io.kotest.property.arbitrary.numericDoubles11import io.kotest.property.checkAll12import io.mehow.ruler.test.DistanceGenerator13import java.math.BigDecimal14import kotlin.Long.Companion.MAX_VALUE15import kotlin.Long.Companion.MIN_VALUE16internal class DistanceSpec : DescribeSpec({17 describe("zero distance") {18 val zeroDistance = Distance.Zero19 it("does not have any meters") {20 zeroDistance.meters shouldBeEqualComparingTo BigDecimal.ZERO21 }22 }23 describe("min distance") {24 val minDistance = Distance.Min25 it("has smallest possible meters") {26 minDistance.meters shouldBeEqualComparingTo MIN_VALUE.toBigDecimal() + 1.toBigDecimal().movePointLeft(9)27 }28 it("cannot be subtracted from") {29 shouldThrow<ArithmeticException> { minDistance - Distance.ofNanometers(1) }30 }31 it("can be added to") {32 checkAll(DistanceGenerator.create(min = Distance.Zero)) { distance ->33 shouldNotThrowAny { minDistance + distance }34 }35 }36 it("cannot have smaller distance") {37 shouldThrow<ArithmeticException> { minDistance - Distance.Epsilon }38 }39 it("absolute value is equal to max distance") {40 minDistance.abs() shouldBe Distance.Max41 }42 }43 describe("max distance") {44 val maxDistance = Distance.Max45 it("has largest possible meters") {46 maxDistance.meters shouldBe MAX_VALUE.toBigDecimal() + 999_999_999.toBigDecimal().movePointLeft(9)47 }48 it("cannot be added to") {49 shouldThrow<ArithmeticException> { maxDistance + Distance.ofNanometers(1) }50 }51 it("can be subtracted from") {52 checkAll(DistanceGenerator.create(min = Distance.Zero)) { distance ->53 shouldNotThrowAny { maxDistance - distance }54 }55 }56 it("cannot have larger distance") {57 shouldThrow<ArithmeticException> { maxDistance + Distance.Epsilon }58 }59 }60 describe("distance") {61 val distanceGenerator = DistanceGenerator.create(62 min = Distance.ofGigameters(-1),63 max = Distance.ofGigameters(1),64 )65 // Filter small values to avoid division explosion.66 val wholeGenerator = Arb.long(-500_000, 500_000).filterNot { it == 0L }67 val realGenerator = Arb.numericDoubles(-500_000.0, 500_000.0).filterNot { it in -0.000_001..0.000_001 }68 context("can be multiplied") {69 it("by a whole number") {70 checkAll(distanceGenerator, wholeGenerator) { distance, multiplier ->71 distance * multiplier shouldBe Distance.create(distance.meters * multiplier.toBigDecimal())72 }73 }74 it("by a real number") {75 checkAll(distanceGenerator, realGenerator) { distance, multiplier ->76 distance * multiplier shouldBe Distance.create(distance.meters * multiplier.toBigDecimal())77 }78 }79 }80 context("can be divided") {81 it("by a whole number") {82 checkAll(distanceGenerator, wholeGenerator) { distance, multiplier ->83 distance / multiplier shouldBe Distance.create(distance.meters / multiplier.toBigDecimal())84 }85 }86 it("by a real number") {87 checkAll(distanceGenerator, realGenerator) { distance, multiplier ->88 distance / multiplier shouldBe Distance.create(distance.meters / multiplier.toBigDecimal())89 }90 }91 }92 it("can have absolute value computed") {93 checkAll(distanceGenerator) { distance ->94 distance.abs() shouldBe Distance.create(distance.meters.abs())95 }96 }97 }98 describe("two distances") {99 val generator = DistanceGenerator.create(100 min = Distance.ofGigameters(-1),101 max = Distance.ofGigameters(1),102 )103 it("can be added") {104 checkAll(generator, generator) { first, second ->105 first + second shouldBe Distance.create(first.meters + second.meters)106 }107 }108 it("can be subtracted") {109 checkAll(generator, generator) { first, second ->110 first - second shouldBe Distance.create(first.meters - second.meters)111 }112 }113 it("can be compared") {114 checkAll(generator, generator) { first, second ->115 first.compareTo(second) shouldBe first.meters.compareTo(second.meters)116 }117 }118 }119})...
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)...
airjourney.kt
Source:airjourney.kt
1package io.kotest.property.arbs.travel2import io.kotest.property.Arb3import io.kotest.property.arbitrary.bind4import io.kotest.property.arbitrary.numericDoubles5import io.kotest.property.kotlinx.datetime.datetime6import kotlinx.datetime.LocalDateTime7data class AirJourney(8 val departure: Airport,9 val arrival: Airport,10 val departureTime: LocalDateTime,11 val arrivalTime: LocalDateTime,12 val distanceKm: Double,13 val airline: Airline,14)15fun Arb.Companion.airJourney() = Arb.bind(16 Arb.airport(),17 Arb.airport(),18 Arb.datetime(),19 Arb.datetime(),20 Arb.airline(),21 Arb.numericDoubles(100.0, 5000.0),22) { departure, arrival, dtime, atime, airline, distance ->23 AirJourney(departure, arrival, dtime, atime, distance, airline)24}...
Arb.Companion.numericDoubles
Using AI Code Generation
1val arbDouble = Arb.numericDoubles()2val arbFloat = Arb.numericFloats()3val arbLong = Arb.numericLongs()4val arbShort = Arb.numericShorts()5val arbString = Arb.strings()6val arbUuid = Arb.uuids()7val arbZonedDateTime = Arb.zonedDateTimes()8val arbZonedTime = Arb.zonedTimes()9val arbZoneId = Arb.zoneIds()10val arbZoneOffset = Arb.zoneOffsets()11val arbZone = Arb.zones()12val arbZonedDateTimeRange = Arb.zonedDateTimeRange()13val arbZonedTimeRange = Arb.zonedTimeRange()14val arbZoneOffsetRange = Arb.zoneOffsetRange()15val arbZoneRange = Arb.zoneRange()16val arbLocalDateRange = Arb.localDateRange()
Arb.Companion.numericDoubles
Using AI Code Generation
1 val arb = Arb.numericDoubles()2 arb.take(100).forEach { println(it) }3 val arb = Arb.numericFloats()4 arb.take(100).forEach { println(it) }5 val arb = Arb.numericInts()6 arb.take(100).forEach { println(it) }7 val arb = Arb.numericLongs()8 arb.take(100).forEach { println(it) }9 val arb = Arb.numericShorts()10 arb.take(100).forEach { println(it) }11 val arb = Arb.numericUBytes()12 arb.take(100).forEach { println(it) }13 val arb = Arb.numericUInts()14 arb.take(100).forEach { println(it) }15 val arb = Arb.numericULongs()16 arb.take(100).forEach { println(it) }17 val arb = Arb.numericUShorts()18 arb.take(100).forEach { println(it) }19 val arb = Arb.numericUdoubles()20 arb.take(100).forEach { println(it) }21 val arb = Arb.numericUfloats()22 arb.take(100).forEach {
Arb.Companion.numericDoubles
Using AI Code Generation
1println ( Arb . Companion . numericDoubles ( 0.0 , 100.0 ). sample ( 10 ))2println ( Arb . Companion . numericDoubles ( - 100.0 , 0.0 ). sample ( 10 ))3println ( Arb . Companion . numericDoubles ( - 100.0 , 100.0 ). sample ( 10 ))4println ( Arb . Companion . numericDoubles ( - 100.0 , 100.0 ). sample ( 10 ))5println ( Arb . Companion . numericDoubles ( 0.0 , 100.0 ). sample ( 10 ))6println ( Arb . Companion . numericDoubles ( - 100.0 , 0.0 ). sample ( 10 ))7println ( Arb . Companion . numericDoubles ( - 100.0 , 100.0 ). sample ( 10 ))8println ( Arb . Companion . numericDoubles ( - 100.0 , 100.0 ). sample ( 10 ))9println ( Arb . Companion . numericDoubles ( 0.0 , 100.0 ). sample ( 10 ))10println ( Arb . Companion . numericDoubles ( - 100.0 , 0.0 ). sample ( 10 ))11println ( Arb . Companion . numericDoubles ( -
Arb.Companion.numericDoubles
Using AI Code Generation
1 fun `numericDoubles should return a numeric double`() {2 Arb.numericDoubles().take(1000).forEach {3 assertTrue(it.isFinite())4 assertTrue(it.isNaN().not())5 }6 }7 fun `list should return a list of arbitrary elements`() {8 val arb = Arb.list(Arb.int())9 arb.take(1000).forEach {10 assertTrue(it.isNotEmpty())11 }12 }13 fun `int should return an integer`() {14 Arb.int().take(1000).forEach {15 assertTrue(it >= Int.MIN_VALUE && it <= Int.MAX_VALUE)16 }17 }18}
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!!