Skip to content

Commit

Permalink
visualize another use case in example
Browse files Browse the repository at this point in the history
  • Loading branch information
bkosm committed Dec 9, 2022
1 parent b9ba285 commit 412c26a
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
@file:Suppress("PrivatePropertyName")

package com.bkosm.ktuple.examples

import com.bkosm.ktuple.Tuple
import com.bkosm.ktuple.examples.InMemoryPowerCalcTest.PowerCalc
import com.bkosm.ktuple.w
import org.junit.jupiter.api.DynamicTest.dynamicTest
import org.junit.jupiter.api.TestFactory
import kotlin.math.pow
import kotlin.test.expect

internal typealias Arguments = Tuple.Of4<Number, Number, Double, String>

internal class InMemoryPowerCalcTest {
fun interface PowerCalc : (Number, Double) -> Double {
companion object
}

private val PowerCalc.Companion.inMemory
get() = PowerCalc { a, b -> a.toDouble().pow(b) }
private val PowerCalc.Companion.InMemory get() = PowerCalc { a, b -> a.toDouble().pow(b) }

@TestFactory
fun `handles exponentiation as expected`() = listOf(
1 w 2 w 1.0 w "with exponent of 2",
2 w 2 w 4.0 w "with base of 2",
3 w 3 w 27.0 w "with exponents higher than 2",
2.2 w 3.3 w 13.489468760533386 w "with doubles",
Arguments(3, 3, 27.0, "with exponents higher than 2"), // can work like named tuples
).map { (a: Number, b: Number, result: Double, name: String) ->
dynamicTest(name) {
val uut = PowerCalc.inMemory
val uut = PowerCalc.InMemory
expect(result) { uut(a, b.toDouble()) }
}
}
Expand Down

0 comments on commit 412c26a

Please sign in to comment.