How to use Between class of io.kotest.matchers.floats package

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

FloatingBigRationalTest.kt

Source:FloatingBigRationalTest.kt Github

copy

Full Screen

1@file:Suppress("NonAsciiCharacters", "RedundantInnerClassModifier")2package hm.binkley.math.floating3import hm.binkley.math.BFixed4import hm.binkley.math.BFloating5import hm.binkley.math.`^`6import hm.binkley.math.big7import hm.binkley.math.ceil8import hm.binkley.math.compareTo9import hm.binkley.math.dec10import hm.binkley.math.divideAndRemainder11import hm.binkley.math.fixed.FixedBigRational12import hm.binkley.math.floating.FloatingBigRational.Companion.NEGATIVE_INFINITY13import hm.binkley.math.floating.FloatingBigRational.Companion.NaN14import hm.binkley.math.floating.FloatingBigRational.Companion.ONE15import hm.binkley.math.floating.FloatingBigRational.Companion.POSITIVE_INFINITY16import hm.binkley.math.floating.FloatingBigRational.Companion.TEN17import hm.binkley.math.floating.FloatingBigRational.Companion.TWO18import hm.binkley.math.floating.FloatingBigRational.Companion.ZERO19import hm.binkley.math.floor20import hm.binkley.math.inc21import hm.binkley.math.isDyadic22import hm.binkley.math.rangeTo23import hm.binkley.math.sqrt24import hm.binkley.math.sqrtApproximated25import hm.binkley.math.toBigInteger26import hm.binkley.math.truncate27import io.kotest.assertions.throwables.shouldThrow28import io.kotest.matchers.booleans.shouldBeFalse29import io.kotest.matchers.booleans.shouldBeTrue30import io.kotest.matchers.shouldBe31import io.kotest.matchers.shouldNotBe32import io.kotest.matchers.types.shouldBeSameInstanceAs33import org.junit.jupiter.api.Nested34import org.junit.jupiter.api.Test35private typealias BigRationalAssertion = (FloatingBigRational) -> Unit36/**37 * NB -- the tests use a mixture of constructors while testing functionality.38 * This is intentional, and raises coverage.39 */40internal class FloatingBigRationalTest {41    @Nested42    inner class ConstructionTests {43        @Test44        fun `should use constant NaN`() {45            (0 over 0) shouldBeSameInstanceAs NaN46        }47        @Test48        fun `should use constant +∞`() {49            (Long.MAX_VALUE over 0L) shouldBeSameInstanceAs POSITIVE_INFINITY50        }51        @Test52        fun `should use constant -∞`() {53            (Long.MIN_VALUE over 0.big) shouldBeSameInstanceAs NEGATIVE_INFINITY54        }55        @Test56        fun `should provide over as a convenience`() {57            (10L over 1) shouldBe TEN58            (10 over 1L) shouldBe TEN59        }60    }61    @Test62    fun `should not be a floating big rational`() {63        FixedBigRational.valueOf(1.big, 1.big).hashCode() shouldNotBe64            (1 over 1).hashCode()65        (FixedBigRational.ONE..FixedBigRational.TWO) shouldNotBe ONE..TWO66        (FixedBigRational.ONE..FixedBigRational.TWO).hashCode() shouldNotBe67            (ONE..TWO).hashCode()68    }69    @Test70    fun `should pretty print`() {71        POSITIVE_INFINITY.toString() shouldBe "Infinity"72        NEGATIVE_INFINITY.toString() shouldBe "-Infinity"73        NaN.toString() shouldBe "NaN"74    }75    @Test76    fun `should divide with remainder`() {77        fun nonFiniteCheck(78            dividend: FloatingBigRational,79            divisor: FloatingBigRational,80            assertion: BigRationalAssertion,81        ) {82            val (quotient, remainder) = dividend.divideAndRemainder(divisor)83            assertion(quotient)84            assertion(remainder)85        }86        nonFiniteCheck((13 over 2), POSITIVE_INFINITY) {87            it.isPositiveInfinity()88        }89        nonFiniteCheck(POSITIVE_INFINITY, (3 over 1)) {90            it.isPositiveInfinity()91        }92        nonFiniteCheck(POSITIVE_INFINITY, POSITIVE_INFINITY) {93            it.isPositiveInfinity()94        }95        nonFiniteCheck((13 over 2), NEGATIVE_INFINITY) {96            it.isNegativeInfinity()97        }98        nonFiniteCheck(NEGATIVE_INFINITY, (3 over 1)) {99            it.isNegativeInfinity()100        }101        nonFiniteCheck(NEGATIVE_INFINITY, NEGATIVE_INFINITY) {102            it.isPositiveInfinity()103        }104        nonFiniteCheck((13 over 2), NaN) {105            it.isNaN()106        }107        nonFiniteCheck(NaN, (3 over 1)) {108            it.isNaN()109        }110        nonFiniteCheck(NaN, NaN) {111            it.isNaN()112        }113    }114    @Nested115    inner class OperatorTests {116        @Test117        fun `should add`() {118            (ONE + POSITIVE_INFINITY).shouldBePositiveInfinity()119            (POSITIVE_INFINITY + POSITIVE_INFINITY).shouldBePositiveInfinity()120            (POSITIVE_INFINITY + NEGATIVE_INFINITY).shouldBeNaN()121            (ONE + NEGATIVE_INFINITY).shouldBeNegativeInfinity()122            (NEGATIVE_INFINITY + NEGATIVE_INFINITY).shouldBeNegativeInfinity()123            (NEGATIVE_INFINITY + POSITIVE_INFINITY).shouldBeNaN()124        }125        @Test126        fun `should subtract`() {127            (POSITIVE_INFINITY - ONE).shouldBePositiveInfinity()128            (POSITIVE_INFINITY - POSITIVE_INFINITY).shouldBeNaN()129            (POSITIVE_INFINITY - NEGATIVE_INFINITY).shouldBePositiveInfinity()130            (NEGATIVE_INFINITY - ONE).shouldBeNegativeInfinity()131            (NEGATIVE_INFINITY - NEGATIVE_INFINITY).shouldBeNaN()132            (NEGATIVE_INFINITY - POSITIVE_INFINITY).shouldBeNegativeInfinity()133        }134        @Test135        fun `should multiply`() {136            (ONE * POSITIVE_INFINITY).shouldBePositiveInfinity()137            (ONE * NEGATIVE_INFINITY).shouldBeNegativeInfinity()138            (ZERO * POSITIVE_INFINITY).shouldBeNaN()139            (POSITIVE_INFINITY * ZERO).shouldBeNaN()140            (ZERO * NEGATIVE_INFINITY).shouldBeNaN()141            (NEGATIVE_INFINITY * ZERO).shouldBeNaN()142            (POSITIVE_INFINITY * POSITIVE_INFINITY).shouldBePositiveInfinity()143        }144        @Test145        fun `should divide`() {146            (ZERO / POSITIVE_INFINITY) shouldBe ZERO147            (ONE / ZERO).shouldBePositiveInfinity()148            (ZERO / NEGATIVE_INFINITY) shouldBe ZERO149            (-ONE / ZERO).shouldBeNegativeInfinity()150            (NEGATIVE_INFINITY / NEGATIVE_INFINITY).shouldBeNaN()151            (POSITIVE_INFINITY / POSITIVE_INFINITY).shouldBeNaN()152            (ONE / NaN).shouldBeNaN()153            (NaN / ONE).shouldBeNaN()154            (ZERO / ZERO).shouldBeNaN()155        }156        @Test157        fun `should find remainder`() {158            (ONE % POSITIVE_INFINITY) shouldBe ZERO159            (POSITIVE_INFINITY % ONE) shouldBe ZERO160            (ONE % NEGATIVE_INFINITY) shouldBe ZERO161            (NEGATIVE_INFINITY % ONE) shouldBe ZERO162            (ONE % NaN).shouldBeNaN()163            (NaN % ONE).shouldBeNaN()164            (ZERO % ZERO).shouldBeNaN()165        }166        @Test167        fun `should increment`() {168            var nonFinite = POSITIVE_INFINITY169            (++nonFinite).shouldBePositiveInfinity()170            nonFinite = NEGATIVE_INFINITY171            (++nonFinite).shouldBeNegativeInfinity()172            nonFinite = NaN173            (++nonFinite).shouldBeNaN()174        }175        @Test176        fun `should decrement`() {177            var nonFinite = POSITIVE_INFINITY178            (--nonFinite).shouldBePositiveInfinity()179            nonFinite = NEGATIVE_INFINITY180            (--nonFinite).shouldBeNegativeInfinity()181            nonFinite = NaN182            (--nonFinite).shouldBeNaN()183        }184    }185    @Nested186    inner class RoundingTests {187        @Test188        fun `should round towards ceiling`() {189            POSITIVE_INFINITY.ceil().shouldBePositiveInfinity()190            NEGATIVE_INFINITY.ceil().shouldBeNegativeInfinity()191            NaN.ceil().shouldBeNaN()192        }193        @Test194        fun `should round towards floor`() {195            POSITIVE_INFINITY.floor().shouldBePositiveInfinity()196            NEGATIVE_INFINITY.floor().shouldBeNegativeInfinity()197            NaN.floor().shouldBeNaN()198        }199        @Test200        fun `should round towards 0`() {201            POSITIVE_INFINITY.truncate().shouldBePositiveInfinity()202            NEGATIVE_INFINITY.truncate().shouldBeNegativeInfinity()203            NaN.truncate().shouldBeNaN()204        }205    }206    @Nested207    inner class SpecialCasesTests {208        @Test209        fun `should round trip NaN and infinities`() {210            POSITIVE_INFINITY.toDouble().toBigRational() shouldBe211                POSITIVE_INFINITY212            NEGATIVE_INFINITY.toDouble().toBigRational() shouldBe213                NEGATIVE_INFINITY214            NaN.toDouble().toBigRational() shouldBe NaN215            POSITIVE_INFINITY.toFloat().toBigRational() shouldBe216                POSITIVE_INFINITY217            NEGATIVE_INFINITY.toFloat().toBigRational() shouldBe218                NEGATIVE_INFINITY219            NaN.toFloat().toBigRational() shouldBe NaN220        }221        @Test222        fun `should be infinity`() {223            (2 over 0).shouldBePositiveInfinity()224            (-2 over 0).shouldBeNegativeInfinity()225        }226        @Test227        fun `should check finitude`() {228            ZERO.isFinite().shouldBeTrue()229            POSITIVE_INFINITY.isFinite().shouldBeFalse()230            NEGATIVE_INFINITY.isFinite().shouldBeFalse()231            NaN.isFinite().shouldBeFalse()232        }233        @Test234        fun `should check infinitude`() {235            ZERO.isInfinite().shouldBeFalse()236            POSITIVE_INFINITY.isInfinite().shouldBeTrue()237            NEGATIVE_INFINITY.isInfinite().shouldBeTrue()238            NaN.isInfinite().shouldBeFalse()239        }240        @Test241        fun `should propagate NaN`() {242            (ZERO + NaN).shouldBeNaN()243            (NaN + NaN).shouldBeNaN()244            (NaN + ONE).shouldBeNaN()245            (NaN - ZERO).shouldBeNaN()246            (NaN - NaN).shouldBeNaN()247            (ZERO - NaN).shouldBeNaN()248            (ONE * NaN).shouldBeNaN()249            (NaN * NaN).shouldBeNaN()250            (NaN * ONE).shouldBeNaN()251            (NaN / ONE).shouldBeNaN()252            (NaN / NaN).shouldBeNaN()253            (ONE / NaN).shouldBeNaN()254            (NaN % ONE).shouldBeNaN()255            (NaN % NaN).shouldBeNaN()256            (ONE % NaN).shouldBeNaN()257        }258        @Test259        fun `should propagate infinities`() {260            (-NEGATIVE_INFINITY).shouldBePositiveInfinity()261            (ONE + POSITIVE_INFINITY).shouldBePositiveInfinity()262            (NEGATIVE_INFINITY - ONE).shouldBeNegativeInfinity()263            (POSITIVE_INFINITY + NEGATIVE_INFINITY).shouldBeNaN()264            (POSITIVE_INFINITY * POSITIVE_INFINITY).shouldBePositiveInfinity()265            (POSITIVE_INFINITY * NEGATIVE_INFINITY).shouldBeNegativeInfinity()266            (NEGATIVE_INFINITY * NEGATIVE_INFINITY).shouldBePositiveInfinity()267            (POSITIVE_INFINITY / POSITIVE_INFINITY).shouldBeNaN()268            (POSITIVE_INFINITY / NEGATIVE_INFINITY).shouldBeNaN()269            (NEGATIVE_INFINITY / NEGATIVE_INFINITY).shouldBeNaN()270        }271        @Test272        fun `should invert infinities incorrectly`() {273            (ONE / POSITIVE_INFINITY) shouldBe ZERO274            (ONE / NEGATIVE_INFINITY) shouldBe ZERO275        }276        @Test277        fun `should cope with various infinities`() {278            (ZERO * POSITIVE_INFINITY).shouldBeNaN()279            (ZERO / POSITIVE_INFINITY) shouldBe ZERO280            (POSITIVE_INFINITY / ZERO).shouldBePositiveInfinity()281            (ZERO * NEGATIVE_INFINITY).shouldBeNaN()282            (ZERO / NEGATIVE_INFINITY) shouldBe ZERO283            (NEGATIVE_INFINITY / ZERO).shouldBeNegativeInfinity()284            (POSITIVE_INFINITY * NEGATIVE_INFINITY).shouldBeNegativeInfinity()285            (POSITIVE_INFINITY / NEGATIVE_INFINITY).shouldBeNaN()286        }287        @Test288        fun `should understand equalities of non-finite values`() {289            POSITIVE_INFINITY shouldBe POSITIVE_INFINITY290            NEGATIVE_INFINITY shouldBe NEGATIVE_INFINITY291            // Cannot use shouldNotBe: It short-circuits for === objects292            @Suppress("KotlinConstantConditions")293            (NaN == NaN).shouldBeFalse()294        }295    }296    @Test297    fun `should note integer rationals`() {298        POSITIVE_INFINITY.isWhole().shouldBeFalse()299        NEGATIVE_INFINITY.isWhole().shouldBeFalse()300        NaN.isWhole().shouldBeFalse()301    }302    @Test303    fun `should note p-adic rationals`() {304        (1 over 3).isPAdic(3).shouldBeTrue()305        (2 over 5).isPAdic(3).shouldBeFalse()306        POSITIVE_INFINITY.isPAdic(3).shouldBeFalse()307        NEGATIVE_INFINITY.isPAdic(3).shouldBeFalse()308        NaN.isPAdic(3).shouldBeFalse()309    }310    @Test311    fun `should note dyadic rationals`() {312        (1 over 2).isDyadic().shouldBeTrue()313        POSITIVE_INFINITY.isDyadic().shouldBeFalse()314        NEGATIVE_INFINITY.isDyadic().shouldBeFalse()315        NaN.isDyadic().shouldBeFalse()316    }317    @Nested318    inner class ProgressionTests {319        @Test320        fun `should equate`() {321            (ONE..TEN).equals(this).shouldBeFalse()322            (ONE..TEN) shouldNotBe323                FixedBigRational.ONE..FixedBigRational.TEN324            (ONE..TEN).hashCode() shouldNotBe325                (FixedBigRational.ONE..FixedBigRational.TEN).hashCode()326        }327    }328    @Nested329    inner class ConversionTests {330        @Test331        fun `should be a number`() {332            ONE.toDouble() shouldBe 1.0333            POSITIVE_INFINITY.toDouble() shouldBe Double.POSITIVE_INFINITY334            NEGATIVE_INFINITY.toDouble() shouldBe Double.NEGATIVE_INFINITY335            NaN.toDouble() shouldBe Double.NaN336            ONE.toFloat() shouldBe 1.0f337            POSITIVE_INFINITY.toFloat() shouldBe Float.POSITIVE_INFINITY338            NEGATIVE_INFINITY.toFloat() shouldBe Float.NEGATIVE_INFINITY339            NaN.toFloat() shouldBe Float.NaN340        }341        @Test342        fun `should not convert extrema to big decimal`() {343            // Note JDK rules for BigDecimal->Double->BigDecimal, and handling344            // of string _vs_ double/long inputs345            ONE.toBigDecimal() shouldBe BFloating("1.0")346            ONE.toBigDecimal(1) shouldBe BFloating("1.0")347            shouldThrow<ArithmeticException> {348                POSITIVE_INFINITY.toBigDecimal()349            }350            shouldThrow<ArithmeticException> {351                POSITIVE_INFINITY.toBigDecimal(1)352            }353            shouldThrow<ArithmeticException> {354                NEGATIVE_INFINITY.toBigDecimal()355            }356            shouldThrow<ArithmeticException> {357                NEGATIVE_INFINITY.toBigDecimal(1)358            }359            shouldThrow<ArithmeticException> {360                NaN.toBigDecimal()361            }362            shouldThrow<ArithmeticException> {363                NaN.toBigDecimal(1)364            }365        }366        @Test367        fun `should not convert extrema to big integer`() {368            ONE.toBigInteger() shouldBe BFixed.ONE369            shouldThrow<ArithmeticException> {370                POSITIVE_INFINITY.toBigInteger()371            }372            shouldThrow<ArithmeticException> {373                NEGATIVE_INFINITY.toBigInteger()374            }375            shouldThrow<ArithmeticException> {376                NaN.toBigInteger()377            }378        }379        @Test380        fun `should not convert extrema to long`() {381            shouldThrow<ArithmeticException> {382                POSITIVE_INFINITY.toLong()383            }384            shouldThrow<ArithmeticException> {385                NEGATIVE_INFINITY.toLong()386            }387            shouldThrow<ArithmeticException> {388                NaN.toLong()389            }390        }391        @Test392        fun `should not convert extrema to int`() {393            shouldThrow<ArithmeticException> {394                POSITIVE_INFINITY.toInt()395            }396            shouldThrow<ArithmeticException> {397                NEGATIVE_INFINITY.toInt()398            }399            shouldThrow<ArithmeticException> {400                NaN.toInt()401            }402        }403        @Test404        fun `should convert big decimal in infix constructor`() {405            0.0.big.toBigRational() shouldBe ZERO406            30.0.big.toBigRational() shouldBe (30 over 1)407            3.0.big.toBigRational() shouldBe (3 over 1)408            BFloating("0.3").toBigRational() shouldBe (3 over 10)409            BFloating("7.70").toBigRational() shouldBe (77 over 10)410            (1.0.big over 1.0.big) shouldBe ONE411            (1.big over 1.0.big) shouldBe ONE412            (1L over 1.0.big) shouldBe ONE413            (1 over 1.0.big) shouldBe ONE414            (1.0 over 1.0.big) shouldBe ONE415            (1.0f over 1.0.big) shouldBe ONE416            (1.0.big over 1L) shouldBe ONE417            (1.0.big over 1) shouldBe ONE418        }419        @Test420        fun `should convert big integer in infix constructor`() {421            0.big.toBigRational() shouldBe ZERO422            BFixed.valueOf(30L).toBigRational() shouldBe (30 over 1)423            3.big.toBigRational() shouldBe (3 over 1)424            (1.big over 1.big) shouldBe ONE425            (1.0.big over 1.big) shouldBe ONE426            (1L over 1.big) shouldBe ONE427            (1 over 1.big) shouldBe ONE428            (1.0 over 1.big) shouldBe ONE429            (1.0f over 1.big) shouldBe ONE430            (1.big over 1L) shouldBe ONE431            (1.big over 1) shouldBe ONE432        }433        @Test434        fun `should convert double in infix constructor`() {435            (1.0.big over 1.0) shouldBe ONE436            (1.big over 1.0) shouldBe ONE437            (1L over 1.0) shouldBe ONE438            (1 over 1.0) shouldBe ONE439            (1.0 over 1.0) shouldBe ONE440            (1.0f over 1.0) shouldBe ONE441            (1.0 over 1.big) shouldBe ONE442            (1.0 over 1L) shouldBe ONE443            (1.0 over 1) shouldBe ONE444        }445        @Test446        fun `should convert float in infix constructor`() {447            (1.0.big over 1.0f) shouldBe ONE448            (1.big over 1.0f) shouldBe ONE449            (1L over 1.0f) shouldBe ONE450            (1 over 1.0f) shouldBe ONE451            (1.0 over 1.0f) shouldBe ONE452            (1.0f over 1.0f) shouldBe ONE453            (1.0f over 1.big) shouldBe ONE454            (1.0f over 1L) shouldBe ONE455            (1.0f over 1) shouldBe ONE456        }457        @Test458        fun `should convert from double`() {459            val doubles = listOf(460                -4.0,461                -3.0,462                -2.0,463                -1.0,464                -0.5,465                -0.3,466                -0.1,467                0.0,468                0.1,469                0.3,470                0.5,471                1.0,472                2.0,473                3.0,474                4.0,475                10.0,476                123.456477            )478            val rationals = listOf(479                -4 over 1,480                -3 over 1,481                -2 over 1,482                -1 over 1,483                -1 over 2,484                -3 over 10,485                -1 over 10,486                ZERO,487                1 over 10,488                3 over 10,489                1 over 2,490                ONE,491                TWO,492                3 over 1,493                4 over 1,494                TEN,495                15432 over 125496            )497            Double.POSITIVE_INFINITY.toBigRational().shouldBePositiveInfinity()498            Double.NEGATIVE_INFINITY.toBigRational().shouldBeNegativeInfinity()499            Double.NaN.toBigRational().shouldBeNaN()500            (-0.0).toBigRational() shouldBe ZERO501            doubles.map {502                it.toBigRational()503            } shouldBe rationals504            rationals.map {505                it.toDouble()506            } shouldBe doubles507        }508        @Test509        fun `should convert from float`() {510            val floats = listOf(511                -4.0f,512                -3.0f,513                -2.0f,514                -1.0f,515                -0.5f,516                -0.3f,517                -0.1f,518                0.0f,519                0.1f,520                0.3f,521                0.5f,522                1.0f,523                2.0f,524                3.0f,525                4.0f,526                10.0f,527                123.456f528            )529            val rationals = listOf(530                -4 over 1,531                -3 over 1,532                -2 over 1,533                -1 over 1,534                -1 over 2,535                -3 over 10,536                -1 over 10,537                ZERO,538                1 over 10,539                3 over 10,540                1 over 2,541                ONE,542                TWO,543                3 over 1,544                4 over 1,545                TEN,546                15432 over 125547            )548            Float.POSITIVE_INFINITY.toBigRational().shouldBePositiveInfinity()549            Float.NEGATIVE_INFINITY.toBigRational().shouldBeNegativeInfinity()550            Float.NaN.toBigRational().shouldBeNaN()551            (-0.0f).toBigRational() shouldBe ZERO552            floats.map {553                it.toBigRational()554            } shouldBe rationals555            rationals.map {556                it.toFloat()557            } shouldBe floats558        }559        @Test560        fun `should convert to fixed equivalent or complain`() {561            ONE.toFixedBigRational() shouldBe FixedBigRational.ONE562            shouldThrow<ArithmeticException> {563                NaN.toFixedBigRational()564            }565            shouldThrow<ArithmeticException> {566                POSITIVE_INFINITY.toFixedBigRational()567            }568            shouldThrow<ArithmeticException> {569                NEGATIVE_INFINITY.toFixedBigRational()570            }571        }572    }573    @Nested574    inner class FunctionTests {575        @Test576        fun `should sort like double`() {577            val sorted = listOf(578                POSITIVE_INFINITY,579                NaN,580                ZERO,581                POSITIVE_INFINITY,582                NaN,583                NEGATIVE_INFINITY,584                ZERO,585                NEGATIVE_INFINITY586            ).sorted()587            val doubleSorted = listOf(588                Double.POSITIVE_INFINITY,589                Double.NaN,590                0.0,591                Double.POSITIVE_INFINITY,592                Double.NaN,593                Double.NEGATIVE_INFINITY,594                0.0,595                Double.NEGATIVE_INFINITY596            ).sorted()597            (sorted.map { it.toDouble() }) shouldBe doubleSorted598        }599        @Test600        fun `should compare other number types`() {601            (Double.POSITIVE_INFINITY > ZERO).shouldBeTrue()602            (ZERO > Double.NEGATIVE_INFINITY).shouldBeTrue()603            (Float.NaN > ZERO).shouldBeTrue()604            (NaN > Float.MAX_VALUE).shouldBeTrue()605        }606        @Test607        fun `should not order NaN values`() {608            @Suppress("KotlinConstantConditions")609            (NaN == NaN).shouldBeFalse()610            (NaN > NaN).shouldBeFalse()611            (NaN < NaN).shouldBeFalse()612        }613        @Test614        fun `should reciprocate`() {615            POSITIVE_INFINITY.unaryDiv() shouldBe ZERO616            NEGATIVE_INFINITY.unaryDiv() shouldBe ZERO617            NaN.unaryDiv().shouldBeNaN()618        }619        @Test620        fun `should absolute`() {621            NaN.absoluteValue.shouldBeNaN()622            POSITIVE_INFINITY.absoluteValue623                .shouldBePositiveInfinity()624            NEGATIVE_INFINITY.absoluteValue625                .shouldBePositiveInfinity()626        }627        @Test628        fun `should signum`() {629            ZERO.sign shouldBe ZERO630            NEGATIVE_INFINITY.sign shouldBe -ONE631            POSITIVE_INFINITY.sign shouldBe ONE632            NaN.sign.shouldBeNaN()633        }634        @Test635        fun `should raise`() {636            POSITIVE_INFINITY `^` 2 shouldBe POSITIVE_INFINITY637            POSITIVE_INFINITY `^` -1 shouldBe ZERO638            NEGATIVE_INFINITY `^` 3 shouldBe NEGATIVE_INFINITY639            NEGATIVE_INFINITY `^` 2 shouldBe POSITIVE_INFINITY640            NEGATIVE_INFINITY `^` -1 shouldBe ZERO641            (NaN `^` 2).shouldBeNaN()642            (POSITIVE_INFINITY `^` 0).shouldBeNaN()643            (NEGATIVE_INFINITY `^` 0).shouldBeNaN()644        }645        @Test646        fun `should square root`() {647            NaN.sqrt().shouldBeNaN()648            POSITIVE_INFINITY.sqrt() shouldBe POSITIVE_INFINITY649        }650        @Test651        fun `should square root approximately`() {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 ONE671            ZERO.mediant(POSITIVE_INFINITY) shouldBe ONE672            ZERO.mediant(NEGATIVE_INFINITY) shouldBe -ONE673            NEGATIVE_INFINITY.mediant(ZERO) shouldBe -ONE674        }675        @Test676        fun `should find continued fraction`() {677            val cfA = (3245 over 1000).toContinuedFraction()678            cfA.isFinite().shouldBeTrue()679            val negCfA = (-3245 over 1000).toContinuedFraction()680            negCfA.isFinite().shouldBeTrue()681            val cfNaN = NaN.toContinuedFraction()682            cfNaN.isFinite().shouldBeFalse()683            cfNaN.toBigRational().shouldBeNaN()684            cfNaN.integerPart.shouldBeNaN()685            val cfPosInf = POSITIVE_INFINITY.toContinuedFraction()686            cfPosInf.isFinite().shouldBeFalse()687            cfPosInf.toBigRational().shouldBeNaN()688            cfPosInf.integerPart.shouldBeNaN()689            val cfNegInf = NEGATIVE_INFINITY.toContinuedFraction()690            cfNegInf.isFinite().shouldBeFalse()691            cfNegInf.toBigRational().shouldBeNaN()692            cfNegInf.integerPart.shouldBeNaN()693        }694    }695}696private fun FloatingBigRational.shouldBePositiveInfinity() =697    isPositiveInfinity().shouldBeTrue()698private fun FloatingBigRational.shouldBeNegativeInfinity() =699    isNegativeInfinity().shouldBeTrue()700private fun FloatingBigRational.shouldBeNaN() = isNaN().shouldBeTrue()...

Full Screen

Full Screen

RulesTest.kt

Source:RulesTest.kt Github

copy

Full Screen

1/*2 * MIT License3 *4 * Copyright (c) 2021. Pela Cristian5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24package pcf.crskdev.inval.id25import io.kotest.assertions.throwables.shouldThrow26import io.kotest.core.spec.style.DescribeSpec27import io.kotest.matchers.shouldBe28import io.mockk.mockk29import java.math.BigDecimal30import java.math.BigInteger31import java.util.Calendar32import java.util.Date33import pcf.crskdev.inval.id.Rules.AssertFalse34import pcf.crskdev.inval.id.Rules.AssertTrue35import pcf.crskdev.inval.id.Rules.Digits36import pcf.crskdev.inval.id.Rules.DigitsInt37import pcf.crskdev.inval.id.Rules.DigitsStr38import pcf.crskdev.inval.id.Rules.Email39import pcf.crskdev.inval.id.Rules.Fractions40import pcf.crskdev.inval.id.Rules.FractionsStr41import pcf.crskdev.inval.id.Rules.Future42import pcf.crskdev.inval.id.Rules.FutureOrPresent43import pcf.crskdev.inval.id.Rules.IGNORE44import pcf.crskdev.inval.id.Rules.Integers45import pcf.crskdev.inval.id.Rules.IntegersStr46import pcf.crskdev.inval.id.Rules.Max47import pcf.crskdev.inval.id.Rules.Min48import pcf.crskdev.inval.id.Rules.MinMax49import pcf.crskdev.inval.id.Rules.Negative50import pcf.crskdev.inval.id.Rules.NegativeOrZero51import pcf.crskdev.inval.id.Rules.NotBlank52import pcf.crskdev.inval.id.Rules.NotEmpty53import pcf.crskdev.inval.id.Rules.Past54import pcf.crskdev.inval.id.Rules.PastOrPresent55import pcf.crskdev.inval.id.Rules.Positive56import pcf.crskdev.inval.id.Rules.PositiveOrZero57import pcf.crskdev.inval.id.Rules.Size58import pcf.crskdev.inval.id.Rules.scale59internal class RulesTest : DescribeSpec({60    describe("Asserts test") {61        it("should apply assert true") {62            (AssertTrue() validates false withId 1)().isFailure shouldBe true63            (AssertTrue() validates true withId 1)().isSuccess shouldBe true64        }65        it("should apply assert false") {66            (AssertFalse() validates true withId 1)().isFailure shouldBe true67            (AssertFalse() validates false withId 1)().isSuccess shouldBe true68        }69    }70    describe("Not Empty Tests") {71        it("should apply rule") {72            (NotEmpty<String>() validates "" withId 1)().isFailure shouldBe true73            (NotEmpty<Array<Any>>() validates emptyArray() withId 1)().isFailure shouldBe true74            (NotEmpty<Collection<Any>>() validates emptyList() withId 1)().isFailure shouldBe true75            (NotEmpty<Map<Any, Any>>() validates emptyMap() withId 1)().isFailure shouldBe true76            (NotEmpty<CharArray>() validates CharArray(0) withId 1)().isFailure shouldBe true77            (NotEmpty<IntArray>() validates IntArray(0) withId 1)().isFailure shouldBe true78            (NotEmpty<ByteArray>() validates ByteArray(0) withId 1)().isFailure shouldBe true79            (NotEmpty<ShortArray>() validates ShortArray(0) withId 1)().isFailure shouldBe true80            (NotEmpty<LongArray>() validates LongArray(0) withId 1)().isFailure shouldBe true81            (NotEmpty<FloatArray>() validates FloatArray(0) withId 1)().isFailure shouldBe true82            (NotEmpty<DoubleArray>() validates DoubleArray(0) withId 1)().isFailure shouldBe true83        }84        it("should throw type is not allowed") {85            shouldThrow<IllegalArgumentException> {86                (NotEmpty<Int>() validates 1 withId 1)()87            }88        }89    }90    describe("Not Blank Tests") {91        it("should apply rule") {92            (NotBlank() validates "   " withId 1)().isFailure shouldBe true93        }94    }95    describe("Min Tests") {96        it("should apply approximation to floats and doubles") {97            (Min(scale = 2.scale())(10.12f) validates 10.118654f withId 1)().isSuccess shouldBe true98            (Min()(10.0) validates 9.99999999 withId 1)().isSuccess shouldBe true99        }100        it("should apply to integer numbers") {101            (Min()(10) validates 9 withId 1)().isFailure shouldBe true102            (Min()(9) validates 9 withId 1)().isSuccess shouldBe true103            (Min()(10L) validates 9L withId 1)().isFailure shouldBe true104            (Min()(9L) validates 9L withId 1)().isSuccess shouldBe true105            (Min()(10.toShort()) validates 9.toShort() withId 1)().isFailure shouldBe true106            (Min()(9.toShort()) validates 9.toShort() withId 1)().isSuccess shouldBe true107            (Min()(10.toByte()) validates 9.toByte() withId 1)().isFailure shouldBe true108            (Min()(9.toByte()) validates 9.toByte() withId 1)().isSuccess shouldBe true109        }110        it("should apply to BigDecimal/BigInteger") {111            (Min()(BigInteger.TEN) validates BigInteger.valueOf(9) withId 1)().isFailure shouldBe true112            (Min()(BigDecimal.valueOf(1.0)) validates BigDecimal.valueOf(0.0) withId 1)().isFailure shouldBe true113        }114        it("should throw if Number type is not supported") {115            shouldThrow<IllegalArgumentException> {116                (Min()(mockk()) validates mockk() withId 1)()117            }118        }119        it("should have custom message on fail") {120            val fail = (Min { input, min -> "Input $input must be min $min" }(10) validates 9 withId 1)()121                .exceptionOrNull()!! as ValidationException122            fail.violations.first().message shouldBe "Input 9 must be min 10"123        }124    }125    describe("Max Tests") {126        it("should apply approximation to floats and doubles") {127            (Max(scale = 2.scale())(10.12f) validates 10.118654f withId 1)().isSuccess shouldBe true128            (Max()(10.0) validates 9.99999999 withId 1)().isSuccess shouldBe true129        }130        it("should apply to integer numbers") {131            (Max()(10) validates 19 withId 1)().isFailure shouldBe true132            (Max()(9) validates 9 withId 1)().isSuccess shouldBe true133            (Max()(10L) validates 19L withId 1)().isFailure shouldBe true134            (Max()(9L) validates 9L withId 1)().isSuccess shouldBe true135            (Max()(10.toShort()) validates 19.toShort() withId 1)().isFailure shouldBe true136            (Max()(9.toShort()) validates 9.toShort() withId 1)().isSuccess shouldBe true137            (Max()(10.toByte()) validates 19.toByte() withId 1)().isFailure shouldBe true138            (Max()(9.toByte()) validates 9.toByte() withId 1)().isSuccess shouldBe true139        }140        it("should apply to BigDecimal/BigInteger") {141            (Max()(BigInteger.TEN) validates BigInteger.valueOf(19) withId 1)().isFailure shouldBe true142            (Max()(BigDecimal.valueOf(0.0)) validates BigDecimal.valueOf(1.0) withId 1)().isFailure shouldBe true143        }144        it("should throw if Number type is not supported") {145            shouldThrow<IllegalArgumentException> {146                (Max()(mockk()) validates mockk() withId 1)()147            }148        }149        it("should have custom message on fail") {150            val fail = (Max { input, max -> "Input $input must be max $max" }(10) validates 19 withId 1)()151                .exceptionOrNull()!! as ValidationException152            fail.violations.first().message shouldBe "Input 19 must be max 10"153        }154    }155    describe("Min Max tests") {156        it("should apply min max") {157            val minMax = MinMax()(10, 20)158            (minMax validates 15 withId 1)().isSuccess shouldBe true159            (minMax validates 10 withId 1)().isSuccess shouldBe true160            (minMax validates 20 withId 1)().isSuccess shouldBe true161            (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 true182        }183        it("should apply to string numbers") {184            (DigitsStr()(3, 2) validates "120.20" withId 1)().isSuccess shouldBe true185            (IntegersStr()(3) validates "120.25345350" withId 1)().isSuccess shouldBe true186            (FractionsStr()(3) validates "120534543.200" withId 1)().isSuccess shouldBe true187        }188        it("should ignore digits part") {189            (Digits()(IGNORE, 3) validates 43242353.233 withId 1)().isSuccess shouldBe true190            (Digits()(IGNORE, 3) validates 43242353.23 withId 1)().isFailure shouldBe true191            (Fractions()(3) validates 43242353.233 withId 1)().isSuccess shouldBe true192            (Fractions()(3) validates 43242353.23 withId 1)().isFailure shouldBe true193        }194        it("should ignore fractions part") {195            (Digits()(3, IGNORE) validates 432.23342342 withId 1)().isSuccess shouldBe true196            (Digits()(3, IGNORE) validates 43.2332324 withId 1)().isFailure shouldBe true197            (Integers()(3) validates 432.23342342 withId 1)().isSuccess shouldBe true198            (Integers()(3) validates 43.2332324 withId 1)().isFailure shouldBe true199        }200        it("should throw if both parts are ignored") {201            shouldThrow<IllegalArgumentException> {202                (Digits()(IGNORE, IGNORE) validates 43.2332324 withId 1)()203            }204        }205    }206    describe("Size tests") {207        it("should apply to string") {208            (Size<String>()(5, 10) validates "12345" withId 1)().isSuccess shouldBe true209            (Size<String>()(5, 10) validates "1234" withId 1)().isFailure shouldBe true210            (Size<String>()(5, 10) validates "12345678910" withId 1)().isFailure shouldBe true211        }212        it("should apply to array") {213            (Size<Array<*>>()(1, 3) validates arrayOf(1, 2, 3) withId 1)().isSuccess shouldBe true214            (Size<Array<*>>()(1, 3) validates emptyArray<Int>() withId 1)().isFailure shouldBe true215            (Size<Array<*>>()(1, 3) validates arrayOf(1, 2, 3, 4) withId 1)().isFailure shouldBe true216        }217        it("should apply to collection") {218            (Size<List<*>>()(1, 3) validates listOf(1, 2, 3) withId 1)().isSuccess shouldBe true219            (Size<List<*>>()(1, 3) validates emptyList<Int>() withId 1)().isFailure shouldBe true220            (Size<List<*>>()(1, 3) validates listOf(1, 2, 3, 4) withId 1)().isFailure shouldBe true221        }222        it("should apply to map") {223            (Size<Map<*, *>>()(1, 3) validates mapOf(1 to 1, 2 to 2, 3 to 3) withId 1)().isSuccess shouldBe true224            (Size<Map<*, *>>()(1, 3) validates emptyMap<Int, Int>() withId 1)().isFailure shouldBe true225            (Size<Map<*, *>>()(1, 3) validates mapOf(1 to 1, 2 to 2, 3 to 3, 4 to 4) withId 1)().isFailure shouldBe true226        }227        it("should throw when type not allowed") {228            shouldThrow<IllegalArgumentException> {229                (Size<Any>()(1, 3) validates Any() withId 1)()230            }231        }232    }233    describe("Pattern test") {234        it("should apply to regex pattern") {235            (Rules.Pattern()("\\d+") validates "12435" withId 1)().isSuccess shouldBe true236            (Rules.Pattern(RegexOption.IGNORE_CASE)("\\d+") validates "12435" withId 1)().isSuccess shouldBe true237            (Rules.Pattern(RegexOption.IGNORE_CASE, RegexOption.COMMENTS)("\\d+") validates "12435" withId 1)().isSuccess shouldBe true238            (Rules.Pattern()("\\d+") validates "12435f" withId 1)().isFailure shouldBe true239        }240    }241    describe("Email test") {242        val email = Email()243        it("should apply email regex and be valid") {244            val check: (CharSequence) -> Unit = {245                (email validates it withId 1)().isSuccess shouldBe true246            }247            check("email@example.com")248            check("firstname.lastname@example.com")249            check("email@subdomain.example.com")250            check("firstname+lastname@example.com")251            check("email@123.123.123.123")252            check("email@[123.123.123.123]")253            check("\"email\"@example.com")254            check("1234567890@example.com")255            check("email@example-one.com")256            check("_______@example.com")257            check("email@example.name")258            check("email@example.museum")259            check("email@example.co.jp")260            check("firstname-lastname@example.com")261            check("mailhost!username@example.org")262            check("\"john..doe\"@example.org")263            check("user%example.com@example.org")264        }265        it("should apply email regex and be invalid") {266            val check: (CharSequence) -> Unit = {267                (email validates it withId 1)().isFailure shouldBe true268            }269            check("#@%^%#\$@#\$@#.com")270            check("@example.com")271            check("Joe Smith <email@example.com>")272            check("email.example.com")273            check("email@example@example.com")274            check(".email@example.com")275            check("email.@example.com")276            check("email..email@example.com")277            check("あいうえお@example.com")278            check("email@example.com (Joe Smith)")279            check("email@example")280            check("email@-example.com")281            check("email@example..com")282            check("Abc..123@example.com")283            check("\"(),:;<>[\\]@example.com")284            check("just”not”right@example.com")285            check("this\\ is\"really\"not\\allowed@example.com")286            check("i_like_underscore@but_its_not_allowed_in_this_part.example.com")287            check("this is\"not\\allowed@example.com")288            check("a\"b(c)d,e:f;g<h>i[j\\k]l@example.com")289            check("this is\"not\\allowed@example.com")290            check("this\\ still\\\"not\\\\allowed@example.co")291            check("just\"not\"right@example.com")292            check("A@b@c@example.com")293            check("1234567890123456789012345678901234567890123456789012345678901234+x@example.com")294        }295    }296    describe("Positive tests") {297        it("should apply positive to number") {298            (Positive() validates 1 withId 1)().isSuccess shouldBe true299            (Positive() validates 1.0 withId 1)().isSuccess shouldBe true300            (Positive() validates 1.0f withId 1)().isSuccess shouldBe true301            (Positive() validates BigDecimal.ONE withId 1)().isSuccess shouldBe true302            (Positive() validates 0 withId 1)().isFailure shouldBe true303        }304        it("should apply positive or zero to number") {305            (PositiveOrZero() validates 0 withId 1)().isSuccess shouldBe true306            (PositiveOrZero() validates 0.0 withId 1)().isSuccess shouldBe true307            (PositiveOrZero() validates 0.0f withId 1)().isSuccess shouldBe true308            (PositiveOrZero() validates BigDecimal.ZERO withId 1)().isSuccess shouldBe true309            (PositiveOrZero() validates -1 withId 1)().isFailure shouldBe true310        }311    }312    describe("Negative tests") {313        it("should apply negative to number") {314            (Negative() validates -1 withId 1)().isSuccess shouldBe true315            (Negative() validates -1.0 withId 1)().isSuccess shouldBe true316            (Negative() validates -1.0f withId 1)().isSuccess shouldBe true317            (Negative() validates BigDecimal.valueOf(-1) withId 1)().isSuccess shouldBe true318            (Negative() validates 0 withId 1)().isFailure shouldBe true319        }320        it("should apply negative or zero to number") {321            (NegativeOrZero() validates 0 withId 1)().isSuccess shouldBe true322            (NegativeOrZero() validates 0.0 withId 1)().isSuccess shouldBe true323            (NegativeOrZero() validates 0.0f withId 1)().isSuccess shouldBe true324            (NegativeOrZero() validates BigDecimal.ZERO withId 1)().isSuccess shouldBe true325            (NegativeOrZero() validates 1 withId 1)().isFailure shouldBe true326        }327    }328    describe("Dates tests") {329        val now = Date()330        fun Date.add(field: Int, amount: Int): Date =331            Calendar.getInstance().run {332                add(field, amount)333                return time334            }335        it("should apply past rule date") {336            (Past({ now }) validates now.add(Calendar.DAY_OF_MONTH, -2) withId 1)().isSuccess shouldBe true337            (Past({ now }) validates now.add(Calendar.DAY_OF_MONTH, 2) withId 1)().isFailure shouldBe true338            (Past({ now }) validates now withId 1)().isFailure shouldBe true339        }340        it("should apply past or present rule date") {341            (PastOrPresent({ now }) validates now.add(Calendar.DAY_OF_MONTH, -2) withId 1)().isSuccess shouldBe true342            (PastOrPresent({ now }) validates now.add(Calendar.DAY_OF_MONTH, 2) withId 1)().isFailure shouldBe true343            (PastOrPresent({ now }) validates now withId 1)().isSuccess shouldBe true344        }345        it("should apply future rule date") {346            (Future({ now }) validates now.add(Calendar.DAY_OF_MONTH, 2) withId 1)().isSuccess shouldBe true347            (Future({ now }) validates now.add(Calendar.DAY_OF_MONTH, -2) withId 1)().isFailure shouldBe true348            (Future({ now }) validates now withId 1)().isFailure shouldBe true349        }350        it("should apply future or present rule date") {351            (FutureOrPresent({ now }) validates now.add(Calendar.DAY_OF_MONTH, 2) withId 1)().isSuccess shouldBe true352            (FutureOrPresent({ now }) validates now.add(Calendar.DAY_OF_MONTH, -2) withId 1)().isFailure shouldBe true353            (FutureOrPresent({ now }) validates now withId 1)().isSuccess shouldBe true354        }355    }356})...

Full Screen

Full Screen

Between.kt

Source:Between.kt Github

copy

Full Screen

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

Full Screen

Full Screen

IconGeneratorSpec.kt

Source:IconGeneratorSpec.kt Github

copy

Full Screen

...14 *  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 {...

Full Screen

Full Screen

Between

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.floats.Between2import io.kotest.matchers.ints.BeBetween3import io.kotest.matchers.longs.BeBetween4import io.kotest.matchers.shorts.BeBetween5import io.kotest.matchers.bytes.BeBetween6import io.kotest.matchers.chars.BeBetween7import io.kotest.matchers.doubles.BeBetween8import io.kotest.matchers.floats.BeBetween9import io.kotest.matchers.ints.BeBetween10import io.kotest.matchers.longs.BeBetween11import io.kotest.matchers.shorts.BeBetween12import io.kotest.matchers.bytes.BeBetween13import io.kotest.matchers.chars.BeBetween14import io.kotest.matchers.doubles.BeBetween15import io.kotest.matchers.floats.BeBetween16import io.kotest.matchers.ints.BeBetween17import io.kotest.matchers.longs.BeBetween

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