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

Best Kotest code snippet using io.kotest.matchers.floats.Between.Float.shouldBeBetween

Between.kt

Source:Between.kt Github

copy

Full Screen

1package io.kotest.matchers.floats2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.MatcherResult.Companion.invoke5import io.kotest.matchers.shouldBe6import io.kotest.matchers.shouldNotBe7import kotlin.math.abs8/**9 * Asserts that this [Float] is in the interval [[a]-[tolerance] , [b]+[tolerance]]10 *11 * Verifies that this [Float] is greater than or equal to ([a] - [tolerance]) and less than or equal to ([b] + [tolerance])12 *13 * Opposite of [Float.shouldNotBeBetween]14 *15 * ```16 * 0.5.shouldBeBetween(0.2, 0.7, 0.0) // Assertion passes17 * 0.5.shouldBeBetween(0.2, 0.3, 0.0) // Assertion fails18 * 0.5.shouldBeBetween(0.2, 0.3, 0.2) // Assertion passes19 * 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" })89 }90 return invoke(91 true,92 { "$value should be smaller than $b and bigger than $a" },93 { "$value should not be smaller than $b and should not be bigger than $a" })94 }95}...

Full Screen

Full Screen

IconGeneratorSpec.kt

Source:IconGeneratorSpec.kt Github

copy

Full Screen

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

Full Screen

Full Screen

IntegrationTest.kt

Source:IntegrationTest.kt Github

copy

Full Screen

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

Full Screen

Full Screen

Float.shouldBeBetween

Using AI Code Generation

copy

Full Screen

1Float.shouldBeBetween(1.0f, 10.0f)2Float.shouldBeBetween(1.0f, 10.0f, true)3Float.shouldBeBetween(1.0f, 10.0f, true, true)4Float.shouldBeBetween(1.0f, 10.0f, false, true)5Float.shouldBeBetween(1.0f, 10.0f, true, false)6Float.shouldBeBetween(1.0f, 10.0f, false, false)7Float.shouldBeBetween(1.0f, 10.0f, 1.0f)8Float.shouldBeBetween(1.0f, 10.0f, 1.0f, true)9Float.shouldBeBetween(1.0f, 10.0f, 1.0f, true, true)10Float.shouldBeBetween(1.0f, 10.0f, 1.0f, false, true)11Float.shouldBeBetween(1.0f, 10.0f, 1.0f, true, false)12Float.shouldBeBetween(1.0f, 10.0f, 1

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.

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!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful