Best Kotest code snippet using io.kotest.property.exhaustive.cartesian.Exhaustive.cartesianPairs
CartesianTest.kt
Source:CartesianTest.kt  
1package com.sksamuel.kotest.property.exhaustive2import io.kotest.core.Tuple43import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.collections.shouldHaveSize5import io.kotest.matchers.shouldBe6import io.kotest.property.Exhaustive7import io.kotest.property.exhaustive.cartesian8import io.kotest.property.exhaustive.cartesianPairs9import io.kotest.property.exhaustive.exhaustive10import io.kotest.property.exhaustive.of11class CartesianTest : FunSpec() {12   init {13      test("Exhaustive.cartesianPairs") {14         listOf(1, 2, 3).exhaustive().cartesianPairs().values shouldBe listOf(15            Pair(1, 1),16            Pair(1, 2),17            Pair(1, 3),18            Pair(2, 1),19            Pair(2, 2),20            Pair(2, 3),21            Pair(3, 1),22            Pair(3, 2),23            Pair(3, 3),24         )25      }26      test("Exhaustive.cartesian(a,b) arity 2") {27         val e = Exhaustive.cartesian(28            Exhaustive.of(1, 2, 3),29            Exhaustive.of(true, false)30         ) { a, b -> Pair(a, b) }31         e.values.shouldHaveSize(6)32         e.values shouldBe listOf(33            Pair(1, true),34            Pair(1, false),35            Pair(2, true),36            Pair(2, false),37            Pair(3, true),38            Pair(3, false),39         )40      }41      test("a.cartesian(b) arity 2") {42         val e = Exhaustive.of(1, 2, 3).cartesian(Exhaustive.of(true, false)) { a, b -> Pair(a, b) }43         e.values.shouldHaveSize(6)44         e.values shouldBe listOf(45            Pair(1, true),46            Pair(1, false),47            Pair(2, true),48            Pair(2, false),49            Pair(3, true),50            Pair(3, false),51         )52      }53      test("a.cartesianPairs(b) arity 2") {54         val e = Exhaustive.of(1, 2, 3).cartesianPairs(Exhaustive.of(true, false))55         e.values.shouldHaveSize(6)56         e.values shouldBe listOf(57            Pair(1, true),58            Pair(1, false),59            Pair(2, true),60            Pair(2, false),61            Pair(3, true),62            Pair(3, false),63         )64      }65      test("Exhaustive.cartesianPairs(a,b) arity 2") {66         val e = Exhaustive.cartesianPairs(67            Exhaustive.of(1, 2, 3),68            Exhaustive.of(true, false)69         )70         e.values.shouldHaveSize(6)71         e.values shouldBe listOf(72            Pair(1, true),73            Pair(1, false),74            Pair(2, true),75            Pair(2, false),76            Pair(3, true),77            Pair(3, false),78         )79      }80      test("Exhaustive.cartesian arity 3") {81         val e = Exhaustive.cartesian(82            Exhaustive.of(1, 2, 3),83            Exhaustive.of("a", "b", "c"),84            Exhaustive.of(true, false)85         ) { a, b, c -> Triple(a, b, c) }86         e.values.shouldHaveSize(18)87         e.values shouldBe listOf(88            Triple(1, "a", true),89            Triple(1, "a", false),90            Triple(1, "b", true),91            Triple(1, "b", false),92            Triple(1, "c", true),93            Triple(1, "c", false),94            Triple(2, "a", true),95            Triple(2, "a", false),96            Triple(2, "b", true),97            Triple(2, "b", false),98            Triple(2, "c", true),99            Triple(2, "c", false),100            Triple(3, "a", true),101            Triple(3, "a", false),102            Triple(3, "b", true),103            Triple(3, "b", false),104            Triple(3, "c", true),105            Triple(3, "c", false)106         )107      }108      test("Exhaustive.cartesian arity 4") {109         val e = Exhaustive.cartesian(110            Exhaustive.of(1, 2),111            Exhaustive.of("a", "b", "c"),112            Exhaustive.of(true, false),113            Exhaustive.of('p', 'q'),114         ) { a, b, c, d -> Tuple4(a, b, c, d) }115         e.values.shouldHaveSize(24)116         e.values shouldBe listOf(117            Tuple4(a = 1, b = "a", c = true, d = 'p'),118            Tuple4(a = 1, b = "a", c = true, d = 'q'),119            Tuple4(a = 1, b = "a", c = false, d = 'p'),120            Tuple4(a = 1, b = "a", c = false, d = 'q'),121            Tuple4(a = 1, b = "b", c = true, d = 'p'),122            Tuple4(a = 1, b = "b", c = true, d = 'q'),123            Tuple4(a = 1, b = "b", c = false, d = 'p'),124            Tuple4(a = 1, b = "b", c = false, d = 'q'),125            Tuple4(a = 1, b = "c", c = true, d = 'p'),126            Tuple4(a = 1, b = "c", c = true, d = 'q'),127            Tuple4(a = 1, b = "c", c = false, d = 'p'),128            Tuple4(a = 1, b = "c", c = false, d = 'q'),129            Tuple4(a = 2, b = "a", c = true, d = 'p'),130            Tuple4(a = 2, b = "a", c = true, d = 'q'),131            Tuple4(a = 2, b = "a", c = false, d = 'p'),132            Tuple4(a = 2, b = "a", c = false, d = 'q'),133            Tuple4(a = 2, b = "b", c = true, d = 'p'),134            Tuple4(a = 2, b = "b", c = true, d = 'q'),135            Tuple4(a = 2, b = "b", c = false, d = 'p'),136            Tuple4(a = 2, b = "b", c = false, d = 'q'),137            Tuple4(a = 2, b = "c", c = true, d = 'p'),138            Tuple4(a = 2, b = "c", c = true, d = 'q'),139            Tuple4(a = 2, b = "c", c = false, d = 'p'),140            Tuple4(a = 2, b = "c", c = false, d = 'q'),141         )142      }143   }144}...cartesian.kt
Source:cartesian.kt  
1package io.kotest.property.exhaustive2import io.kotest.property.Exhaustive3fun <A, B, C> Exhaustive<A>.cartesian(other: Exhaustive<B>, f: (A, B) -> C): Exhaustive<C> {4   val cs = values.flatMap { _a ->5      other.values.map { _b ->6         f(_a, _b)7      }8   }9   return cs.exhaustive()10}11fun <A, B> Exhaustive<A>.cartesianPairs(other: Exhaustive<B>): Exhaustive<Pair<A, B>> {12   val pairs = values.flatMap { _a ->13      other.values.map { _b ->14         Pair(_a, _b)15      }16   }17   return pairs.exhaustive()18}19/**20 * Returns the cartesian join of this exhaustive with itself, with the results as pairs.21 */22fun <A> Exhaustive<A>.cartesianPairs(): Exhaustive<Pair<A, A>> {23   val cs = values.flatMap { _a ->24      values.map { _b ->25         Pair(_a, _b)26      }27   }28   return cs.exhaustive()29}30fun <A, B, C> Exhaustive.Companion.cartesian(a: Exhaustive<A>, b: Exhaustive<B>, f: (A, B) -> C): Exhaustive<C> {31   val cs = a.values.flatMap { _a ->32      b.values.map { _b ->33         f(_a, _b)34      }35   }36   return cs.exhaustive()37}38fun <A, B> Exhaustive.Companion.cartesianPairs(a: Exhaustive<A>, b: Exhaustive<B>): Exhaustive<Pair<A, B>> {39   val pairs = a.values.flatMap { _a ->40      b.values.map { _b ->41         Pair(_a, _b)42      }43   }44   return pairs.exhaustive()45}46fun <A, B, C, D> Exhaustive.Companion.cartesian(47   a: Exhaustive<A>,48   b: Exhaustive<B>,49   c: Exhaustive<C>,50   f: (A, B, C) -> D51): Exhaustive<D> {52   val ds = a.values.flatMap { _a ->53      b.values.flatMap { _b ->54         c.values.map { _c ->55            f(_a, _b, _c)56         }57      }58   }59   return ds.exhaustive()60}61fun <A, B, C, D, E> Exhaustive.Companion.cartesian(62   a: Exhaustive<A>,63   b: Exhaustive<B>,64   c: Exhaustive<C>,65   d: Exhaustive<D>,66   f: (A, B, C, D) -> E67): Exhaustive<E> {68   val es = a.values.flatMap { _a ->69      b.values.flatMap { _b ->70         c.values.flatMap { _c ->71            d.values.map { _d ->72               f(_a, _b, _c, _d)73            }74         }75      }76   }77   return es.exhaustive()78}79fun <A, B, C, D, E, F> Exhaustive.Companion.cartesian(80   a: Exhaustive<A>,81   b: Exhaustive<B>,82   c: Exhaustive<C>,83   d: Exhaustive<D>,84   e: Exhaustive<E>,85   f: (A, B, C, D, E) -> F86): Exhaustive<F> {87   val fs = a.values.flatMap { _a ->88      b.values.flatMap { _b ->89         c.values.flatMap { _c ->90            d.values.flatMap { _d ->91               e.values.map { _e ->92                  f(_a, _b, _c, _d, _e)93               }94            }95         }96      }97   }98   return fs.exhaustive()99}...Exhaustive.cartesianPairs
Using AI Code Generation
1    fun <A, B> cartesianPairs(a: Exhaustive<A>, b: Exhaustive<B>): Exhaustive<Pair<A, B>> =2        a.cartesian(b)3    fun <A, B, C> cartesianTriples(a: Exhaustive<A>, b: Exhaustive<B>, c: Exhaustive<C>): Exhaustive<Triple<A, B, C>> =4        a.cartesian(b, c)5    import io.kotest.property.Exhaustive6    import io.kotest.property.Exhaustive.Companion.empty7    import io.kotest.property.Exhaustive.Companion.exhaustive8    import io.kotest.property.Exhaustive.Companion.single9    import io.kotest.property.Exhaustive.Companion.zip10    import io.kotest.property.RandomSource11    import io.kotest.property.RandomSource.Companion.random12    import io.kotest.property.Shrinker13    interface Exhaustive<out A> {14      fun iterator(): Iterator<A>15      fun random(rs: RandomSource = random()): A = iterator().random(rs)16      fun shrinker(): Shrinker<A> = Shrinker { empty() }17      fun toList(): List<A> = iterator().toList()18      companion object {19        fun <A> exhaustive(vararg a: A): Exhaustive<A> = exhaustive(a.toList())20        fun <A> exhaustive(a: List<A>): Exhaustive<A> = ExhaustiveImpl(a)21        fun <A> single(a: A): Exhaustive<A> = ExhaustiveImpl(listOf(a))22        fun <A> empty(): Exhaustive<A> = ExhaustiveImpl(emptyList())23        fun <A> zip(a: Exhaustive<AExhaustive.cartesianPairs
Using AI Code Generation
1    fun testCartesianPairs() {2        val a = listOf(1, 2, 3)3        val b = listOf(4, 5, 6)4        val pairs = a.cartesianPairs(b)5        println(pairs)6    }7    fun testCartesianTriples() {8        val a = listOf(1, 2, 3)9        val b = listOf(4, 5, 6)10        val c = listOf(7, 8, 9)11        val triples = a.cartesianTriples(b, c)12        println(triples)13    }14    fun testCartesianQuadruples() {15        val a = listOf(1, 2, 3)16        val b = listOf(4, 5, 6)17        val c = listOf(7, 8, 9)18        val d = listOf(10, 11, 12)19        val quadruples = a.cartesianQuadruples(b, c, d)20        println(quadruples)21    }22    fun testCartesianQuintuples() {23        val a = listOf(1, 2, 3)24        val b = listOf(4, 5, 6)25        val c = listOf(7, 8, 9)26        val d = listOf(10, 11, 12)27        val e = listOf(13, 14, 15)28        val quintuples = a.cartesianQuintuples(b, c, d, e)29        println(quintuples)30    }31    fun testCartesianSextuples() {32        val a = listOf(1, 2, 3)33        val b = listOf(4, 5, 6)34        val c = listOf(7, 8, 9)Exhaustive.cartesianPairs
Using AI Code Generation
1val list = listOf(1,2,3,4,5)2val result = list.cartesianPairs()3println(result.toList())4val list = listOf(1,2,3,4,5)5val result = list.cartesianTriples()6println(result.toList())7val list = listOf(1,2,3,4,5)8val result = list.cartesianQuadruples()9println(result.toList())10val list = listOf(1,2,3,4,5)11val result = list.cartesianQuintuples()12println(result.toList())13val list = listOf(1,2,3,4,5)14val result = list.cartesianSextuples()15println(result.toList())16val list = listOf(1,2,3,4,5)17val result = list.cartesianSeptuples()18println(result.toList())19val list = listOf(1,2,3,4,5)20val result = list.cartesianOctuples()21println(result.toList())22val list = listOf(1,2,3,4,5)23val result = list.cartesianNonuples()24println(result.toList())25val list = listOf(1,2,3,4,5)26val result = list.cartesianDecuples()27println(result.toList())28val list = listOf(1,2,3,4,5)Exhaustive.cartesianPairs
Using AI Code Generation
1val a = listOf(1, 2, 3, 4, 5)2val b = listOf("a", "b", "c")3val pairs = a.cartesianPairs(b)4pairs.forEach { println(it) }5val a = listOf(1, 2, 3, 4, 5)6val b = listOf("a", "b", "c")7val c = listOf("x", "y", "z")8val triples = a.cartesianTriples(b, c)9triples.forEach { println(it) }Exhaustive.cartesianPairs
Using AI Code Generation
1    val pair = Exhaustive.cartesianPairs(Exhaustive.ints(1..5), Exhaustive.ints(1..5))2    pair.forEach { println(it) }3    val triple = Exhaustive.cartesianTriples(Exhaustive.ints(1..5), Exhaustive.ints(1..5), Exhaustive.ints(1..5))4    triple.forEach { println(it) }5    val quadruple = Exhaustive.cartesianQuadruples(Exhaustive.ints(1..5), Exhaustive.ints(1..5), Exhaustive.ints(1..5), Exhaustive.ints(1..5))6    quadruple.forEach { println(it) }Exhaustive.cartesianPairs
Using AI Code Generation
1    fun `cartesian pairs`() {2        val pairs = Exhaustive.cartesianPairs(Exhaustive.ints(1..3), Exhaustive.ints(4..6))3        pairs.values shouldBe listOf(4                Pair(1, 4), Pair(1, 5), Pair(1, 6),5                Pair(2, 4), Pair(2, 5), Pair(2, 6),6                Pair(3, 4), Pair(3, 5), Pair(3, 6)7    }8    fun `cartesian triples`() {9        val triples = Exhaustive.cartesianTriples(Exhaustive.ints(1..2), Exhaustive.ints(3..4), Exhaustive.ints(5..6))10        triples.values shouldBe listOf(11                Triple(1, 3, 5), Triple(1, 3, 6),12                Triple(1, 4, 5), Triple(1, 4, 6),13                Triple(2, 3, 5), Triple(2, 3, 6),14                Triple(2, 4, 5), Triple(2, 4, 6)15    }16    fun `cartesian quadruples`() {17        val quadruples = Exhaustive.cartesianQuadruples(Exhaustive.ints(1..2), Exhaustive.ints(3..4), Exhaustive.ints(5..6), Exhaustive.ints(7..8))18        quadruples.values shouldBe listOf(19                Quadruple(1, 3, 5, 7), Quadruple(1, 3, 5, 8),20                Quadruple(1, 3, 6, 7), Quadruple(1, 3, 6, 8),21                Quadruple(1, 4, 5, 7), Quadruple(1, 4, 5, 8),22                Quadruple(1, 4, 6, 7), Quadruple(1, 4, 6,Exhaustive.cartesianPairs
Using AI Code Generation
1    val property = forAll(exhaustiveCartesianPairs(exhaustiveInts(), exhaustiveInts())) { (a, b) ->2    }3    property.check()4}5fun main() {6    val property = forAll(exhaustiveCartesianPairs(exhaustiveInts(), exhaustiveInts())) { (a, b) ->Exhaustive.cartesianPairs
Using AI Code Generation
1    val list1 = listOf("a","b","c")2    val list2 = listOf("1","2","3")3    val cartesianPairs = Exhaustive.cartesianPairs(list1,list2)4    cartesianPairs.forEach { println(it) }5    val cartesianTriples = Exhaustive.cartesianTriples(list1,list2,list1)6    cartesianTriples.forEach { println(it) }7    val cartesianQuadruples = Exhaustive.cartesianQuadruples(list1,list2,list1,list2)8    cartesianQuadruples.forEach { println(it) }9    val cartesianQuintuples = Exhaustive.cartesianQuintuples(list1,list2,list1,list2,list1)10    cartesianQuintuples.forEach { println(it) }11    val cartesianSextuples = Exhaustive.cartesianSextuples(list1,list2,list1,list2,list1,list2)12    cartesianSextuples.forEach { println(it) }13    val cartesianSeptuples = Exhaustive.cartesianSeptuples(list1,list2,list1,list2,list1,list2,list1)14    cartesianSeptuples.forEach { println(it) }15    val cartesianOctuples = Exhaustive.cartesianOctuples(list1,list2,list1,list2,list1,list2,list1,list2)16    cartesianOctuples.forEach { println(it) }Exhaustive.cartesianPairs
Using AI Code Generation
1    fun testExhaustiveCartesianPairs() {2        val list = listOf("A", "B", "C")3        val result = list.cartesianPairs()4        println(result)5    }6    fun testExhaustiveCartesianTriples() {7        val list = listOf("A", "B", "C")8        val result = list.cartesianTriples()9        println(result)10    }11    fun testExhaustiveCartesianQuadruples() {12        val list = listOf("A", "B", "C")13        val result = list.cartesianQuadruples()14        println(result)15    }16    fun testExhaustiveCartesianQuintuples() {17        val list = listOf("A", "B", "C")18        val result = list.cartesianQuintuples()19        println(result)20    }21    fun testExhaustiveCartesianSextuples() {22        val list = listOf("A", "B", "C")23        val result = list.cartesianSextuples()24        println(result)25    }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!!
