How to use Double.shouldBePositiveInfinity method of io.kotest.matchers.doubles.Infinity class

Best Kotest code snippet using io.kotest.matchers.doubles.Infinity.Double.shouldBePositiveInfinity

DoubleMatchersTest.kt

Source:DoubleMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.doubles2import io.kotest.assertions.shouldFail3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.FreeSpec5import io.kotest.matchers.doubles.beGreaterThan6import io.kotest.matchers.doubles.beGreaterThanOrEqualTo7import io.kotest.matchers.doubles.beLessThan8import io.kotest.matchers.doubles.beLessThanOrEqualTo9import io.kotest.matchers.doubles.beNaN10import io.kotest.matchers.doubles.beNegativeInfinity11import io.kotest.matchers.doubles.bePositiveInfinity12import io.kotest.matchers.doubles.between13import io.kotest.matchers.doubles.gt14import io.kotest.matchers.doubles.gte15import io.kotest.matchers.doubles.lt16import io.kotest.matchers.doubles.lte17import io.kotest.matchers.doubles.negative18import io.kotest.matchers.doubles.positive19import io.kotest.matchers.doubles.shouldBeBetween20import io.kotest.matchers.doubles.shouldBeGreaterThan21import io.kotest.matchers.doubles.shouldBeGreaterThanOrEqual22import io.kotest.matchers.doubles.shouldBeLessThan23import io.kotest.matchers.doubles.shouldBeLessThanOrEqual24import io.kotest.matchers.doubles.shouldBeMultipleOf25import io.kotest.matchers.doubles.shouldBeNaN26import io.kotest.matchers.doubles.shouldBeNegative27import io.kotest.matchers.doubles.shouldBeNegativeInfinity28import io.kotest.matchers.doubles.shouldBePositive29import io.kotest.matchers.doubles.shouldBePositiveInfinity30import io.kotest.matchers.doubles.shouldBeZero31import io.kotest.matchers.doubles.shouldNotBeBetween32import io.kotest.matchers.doubles.shouldNotBeGreaterThan33import io.kotest.matchers.doubles.shouldNotBeGreaterThanOrEqual34import io.kotest.matchers.doubles.shouldNotBeLessThan35import io.kotest.matchers.doubles.shouldNotBeLessThanOrEqual36import io.kotest.matchers.doubles.shouldNotBeNaN37import io.kotest.matchers.doubles.shouldNotBeNegative38import io.kotest.matchers.doubles.shouldNotBeNegativeInfinity39import io.kotest.matchers.doubles.shouldNotBePositive40import io.kotest.matchers.doubles.shouldNotBePositiveInfinity41import io.kotest.matchers.doubles.shouldNotBeZero42import io.kotest.matchers.should43import io.kotest.matchers.shouldBe44import io.kotest.matchers.shouldNot45import io.kotest.matchers.shouldNotBe46import io.kotest.property.arbitrary.filterNot47import io.kotest.property.checkAll48import kotlin.Double.Companion.MAX_VALUE49import kotlin.Double.Companion.MIN_VALUE50import kotlin.Double.Companion.NEGATIVE_INFINITY51import kotlin.Double.Companion.NaN52import kotlin.Double.Companion.POSITIVE_INFINITY53import kotlin.math.absoluteValue54class DoubleMatchersTest : FreeSpec() {55  init {56    "Between matcher" - {57      "Every numeric double that is not Double.MAX_VALUE" - {58        "Should match between" - {59          "When it's equal to the first number of the range" - {60            "With tolerance" {61              checkAll(100, nonMinNorMaxValueDoubles) {62                it.shouldMatchBetween(it, it.slightlyGreater(), it.toleranceValue())63              }64            }65            "Without tolerance" {66              checkAll(100, nonMinNorMaxValueDoubles) {67                it.shouldMatchBetween(it, it.slightlyGreater(), 0.0)68              }69            }70          }71          "When it's between the first number of the range and the last one" - {72            "With tolerance" {73              checkAll(100, nonMinNorMaxValueDoubles) {74                it.shouldMatchBetween(it.slightlySmaller(), it.slightlyGreater(), it.toleranceValue())75              }76            }77            "Without tolerance" {78              checkAll(100, nonMinNorMaxValueDoubles) {79                it.shouldMatchBetween(it.slightlySmaller(), it.slightlyGreater(), 0.0)80              }81            }82          }83          "When it's equal to the last number of the range" - {84            "With tolerance" {85              checkAll(100, nonMinNorMaxValueDoubles) {86                it.shouldMatchBetween(it.slightlySmaller(), it, it.toleranceValue())87              }88            }89            "Without tolerance" {90              checkAll(100, nonMinNorMaxValueDoubles) {91                it.shouldMatchBetween(it.slightlySmaller(), it, 0.0)92              }93            }94          }95        }96        "Should not match between" - {97          "When it's smaller than the first number of the range" - {98            "With tolerance" {99              checkAll(100, nonMinNorMaxValueDoubles) {100                it.shouldNotMatchBetween(it.slightlyGreater(), it.muchGreater(), it.toleranceValue())101              }102            }103            "Without tolerance" {104              checkAll(100, nonMinNorMaxValueDoubles) {105                it.shouldNotMatchBetween(it.slightlyGreater(), it.muchGreater(), 0.0)106              }107            }108          }109          "When it's bigger than the last number of the range" - {110            "With tolerance" {111              checkAll(100, nonMinNorMaxValueDoubles) {112                it.shouldNotMatchBetween(it.muchSmaller(), it.slightlySmaller(), it.toleranceValue())113              }114            }115            "Without tolerance" {116              checkAll(100, nonMinNorMaxValueDoubles) {117                it.shouldNotMatchBetween(it.muchSmaller(), it.slightlySmaller(), 0.0)118              }119            }120          }121        }122      }123    }124    "Less than matcher" - {125      "Every numeric double" - {126        "Should be less than" - {127          "Numbers bigger than itself" {128            checkAll(100, nonMinNorMaxValueDoubles) {129              it shouldMatchLessThan it.slightlyGreater()130              it shouldMatchLessThan it.muchGreater()131            }132          }133          "Infinity" {134            checkAll(100, nonMinNorMaxValueDoubles) {135              it shouldMatchLessThan POSITIVE_INFINITY136            }137          }138        }139        "Should not be less than" - {140          "Itself" {141            checkAll(100, nonMinNorMaxValueDoubles) {142              it shouldNotMatchLessThan it143            }144          }145          "Numbers smaller than itself" {146            checkAll(100, nonMinNorMaxValueDoubles) {147              it shouldNotMatchLessThan it.slightlySmaller()148              it shouldNotMatchLessThan it.muchSmaller()149            }150          }151          "Negative Infinity" {152            checkAll(100, nonMinNorMaxValueDoubles) {153              it shouldNotMatchLessThan it154            }155          }156          "NaN" {157            checkAll(100, nonMinNorMaxValueDoubles) {158              it shouldNotMatchLessThan NaN159            }160          }161        }162      }163      "The non-numeric double" - {164        "NaN" - {165          "Should not be less than" - {166            "Any numeric double" {167              checkAll(100, nonMinNorMaxValueDoubles) {168                NaN shouldNotMatchLessThan it169              }170            }171            "Any non-numeric double" {172              nonNumericDoubles.forEach {173                NaN shouldNotMatchLessThan it174              }175            }176          }177        }178        "Positive Infinity" - {179          "Should not be less than" - {180            "Any numeric double" {181              checkAll(100, nonMinNorMaxValueDoubles) {182                POSITIVE_INFINITY shouldNotMatchLessThan it183              }184            }185            "Any non-numeric double" {186              nonNumericDoubles.forEach {187                POSITIVE_INFINITY shouldNotMatchLessThan it188              }189            }190          }191        }192        "Negative Infinity" - {193          "Should be less than" - {194            "Any numeric double" {195              checkAll(100, nonMinNorMaxValueDoubles) {196                NEGATIVE_INFINITY shouldMatchLessThan it197              }198            }199            "Positive Infinity" {200              NEGATIVE_INFINITY shouldMatchLessThan POSITIVE_INFINITY201            }202          }203          "Should not be less than" - {204            "Itself" {205              NEGATIVE_INFINITY shouldNotMatchLessThan NEGATIVE_INFINITY206            }207            "NaN" {208              NEGATIVE_INFINITY shouldNotMatchLessThan NaN209            }210          }211        }212      }213    }214    "Positive matcher" - {215      "Zero" - {216        "Should not be positive" {217          0.0.shouldNotMatchPositive()218        }219      }220      "Every positive number" - {221        "Should be positive" {222          checkAll(100, numericDoubles.filterNot { it == 0.0 }) {223            it.absoluteValue.shouldMatchPositive()224          }225        }226      }227      "Every non-positive number" - {228        "Should not be positive" {229          checkAll(100, numericDoubles) {230            (-it.absoluteValue).shouldNotMatchPositive()231          }232        }233      }234      "The non-numeric double" - {235        "Positive Infinity" - {236          "Should be positive" {237            POSITIVE_INFINITY.shouldMatchPositive()238          }239        }240        "Negative Infinity" - {241          "Should not be positive" {242            NEGATIVE_INFINITY.shouldNotMatchPositive()243          }244        }245        "NaN" - {246          "Should not be positive" {247            NaN.shouldNotMatchPositive()248          }249        }250      }251    }252    "Negative matcher" - {253      "Zero" - {254        "Should not be negative" {255          0.0.shouldNotMatchNegative()256        }257      }258      "Every negative number" - {259        "Should be negative" {260          checkAll(100, numericDoubles.filterNot { it == 0.0 }) {261            (-it.absoluteValue).shouldMatchNegative()262          }263        }264      }265      "Every non-negative number" - {266        "Should not be negative" {267          checkAll(100, numericDoubles) {268            it.absoluteValue.shouldNotMatchNegative()269          }270        }271      }272      "The non-numeric double" - {273        "Positive Infinity" - {274          "Should not be negative" {275            POSITIVE_INFINITY.shouldNotMatchNegative()276          }277        }278        "Negative Infinity" - {279          "Should be negative" {280            NEGATIVE_INFINITY.shouldMatchNegative()281          }282        }283        "NaN" - {284          "Should not be negative" {285            NaN.shouldNotMatchNegative()286          }287        }288      }289    }290     "MultipleOf matcher" - {291        "Matches a simple multiple" {292           300.0 shouldBeMultipleOf 1.0293        }294        "Fails due to precision problems" {295           shouldFail {296              3.6e300 shouldBeMultipleOf 1.2297           }298        }299     }300    "Less than or equal matcher" - {301      "Every numeric double" - {302        "Should be less than or equal" - {303          "Itself" {304            checkAll(100, nonMinNorMaxValueDoubles) {305              it shouldMatchLessThanOrEqual it306            }307          }308          "Numbers bigger than itself" {309            checkAll(100, nonMinNorMaxValueDoubles) {310              it shouldMatchLessThanOrEqual it.muchGreater()311              it shouldMatchLessThanOrEqual it.slightlyGreater()312            }313          }314          "Positive Infinity" {315            checkAll(100, nonMinNorMaxValueDoubles) {316              it shouldMatchLessThanOrEqual POSITIVE_INFINITY317            }318          }319        }320        "Should not be less than or equal" - {321          "Any number smaller than itself" {322            checkAll(100, nonMinNorMaxValueDoubles) {323              it shouldNotMatchLessThanOrEqual it.slightlySmaller()324              it shouldNotMatchLessThanOrEqual it.muchSmaller()325            }326          }327          "Negative Infinity" {328            checkAll(100, nonMinNorMaxValueDoubles) {329              it shouldNotMatchLessThanOrEqual NEGATIVE_INFINITY330            }331          }332          "NaN" {333            checkAll(100, nonMinNorMaxValueDoubles) {334              it shouldNotMatchLessThanOrEqual NaN335            }336          }337        }338      }339      "The non-numeric double" - {340        "NaN" {341          "Should not be less than or equal" - {342            "Any numeric double" {343              checkAll(100, nonMinNorMaxValueDoubles) {344                NaN shouldNotMatchLessThanOrEqual it345              }346            }347            "Positive Infinity" {348              NaN shouldNotMatchLessThanOrEqual POSITIVE_INFINITY349            }350            "Negative Infinity" {351              NaN shouldNotMatchLessThanOrEqual NEGATIVE_INFINITY352            }353            "Itself" {354              NaN shouldNotMatchLessThanOrEqual NaN355            }356          }357        }358        "Positive Infinity" - {359          "Should be less than or equal" - {360            "Positive Infinity" {361              POSITIVE_INFINITY shouldMatchLessThanOrEqual POSITIVE_INFINITY362            }363          }364          "Should not be less than or equal" - {365            "Any numeric double" {366              checkAll(100, nonMinNorMaxValueDoubles) {367                POSITIVE_INFINITY shouldNotMatchLessThanOrEqual it368              }369            }370            "Negative Infinity" {371              POSITIVE_INFINITY shouldNotMatchLessThanOrEqual NEGATIVE_INFINITY372            }373            "NaN" {374              POSITIVE_INFINITY shouldNotMatchLessThanOrEqual NaN375            }376          }377        }378        "Negative Infinity" - {379          "Should be less than or equal" - {380            "Any numeric double" {381              checkAll(100, nonMinNorMaxValueDoubles) {382                NEGATIVE_INFINITY shouldMatchLessThanOrEqual it383              }384            }385            "Positive Infinity" {386              NEGATIVE_INFINITY shouldMatchLessThanOrEqual POSITIVE_INFINITY387            }388            "Itself" {389              NEGATIVE_INFINITY shouldMatchLessThanOrEqual NEGATIVE_INFINITY390            }391          }392          "Should not be less than or equal" - {393            "NaN" {394              NEGATIVE_INFINITY shouldNotMatchLessThanOrEqual NaN395            }396          }397        }398      }399    }400    "Greater than matcher" - {401      "Every numeric double" - {402        "Should be greater than" - {403          "Numbers smaller than itself" {404            checkAll(100, nonMinNorMaxValueDoubles) {405              it shouldMatchGreaterThan it.slightlySmaller()406              it shouldMatchGreaterThan it.muchSmaller()407            }408          }409          "Negative infinity" {410            checkAll(100, nonMinNorMaxValueDoubles) {411              it shouldMatchGreaterThan NEGATIVE_INFINITY412            }413          }414        }415        "Should not be greater than" - {416          "Itself" {417            checkAll(100, nonMinNorMaxValueDoubles) {418              it shouldNotMatchGreaterThan it419            }420          }421          "Numbers greater than itself" {422            checkAll(100, nonMinNorMaxValueDoubles) {423              it shouldNotMatchGreaterThan it.slightlyGreater()424              it shouldNotMatchGreaterThan it.muchGreater()425            }426          }427          "NaN" {428            checkAll(100, nonMinNorMaxValueDoubles) {429              it shouldNotMatchGreaterThan NaN430            }431          }432          "Positive Infinity" {433            checkAll(100, nonMinNorMaxValueDoubles) {434              it shouldNotMatchGreaterThan POSITIVE_INFINITY435            }436          }437        }438      }439      "The non-numeric double" - {440        "NaN" - {441          "Should not be greater than" - {442            "Itself" {443              NaN shouldNotMatchGreaterThan NaN444            }445            "Any numeric double" {446              checkAll(100, numericDoubles) {447                NaN shouldNotMatchGreaterThan it448              }449            }450            "Positive Infinity" {451              NaN shouldNotMatchGreaterThan POSITIVE_INFINITY452            }453            "Negative Infinity" {454              NaN shouldNotMatchGreaterThan NEGATIVE_INFINITY455            }456          }457        }458      }459    }460    "Greater than or equal matcher" - {461      "Every numeric double" - {462        "Should be greater than or equal to" - {463          "Itself" {464            checkAll(100, nonMinNorMaxValueDoubles) {465              it shouldMatchGreaterThanOrEqual it466            }467          }468          "Numbers smaller than itself" {469            checkAll(100, nonMinNorMaxValueDoubles) {470              it shouldMatchGreaterThanOrEqual it.slightlySmaller()471              it shouldMatchGreaterThanOrEqual it.muchSmaller()472            }473          }474          "Negative Infinity" {475            checkAll(100, nonMinNorMaxValueDoubles) {476              it shouldMatchGreaterThanOrEqual NEGATIVE_INFINITY477            }478          }479        }480        "Should not be greater than or equal to" - {481          "Numbers bigger than itself" {482            checkAll(100, nonMinNorMaxValueDoubles) {483              it shouldNotMatchGreaterThanOrEqual it.slightlyGreater()484              it shouldNotMatchGreaterThanOrEqual it.muchGreater()485            }486          }487          "Positive Infinity" {488            checkAll(100, nonMinNorMaxValueDoubles) {489              it shouldNotMatchGreaterThanOrEqual POSITIVE_INFINITY490            }491          }492          "NaN" {493            checkAll(100, nonMinNorMaxValueDoubles) {494              it shouldNotMatchGreaterThanOrEqual NaN495            }496          }497        }498      }499      "The non-numeric double" - {500        "NaN" - {501          "Should not be greater than or equal to" - {502            "Itself" {503              NaN shouldNotMatchGreaterThanOrEqual NaN504            }505            "Any numeric double" {506              checkAll(100, numericDoubles) {507                NaN shouldNotMatchGreaterThanOrEqual it508              }509            }510            "Positive Infinity" {511              NaN shouldNotMatchGreaterThanOrEqual POSITIVE_INFINITY512            }513            "Negative Infinity" {514              NaN shouldNotMatchGreaterThanOrEqual NEGATIVE_INFINITY515            }516          }517        }518        "Positive Infinity" - {519          "Should be greater than or equal to" - {520            "Itself" {521              POSITIVE_INFINITY shouldMatchGreaterThanOrEqual POSITIVE_INFINITY522            }523            "Negative Infinity" {524              POSITIVE_INFINITY shouldMatchGreaterThanOrEqual NEGATIVE_INFINITY525            }526            "Any numeric double" {527              checkAll(100, numericDoubles) {528                POSITIVE_INFINITY shouldMatchGreaterThanOrEqual it529              }530            }531          }532          "Should not be greater than or equal to" - {533            "NaN" {534              POSITIVE_INFINITY shouldNotMatchGreaterThanOrEqual NaN535            }536          }537        }538        "Negative Infinity" - {539          "Should be greater than or equal to" - {540            "Itself" {541              NEGATIVE_INFINITY shouldMatchGreaterThanOrEqual NEGATIVE_INFINITY542            }543          }544          "Should not be greater than or equal to" - {545            "Any numeric double" {546              checkAll(100, numericDoubles) {547                NEGATIVE_INFINITY shouldNotMatchGreaterThanOrEqual it548              }549            }550            "Positive Infinity" {551              NEGATIVE_INFINITY shouldNotMatchGreaterThanOrEqual POSITIVE_INFINITY552            }553            "NaN" {554              NEGATIVE_INFINITY shouldNotMatchGreaterThanOrEqual NaN555            }556          }557        }558      }559    }560    "NaN matcher" - {561      "Every numeric double" - {562        "Should not be NaN" {563          checkAll(100, numericDoubles) {564            it.shouldNotMatchNaN()565          }566        }567      }568      "The non-numeric double" - {569        "NaN" - {570          "Should match NaN" {571            NaN.shouldMatchNaN()572          }573        }574        "Positive Infinity" - {575          "Should not match NaN" {576            POSITIVE_INFINITY.shouldNotMatchNaN()577          }578        }579        "Negative Infinity" - {580          "Should not match NaN" {581            NEGATIVE_INFINITY.shouldNotMatchNaN()582          }583        }584      }585    }586    "Positive Infinity matcher" - {587      "Any numeric double" - {588        "Should not match positive infinity" {589          checkAll(100, numericDoubles) {590            it.shouldNotMatchPositiveInfinity()591          }592        }593      }594      "The non-numeric double" - {595        "Positive Infinity" - {596          "Should match positive infinity" {597            POSITIVE_INFINITY.shouldMatchPositiveInfinity()598          }599        }600        "Negative Infinity" - {601          "Should not match positive infinity" {602            NEGATIVE_INFINITY.shouldNotMatchPositiveInfinity()603          }604        }605        "NaN" - {606          "Should not match positive infinity" {607            NaN.shouldNotMatchPositiveInfinity()608          }609        }610      }611    }612    "Negative Infinity matcher" - {613      "Any numeric double" - {614        "Should not match negative infinity" {615          checkAll(100, numericDoubles) {616            it.shouldNotMatchNegativeInfinity()617          }618        }619      }620      "The non-numeric double" - {621        "Negative Infinity" - {622          "Should match negative infinity" {623            NEGATIVE_INFINITY.shouldMatchNegativeInfinity()624          }625        }626        "Positive Infinity" - {627          "Should not match negative infinity" {628            POSITIVE_INFINITY.shouldNotMatchNegativeInfinity()629          }630        }631        "NaN" - {632          "Should not match negative infinity" {633            NaN.shouldNotMatchNegativeInfinity()634          }635        }636      }637    }638    "shouldBeZero" {639      (0.0).shouldBeZero()640      (-0.1).shouldNotBeZero()641      (0.1).shouldNotBeZero()642      MIN_VALUE.shouldNotBeZero()643      MAX_VALUE.shouldNotBeZero()644      NaN.shouldNotBeZero()645      POSITIVE_INFINITY.shouldNotBeZero()646      NEGATIVE_INFINITY.shouldNotBeZero()647    }648  }649  private fun shouldThrowAssertionError(message: String, vararg expression: () -> Any?) {650    expression.forEach {651      val exception = shouldThrow<AssertionError>(it)652      exception.message shouldBe message653    }654  }655  private fun Double.shouldMatchBetween(a: Double, b: Double, tolerance: Double) {656    this.shouldBeBetween(a, b, tolerance)657    this shouldBe between(a, b, tolerance)658    this.shouldThrowExceptionOnNotBetween(a, b, tolerance)659  }660  private fun Double.shouldNotMatchBetween(a: Double, b: Double, tolerance: Double) {661    this.shouldNotBeBetween(a, b, tolerance)662    this shouldNotBe between(a, b, tolerance)663    this.shouldThrowExceptionOnBetween(a, b, tolerance)664  }665  private fun Double.shouldThrowExceptionOnBetween(a: Double, b: Double, tolerance: Double) {666    when {667      this < a -> this.shouldThrowMinimumExceptionOnBetween(a, b, tolerance)668      this > b -> this.shouldThrowMaximumExceptionOnBetween(a, b, tolerance)669      else     -> throw IllegalStateException()670    }671  }672  private fun Double.shouldThrowMinimumExceptionOnBetween(a: Double, b: Double, tolerance: Double) {673    val message = "$this should be bigger than $a"674    shouldThrowExceptionOnBetween(a, b, tolerance, message)675  }676  private fun Double.shouldThrowMaximumExceptionOnBetween(a: Double, b: Double, tolerance: Double) {677    val message = "$this should be smaller than $b"678    shouldThrowExceptionOnBetween(a, b, tolerance, message)679  }680  private fun Double.shouldThrowExceptionOnBetween(681    a: Double,682    b: Double,683    tolerance: Double,684    message: String = "$this should be smaller than $b and bigger than $a"685  ) {686    shouldThrowAssertionError(message,687                              { this.shouldBeBetween(a, b, tolerance) },688                              { this shouldBe between(a, b, tolerance) })689  }690  private fun Double.shouldThrowExceptionOnNotBetween(691    a: Double,692    b: Double,693    tolerance: Double,694    message: String = "$this should not be smaller than $b and should not be bigger than $a"695  ) {696    shouldThrowAssertionError(message,697                              { this.shouldNotBeBetween(a, b, tolerance) },698                              { this shouldNotBe between(a, b, tolerance) })699  }700  private infix fun Double.shouldMatchLessThan(x: Double) {701    this shouldBe lt(x)702    this shouldBeLessThan x703    this should beLessThan(x)704    this shouldThrowExceptionOnNotLessThan x705  }706  private infix fun Double.shouldThrowExceptionOnNotLessThan(x: Double) {707    shouldThrowAssertionError("$this should not be < $x",708                              { this shouldNotBe lt(x) },709                              { this shouldNotBeLessThan x },710                              { this shouldNot beLessThan(x) })711  }712  private infix fun Double.shouldNotMatchLessThan(x: Double) {713    this shouldNotBe lt(x)714    this shouldNotBeLessThan x715    this shouldNot beLessThan(x)716    this shouldThrowExceptionOnLessThan x717  }718  private infix fun Double.shouldThrowExceptionOnLessThan(x: Double) {719    shouldThrowAssertionError("$this should be < $x",720                              { this shouldBe lt(x) },721                              { this shouldBeLessThan x },722                              { this should beLessThan(x) }723    )724  }725  private fun Double.shouldMatchPositive() {726    this.shouldBePositive()727    this shouldBe positive()728    this.shouldThrowExceptionOnNotPositive()729  }730  private fun Double.shouldThrowExceptionOnNotPositive() {731    shouldThrowAssertionError("$this should not be > 0.0",732                              { this shouldNotBe positive() },733                              { this.shouldNotBePositive() }734    )735  }736  private fun Double.shouldNotMatchPositive() {737    this.shouldNotBePositive()738    this shouldNotBe positive()739    this.shouldThrowExceptionOnPositive()740  }741  private fun Double.shouldThrowExceptionOnPositive() {742    shouldThrowAssertionError("$this should be > 0.0",743                              { this shouldBe positive() },744                              { this.shouldBePositive() }745    )746  }747  private fun Double.shouldMatchNegative() {748    this.shouldBeNegative()749    this shouldBe negative()750    this.shouldThrowExceptionOnNotNegative()751  }752  private fun Double.shouldThrowExceptionOnNotNegative() {753    shouldThrowAssertionError("$this should not be < 0.0",754                              { this shouldNotBe negative() },755                              { this.shouldNotBeNegative() }756    )757  }758  private fun Double.shouldNotMatchNegative() {759    this.shouldNotBeNegative()760    this shouldNotBe negative()761    this.shouldThrowExceptionOnNegative()762  }763  private fun Double.shouldThrowExceptionOnNegative() {764    shouldThrowAssertionError("$this should be < 0.0",765                              { this shouldBe negative() },766                              { this.shouldBeNegative() }767    )768  }769  private infix fun Double.shouldMatchLessThanOrEqual(x: Double) {770    this should beLessThanOrEqualTo(x)771    this shouldBe lte(x)772    this shouldBeLessThanOrEqual x773    this shouldThrowExceptionOnNotLessThanOrEqual x774  }775  private infix fun Double.shouldThrowExceptionOnNotLessThanOrEqual(x: Double) {776    shouldThrowAssertionError("$this should not be <= $x",777                              { this shouldNot beLessThanOrEqualTo(x) },778                              { this shouldNotBe lte(x) },779                              { this shouldNotBeLessThanOrEqual x }780    )781  }782  private infix fun Double.shouldNotMatchLessThanOrEqual(x: Double) {783    this shouldNot beLessThanOrEqualTo(x)784    this shouldNotBe lte(x)785    this shouldNotBeLessThanOrEqual x786    this shouldThrowExceptionOnLessThanOrEqual x787  }788  private infix fun Double.shouldThrowExceptionOnLessThanOrEqual(x: Double) {789    shouldThrowAssertionError("$this should be <= $x",790                              { this should beLessThanOrEqualTo(x) },791                              { this shouldBe lte(x) },792                              { this shouldBeLessThanOrEqual x }793    )794  }795  private infix fun Double.shouldMatchGreaterThan(x: Double) {796    this should beGreaterThan(x)797    this shouldBe gt(x)798    this shouldBeGreaterThan x799    this shouldThrowExceptionOnNotGreaterThan x800  }801  private infix fun Double.shouldThrowExceptionOnNotGreaterThan(x: Double) {802    shouldThrowAssertionError("$this should not be > $x",803                              { this shouldNot beGreaterThan(x) },804                              { this shouldNotBeGreaterThan (x) },805                              { this shouldNotBe gt(x) })806  }807  private infix fun Double.shouldNotMatchGreaterThan(x: Double) {808    this shouldNot beGreaterThan(x)809    this shouldNotBe gt(x)810    this shouldNotBeGreaterThan x811    this shouldThrowExceptionOnGreaterThan (x)812  }813  private infix fun Double.shouldThrowExceptionOnGreaterThan(x: Double) {814    shouldThrowAssertionError("$this should be > $x",815                              { this should beGreaterThan(x) },816                              { this shouldBe gt(x) },817                              { this shouldBeGreaterThan x })818  }819  private infix fun Double.shouldMatchGreaterThanOrEqual(x: Double) {820    this should beGreaterThanOrEqualTo(x)821    this shouldBe gte(x)822    this shouldBeGreaterThanOrEqual x823    this shouldThrowExceptionOnNotGreaterThanOrEqual (x)824  }825  private infix fun Double.shouldThrowExceptionOnNotGreaterThanOrEqual(x: Double) {826    shouldThrowAssertionError("$this should not be >= $x",827                              { this shouldNot beGreaterThanOrEqualTo(x) },828                              { this shouldNotBe gte(x) },829                              { this shouldNotBeGreaterThanOrEqual x })830  }831  private infix fun Double.shouldNotMatchGreaterThanOrEqual(x: Double) {832    this shouldNot beGreaterThanOrEqualTo(x)833    this shouldNotBe gte(x)834    this shouldNotBeGreaterThanOrEqual x835    this shouldThrowExceptionOnGreaterThanOrEqual (x)836  }837  private infix fun Double.shouldThrowExceptionOnGreaterThanOrEqual(x: Double) {838    shouldThrowAssertionError("$this should be >= $x",839                              { this should beGreaterThanOrEqualTo(x) },840                              { this shouldBe gte(x) },841                              { this shouldBeGreaterThanOrEqual x })842  }843  private fun Double.shouldMatchNaN() {844    this should beNaN()845    this.shouldBeNaN()846    this.shouldThrowExceptionOnNotBeNaN()847  }848  private fun Double.shouldThrowExceptionOnNotBeNaN() {849    shouldThrowAssertionError("$this should not be NaN",850                              { this.shouldNotBeNaN() },851                              { this shouldNot beNaN() })852  }853  private fun Double.shouldNotMatchNaN() {854    this shouldNot beNaN()855    this.shouldNotBeNaN()856    this.shouldThrowExceptionOnBeNaN()857  }858  private fun Double.shouldThrowExceptionOnBeNaN() {859    shouldThrowAssertionError("$this should be NaN",860                              { this.shouldBeNaN() },861                              { this should beNaN() })862  }863  private fun Double.shouldMatchPositiveInfinity() {864    this should bePositiveInfinity()865    this.shouldBePositiveInfinity()866    this.shouldThrowExceptionOnNotBePositiveInfinity()867  }868  private fun Double.shouldThrowExceptionOnNotBePositiveInfinity() {869    shouldThrowAssertionError("$this should not be POSITIVE_INFINITY",870                              { this shouldNot bePositiveInfinity() },871                              { this.shouldNotBePositiveInfinity() })872  }873  private fun Double.shouldNotMatchPositiveInfinity() {874    this shouldNot bePositiveInfinity()875    this.shouldNotBePositiveInfinity()876    this.shouldThrowExceptionOnBePositiveInfinity()877  }878  private fun Double.shouldThrowExceptionOnBePositiveInfinity() {879    shouldThrowAssertionError("$this should be POSITIVE_INFINITY",880                              { this should bePositiveInfinity() },881                              { this.shouldBePositiveInfinity() })882  }883  private fun Double.shouldMatchNegativeInfinity() {884    this should beNegativeInfinity()885    this.shouldBeNegativeInfinity()886    this.shouldThrowExceptionOnNotBeNegativeInfinity()887  }888  private fun Double.shouldThrowExceptionOnNotBeNegativeInfinity() {889    shouldThrowAssertionError("$this should not be NEGATIVE_INFINITY",890            { this shouldNot beNegativeInfinity() },891            { this.shouldNotBeNegativeInfinity() })892  }893  private fun Double.shouldNotMatchNegativeInfinity() {894    this shouldNot beNegativeInfinity()895    this.shouldNotBeNegativeInfinity()896    this.shouldThrowExceptionOnBeNegativeInfinity()897  }898  private fun Double.shouldThrowExceptionOnBeNegativeInfinity() {899    shouldThrowAssertionError("$this should be NEGATIVE_INFINITY",900            { this should beNegativeInfinity() },901            { this.shouldBeNegativeInfinity() })902  }903}...

Full Screen

Full Screen

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

Infinity.kt

Source:Infinity.kt Github

copy

Full Screen

1package io.kotest.matchers.doubles2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6/**7 * Verifies that this double is [Double.POSITIVE_INFINITY]8 *9 * Opposite of [shouldNotBePositiveInfinity]10 *11 * ```12 * Double.POSITIVE_INFINITY.shouldBePositiveInfinity()    // Assertion passes13 * 1.0.shouldBePositiveInfinity()                         // Assertion fails14 * ```15 *16 * @see [bePositiveInfinity]17 */18fun Double.shouldBePositiveInfinity() = this should bePositiveInfinity()19/**20 * Verifies that this double is NOT [Double.POSITIVE_INFINITY]21 *22 * Opposite of [shouldBePositiveInfinity]23 *24 * ```25 * Double.POSITIVE_INFINITY.shouldNotBePositiveInfinity()       // Assertion fails26 * 1.0.shouldBeNotPositiveInfinity()                            // Assertion passes27 * ```28 *29 * @see [bePositiveInfinity]30 */31fun Double.shouldNotBePositiveInfinity() = this shouldNot bePositiveInfinity()32/**33 * Matcher that matches whether a double is [Double.POSITIVE_INFINITY] or not34 *35 * ```36 * Double.POSITIVE_INFINITY should bePositiveInfinity()   // Assertion passes37 * 1.0 should bePositiveInfinity()                        // Assertion fails38 * ```39 */40fun bePositiveInfinity() = object : Matcher<Double> {41  override fun test(value: Double) = MatcherResult(42    value == Double.POSITIVE_INFINITY,43    "$value should be POSITIVE_INFINITY",44    "$value should not be POSITIVE_INFINITY"45  )46}47/**48 * Verifies that this double is [Double.NEGATIVE_INFINITY]49 *50 * Opposite of [shouldNotBeNegativeInfinity]51 *52 * ```53 * Double.NEGATIVE_INFINITY.shouldBeNegativeInfinity()    // Assertion passes54 * 1.0.shouldBeNegativeInfinity()                         // Assertion fails55 * ```56 *57 * @see [beNegativeInfinity]58 */59fun Double.shouldBeNegativeInfinity() = this should beNegativeInfinity()60/**61 * Verifies that this double is NOT [Double.NEGATIVE_INFINITY]62 *63 * Opposite of [shouldBeNegativeInfinity]64 *65 * ```66 * Double.NEGATIVE_INFINITY.shouldNotBeNegativeInfinity()       // Assertion fails67 * 1.0.shouldBeNotNegativeInfinity()                            // Assertion passes68 * ```69 *70 * @see [beNegativeInfinity]71 */72fun Double.shouldNotBeNegativeInfinity() = this shouldNot beNegativeInfinity()73/**74 * Matcher that matches whether a double is [Double.NEGATIVE_INFINITY] or not75 *76 * ```77 * Double.NEGATIVE_INFINITY should beNegativeInfinity()   // Assertion passes78 * 1.0 should beNegativeInfinity()                        // Assertion fails79 * ```80 */81fun beNegativeInfinity() = object : Matcher<Double> {82  override fun test(value: Double) = MatcherResult(83    value == Double.NEGATIVE_INFINITY,84    "$value should be NEGATIVE_INFINITY",85    "$value should not be NEGATIVE_INFINITY"86  )87}...

Full Screen

Full Screen

DoubleNumberTests.kt

Source:DoubleNumberTests.kt Github

copy

Full Screen

1package com.github.jvmusin.universalconverter.number2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.booleans.shouldBeFalse4import io.kotest.matchers.booleans.shouldBeTrue5import io.kotest.matchers.doubles.plusOrMinus6import io.kotest.matchers.doubles.shouldBePositiveInfinity7import io.kotest.matchers.shouldBe8class DoubleNumberTests : StringSpec({9    "3 * 9.1 = 18.3" {10        (DoubleNumber(3.0) * DoubleNumber(9.1)).value shouldBe (27.3 plusOrMinus 1e-10)11    }12    "3 / 2 = 1.5" {13        (DoubleNumber(3.0) / DoubleNumber(2.0)) shouldBe DoubleNumber(1.5)14    }15    "3 / 0 = +INF" {16        (DoubleNumber(3.0) / DoubleNumber(0.0)).value.shouldBePositiveInfinity()17    }18    "обратное к 5 = 0.2" {19        DoubleNumber(5.0).inverse() shouldBe DoubleNumber(0.2)20    }21    "обратное к 0 = +INF" {22        DoubleNumber(0.0).inverse().value.shouldBePositiveInfinity()23    }24    "5 положительно" {25        DoubleNumber(5.0).isPositive.shouldBeTrue()26    }27    "1e-30 положительно" {28        DoubleNumber(1e-30).isPositive.shouldBeTrue()29    }30    "0 не положительно" {31        DoubleNumber(0.0).isPositive.shouldBeFalse()32    }33    "Double.MIN_VALUE положительно" {34        DoubleNumber(Double.MIN_VALUE).isPositive.shouldBeTrue()35    }36    "-3 не положительно" {37        DoubleNumber(-3.0).isPositive.shouldBeFalse()38    }39    "13.565 в строку и в double даёт то же самое число" {40        val x = 13.56541        DoubleNumber(x).toString().toDouble() shouldBe x42    }43    // Kind of acceptance tests below44    "1/3 * 1000 в строку отдаёт 34 значащие цифры и округляет вниз" {45        val x = DoubleNumber(1.0).divideBy(DoubleNumber(3.0)).multiplyBy(DoubleNumber(1000.0))46        x.toString() shouldBe "333.3333333333333143855270463973284"47    }48    "2/3 * 1000 в строку отдаёт 34 значащие цифры и округляет вверх" {49        val x = DoubleNumber(2.0).divideBy(DoubleNumber(3.0)).multiplyBy(DoubleNumber(1000.0))50        x.toString() shouldBe "666.6666666666666287710540927946568"51    }52})...

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1    io.kotest.matchers.doubles.Infinity.shouldBePositiveInfinity()2    io.kotest.matchers.doubles.Infinity.shouldBeNegativeInfinity()3    io.kotest.matchers.doubles.NaN.shouldBeNaN()4    io.kotest.matchers.doubles.Finite.shouldBeFinite()5    io.kotest.matchers.doubles.Zero.shouldBeZero()6    io.kotest.matchers.doubles.NonZero.shouldBeNonZero()7    io.kotest.matchers.doubles.Positive.shouldBePositive()8    io.kotest.matchers.doubles.Negative.shouldBeNegative()9    io.kotest.matchers.doubles.Greater.shouldBeGreaterThan()10    io.kotest.matchers.doubles.GreaterOrEqual.shouldBeGreaterThanOrEqual()11    io.kotest.matchers.doubles.Less.shouldBeLessThan()12    io.kotest.matchers.doubles.LessOrEqual.shouldBeLessThanOrEqual()13    io.kotest.matchers.doubles.Between.shouldBeBetween()

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1    io.kotest.matchers.doubles.shouldBePositiveInfinity()2    io.kotest.matchers.doubles.shouldBeNegativeInfinity()3    io.kotest.matchers.doubles.shouldBeInfinity()4    io.kotest.matchers.doubles.shouldBeNaN()5    io.kotest.matchers.doubles.shouldBeNaN()6    io.kotest.matchers.doubles.shouldBeNaN()7    io.kotest.matchers.doubles.shouldBeNaN()8    io.kotest.matchers.doubles.shouldBeNaN()9    io.kotest.matchers.doubles.shouldBeNaN()10    io.kotest.matchers.doubles.shouldBeNaN()11    io.kotest.matchers.doubles.shouldBeNaN()12    io.kotest.matchers.doubles.shouldBeNaN()13    io.kotest.matchers.doubles.shouldBeNaN()14    io.kotest.matchers.doubles.shouldBeNaN()

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1    Double.shouldBePositiveInfinity()2    Double.shouldBeNegativeInfinity()3    Double.shouldBeFinite()4    Double.shouldBeInfinite()5    Double.shouldBeNaN()6    Double.shouldBeNormal()7    Double.shouldBeSubnormal()8    Double.shouldBeZero()9    Double.shouldBeNonZero()10    Double.shouldBeNegative()11    Double.shouldBePositive()12    Double.shouldBeNonPositive()13    Double.shouldBeNonNegative()14    Double.shouldBeCloseTo()15    Double.shouldBeGreaterThan()16    Double.shouldBeLessThan()17    Double.shouldBeGreaterThanOrEqual()

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1    Double.shouldBePositiveInfinity()2    Double.shouldBeNegativeInfinity()3    Double.shouldBeFinite()4    Double.shouldBeInfinite()5    Double.shouldBeNaN()6    Double.shouldBeNormal()7    Double.shouldBeSubnormal()8    Double.shouldBeZero()9    Double.shouldBeNonZero()10    Double.shouldBeNegative()ual method of io.koteal method of io.kotest.mstchers.d

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1        double.shouldBePositiveInfinity()2        double.shouldBeNegativeInfinity()3        double.shouldBeInfinity()4        double.shouldNotBeInfinity()5        double.shouldBePositiveInfinity()6        double.shouldBeNegativeInfinity()7        double.shouldBeInfinity()8        double.shouldNotBeInfinity()9        double.shouldBePositiveInfinity()10        double.shouldBeNegativeInfinity()11        double.shouldBeInfinity()

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1        double.shouldBePositiveInfinity()2        double.shouldBeNegativeInfinity()3        double.shouldBeInfinity()4        double.shouldNotBeInfinity()5        double.shouldBePositiveInfinity()6        double.shouldBeNegativeInfinity()7        double.shouldBeInfinity()8        double.shouldNotBeInfinity()9        double.shouldBePositiveInfinity()10        double.shouldBeNegativeInfinity()11        double.shouldBeInfinity()

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1        io.kotest.matchers.doubles.Infinity.shouldBePositiveInfinity(0.0)2    }3}4    Double.shouldBePositive()5    Double.shouldBeNonPositive()6    Double.shouldBeNonNegative()7    Double.shouldBeCloseTo()8    Double.shouldBeGreaterThan()9    Double.shouldBeLessThan()10    Double.shouldBeGreaterThanOrEqual()

Full Screen

Full Screen

Double.shouldBePositiveInfinity

Using AI Code Generation

copy

Full Screen

1+    0.0.shouldBePositiveInfinity()2+    0.0.shouldBeNegativeInfinity()3+    0.0.shouldBeFinite()4+    0.0.shouldBeNaN()5+    0.0.shouldBeZero()6+    0.0.shouldBeNonZero()7+    0.0.shouldBeOne()8+    0.0.shouldBePositive()9+    0.0.shouldBeNegative()10+    0.0.shouldBeNonPositive()11+    0.0.shouldBeNonNegative()12+    0.0.shouldBeLessThan(0.0)13+    0.0.shouldBeLessThanOrEqual(0.0)14+    0.0.shouldBeGreaterThan(0.0)

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