How to use between method of io.kotest.matchers.floats.Between class

Best Kotest code snippet using io.kotest.matchers.floats.Between.between

FloatingBigRationalTest.kt

Source:FloatingBigRationalTest.kt

`...652 NaN.sqrtApproximated().shouldBeNaN()653 POSITIVE_INFINITY.sqrtApproximated() shouldBe POSITIVE_INFINITY654 }655 @Test656 fun `should find between`() {657 NaN.mediant(NaN).shouldBeNaN()658 NaN.mediant(POSITIVE_INFINITY).shouldBeNaN()659 NaN.mediant(NEGATIVE_INFINITY).shouldBeNaN()660 POSITIVE_INFINITY.mediant(POSITIVE_INFINITY)661 .shouldBePositiveInfinity()662 NEGATIVE_INFINITY.mediant(NEGATIVE_INFINITY)663 .shouldBeNegativeInfinity()664 NaN.mediant(ZERO).shouldBeNaN()665 ZERO.mediant(NaN).shouldBeNaN()666 POSITIVE_INFINITY.mediant(NaN).shouldBeNaN()667 NEGATIVE_INFINITY.mediant(NaN).shouldBeNaN()668 POSITIVE_INFINITY.mediant(NEGATIVE_INFINITY) shouldBe ZERO669 NEGATIVE_INFINITY.mediant(POSITIVE_INFINITY) shouldBe ZERO670 POSITIVE_INFINITY.mediant(ZERO) shouldBe ONE...`

RulesTest.kt

Source:RulesTest.kt

`...161 (minMax validates 9 withId 1)().isFailure shouldBe true162 (minMax validates 29 withId 1)().isFailure shouldBe true163 }164 it("should have custom message") {165 val minMax = MinMax { input, min, max -> "Bad input value \$input. Must be between \$min and \$max" }166 val fail = (minMax(10, 20) validates 25 withId 1)().exceptionOrNull()!! as ValidationException167 fail.violations.first().message shouldBe "Bad input value 25. Must be between 10 and 20"168 }169 }170 describe("Digits tests") {171 it("should apply to int") {172 (Digits()(3, 0) validates 10 withId 1)().isFailure shouldBe true173 (Digits()(3, 0) validates 100 withId 1)().isSuccess shouldBe true174 (DigitsInt()(3) validates 120 withId 1)().isSuccess shouldBe true175 (DigitsInt()(3) validates 12 withId 1)().isFailure shouldBe true176 }177 it("should apply to floats/doubles") {178 (Digits()(3, 2) validates 103.22 withId 1)().isSuccess shouldBe true179 (Digits()(3, 2) validates 10.202f withId 1)().isFailure shouldBe true180 (Digits()(3, 2) validates 100.22f withId 1)().isSuccess shouldBe true181 (Digits()(1, 5) validates 1.12345f withId 1)().isSuccess shouldBe true...`

Between.kt

Source:Between.kt

`...19 * 0.5.shouldBeBetween(0.2, 0.3, 0.1) // Assertion fails20 * ```21 */22fun Float.shouldBeBetween(a: Float, b: Float, tolerance: Float): Float {23 this shouldBe between(a, b, tolerance)24 return this25}26/**27 * Asserts that this [Float] is NOT in the interval [[a]-[tolerance] , [b]+[tolerance]]28 *29 * Verifies that this [Float] is not:30 * - Greater than or equal to ([a] - [tolerance])31 * - Less than or equal to ([b] + [tolerance])32 *33 * If and only if both the assertions are true, which means that this [Float] is in the interval, this assertion fails.34 *35 * Opposite of [Float.shouldBeBetween]36 *37 *38 * ```39 * 0.5.shouldNotBeBetween(0.2, 0.7, 0.0) // Assertion fails40 * 0.5.shouldNotBeBetween(0.2, 0.3, 0.0) // Assertion passes41 * 0.5.shouldNotBeBetween(0.2, 0.3, 0.2) // Assertion fails42 * 0.5.shouldNotBeBetween(0.2, 0.3, 0.1) // Assertion passes43 * ```44 */45fun Float.shouldNotBeBetween(a: Float, b: Float, tolerance: Float): Float {46 this shouldNotBe between(a, b, tolerance)47 return this48}49/**50 * Matcher that matches floats and intervals51 *52 * Verifies that a specific [Float] is in the interval [[a] - [tolerance] , [b] + [tolerance]].53 *54 * For example:55 *56 * 0.5 is in the interval [0.4 , 0.6], because 0.4 <= 0.5 <= 0.6.57 *58 * This matcher also includes the bonds of the interval, so:59 *60 * 0.5 is in the interval [0.5, 0.6] because 0.5 <= 0.5 <= 0.6.61 *62 * The parameter [tolerance] is used to allow a slightly wider range, to include possible imprecision, and can be 0.0.63 *64 * 0.5 is in the interval [0.6, 0.7] when there's a tolerance of 0.1, because (0.6 - 0.1) <= 0.5 <= (0.7 + 0.1)65 *66 * ```67 * 0.5 shouldBe between(0.1, 1.0, 0.0) // Assertion passes68 * 0.5 shouldNotBe between(1.0, 2.0, 0.1) // Assertion passes69 * ```70 *71 * @see [Float.shouldBeBetween]72 * @see [Float.shouldNotBeBetween]73 */74fun between(a: Float, b: Float, tolerance: Float): Matcher<Float> = object : Matcher<Float> {75 override fun test(value: Float): MatcherResult {76 val differenceToMinimum = value - a77 val differenceToMaximum = b - value78 if (differenceToMinimum < 0 && abs(differenceToMinimum) > tolerance) {79 return invoke(80 false,81 { "\$value should be bigger than \$a" },82 { "\$value should not be bigger than \$a" })83 }84 if (differenceToMaximum < 0 && abs(differenceToMaximum) > tolerance) {85 return invoke(86 false,87 { "\$value should be smaller than \$b" },88 { "\$value should not be smaller than \$b" })...`

IconGeneratorSpec.kt

Source:IconGeneratorSpec.kt

`1/*2 * Copyright 2022 Thibault Seisel3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.github.thibseisel.identikon.rendering17import io.kotest.core.spec.style.DescribeSpec18import io.kotest.matchers.floats.shouldBeBetween19import io.kotest.matchers.floats.shouldBeExactly20import io.kotest.matchers.ints.shouldBeBetween21import kotlin.random.Random22internal class IconGeneratorSpec : DescribeSpec({23 describe("The default hue generation algorithm") {24 val generator = IconGenerator()25 it("always computes hue in [0, 1]") {26 val randomBytes = generateRandomBytes()27 val hue: Float = generator.computeHue(randomBytes)28 hue.shouldBeBetween(0f, 1f, 0f)29 }30 it("returns 0 when bytes are all zero") {31 val bytes = ByteArray(4)32 val hue = generator.computeHue(bytes)33 hue.shouldBeExactly(0f)34 }35 }36 describe("The default octet selector algorithm") {37 val generator = IconGenerator()38 it("always returns a value in [0, 255]") {39 val bytes = byteArrayOf(-0xf, -0x7, 0x0, 0x7, 0xf)40 repeat(5) { index ->41 val octet = generator.getOctet(bytes, index)42 octet.shouldBeBetween(0, 255)43 }44 }45 }46})47private fun generateRandomBytes(): ByteArray {48 val bytes = ByteArray(8)49 return Random.nextBytes(bytes)50}...`

IntegrationTest.kt

Source:IntegrationTest.kt

`1package dev.cyberdeck.lisp2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.floats.shouldBeBetween4import io.kotest.matchers.result.shouldBeSuccess5import io.kotest.matchers.shouldBe6import io.kotest.matchers.types.shouldBeInstanceOf7fun run(prog: String) = Result.runCatching {8 val tokens = tokenize(prog)9 val ast = readFromTokens(tokens)10 val env = standardEnv()11 eval(ast, env)12}13class IntegrationTest : StringSpec({14 "eval should work on complex expressions" {15 run("(begin (define r 10.0) (* pi (* r r)))").shouldBeSuccess {16 it.shouldBeInstanceOf<Num>().num.shouldBeInstanceOf<Float>().shouldBeBetween(314.1592f, 314.1593f, 0.0f)17 }18 }19 "head returns the first element" {20 run("(begin (head (quote (hello))))").shouldBeSuccess {21 it.shouldBe(Symbol("hello"))22 }23 }24 "tail returns the rest of the list" {25 run("(begin (tail (quote (hello from the evaluator))))").shouldBeSuccess {26 it.shouldBe(L(listOf(Symbol("from"), Symbol("the"), Symbol("evaluator"))))27 }28 }29 "cons creates a new list" {30 run("(begin (cons 1 (quote (2 3))))").shouldBeSuccess {31 it.shouldBe(L(listOf(Num(1), Num(2), Num(3))))32 }33 }34})...`

between

Using AI Code Generation

`1import io.kotest.matchers.ints.shouldBeBetween2fun main() {3 5.shouldBeBetween(1, 10)4 5.shouldBeBetween(1, 10, true)5 5.shouldBeBetween(1, 10, true, true)6 5.shouldBeBetween(1, 10, inclusiveStart = true, inclusiveEnd = true)7}8import io.kotest.matchers.longs.shouldBeBetween9fun main() {10 5L.shouldBeBetween(1L, 10L)11 5L.shouldBeBetween(1L, 10L, true)12 5L.shouldBeBetween(1L, 10L, true, true)13 5L.shouldBeBetween(1L, 10L, inclusiveStart = true, inclusiveEnd = true)14}15import io.kotest.matchers.shorts.shouldBeBetween16fun main() {17 5.toShort().shouldBeBetween(1.toShort(), 10.toShort())18 5.toShort().shouldBeBetween(1.toShort(), 10.toShort(), true)19 5.toShort().shouldBeBetween(1.toShort(), 10.toShort(), true, true)20 5.toShort().shouldBeBetween(1.toShort(), 10.toShort(), inclusiveStart = true, inclusiveEnd = true)21}22import io.kotest.matchers.bytes.shouldBeBetween23fun main() {24 5.toByte().shouldBeBetween(1.toByte(), 10.toByte())25 5.toByte().shouldBeBetween(1.toByte(), 10.toByte(), true)26 5.toByte().shouldBeBetween(1.toByte(), 10.toByte(), true, true)27 5.toByte().shouldBeBetween(1.toByte(), 10.toByte(), inclusiveStart = true, inclusiveEnd = true)28}29import io.kotest.matchers.doubles.shouldBeBetween30fun main() {31 5.0.shouldBeBetween(1.0, 10.0)`

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:

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

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!