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

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

FloatingBigRationalTest.kt

Source:FloatingBigRationalTest.kt Github

copy

Full Screen

...9import 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

FzyFloatEqualityTest.kt

Source:FzyFloatEqualityTest.kt Github

copy

Full Screen

...13class FzyFloatEqualityTest : FunSpec({14 beforeTest {15 }16 test("pathology") {17 Float.NaN.fzyEqual(1.0f) shouldBe false18 Float.NaN.fzyEqual(Float.NaN) shouldBe false19 Float.NaN.asFzyFloat().isZero() shouldBe false20 Float.POSITIVE_INFINITY.fzyEqual(Float.POSITIVE_INFINITY) shouldBe true21 Float.NEGATIVE_INFINITY.fzyEqual(Float.POSITIVE_INFINITY) shouldBe false22 Float.POSITIVE_INFINITY.fzyEqual(Float.NEGATIVE_INFINITY) shouldBe false23 Float.NEGATIVE_INFINITY.fzyEqual(Float.NEGATIVE_INFINITY) shouldBe true24 FzyFloat(Float.NaN).equals(Float.NaN) shouldBe false25 (FzyFloat(Float.NaN) == FzyFloat(Float.NaN)) shouldBe false26 FzyFloat(Float.POSITIVE_INFINITY).equals(Float.POSITIVE_INFINITY) shouldBe true27 FzyFloat(Float.NEGATIVE_INFINITY).equals(Float.POSITIVE_INFINITY) shouldBe false28 FzyFloat(Float.POSITIVE_INFINITY).equals(Float.NEGATIVE_INFINITY) shouldBe false29 FzyFloat(Float.NEGATIVE_INFINITY).equals(Float.NEGATIVE_INFINITY) shouldBe true30 Float.MAX_VALUE.fzyEqual(Float.MAX_VALUE) shouldBe true31 }32 test("zeroes") {33 val ff0 = FzyFloat(0.0f)34 val ff1 = FzyFloat(Float.MIN_VALUE)35 val ff2 = FzyFloat(-Float.MIN_VALUE)36 val ff3 = FzyFloat(1.0f)37 ff0.isZero() shouldBe true38 ff1.isZero() shouldBe true39 ff2.isZero() shouldBe true40 ff3.isZero() shouldBe false41 Float.MIN_VALUE.fzyIsZero() shouldBe true42 0.0f.fzyIsZero() shouldBe true43 (-Float.MIN_VALUE).fzyIsZero() shouldBe true44 1.0f.fzyIsZero() shouldBe false45 Float.NaN.fzyIsZero() shouldBe false46 Float.POSITIVE_INFINITY.fzyIsZero() shouldBe false47 Float.NEGATIVE_INFINITY.fzyIsZero() shouldBe false48 }49 test("unity") {50 val fd0 = FzyFloat(1.0f)51 val fd00 = FzyFloat(1.0f, floatEps, true)52 val fd1 = FzyFloat(1.0f+defaultFloatTol)53 val fd2 = FzyFloat(1.0f-defaultFloatTol)54 val fd3 = FzyFloat(1.0f+defaultFloatTol, 2.0f*defaultFloatTol)55 val fd4 = FzyFloat(1.0f-defaultFloatTol, 2.0f*defaultFloatTol)56 fd0.isUnity() shouldBe true57 fd00.isUnity() shouldBe true58 fd1.isUnity() shouldBe false59 fd2.isUnity() shouldBe false60 fd3.isUnity() shouldBe true61 fd4.isUnity() shouldBe true62 1.0f.fzyIsUnity() shouldBe true63 (1.0f+defaultFloatTol).fzyIsUnity() shouldBe false64 (1.0f-defaultFloatTol).fzyIsUnity() shouldBe false65 Float.NaN.fzyIsUnity() shouldBe false66 Float.NEGATIVE_INFINITY.fzyIsUnity() shouldBe false67 Float.POSITIVE_INFINITY.fzyIsUnity() shouldBe false68 }69 test("throw if defeated"){70 shouldThrow<IllegalArgumentException> {71 FzyFloat(1.0f, floatEps)72 }73 shouldThrow<IllegalArgumentException> {74 FzyFloat(1.0f, -0.0004f)75 }76 }77 test("bounds") {78 (FzyFloat(Float.MAX_VALUE) == FzyFloat(Float.MAX_VALUE)) shouldBe true79 (FzyFloat(-Float.MAX_VALUE) == FzyFloat(-Float.MAX_VALUE)) shouldBe true80 FzyFloat(Float.MAX_VALUE).equals(Float.MAX_VALUE) shouldBe true81 FzyFloat(-Float.MAX_VALUE).equals(-Float.MAX_VALUE) shouldBe true82 (FzyFloat(Float.MIN_VALUE) == FzyFloat(Float.MIN_VALUE)) shouldBe true83 (FzyFloat(Float.MIN_VALUE) == FzyFloat(-Float.MIN_VALUE)) shouldBe true84 (FzyFloat(-Float.MIN_VALUE) == FzyFloat(-Float.MIN_VALUE)) shouldBe true85 FzyFloat(Float.MIN_VALUE).equals(Float.MIN_VALUE) shouldBe true86 FzyFloat(Float.MIN_VALUE).equals(-Float.MIN_VALUE) shouldBe true87 FzyFloat(-Float.MIN_VALUE).equals(-Float.MIN_VALUE) shouldBe true88 Float.MAX_VALUE.fzyEqual(Float.MAX_VALUE) shouldBe true89 (-Float.MAX_VALUE).fzyEqual(-Float.MAX_VALUE) shouldBe true90 Float.MAX_VALUE.fzyEqual(FzyFloat(Float.MAX_VALUE)) shouldBe true91 (-Float.MAX_VALUE).fzyEqual(FzyFloat(-Float.MAX_VALUE)) shouldBe true92 Float.MIN_VALUE.fzyEqual(Float.MIN_VALUE) shouldBe true93 Float.MIN_VALUE.fzyEqual(-Float.MIN_VALUE) shouldBe true94 (-Float.MIN_VALUE).fzyEqual(-Float.MIN_VALUE) shouldBe true95 Float.MIN_VALUE.fzyEqual(FzyFloat(Float.MIN_VALUE)) shouldBe true96 Float.MIN_VALUE.fzyEqual(FzyFloat(-Float.MIN_VALUE)) shouldBe true97 (-Float.MIN_VALUE).fzyEqual(FzyFloat(-Float.MIN_VALUE)) shouldBe true98 }99 test("underflow") {100 (FzyFloat(Float.MAX_VALUE) == FzyFloat(1E-10f)) shouldBe false101 (FzyFloat(-Float.MAX_VALUE) == FzyFloat(-1E-10f)) shouldBe false102 FzyFloat(Float.MAX_VALUE).equals(1E-10f) shouldBe false103 FzyFloat(-Float.MAX_VALUE).equals(-1E-10f) shouldBe false104 }105 test("overflow") {106 (FzyFloat(1E-10f) == FzyFloat(Float.MAX_VALUE)) shouldBe false107 (FzyFloat(-1E-10f) == FzyFloat(-Float.MAX_VALUE)) shouldBe false108 FzyFloat(1E-10f).equals(Float.MAX_VALUE) shouldBe false109 FzyFloat(-1E-10f).equals(-Float.MAX_VALUE) shouldBe false110 }111 test("same zeroes") {112 val fd0a = FzyFloat.zero()113 val fd0b = FzyFloat.zero()114 val fd00 = FzyFloat.zero(defaultFloatTol*2.0f)115 val fd1 = FzyFloat.unity()116 FzyFloatEquality.isSameZeroes(fd0a, fd0a) shouldBe true117 FzyFloatEquality.isSameZeroes(fd0a, fd0b) shouldBe true118 FzyFloatEquality.isSameZeroes(fd0b, fd0a) shouldBe true119 FzyFloatEquality.isSameZeroes(fd0a, fd00) shouldBe false120 FzyFloatEquality.isSameZeroes(fd00, fd0a) shouldBe false121 FzyFloatEquality.isSameZeroes(fd0a, fd1) shouldBe false122 FzyFloatEquality.isSameZeroes(fd1, fd0a) shouldBe false123 }124 test("float properties, reflexive") {125 checkAll(Arb.float(), Arb.numericFloats()) { f1, f2 ->126 val ff1a = FzyFloat(f1)127 val ff1b = FzyFloat(f1)128 val ff2a = FzyFloat(f2)129 val ff2b = FzyFloat(f2)130 ff1a.equals(f1) shouldBe !f1.isNaN()131 ff1b.equals(f1) shouldBe !f1.isNaN()132 ff2a.equals(f2) shouldBe true133 ff2b.equals(f2) shouldBe true134 f1.fzyEqual(f1) shouldBe !f1.isNaN()135 f1.fzyEqual(ff1a) shouldBe !f1.isNaN()136 f1.fzyEqual(ff1b) shouldBe !f1.isNaN()137 ff1a.equals(f1) shouldBe !f1.isNaN()138 ff1b.equals(f1) shouldBe !f1.isNaN()139 (ff1a == ff1b) shouldBe !f1.isNaN()140 (ff1b == ff1a) shouldBe !f1.isNaN()141 ff2a.equals(f2) shouldBe true142 ff2b.equals(f2) shouldBe true143 (ff2a == ff2b) shouldBe true144 (ff2b == ff2a) shouldBe true145 }146 }147 test("float properties, symmetric") {148 checkAll(Arb.float(), Arb.numericFloats()) { f1, f2 ->149 val ff1 = FzyFloat(f1)150 val f1d = ff1.qty / (1.0f + (defaultFloatTol /2.0f))151 val ff1d = FzyFloat(f1d)152 val ff2 = FzyFloat(f2)153 val f2d = ff2.qty / (1.0f + (defaultFloatTol /2.0f))154 val ff2d = FzyFloat(f2d)155 (!ff1.equals(f1d) && f1.fzyIsZero()) shouldBe false156 (f1.fzyIsZero() && ff1 != ff1d) shouldBe false157 f1.fzyEqual(f1d) shouldBe !f1.isNaN()158 f1.fzyEqual(ff1d) shouldBe !f1.isNaN()159 f1d.fzyEqual(f1) shouldBe !f1.isNaN()160 f1d.fzyEqual(ff1) shouldBe !f1.isNaN()161 ff1.equals(f1d) shouldBe !f1.isNaN()162 (ff1d == ff1) shouldBe !f1.isNaN()163 ff1d.equals(f1) shouldBe !f1.isNaN()164 ff1.equals(f1d) shouldBe !f1.isNaN()165 (!ff2.equals(f2d) && f2.fzyIsZero()) shouldBe false166 (f2.fzyIsZero() && ff2 != ff2d) shouldBe false167 f2.fzyEqual(f2d) shouldBe true168 f2.fzyEqual(ff2d) shouldBe true169 f2d.fzyEqual(f2) shouldBe true170 f2d.fzyEqual(ff2) shouldBe true171 ff2.equals(f2d) shouldBe true172 (ff2d == ff2) shouldBe true173 ff2d.equals(f2) shouldBe true174 ff2.equals(f2d) shouldBe true175 }176 }177 test("float properties, transitive") {178 checkAll(Arb.float(), Arb.numericFloats()) { f1, f2 ->179 val ff1 = FzyFloat(f1)180 val f1a = f1 / (1.0f + (defaultFloatTol /1.7f))181 val ff1a = FzyFloat(f1a)182 val f1b = f1 / (1.0f + (defaultFloatTol /1.3f))183 val ff1b = FzyFloat(f1b)184 val ff2 = FzyFloat(f2)185 val f2a = f2 / (1.0f + (defaultFloatTol /1.9f))186 val ff2a = FzyFloat(f2a)187 val f2b = f2 / (1.0f + (defaultFloatTol /1.5f))188 val ff2b = FzyFloat(f2b)189 (!ff1.equals(f1a) && f1.fzyIsZero()) shouldBe false190 (f1.fzyIsZero() && ff1 != ff1a) shouldBe false191 (!ff1.equals(f1b) && f1.fzyIsZero()) shouldBe false192 (f1.fzyIsZero() && ff1 != ff1b) shouldBe false193 // f1 == f1a, f1a == f1b => f1 == f1b194 f1.fzyEqual(f1a) shouldBe !f1.isNaN()195 f1.fzyEqual(ff1a) shouldBe !f1.isNaN()196 f1a.fzyEqual(f1b) shouldBe !f1.isNaN()197 f1a.fzyEqual(ff1b) shouldBe !f1.isNaN()198 f1b.fzyEqual(f1) shouldBe !f1.isNaN()199 f1b.fzyEqual(ff1) shouldBe !f1.isNaN()200 ff1.equals(f1a) shouldBe !f1.isNaN()201 (ff1 == ff1a) shouldBe !f1.isNaN()202 ff1a.equals(f1b) shouldBe !f1.isNaN()203 (ff1a == ff1b) shouldBe !f1.isNaN()204 ff1b.equals(f1) shouldBe !f1.isNaN()205 (ff1b == ff1) shouldBe !f1.isNaN()206 (!ff2.equals(f2a) && f2.fzyIsZero()) shouldBe false207 (f2.fzyIsZero() && ff2 != ff2a) shouldBe false208 (!ff2.equals(f2b) && f2.fzyIsZero()) shouldBe false209 (f2.fzyIsZero() && ff2 != ff2b) shouldBe false210 // f2 == f2a, f2a == f2b => f2 == f2b211 f2.fzyEqual(f2a) shouldBe true212 f2.fzyEqual(ff2a) shouldBe true213 f2a.fzyEqual(f2b) shouldBe true214 f2a.fzyEqual(ff2b) shouldBe true215 f2b.fzyEqual(f2) shouldBe true216 f2b.fzyEqual(ff2) shouldBe true217 ff2.equals(f2a) shouldBe true218 (ff2 == ff2a) shouldBe true219 ff2a.equals(f2b) shouldBe true...

Full Screen

Full Screen

FixedBigRationalTest.kt

Source:FixedBigRationalTest.kt Github

copy

Full Screen

...84 }85 @Test86 fun `should not convert non-finite doubles`() {87 shouldThrow<ArithmeticException> {88 valueOf(Double.NaN)89 }90 shouldThrow<ArithmeticException> {91 valueOf(Double.POSITIVE_INFINITY)92 }93 shouldThrow<ArithmeticException> {94 valueOf(Double.NEGATIVE_INFINITY)95 }96 }97 @Test98 fun `should convert float in infix constructor`() {99 (1.0.big over 1.0f) shouldBe ONE100 (1.big over 1.0f) shouldBe ONE101 (1L over 1.0f) shouldBe ONE102 (1 over 1.0f) shouldBe ONE103 (1.0 over 1.0f) shouldBe ONE104 (1.0f over 1.0f) shouldBe ONE105 (1.0f over 1.big) shouldBe ONE106 (1.0f over 1L) shouldBe ONE107 (1.0f over 1) shouldBe ONE108 }109 @Test110 fun `should convert integral types in infix constructor`() {111 (1L over 1) shouldBe ONE112 (1 over 1L) shouldBe ONE113 }114 @Test115 fun `should convert to and from big decimal`() {116 val decimals = listOf(117 -4.0,118 -3.0,119 -2.0,120 -1.0,121 -0.5,122 -0.3,123 -0.1,124 0.0,125 0.1,126 0.3,127 0.5,128 1.0,129 2.0,130 3.0,131 4.0,132 123.456133 ).map { it.toBigDecimal() }134 val rationals = listOf(135 -4 over 1,136 -3 over 1,137 -2 over 1,138 -1 over 1,139 -1 over 2,140 -3 over 10,141 -1 over 10,142 ZERO,143 1 over 10,144 3 over 10,145 1 over 2,146 ONE,147 2 over 1,148 3 over 1,149 4 over 1,150 15432 over 125151 )152 decimals.map {153 it.toBigRational()154 } shouldBe rationals155 rationals.map {156 it.toBigDecimal()157 } shouldBe decimals158 }159 @Test160 fun `should convert to and from double`() {161 val doubles = listOf(162 -4.0,163 -3.0,164 -2.0,165 -1.0,166 -0.5,167 -0.3,168 -0.1,169 0.0,170 0.1,171 0.3,172 0.5,173 1.0,174 2.0,175 3.0,176 4.0,177 123.456178 )179 val rationals = listOf(180 -4 over 1,181 -3 over 1,182 -2 over 1,183 -1 over 1,184 -1 over 2,185 -3 over 10,186 -1 over 10,187 ZERO,188 1 over 10,189 3 over 10,190 1 over 2,191 ONE,192 2 over 1,193 3 over 1,194 4 over 1,195 15432 over 125196 )197 doubles.map {198 it.toBigRational()199 } shouldBe rationals200 rationals.map {201 it.toDouble()202 } shouldBe doubles203 }204 @Test205 fun `should convert to and from float`() {206 val floats = listOf(207 -4.0f,208 -3.0f,209 -2.0f,210 -1.0f,211 -0.5f,212 -0.3f,213 -0.1f,214 0.0f,215 0.1f,216 0.3f,217 0.5f,218 1.0f,219 2.0f,220 3.0f,221 4.0f,222 123.456f223 )224 val rationals = listOf(225 -4 over 1,226 -3 over 1,227 -2 over 1,228 -1 over 1,229 -1 over 2,230 -3 over 10,231 -1 over 10,232 ZERO,233 1 over 10,234 3 over 10,235 1 over 2,236 ONE,237 2 over 1,238 3 over 1,239 4 over 1,240 15432 over 125241 )242 floats.map {243 it.toBigRational()244 } shouldBe rationals245 rationals.map {246 it.toFloat()247 } shouldBe floats248 }249 @Test250 fun `should convert to floating equivalent`() {251 ONE.toFloatingBigRational() shouldBe FloatingBigRational.ONE252 }253 }254 @Nested255 inner class FunctionTests {256 @Test257 fun `should compare other number types`() {258 shouldThrow<ArithmeticException> {259 Double.POSITIVE_INFINITY > ZERO260 }261 shouldThrow<ArithmeticException> {262 ZERO < Double.POSITIVE_INFINITY263 }264 shouldThrow<ArithmeticException> {265 ZERO > Double.NEGATIVE_INFINITY266 }267 shouldThrow<ArithmeticException> {268 Double.NEGATIVE_INFINITY < ZERO269 }270 shouldThrow<ArithmeticException> {271 Float.NaN > ZERO272 }273 shouldThrow<ArithmeticException> {274 ZERO < Float.NaN275 }276 }277 @Test278 fun `should multiplicatively invert`() {279 shouldThrow<ArithmeticException> {280 ZERO.unaryDiv()281 }282 }283 }284}...

Full Screen

Full Screen

FloatMatchersTest.kt

Source:FloatMatchersTest.kt Github

copy

Full Screen

...81 1f shouldNotBeExactly -1f82 1f shouldNotBeExactly 1.000001f83 1f shouldNotBeExactly 0.999999f84 Float.MIN_VALUE shouldNotBeExactly Float.MAX_VALUE85 Float.MIN_VALUE shouldNotBeExactly Float.NaN86 Float.MIN_VALUE shouldNotBeExactly Float.POSITIVE_INFINITY87 Float.MIN_VALUE shouldNotBeExactly Float.NEGATIVE_INFINITY88 Float.MAX_VALUE shouldNotBeExactly Float.MIN_VALUE89 Float.MAX_VALUE shouldNotBeExactly Float.NaN90 Float.MAX_VALUE shouldNotBeExactly Float.POSITIVE_INFINITY91 Float.MAX_VALUE shouldNotBeExactly Float.NEGATIVE_INFINITY92 }93 "shouldBeZero" {94 0f.shouldBeZero()95 }96 "shouldNotBeZero" {97 0.000001f.shouldNotBeZero()98 (-0.000001f).shouldNotBeZero()99 Float.MIN_VALUE.shouldNotBeZero()100 Float.MAX_VALUE.shouldNotBeZero()101 Float.NaN.shouldNotBeZero()102 Float.POSITIVE_INFINITY.shouldNotBeZero()103 Float.NEGATIVE_INFINITY.shouldNotBeZero()104 }105 }106}...

Full Screen

Full Screen

FloatNaNTest.kt

Source:FloatNaNTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.floats2import com.sksamuel.kotest.matchers.doubles.numericFloats3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.FunSpec5import io.kotest.matchers.floats.beNaN6import io.kotest.matchers.floats.shouldBeNaN7import io.kotest.matchers.floats.shouldNotBeNaN8import io.kotest.matchers.should9import io.kotest.matchers.shouldBe10import io.kotest.matchers.shouldNot11import io.kotest.property.checkAll12class FloatNaNTest : FunSpec() {13 init {14 context("NaN matcher") {15 test("Every numeric float should not be NaN") {16 checkAll(100, numericFloats) {17 it.shouldNotMatchNaN()18 }19 }20 test("The non-numeric floats") {21 Float.NaN.shouldMatchNaN()22 Float.POSITIVE_INFINITY.shouldNotMatchNaN()23 Float.NEGATIVE_INFINITY.shouldNotMatchNaN()24 }25 }26 }27 private fun Float.shouldMatchNaN() {28 this should beNaN()29 this.shouldBeNaN()30 this.shouldThrowExceptionOnNotBeNaN()31 }32 private fun Float.shouldNotMatchNaN() {33 this shouldNot beNaN()34 this.shouldNotBeNaN()35 this.shouldThrowExceptionOnBeNaN()36 }37 private fun Float.shouldThrowExceptionOnNotBeNaN() {38 shouldThrowAssertionError("$this should not be NaN",39 { this.shouldNotBeNaN() },40 { this shouldNot beNaN() })41 }42 private fun Float.shouldThrowExceptionOnBeNaN() {43 shouldThrowAssertionError("$this should be NaN",44 { this.shouldBeNaN() },45 { this should beNaN() })46 }47 private fun shouldThrowAssertionError(message: String, vararg expression: () -> Any?) {48 expression.forEach {49 val exception = shouldThrow<AssertionError>(it)50 exception.message shouldBe message51 }52 }53}...

Full Screen

Full Screen

NaN.kt

Source:NaN.kt Github

copy

Full Screen

...3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6/**7 * Asserts that this [Float] is [Float.NaN]8 *9 * Verifies that this [Float] is the Not-a-Number constant [Float.NaN]10 *11 * Opposite of [shouldNotBeNaN]12 *13 * ```14 * Float.NaN.shouldBeNaN() // Assertion passes15 * 1f.shouldBeNaN() // Assertion fails16 * ```17 * @see [beNaN]18 */19fun Float.shouldBeNaN(): Float {20 this should beNaN()21 return this22}23/**24 * Assert that this [Float] is not [Float.NaN]25 *26 * Verifies that this [Float] is NOT the Not-a-Number constant [Float.NaN]27 *28 * Opposite of [shouldBeNaN]29 *30 * ```31 * 1f.shouldNotBeNaN() // Assertion passes32 * Float.NaN.shouldNotBeNaN() // Assertion fails33 * ```34 * @see [beNaN]35 */36fun Float.shouldNotBeNaN(): Float {37 this shouldNot beNaN()38 return this39}40/**41 * Matcher that matches [Float.NaN]42 *43 * Verifies that a specific [Float] is the Not-a-Number constant [Float.NaN]44 *45 * ```46 * 0.5f should beNaN() // Assertion fails47 * Float.NaN should beNaN() // Assertion passes48 * ```49 *50 * @see [Float.shouldBeNaN]51 * @see [Float.shouldNotBeNaN]52 */53fun beNaN() = object : Matcher<Float> {54 override fun test(value: Float) = MatcherResult(55 value.isNaN(),56 { "$value should be NaN" },57 { "$value should not be NaN" })58}...

Full Screen

Full Screen

FloatMatchers.kt

Source:FloatMatchers.kt Github

copy

Full Screen

...4import kotlin.math.abs5infix fun Float.plusOrMinus(tolerance: Float): FloatToleranceMatcher = FloatToleranceMatcher(this, tolerance)6class FloatToleranceMatcher(private val expected: Float, private val tolerance: Float) : Matcher<Float> {7 override fun test(value: Float): MatcherResult {8 return if (expected.isNaN() && value.isNaN()) {9 println("[WARN] By design, Float.Nan != Float.Nan; see https://stackoverflow.com/questions/8819738/why-does-double-nan-double-nan-return-false/8819776#8819776")10 MatcherResult(11 false,12 "By design, Float.Nan != Float.Nan; see https://stackoverflow.com/questions/8819738/why-does-double-nan-double-nan-return-false/8819776#8819776",13 "By design, Float.Nan != Float.Nan; see https://stackoverflow.com/questions/8819738/why-does-double-nan-double-nan-return-false/8819776#8819776"14 )15 } else {16 if (tolerance == 0.0F)17 println("[WARN] When comparing Float consider using tolerance, eg: a shouldBe b plusOrMinus c")18 val diff = abs(value - expected)19 MatcherResult(diff <= tolerance, "$value should be equal to $expected", "$value should not be equal to $expected")20 }21 }22 infix fun plusOrMinus(tolerance: Float): FloatToleranceMatcher = FloatToleranceMatcher(expected, tolerance)...

Full Screen

Full Screen

arbs.kt

Source:arbs.kt Github

copy

Full Screen

2import io.kotest.property.Arb3import io.kotest.property.arbitrary.double4import io.kotest.property.arbitrary.filterNot5import io.kotest.property.arbitrary.float6val nonNumericDoubles = listOf(Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)7val numericDoubles = Arb.double().filterNot { it in nonNumericDoubles }8val nonMinNorMaxValueDoubles = numericDoubles.filterNot { it in listOf(Double.MAX_VALUE, Double.MIN_VALUE) }9val nonNumericFloats = listOf(Float.NaN, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY)10val numericFloats = Arb.float().filterNot { it in nonNumericFloats }...

Full Screen

Full Screen

NaN

Using AI Code Generation

copy

Full Screen

1 import io.kotest.matchers.doubles.*2 import io.kotest.matchers.doubles.beNaN3 import io.kotest.matchers.doubles.bePositieInfinity4 import io.kotest.matchers.doubles.beNegativeInfinity5 import io.kotest.matchers.doubles.beFinite6 import io.kotest.matchers.doubles.beInfinite7 import io.kotest.matchers.doubles.beNorm8 import io.kotest.mtchers.doubles.beSubnormal9 import io.kotest.matchers.doubles.beZero10 import io.kotest.matchers.doubles.beNonZero11 import io.kotest.matchers.longs.*12 import io.kotest.matchers.longs.beNaN13 import io.kotest.matchers.longs.bePositiveInfinity14 import io.kotest.matchers.longs.beNegativeInfinity15 import io.kotest.matchers.longs.beFinite16 import io.kotest.matchers.longs.beInfinite17 import io.kotest.matchers.longs.beNormal18 import io.kotest.matchers.longs.beSubnormal19 import io.kotest.matchers.longs.beZero20 import io.kotest.matchers.longs.beNonZero21 import io.kotest.matchers.ints.*22 import io.kotest.matchers.ints.beNaN23 import io.kotest.matchers.ints.bePositiveInfinity24 import io.kotest.matchers.ints.beNegativeInfinity25 import io.kotest.matchers.ints.beFinite26 import io.kotest.matchers.ints.beInfinite27 import io.kotest.matchers.ints.beNormal28 import io.kotest.matchers.ints.beSubnormal29 import io.kotest.matchers.ints.beZero30 import io.kotest.matchers.ints.beNonZero31 import io.kotest.matchers.shorts.*32 import io.kotest.matchers.shorts.beNaN33 import io.kotest.matchers.shorts.bePositiveInfinity34 import io.kotest.match

Full Screen

Full Screen

NaN

Using AI Code Generation

copy

Full Screen

1 import io.kotest.matchers.doubles.*2 import io.kotest.matchers.doubles.beNaN3 import io.kotest.matchers.doubles.bePositiveInfinity4 import io.kotest.matchers.doubles.beNegativeInfinity5 import io.kotest.matchers.doubles.beFinite6 import io.kotest.matchers.doubles.beInfinite7 import io.kotest.matchers.doubles.beNormal8 import io.kotest.matchers.doubles.beSubnormal9 import io.kotest.matchers.doubles.beZero10 import io.kotest.matchers.doubles.beNonZero11 import io.kotest.matchers.longs.*12 import io.kotest.matchers.longs.beNaN13 import io.kotest.matchers.longs.bePositiveInfinity14 import io.kotest.matchers.longs.beNegativeInfinity15 import io.kotest.matchers.longs.beFinite16 import io.kotest.matchers.longs.beInfinite17 import io.kotest.matchers.longs.beNormal18 import io.kotest.matchers.longs.beSubnormal19 import io.kotest.matchers.longs.beZero20 import io.kotest.matchers.longs.beNonZero21 import io.kotest.matchers.ints.*22 import io.kotest.matchers.ints.beNaN23 import io.kotest.matchers.ints.bePositiveInfinity24 import io.kotest.matchers.ints.beNegativeInfinity25 import io.kotest.matchers.ints.beFinite26 import io.kotest.matchers.ints.beInfinite27 import io.kotest.matchers.ints.beNormal28 import io.kotest.matchers.ints.beSubnormal29 import io.kotest.matchers.ints.beZero30 import io.kotest.matchers.ints.beNonZero31 import io.kotest.matchers.shorts.*32 import io.kotest.matchers.shorts.beNaN33 import io.kotest.matchers.shorts.bePositiveInfinity34 import io.kotest.match

Full Screen

Full Screen

NaN

Using AI Code Generation

copy

Full Screen

1result.shouldNotBeNaN()2result.shouldBeNaN()3result.shouldNotBeNaN()4result.shouldBeNaN()5result.shouldNotBeNaN()6result.shouldBeNaN()7result.shouldNotBeNaN()8result.shouldBeNaN()9val result = 1.toShort() / 0.toShort()10result.shouldNotBeNaN()11result.shouldBeNaN()12val result = 1.toByte() / 0.toByte()13result.shouldNotBeNaN()14result.shouldBeNaN()15val result = 1.toChar() / 0.toChar()16result.shouldNotBeNaN()17result.shouldBeNaN()N class of io.kotest.matchers.longs package18val result = 1.toBigInteger() / 0.toBigInteger()19result.shouldNotBeNaN()20result.shouldBeNaN()21val result = 1.toBigDecimal() / 0.toBigDecimal()22result.shouldNotBeNaN()23result.shouldBeNaN()24result.shouldNotBeNaN()25result.shouldBeNaN()26val result = "1".toBigDecimal() / "0".toBigDecimal()27result.shouldNotBeNaN()28result.shouldBeNaN()29result.shouldNotBeNaN()30result.shouldBeNaN()

Full Screen

Full Screen

NaN

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.floats.* import io.kotest.matchers.floats.*2import io.kotest.matchers.ints.* import io.kotest.matchers.ints.*3import io.kotest.matchers.longs.* import io.kotest.matchers.longs.*4import io.kotest.matchers.doubles.* import io.kotest.matchers.doubles.*5import io.kotest.matchers.shorts.* import io.kotest.matchers.shorts.*6import io.kotest.matchers.booleans.* import io.kotest.matchers.booleans.*7import io.kotest.matchers.chars.* import io.kotest.matchers.chars.*8import io.kotest.matchers.bytes.* import io.kotest.matchers.bytes.*9import io.kotest.matchers.longs.* import io.kotest.matchers.longs.*10import io.kotest.matchers.shorts.* import io.kotest.matchers.shorts.*11import io.kotest.matchers.booleans.* import io.kotest.matchers.booleans.*12import io.kotest.matchers.chars.* import io.kotest.matchers.chars.*13import io.kotest.matchers.bytes.* import io.kotest.matchers.bytes.*14import io.kotest.matchers.longs.* import io.kotest.matchers.longs.*15import io.kotest.matchers.shorts.* import io.kotest.matchers.shorts.*16import io.kotest.matchers.booleans.* import io.kotest.matchers.booleans.*17import io.kotest.matchers.chars.* import io.kotest.matchers.chars.*18import io.kotest.matchers.bytes.* import io.kotest.matchers.bytes.*19import io.kotest.matchers.longs.* import io.kotest.matchers.longs.*20import io.kotest.matchers.shorts.* import io.kotest.matchers.shorts.*21import io.kotest.matchers.booleans.* import io.kotest.matchers.booleans.*22import io.kotest.matchers.chars.* import io.kotest.matchers.chars.*23import io.kotest.matchers.bytes.* import io.kotest.matchers.bytes.*24import io.kotest.matchers.longs.* import io.kotest.matchers.longs.*25import io.kotest.matchers.shorts.* import io.kotest.matchers.sh

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