How to use Arb.Companion.double method of io.kotest.property.arbitrary.doubles class

Best Kotest code snippet using io.kotest.property.arbitrary.doubles.Arb.Companion.double

FzyDoubleEqualityTest.kt

Source:FzyDoubleEqualityTest.kt Github

copy

Full Screen

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})...

Full Screen

Full Screen

Arb.kt

Source:Arb.kt Github

copy

Full Screen

1package arrow.fx.coroutines.stream2import io.kotest.property.Arb3import io.kotest.property.RandomSource4import io.kotest.property.Sample5import io.kotest.property.Shrinker6import io.kotest.property.arbitrary.arb7import io.kotest.property.arbitrary.bool8import io.kotest.property.arbitrary.byte9import io.kotest.property.arbitrary.choice10import io.kotest.property.arbitrary.choose11import io.kotest.property.arbitrary.double12import io.kotest.property.arbitrary.float13import io.kotest.property.arbitrary.int14import io.kotest.property.arbitrary.list15import io.kotest.property.arbitrary.long16import io.kotest.property.arbitrary.map17import io.kotest.property.arbitrary.set18import io.kotest.property.arbitrary.short19import kotlin.random.nextInt20@JvmOverloads21inline fun <reified A> Arb.Companion.array(22 gen: Arb<A>,23 range: IntRange = 0..10024): Arb<Array<A>> {25 check(!range.isEmpty())26 check(range.first >= 0)27 return arb(edgecases = listOf(emptyArray<A>()) + gen.edgecases().map { arrayOf(it) }) {28 sequence {29 val genIter = gen.generate(it).iterator()30 while (true) {31 val targetSize = it.random.nextInt(range)32 val list = ArrayList<A>(targetSize)33 while (list.size < targetSize && genIter.hasNext()) {34 list.add(genIter.next().value)35 }36 check(list.size == targetSize)37 yield(list.toArray() as Array<A>)38 }39 }40 }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) }85 override fun values(rs: RandomSource): Sequence<Sample<Chunk<A>>> =86 Arb.choose(87 5 to arb.map { Chunk.just(it) },88 10 to Arb.list(arb, 0..20).map { Chunk.iterable(it) },89 10 to Arb.set(arb, 0..20).map { Chunk.iterable(it) },90 10 to Arb.array(arb, 0..20).map { Chunk.array(it) },91 10 to Arb.boxedChunk(arb)92 ).values(rs)93 }94inline fun <reified A> Arb.Companion.boxedChunk(arb: Arb<A>): Arb<Chunk<A>> =95 object : Arb<Chunk<A>>() {96 override fun edgecases(): List<Chunk<A>> =97 listOf(Chunk.empty<A>()) + arb.edgecases().map { Chunk(it) }98 override fun values(rs: RandomSource): Sequence<Sample<Chunk<A>>> =99 arrayChunkGenerator(arb, ChunkShrinker()) { values, offset, length ->100 Chunk.boxed(values.toTypedArray(), offset, length)101 }.values(rs)102 }103fun Arb.Companion.booleanChunk(): Arb<Chunk<Boolean>> =104 object : Arb<Chunk<Boolean>>() {105 override fun edgecases(): List<Chunk<Boolean>> =106 listOf(Chunk.empty<Boolean>()) + Arb.bool().edgecases().map { Chunk(it) }107 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Boolean>>> =108 Arb.choice(109 arrayChunkGenerator(Arb.bool(), ChunkShrinker()) { values, offset, length ->110 Chunk.booleans(values.toBooleanArray(), offset, length)111 },112 arrayChunkGenerator(Arb.bool(), ChunkShrinker()) { values, _, _ ->113 Chunk.array(values.toTypedArray())114 }115 ).values(rs)116 }117fun Arb.Companion.byteChunk(): Arb<Chunk<Byte>> =118 object : Arb<Chunk<Byte>>() {119 override fun edgecases(): List<Chunk<Byte>> =120 listOf(Chunk.empty<Byte>()) + Arb.byte().edgecases().map { Chunk(it) }121 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Byte>>> =122 Arb.choice(123 arrayChunkGenerator(Arb.byte(), ChunkShrinker()) { values, offset, length ->124 Chunk.bytes(values.toByteArray(), offset, length)125 },126 arrayChunkGenerator(Arb.byte(), ChunkShrinker()) { values, _, _ ->127 Chunk.array(values.toTypedArray())128 }129 ).values(rs)130 }131fun Arb.Companion.intChunk(): Arb<Chunk<Int>> =132 object : Arb<Chunk<Int>>() {133 override fun edgecases(): List<Chunk<Int>> =134 listOf(Chunk.empty<Int>()) + Arb.int().edgecases().map { Chunk(it) }135 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Int>>> =136 Arb.choice(137 arrayChunkGenerator(Arb.int(), ChunkShrinker()) { values, offset, length ->138 Chunk.ints(values.toIntArray(), offset, length)139 },140 arrayChunkGenerator(Arb.int(), ChunkShrinker()) { values, _, _ ->141 Chunk.array(values.toTypedArray())142 }143 ).values(rs)144 }145fun Arb.Companion.longChunk(): Arb<Chunk<Long>> =146 object : Arb<Chunk<Long>>() {147 override fun edgecases(): List<Chunk<Long>> =148 listOf(Chunk.empty<Long>()) + Arb.long().edgecases().map { Chunk(it) }149 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Long>>> =150 Arb.choice(151 arrayChunkGenerator(Arb.long(), ChunkShrinker()) { values, offset, length ->152 Chunk.longs(values.toLongArray(), offset, length)153 },154 arrayChunkGenerator(Arb.long(), ChunkShrinker()) { values, _, _ ->155 Chunk.array(values.toTypedArray())156 }157 ).values(rs)158 }159fun Arb.Companion.doubleChunk(): Arb<Chunk<Double>> =160 object : Arb<Chunk<Double>>() {161 override fun edgecases(): List<Chunk<Double>> =162 listOf(Chunk.empty<Double>()) + Arb.double().edgecases().map { Chunk(it) }163 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Double>>> =164 Arb.choice(165 arrayChunkGenerator(Arb.double(), ChunkShrinker()) { values, offset, length ->166 Chunk.doubles(values.toDoubleArray(), offset, length)167 },168 arrayChunkGenerator(Arb.double(), ChunkShrinker()) { values, _, _ ->169 Chunk.array(values.toTypedArray())170 }171 ).values(rs)172 }173fun Arb.Companion.floatChunk(): Arb<Chunk<Float>> =174 object : Arb<Chunk<Float>>() {175 override fun edgecases(): List<Chunk<Float>> =176 listOf(Chunk.empty<Float>()) + Arb.float().edgecases().map { Chunk(it) }177 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Float>>> =178 Arb.choice(179 arrayChunkGenerator(Arb.float(), ChunkShrinker()) { values, offset, length ->180 Chunk.floats(values.toFloatArray(), offset, length)181 },182 arrayChunkGenerator(Arb.float(), ChunkShrinker()) { values, _, _ ->183 Chunk.array(values.toTypedArray())184 }185 ).values(rs)186 }187fun Arb.Companion.shortChunk(): Arb<Chunk<Short>> =188 object : Arb<Chunk<Short>>() {189 override fun edgecases(): List<Chunk<Short>> =190 listOf(Chunk.empty<Short>()) + Arb.short().edgecases().map { Chunk(it) }191 override fun values(rs: RandomSource): Sequence<Sample<Chunk<Short>>> =192 Arb.choice(193 arrayChunkGenerator(Arb.short(), ChunkShrinker()) { values, offset, length ->194 Chunk.shorts(values.toShortArray(), offset, length)195 },196 arrayChunkGenerator(Arb.short(), ChunkShrinker()) { values, _, _ ->197 Chunk.array(values.toTypedArray())198 }199 ).values(rs)200 }...

Full Screen

Full Screen

Generators.kt

Source:Generators.kt Github

copy

Full Screen

1package arrow.core.test.generators2import arrow.core.Const3import arrow.core.Either4import arrow.core.Endo5import arrow.core.Eval6import arrow.core.Ior7import arrow.core.Option8import arrow.core.Tuple109import arrow.core.Tuple410import arrow.core.Tuple511import arrow.core.Tuple612import arrow.core.Tuple713import arrow.core.Tuple814import arrow.core.Tuple915import arrow.core.Validated16import arrow.core.left17import arrow.core.right18import arrow.core.toOption19import io.kotest.property.Arb20import io.kotest.property.arbitrary.bind21import io.kotest.property.arbitrary.boolean22import io.kotest.property.arbitrary.byte23import io.kotest.property.arbitrary.choice24import io.kotest.property.arbitrary.constant25import io.kotest.property.arbitrary.filter26import io.kotest.property.arbitrary.flatMap27import io.kotest.property.arbitrary.int28import io.kotest.property.arbitrary.long29import io.kotest.property.arbitrary.map30import io.kotest.property.arbitrary.numericDoubles31import io.kotest.property.arbitrary.numericFloats32import io.kotest.property.arbitrary.of33import io.kotest.property.arbitrary.orNull34import io.kotest.property.arbitrary.short35import io.kotest.property.arbitrary.string36import kotlin.Result.Companion.failure37import kotlin.Result.Companion.success38import kotlin.math.abs39public fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> =40 arb.map { b: B -> { _: A -> b } }41public fun <A> Arb.Companion.functionAAToA(arb: Arb<A>): Arb<(A, A) -> A> =42 arb.map { a: A -> { _: A, _: A -> a } }43public fun <A, B> Arb.Companion.functionBAToB(arb: Arb<B>): Arb<(B, A) -> B> =44 arb.map { b: B -> { _: B, _: A -> b } }45public fun <A, B> Arb.Companion.functionABToB(arb: Arb<B>): Arb<(A, B) -> B> =46 arb.map { b: B -> { _: A, _: B -> b } }47public fun <A> Arb.Companion.functionToA(arb: Arb<A>): Arb<() -> A> =48 arb.map { a: A -> { a } }49public fun Arb.Companion.throwable(): Arb<Throwable> =50 Arb.of(listOf(RuntimeException(), NoSuchElementException(), IllegalArgumentException()))51public fun <A> Arb.Companion.result(arbA: Arb<A>): Arb<Result<A>> =52 Arb.choice(arbA.map(::success), throwable().map(::failure))53public fun Arb.Companion.doubleSmall(): Arb<Double> =54 Arb.numericDoubles(from = 0.0, to = 100.0)55public fun Arb.Companion.floatSmall(): Arb<Float> =56 Arb.numericFloats(from = 0F, to = 100F)57public fun Arb.Companion.intSmall(factor: Int = 10000): Arb<Int> =58 Arb.int((Int.MIN_VALUE / factor)..(Int.MAX_VALUE / factor))59public fun Arb.Companion.byteSmall(): Arb<Byte> =60 Arb.byte(min = (Byte.MIN_VALUE / 10).toByte(), max = (Byte.MAX_VALUE / 10).toByte())61public fun Arb.Companion.shortSmall(): Arb<Short> {62 val range = (Short.MIN_VALUE / 1000)..(Short.MAX_VALUE / 1000)63 return Arb.short().filter { it in range }64}65public fun Arb.Companion.longSmall(): Arb<Long> =66 Arb.long((Long.MIN_VALUE / 100000L)..(Long.MAX_VALUE / 100000L))67public fun <A, B, C, D> Arb.Companion.tuple4(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>): Arb<Tuple4<A, B, C, D>> =68 Arb.bind(arbA, arbB, arbC, arbD, ::Tuple4)69public fun <A, B, C, D, E> Arb.Companion.tuple5(70 arbA: Arb<A>,71 arbB: Arb<B>,72 arbC: Arb<C>,73 arbD: Arb<D>,74 arbE: Arb<E>75): Arb<Tuple5<A, B, C, D, E>> =76 Arb.bind(arbA, arbB, arbC, arbD, arbE, ::Tuple5)77public fun <A, B, C, D, E, F> Arb.Companion.tuple6(78 arbA: Arb<A>,79 arbB: Arb<B>,80 arbC: Arb<C>,81 arbD: Arb<D>,82 arbE: Arb<E>,83 arbF: Arb<F>84): Arb<Tuple6<A, B, C, D, E, F>> =85 Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, ::Tuple6)86public fun <A, B, C, D, E, F, G> Arb.Companion.tuple7(87 arbA: Arb<A>,88 arbB: Arb<B>,89 arbC: Arb<C>,90 arbD: Arb<D>,91 arbE: Arb<E>,92 arbF: Arb<F>,93 arbG: Arb<G>94): Arb<Tuple7<A, B, C, D, E, F, G>> =95 Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, arbG, ::Tuple7)96public fun <A, B, C, D, E, F, G, H> Arb.Companion.tuple8(97 arbA: Arb<A>,98 arbB: Arb<B>,99 arbC: Arb<C>,100 arbD: Arb<D>,101 arbE: Arb<E>,102 arbF: Arb<F>,103 arbG: Arb<G>,104 arbH: Arb<H>105): Arb<Tuple8<A, B, C, D, E, F, G, H>> =106 Arb.bind(107 Arb.tuple7(arbA, arbB, arbC, arbD, arbE, arbF, arbG),108 arbH109 ) { (a, b, c, d, e, f, g), h ->110 Tuple8(a, b, c, d, e, f, g, h)111 }112public fun <A, B, C, D, E, F, G, H, I> Arb.Companion.tuple9(113 arbA: Arb<A>,114 arbB: Arb<B>,115 arbC: Arb<C>,116 arbD: Arb<D>,117 arbE: Arb<E>,118 arbF: Arb<F>,119 arbG: Arb<G>,120 arbH: Arb<H>,121 arbI: Arb<I>122): Arb<Tuple9<A, B, C, D, E, F, G, H, I>> =123 Arb.bind(124 Arb.tuple8(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH),125 arbI126 ) { (a, b, c, d, e, f, g, h), i ->127 Tuple9(a, b, c, d, e, f, g, h, i)128 }129public fun <A, B, C, D, E, F, G, H, I, J> Arb.Companion.tuple10(130 arbA: Arb<A>,131 arbB: Arb<B>,132 arbC: Arb<C>,133 arbD: Arb<D>,134 arbE: Arb<E>,135 arbF: Arb<F>,136 arbG: Arb<G>,137 arbH: Arb<H>,138 arbI: Arb<I>,139 arbJ: Arb<J>140): Arb<Tuple10<A, B, C, D, E, F, G, H, I, J>> =141 Arb.bind(142 Arb.tuple9(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH, arbI),143 arbJ144 ) { (a, b, c, d, e, f, g, h, i), j ->145 Tuple10(a, b, c, d, e, f, g, h, i, j)146 }147public fun Arb.Companion.nonZeroInt(): Arb<Int> = Arb.int().filter { it != 0 }148public fun Arb.Companion.intPredicate(): Arb<(Int) -> Boolean> =149 Arb.nonZeroInt().flatMap { num ->150 val absNum = abs(num)151 Arb.of(152 listOf<(Int) -> Boolean>(153 { it > num },154 { it <= num },155 { it % absNum == 0 },156 { it % absNum == absNum - 1 }157 )158 )159 }160public fun <A> Arb.Companion.endo(arb: Arb<A>): Arb<Endo<A>> = arb.map { a: A -> Endo<A> { a } }161public fun <B> Arb.Companion.option(arb: Arb<B>): Arb<Option<B>> =162 arb.orNull().map { it.toOption() }163public fun <E, A> Arb.Companion.either(arbE: Arb<E>, arbA: Arb<A>): Arb<Either<E, A>> {164 val arbLeft = arbE.map { Either.Left(it) }165 val arbRight = arbA.map { Either.Right(it) }166 return Arb.choice(arbLeft, arbRight)167}168public fun <E, A> Arb<E>.or(arbA: Arb<A>): Arb<Either<E, A>> = Arb.either(this, arbA)169public fun <E, A> Arb.Companion.validated(arbE: Arb<E>, arbA: Arb<A>): Arb<Validated<E, A>> =170 Arb.either(arbE, arbA).map { Validated.fromEither(it) }171public fun Arb.Companion.unit(): Arb<Unit> =172 Arb.constant(Unit)173public fun <A, B> Arb.Companion.ior(arbA: Arb<A>, arbB: Arb<B>): Arb<Ior<A, B>> =174 arbA.alignWith(arbB) { it }175public fun <A, B> Arb.Companion.arbConst(arb: Arb<A>): Arb<Const<A, B>> =176 arb.map { Const<A, B>(it) }177public fun <A> Arb<A>.eval(): Arb<Eval<A>> =178 map { Eval.now(it) }179private fun <A, B, R> Arb<A>.alignWith(arbB: Arb<B>, transform: (Ior<A, B>) -> R): Arb<R> =180 Arb.bind(this, arbB) { a, b -> transform(Ior.Both(a, b)) }181public fun Arb.Companion.suspendFunThatReturnsEitherAnyOrAnyOrThrows(): Arb<suspend () -> Either<Any, Any>> =182 choice(183 suspendFunThatReturnsAnyRight(),184 suspendFunThatReturnsAnyLeft(),185 suspendFunThatThrows()186 )187public fun Arb.Companion.suspendFunThatReturnsAnyRight(): Arb<suspend () -> Either<Any, Any>> =188 any().map { suspend { it.right() } }189public fun Arb.Companion.suspendFunThatReturnsAnyLeft(): Arb<suspend () -> Either<Any, Any>> =190 any().map { suspend { it.left() } }191public fun Arb.Companion.suspendFunThatThrows(): Arb<suspend () -> Either<Any, Any>> =192 throwable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>193public fun Arb.Companion.any(): Arb<Any> =194 choice(195 Arb.string() as Arb<Any>,196 Arb.int() as Arb<Any>,197 Arb.long() as Arb<Any>,198 Arb.boolean() as Arb<Any>,199 Arb.throwable() as Arb<Any>,200 Arb.unit() as Arb<Any>201 )...

Full Screen

Full Screen

FzyDoubleTest.kt

Source:FzyDoubleTest.kt Github

copy

Full Screen

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})...

Full Screen

Full Screen

doubles.kt

Source:doubles.kt Github

copy

Full Screen

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)...

Full Screen

Full Screen

OrNullTest.kt

Source:OrNullTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.property.arbitrary2import io.kotest.assertions.retry3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.FunSpec5import io.kotest.inspectors.forAll6import io.kotest.matchers.doubles.plusOrMinus7import io.kotest.matchers.ints.shouldBeBetween8import io.kotest.matchers.shouldBe9import io.kotest.property.Arb10import io.kotest.property.RandomSource11import io.kotest.property.Sample12import io.kotest.property.arbitrary.constant13import io.kotest.property.arbitrary.int14import io.kotest.property.arbitrary.long15import io.kotest.property.arbitrary.orNull16import io.kotest.property.forAll17import kotlin.time.Duration.Companion.seconds18class OrNullTest : FunSpec({19 test("Arb.orNull() should add null values to those generated") {20 val iterations = 100021 val classifications =22 forAll(iterations, Arb.int().orNull()) { num ->23 classify(num == null, "null", "non-null")24 true25 }.classifications()26 classifications["null"]?.shouldBeBetween(300, 600)27 classifications["non-null"]?.shouldBeBetween(300, 600)28 }29 test("null probability values can be specified") {30 retry(3, timeout = 2.seconds, delay = 0.1.seconds) {31 listOf(0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0)32 .forAll { p: Double ->33 val nullCount = Arb.long().orNull(nullProbability = p).samples(RandomSource.default())34 .map(Sample<Long?>::value)35 .take(1000)36 .filter { it == null }37 .count()38 (nullCount.toDouble() / 1000) shouldBe (p plusOrMinus 0.05)39 }40 }41 }42 test("invalid null probability raise an IllegalArgumentException") {43 listOf(1.01, -0.1, 4.9, 99.9)44 .forAll { illegalVal ->45 shouldThrow<IllegalArgumentException> { Arb.long().orNull(nullProbability = illegalVal) }46 }47 }48 test("functions can be supplied to determine null frequency") {49 listOf(true, false).forAll { isNextNull: Boolean ->50 val allNull = Arb.int().orNull(isNextNull = { isNextNull }).samples(RandomSource.default())51 .map(Sample<Int?>::value)52 .take(100)53 .all { it == null }54 allNull shouldBe isNextNull55 }56 }57 test("orNull has a shrink to null"){58 val iterations = 100059 val rs = RandomSource.default()60 val classifications =61 forAll(iterations, Arb.constant(Arb.int().orNull())) { orNullArb ->62 val sample = orNullArb.sample(rs)63 val hasNullShrink = sample.shrinks.children.value.map{it.value()}.any{it == null}64 classify(hasNullShrink, "nullShrink", "noNullShrink")65 true66 }.classifications()67 classifications["nullShrink"]?.shouldBeBetween(800, 1000)68 classifications["noNullShrink"]?.shouldBeBetween(0, 200)69 }70})...

Full Screen

Full Screen

airjourney.kt

Source:airjourney.kt Github

copy

Full Screen

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}...

Full Screen

Full Screen

Arb.Companion.double

Using AI Code Generation

copy

Full Screen

1import io.kotest.property.arbitrary.double2import io.kotest.property.arbitrary.doubles3import io.kotest.property.arbitrary.filter4import io.kotest.property.arbitrary.int5import io.kotest.property.arbitrary.ints6import io.kotest.property.arbitrary.map7import io.kotest.property.arbitrary.next8import io.kotest.property.arbitrary.string9import io.kotest.property.arbitrary.strings10import io.kotest.property.checkAll11import kotlin.random.Random12fun main() {13 checkAll(100, Arb.ints(0..100)) {14 it.shouldBeLessThanOrEqual(100)15 }16 checkAll(100, Arb.ints(0..100).map { it * 2 }) {17 it.shouldBeLessThanOrEqual(200)18 }19 checkAll(100, Arb.ints(0..100).filter { it % 2 == 0 }) {20 it.shouldBeLessThanOrEqual(100)21 }22 checkAll(100, Arb.ints(0..100).filter { it % 2 == 0 }.map { it * 2 }) {23 it.shouldBeLessThanOrEqual(200)24 }25 checkAll(100, Arb.ints(0..100).map { it * 2 }.filter { it % 2 == 0 }) {26 it.shouldBeLessThanOrEqual(200)27 }28 checkAll(100, Arb.ints(0..100).filter { it % 2 == 0 }.map { it * 2 }.filter { it % 2 == 0 }) {29 it.shouldBeLessThanOrEqual(200)30 }

Full Screen

Full Screen

Arb.Companion.double

Using AI Code Generation

copy

Full Screen

1+val doubleGen = Arb.double()2+val floatGen = Arb.float()3+val intGen = Arb.int()4+val longGen = Arb.long()5+val shortGen = Arb.short()6+val byteGen = Arb.byte()7+val charGen = Arb.char()8+val stringGen = Arb.string()9+val stringGen = Arb.string(10)10+val stringGen = Arb.string(5..10)11+val stringGen = Arb.string(5..10, charset = Charsets.UTF_8)

Full Screen

Full Screen

Arb.Companion.double

Using AI Code Generation

copy

Full Screen

1 val arb = Arb.double(0.0, 100.0)2 checkAll(arb) { a ->3 a.shouldBeLessThanOrEqual(100.0)4 a.shouldBeGreaterThanOrEqual(0.0)5 }6 }7}

Full Screen

Full Screen

Arb.Companion.double

Using AI Code Generation

copy

Full Screen

1+val doubleGen = Arb.doubles(0.0, 100.0)2+val doubleGen = Arb.doubles(0.0, 100.0)3+val doubleGen = Arb.doubles(0.0, 100.0)4+val doubleGen = Arb.doubles(0.0, 100.0)5+val doubleGen = Arb.doubles(0.0, 100.0)6+val doubleGen = Arb.doubles(0.0, 100.0)7+val doubleGen = Arb.doubles(0.0, 100.0)8+val doubleGen = Arb.doubles(0.0, 100.0)9+val doubleGen = Arb.doubles(0.0, 100.0)10+val doubleGen = Arb.doubles(0.0, 100.0)11+val doubleGen = Arb.doubles(0.0, 100.0)12+val doubleGen = Arb.doubles(0.0, 100.0)13+val doubleGen = Arb.doubles(0.0, 100.0)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful