How to use Collection.shouldHaveMean method of io.kotest.matchers.stats.matchers class

Best Kotest code snippet using io.kotest.matchers.stats.matchers.Collection.shouldHaveMean

StatsMatchersTest.kt

Source:StatsMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.stats2import io.kotest.data.forAll3import io.kotest.matchers.stats.shouldHaveMean4import io.kotest.matchers.stats.shouldHaveStandardDeviation5import io.kotest.matchers.stats.shouldHaveVariance6import io.kotest.matchers.stats.shouldNotHaveMean7import io.kotest.matchers.stats.shouldNotHaveStandardDeviation8import io.kotest.matchers.stats.shouldNotHaveVariance9import io.kotest.core.spec.style.StringSpec10import io.kotest.data.row11import java.math.BigDecimal12class StatsMatchersTest : StringSpec() {13   init {14      "Mean should be ZERO if collection is empty" {15         emptyList<Int>().shouldHaveMean(BigDecimal("0"))16         emptyList<Int>().shouldHaveMean(BigDecimal(0))17         emptyList<Int>().shouldHaveMean(BigDecimal(0.0))18         emptyList<Int>().shouldHaveMean(0.0)19         emptyList<Int>().shouldHaveMean(0.000000)20         emptyList<Int>().shouldHaveMean(BigDecimal("0"), 5)21         emptyList<Int>().shouldHaveMean(BigDecimal(0), 4)22         emptyList<Int>().shouldHaveMean(BigDecimal(0.0), 3)23         emptyList<Int>().shouldHaveMean(0.0, 2)24         emptyList<Int>().shouldHaveMean(0.000000, 1)25      }26      "Variance should be ZERO if collection is empty" {27         emptyList<Int>().shouldHaveVariance(BigDecimal("0"))28         emptyList<Int>().shouldHaveVariance(BigDecimal(0))29         emptyList<Int>().shouldHaveVariance(BigDecimal(0.0))30         emptyList<Int>().shouldHaveVariance(0.0)31         emptyList<Int>().shouldHaveVariance(0.000000)32         emptyList<Int>().shouldHaveVariance(BigDecimal("0"), 5)33         emptyList<Int>().shouldHaveVariance(BigDecimal(0), 4)34         emptyList<Int>().shouldHaveVariance(BigDecimal(0.0), 3)35         emptyList<Int>().shouldHaveVariance(0.0, 2)36         emptyList<Int>().shouldHaveVariance(0.000000, 1)37      }38      "Standard deviation should be ZERO if collection is empty" {39         emptyList<Int>().shouldHaveStandardDeviation(BigDecimal("0"))40         emptyList<Int>().shouldHaveStandardDeviation(BigDecimal(0))41         emptyList<Int>().shouldHaveStandardDeviation(BigDecimal(0.0))42         emptyList<Int>().shouldHaveStandardDeviation(0.0)43         emptyList<Int>().shouldHaveStandardDeviation(0.000000)44         emptyList<Int>().shouldHaveStandardDeviation(BigDecimal("0"), 5)45         emptyList<Int>().shouldHaveStandardDeviation(BigDecimal(0), 4)46         emptyList<Int>().shouldHaveStandardDeviation(BigDecimal(0.0), 3)47         emptyList<Int>().shouldHaveStandardDeviation(0.0, 2)48         emptyList<Int>().shouldHaveStandardDeviation(0.000000, 1)49      }50      "Collection<Int> should have correct mean value with default precision" {51         forAll(52            row(1.0, listOf(1)),53            row(0.0, listOf<Int>()),54            row(0.0, listOf(0)),55            row(0.0, listOf(-1, 1)),56            row(-0.0, listOf<Int>()),57            row(-0.0, listOf(0)),58            row(-0.0, listOf(-1, 1)),59            row(2.0, listOf(1, 3)),60            row(-2.0, listOf(-1, -3)),61            row(20.0, listOf(20, 10, 30)),62            row(2.000, listOf(1, 3)),63            row(0.3333, listOf(1, 0, 0)),64            row(1.3333, listOf(1, 2, 1)),65            row(266.6667, listOf(1, 2, 797)),66            row(1.5, listOf(1, 2)),67            row(0.0, listOf(-1, 1)),68            row(0.0909, listOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1))69         ) { expectedMean: Double, value: Collection<Int> ->70            value.shouldHaveMean(BigDecimal(expectedMean.toString()))71            value.shouldHaveMean(expectedMean)72         }73      }74      "Collection<Int> should NOT have correct mean value with default precision" {75         forAll(76            row(1.0, listOf(-1, 1)),77            row(0.1, listOf(-1, 1)),78            row(0.01, listOf(-1, 1)),79            row(0.001, listOf(-1, 1)),80            row(0.0001, listOf(-1, 1)),81            row(0.00001, listOf(-1, 1)),82            row(0.000001, listOf(-1, 1)),83            row(0.0000001, listOf(-1, 1)),84            row(0.00000001, listOf(-1, 1)),85            row(0.000000001, listOf(-1, 1)),86            row(0.0000000001, listOf(-1, 1)),87            row(266.6666, listOf(1, 2, 797)),88            row(266.6676, listOf(1, 2, 797)),89            row(266.6665, listOf(1, 2, 797)),90            row(266.66666, listOf(1, 2, 797)),91            row(266.66667, listOf(1, 2, 797)),92            row(266.66665, listOf(1, 2, 797)),93            row(266.666, listOf(1, 2, 797)),94            row(266.667, listOf(1, 2, 797)),95            row(266.665, listOf(1, 2, 797))96         ) { wrongMean: Double, value: Collection<Int> ->97            value.shouldNotHaveMean(BigDecimal(wrongMean.toString()))98            value.shouldNotHaveMean(wrongMean)99         }100      }101      "Collection<Int> should have correct mean with specific precision" {102         forAll(103            row(266.6666666667, 10, listOf(1, 2, 797)),104            row(266.666667, 6, listOf(1, 2, 797)),105            row(266.66667, 5, listOf(1, 2, 797)),106            row(266.6667, 4, listOf(1, 2, 797)),107            row(266.667, 3, listOf(1, 2, 797)),108            row(266.67, 2, listOf(1, 2, 797)),109            row(266.7, 1, listOf(1, 2, 797)),110            row(267.0, 0, listOf(1, 2, 797))111         ) { mean: Double, precision: Int, value: Collection<Int> ->112            value.shouldHaveMean(BigDecimal(mean.toString()), precision)113            value.shouldHaveMean(mean, precision)114         }115      }116      "Collection<Double> should have correct mean value with default precision" {117         forAll(118            row(1.0, listOf(1.0)),119            row(0.0, listOf()),120            row(0.0, listOf(0.0)),121            row(0.0, listOf(-1.0, 1.0)),122            row(-0.0, listOf()),123            row(-0.0, listOf(0.0)),124            row(-0.0, listOf(-1.0, 1.0)),125            row(2.0, listOf(1.0, 3.0)),126            row(-2.0, listOf(-1.0, -3.0)),127            row(20.0, listOf(20.0, 10.0, 30.0)),128            row(2.000, listOf(1.0, 3.0)),129            row(0.3333, listOf(1.0, 0.0, 0.0)),130            row(1.3333, listOf(1.0, 2.0, 1.0)),131            row(266.6667, listOf(1.0, 2.0, 797.0)),132            row(1.5, listOf(1.0, 2.0)),133            row(0.0, listOf(-1.0, 1.0)),134            row(0.0909, listOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0))135         ) { expectedMean: Double, value: Collection<Double> ->136            value.shouldHaveMean(BigDecimal(expectedMean.toString()))137            value.shouldHaveMean(expectedMean)138         }139      }140      "Collection<Double> should NOT have correct mean value with default precision" {141         forAll(142            row(1.0, listOf(-1.0, 1.0)),143            row(0.1, listOf(-1.0, 1.0)),144            row(0.01, listOf(-1.0, 1.0)),145            row(0.001, listOf(-1.0, 1.0)),146            row(0.0001, listOf(-1.0, 1.0)),147            row(0.00001, listOf(-1.0, 1.0)),148            row(0.000001, listOf(-1.0, 1.0)),149            row(0.0000001, listOf(-1.0, 1.0)),150            row(0.00000001, listOf(-1.0, 1.0)),151            row(0.000000001, listOf(-1.0, 1.0)),152            row(0.0000000001, listOf(-1.0, 1.0)),153            row(266.6666, listOf(1.0, 2.0, 797.0)),154            row(266.6676, listOf(1.0, 2.0, 797.0)),155            row(266.6665, listOf(1.0, 2.0, 797.0)),156            row(266.66666, listOf(1.0, 2.0, 797.0)),157            row(266.66667, listOf(1.0, 2.0, 797.0)),158            row(266.66665, listOf(1.0, 2.0, 797.0)),159            row(266.666, listOf(1.0, 2.0, 797.0)),160            row(266.667, listOf(1.0, 2.0, 797.0)),161            row(266.665, listOf(1.0, 2.0, 797.0))162         ) { wrongMean: Double, value: Collection<Double> ->163            value.shouldNotHaveMean(BigDecimal(wrongMean.toString()))164            value.shouldNotHaveMean(wrongMean)165         }166      }167      "Collection<Double> should have correct mean with specific precision" {168         forAll(169            row(266.6666666667, 10, listOf(1.0, 2.0, 797.0)),170            row(266.666667, 6, listOf(1.0, 2.0, 797.0)),171            row(266.66667, 5, listOf(1.0, 2.0, 797.0)),172            row(266.6667, 4, listOf(1.0, 2.0, 797.0)),173            row(266.667, 3, listOf(1.0, 2.0, 797.0)),174            row(266.67, 2, listOf(1.0, 2.0, 797.0)),175            row(266.7, 1, listOf(1.0, 2.0, 797.0)),176            row(267.0, 0, listOf(1.0, 2.0, 797.0))177         ) { mean: Double, precision: Int, value: Collection<Double> ->178            value.shouldHaveMean(BigDecimal(mean.toString()), precision)179            value.shouldHaveMean(mean, precision)180         }181      }182      "Collection<Number> should have correct mean with mix types" {183         forAll(184            row(266.6666666667, 10, listOf<Number>(1.0f, 2, 797.0)),185            row(3.43333, 5, listOf<Number>(1, 2.0, 3.1f, 4L, 5.toBigInteger(), BigDecimal("5.5")))186         ) { mean: Double, precision: Int, value: Collection<Number> ->187            value.shouldHaveMean(BigDecimal(mean.toString()), precision)188            value.shouldHaveMean(mean, precision)189         }190      }191      "Collection<Int> should have correct variance value with default precision" {192         forAll(193            row(0.6667, listOf(1, 3, 2)),194            row(0.6667, listOf(-1, -3, -2)),195            row(4.2222, listOf(-1, 2, -3)),196            row(0.0, listOf(1))197         ) { variance: Double, collection: Collection<Int> ->198            collection.shouldHaveVariance(BigDecimal(variance.toString()))199            collection.shouldHaveVariance(variance)200         }201      }202      "Collection<Int> should NOT have correct variance value with default precision" {203         forAll(204            row(0.1234, listOf(1, 3, 2)),205            row(0.6666, listOf(-1, -3, -2)),206            row(4.0, listOf(-1, 2, -3)),207            row(0.00000001, listOf(1))208         ) { variance: Double, collection: Collection<Int> ->209            collection.shouldNotHaveVariance(BigDecimal(variance.toString()))210            collection.shouldNotHaveVariance(variance)211         }212      }213      "Collection<Int> should have correct variance value with specific precision" {214         forAll(215            row(1.0, 0, listOf(1, 3, 2)),216            row(0.7, 1, listOf(1, 3, 2)),217            row(0.67, 2, listOf(1, 3, 2)),218            row(0.667, 3, listOf(1, 3, 2)),219            row(0.6667, 4, listOf(1, 3, 2)),220            row(0.66667, 5, listOf(1, 3, 2))221         ) { variance: Double, precision: Int, collection: Collection<Int> ->222            collection.shouldHaveVariance(BigDecimal(variance.toString()), precision)223            collection.shouldHaveVariance(variance, precision)224         }225      }226      "Collection<Double> should have correct variance value with default precision" {227         forAll(228            row(0.6667, listOf(1.0, 3.0, 2.0)),229            row(0.6667, listOf(-1.0, -3.0, -2.0)),230            row(4.2222, listOf(-1.0, 2.0, -3.0)),231            row(0.0, listOf(1.0))232         ) { variance: Double, collection: Collection<Double> ->233            collection.shouldHaveVariance(BigDecimal(variance.toString()))234            collection.shouldHaveVariance(variance)235         }236      }237      "Collection<Double> should NOT have correct variance value with default precision" {238         forAll(239            row(0.1234, listOf(1.0, 3.0, 2.0)),240            row(0.6666, listOf(-1.0, -3.0, -2.0)),241            row(4.0, listOf(-1.0, 2.0, -3.0)),242            row(0.00000001, listOf(1.0))243         ) { variance: Double, collection: Collection<Double> ->244            collection.shouldNotHaveVariance(BigDecimal(variance.toString()))245            collection.shouldNotHaveVariance(variance)246         }247      }248      "Collection<Double> should have correct variance value with specific precision" {249         forAll(250            row(1.0, 0, listOf(1.0, 3.0, 2.0)),251            row(0.7, 1, listOf(1.0, 3.0, 2.0)),252            row(0.67, 2, listOf(1.0, 3.0, 2.0)),253            row(0.667, 3, listOf(1.0, 3.0, 2.0)),254            row(0.6667, 4, listOf(1.0, 3.0, 2.0)),255            row(0.66667, 5, listOf(1.0, 3.0, 2.0))256         ) { variance: Double, precision: Int, collection: Collection<Double> ->257            collection.shouldHaveVariance(BigDecimal(variance.toString()), precision)258            collection.shouldHaveVariance(variance, precision)259         }260      }261      "Collection<Int> should have correct standard deviation value with default precision" {262         forAll(263            row(0.8165, listOf(1, 3, 2)),264            row(0.8165, listOf(-1, -3, -2)),265            row(2.0548, listOf(-1, 2, -3)),266            row(0.0, listOf(1))267         ) { standardDeviation: Double, collection: Collection<Int> ->268            collection.shouldHaveStandardDeviation(BigDecimal(standardDeviation.toString()))269            collection.shouldHaveStandardDeviation(standardDeviation)270         }271      }272      "Collection<Int> should NOT have correct standard deviation value with default precision" {273         forAll(274            row(0.8165, listOf(1, 3, 2, 4)),275            row(0.81, listOf(-1, -3, -2, 0)),276            row(2.0548, listOf(-1, 2)),277            row(0.0, listOf(10, 1))278         ) { standardDeviation: Double, collection: Collection<Int> ->279            collection.shouldNotHaveStandardDeviation(BigDecimal(standardDeviation.toString()))280            collection.shouldNotHaveStandardDeviation(standardDeviation)281         }282      }283      "Collection<Int> should have correct standard deviation value with specific precision" {284         forAll(285            row(1.0, 0, listOf(1, 3, 2)),286            row(0.8, 1, listOf(1, 3, 2)),287            row(0.82, 2, listOf(1, 3, 2)),288            row(0.816, 3, listOf(1, 3, 2)),289            row(2.0, 0, listOf(-1, 2, -3)),290            row(2.1, 1, listOf(-1, 2, -3)),291            row(2.05, 2, listOf(-1, 2, -3)),292            row(2.055, 3, listOf(-1, 2, -3))293         ) { standardDeviation: Double, precision: Int, collection: Collection<Int> ->294            collection.shouldHaveStandardDeviation(BigDecimal(standardDeviation.toString()), precision)295            collection.shouldHaveStandardDeviation(standardDeviation, precision)296         }297      }298      "Collection<Double> should have correct standard deviation value with default precision" {299         forAll(300            row(0.8165, listOf(1.0, 3.0, 2.0)),301            row(0.8165, listOf(-1.0, -3.0, -2.0)),302            row(2.0548, listOf(-1.0, 2.0, -3.0)),303            row(0.0, listOf(1.0))304         ) { standardDeviation: Double, collection: Collection<Double> ->305            collection.shouldHaveStandardDeviation(BigDecimal(standardDeviation.toString()))306            collection.shouldHaveStandardDeviation(standardDeviation)307         }308      }309      "Collection<Double> should NOT have correct standard deviation value with default precision" {310         forAll(311            row(0.8165, listOf(1.0, 3.0, 2.0, 4.0)),312            row(0.81, listOf(-1.0, -3.0, -2.0, 0.0)),313            row(2.0548, listOf(-1.0, 2.0)),314            row(0.0, listOf(10.0, 1.0))315         ) { standardDeviation: Double, collection: Collection<Double> ->316            collection.shouldNotHaveStandardDeviation(BigDecimal(standardDeviation.toString()))317            collection.shouldNotHaveStandardDeviation(standardDeviation)318         }319      }320      "Collection<Double> should have correct standard deviation value with specific precision" {321         forAll(322            row(1.0, 0, listOf(1.0, 3.0, 2.0)),323            row(0.8, 1, listOf(1.0, 3.0, 2.0)),324            row(0.82, 2, listOf(1.0, 3.0, 2.0)),325            row(0.816, 3, listOf(1.0, 3.0, 2.0)),326            row(2.0, 0, listOf(-1.0, 2.0, -3.0)),327            row(2.1, 1, listOf(-1.0, 2.0, -3.0)),328            row(2.05, 2, listOf(-1.0, 2.0, -3.0)),329            row(2.055, 3, listOf(-1.0, 2.0, -3.0))330         ) { standardDeviation: Double, precision: Int, collection: Collection<Double> ->331            collection.shouldHaveStandardDeviation(BigDecimal(standardDeviation.toString()), precision)332            collection.shouldHaveStandardDeviation(standardDeviation, precision)333         }334      }335      "check matchers with big numbers" {336         forAll(337            row(338               BigDecimal("493833345678.55532725"),339               BigDecimal("243859180040573015450155.6869068511"),340               BigDecimal("493820999999.56767275"),341               listOf(342                  12345678.9876545,343                  987654345678.123344               ))345         ) { mean, variance, standardDeviation, collection: Collection<Number> ->346            collection.shouldHaveMean(mean, 10)347            collection.shouldHaveVariance(variance, 10)348            collection.shouldHaveStandardDeviation(standardDeviation, 10)349         }350      }351   }352}...

Full Screen

Full Screen

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1package io.kotest.matchers.stats2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6import java.math.BigDecimal7import java.math.MathContext8import java.math.RoundingMode9import kotlin.math.sqrt10/**11 * Asserts that mean of the Collection elements equals to [value] with default12 * or specific [precision]. Default precision equals 4 digits after decimal point.13 *14 * Opposite of [shouldNotHaveMean]15 *16 * Example:17 * ```18 *19 * val collection = listOf(1, 1, 3)20 * val firstMean   = BigDecimal("1.66667")21 * val secondMean = BigDecimal("1.6667")22 *23 * collection.shouldHaveMean(firstMean, 5)      // Assertion passes24 * collection.shouldHaveMean(secondMean)       // Assertion passes25 *26 * collection.shouldHaveMean(firstMean)         // Assertion fails27 * collection.shouldHaveMean(secondMean, 5)    // Assertion fails28 * ```29 *30 * @param value - expected mean value31 * @param precision - precision, by default - 4 digits after decimal point32 *33 */34fun <T : Number> Collection<T>.shouldHaveMean(value: BigDecimal, precision: Int = 4) = this should haveMean<T>(value, precision)35/**36 * Asserts that mean of the Collection elements equals to [value] with default37 * or specific [precision]. Default precision equals 4 digits after decimal point.38 *39 * Opposite of [shouldNotHaveMean]40 *41 * Example:42 * ```43 *44 * val collection = listOf(1, 1, 3)45 * val firstMean   = 1.6666746 * val secondMean = 1.666747 *48 * collection.shouldHaveMean(firstMean, 5)      // Assertion passes49 * collection.shouldHaveMean(secondMean)       // Assertion passes50 *51 * collection.shouldHaveMean(firstMean)         // Assertion fails52 * collection.shouldHaveMean(secondMean, 5)    // Assertion fails53 * ```54 *55 * @param value - expected mean value56 * @param precision - precision, by default - 4 digits after decimal point57 *58 */59fun <T : Number> Collection<T>.shouldHaveMean(value: Double, precision: Int = 4) = this should haveMean<T>(value, precision)60/**61 * Asserts that mean of the Collection elements doesn't equal to [value] with default62 * or specific [precision]. Default precision equals 4 digits after decimal point.63 *64 * Opposite of [shouldHaveMean]65 *66 * Example:67 * ```68 *69 * val collection = listOf(1, 1, 3)70 * val firstMean   = BigDecimal("2.0")71 * val secondMean = BigDecimal("1.6666667")72 *73 * collection.shouldNotHaveMean(firstMean)                // Assertion passes74 * collection.shouldNotHaveMean(secondMean, 5)           // Assertion passes75 *76 * collection.shouldNotHaveMean(BigDecimal("1.6667"))    // Assertion fails77 * collection.shouldNotHaveMean(BigDecimal("1.6667"), 4) // Assertion fails78 * ```79 *80 * @param value - not expected mean value81 * @param precision - precision, by default - 4 digits after decimal point82 *83 */84fun <T : Number> Collection<T>.shouldNotHaveMean(value: BigDecimal, precision: Int = 4) = this shouldNot haveMean<T>(value, precision)85/**86 * Asserts that mean of the Collection elements doesn't equal to [value] with default87 * or specific [precision]. Default precision equals 4 digits after decimal point.88 *89 * Opposite of [shouldHaveMean]90 *91 * Example:92 * ```93 *94 * val collection = listOf(1, 1, 3)95 *96 * collection.shouldNotHaveMean(2.0)          // Assertion passes97 * collection.shouldNotHaveMean(1.67, 5)      // Assertion passes98 *99 * collection.shouldNotHaveMean(1.6667)       // Assertion fails100 * collection.shouldNotHaveMean(1.6667, 4)    // Assertion fails101 * ```102 *103 * @param value - not expected mean value104 * @param precision - precision, by default - 4 digits after decimal point105 *106 */107fun <T : Number> Collection<T>.shouldNotHaveMean(value: Double, precision: Int = 4) = this shouldNot haveMean<T>(value, precision)108/**109 * Asserts that variance of the Collection elements equals to [value] with default110 * or specific [precision]. Default precision equals 4 digits after decimal point.111 *112 * Opposite of [shouldNotHaveVariance]113 *114 * Example:115 * ```116 *117 * val collection = listOf(1, 2, 3)118 * val firstVariance   = BigDecimal("0.66667")119 * val secondVariance = BigDecimal("0.6667")120 *121 * collection.shouldHaveVariance(firstVariance, 5)      // Assertion passes122 * collection.shouldHaveVariance(secondVariance)       // Assertion passes123 *124 * collection.shouldHaveVariance(firstVariance)         // Assertion fails125 * collection.shouldHaveVariance(secondVariance, 5)    // Assertion fails126 * ```127 *128 * @param value - expected variance value129 * @param precision - precision, by default - 4 digits after decimal point130 *131 */132fun <T : Number> Collection<T>.shouldHaveVariance(value: BigDecimal, precision: Int = 4) = this should haveVariance<T>(value, precision)133/**134 * Asserts that variance of the Collection elements equals to [value] with default135 * or specific [precision]. Default precision equals 4 digits after decimal point.136 *137 * Opposite of [shouldNotHaveVariance]138 *139 * Example:140 * ```141 *142 * val collection = listOf(1, 2, 3)143 *144 * collection.shouldHaveVariance(0.66667, 5)      // Assertion passes145 * collection.shouldHaveVariance(0.6667)          // Assertion passes146 *147 * collection.shouldHaveVariance(0.67)         // Assertion fails148 * collection.shouldHaveVariance(0.6667, 5)    // Assertion fails149 * ```150 *151 * @param value - expected variance value152 * @param precision - precision, by default - 4 digits after decimal point153 *154 */155fun <T : Number> Collection<T>.shouldHaveVariance(value: Double, precision: Int = 4) = this should haveVariance<T>(value, precision)156/**157 * Asserts that variance of the Collection elements doesn't equal to [value] with default158 * or specific [precision]. Default precision equals 4 digits after decimal point.159 *160 * Opposite of [shouldHaveVariance]161 *162 * Example:163 * ```164 *165 * val collection = listOf(1, 2, 3)166 *167 * collection.shouldNotHaveVariance(BigDecimal("1.01"), 5)        // Assertion passes168 * collection.shouldNotHaveVariance(BigDecimal("0.666667"))       // Assertion passes169 *170 * collection.shouldNotHaveVariance(BigDecimal("0.6667"))         // Assertion fails171 * collection.shouldNotHaveVariance(BigDecimal("0.66667"), 5)     // Assertion fails172 * ```173 *174 * @param value - not expected variance value175 * @param precision - precision, by default - 4 digits after decimal point176 *177 */178fun <T : Number> Collection<T>.shouldNotHaveVariance(value: BigDecimal, precision: Int = 4) = this shouldNot haveVariance<T>(value, precision)179/**180 * Asserts that variance of the Collection elements doesn't equal to [value] with default181 * or specific [precision]. Default precision equals 4 digits after decimal point.182 *183 * Opposite of [shouldHaveVariance]184 *185 * Example:186 * ```187 *188 * val collection = listOf(1, 2, 3)189 *190 * collection.shouldNotHaveVariance(1.01, 5)        // Assertion passes191 * collection.shouldNotHaveVariance(0.666667)       // Assertion passes192 *193 * collection.shouldNotHaveVariance(0.6667)         // Assertion fails194 * collection.shouldNotHaveVariance(0.66667, 5)     // Assertion fails195 * ```196 *197 * @param value - not expected variance value198 * @param precision - precision, by default - 4 digits after decimal point199 *200 */201fun <T : Number> Collection<T>.shouldNotHaveVariance(value: Double, precision: Int = 4) = this shouldNot haveVariance<T>(value, precision)202/**203 * Asserts that standard deviation of the Collection elements equals to [value] with default204 * or specific [precision]. Default precision equals 4 digits after decimal point.205 *206 * Opposite of [shouldNotHaveStandardDeviation]207 *208 * Example:209 * ```210 *211 * val collection = listOf(1, 2, 3)212 *213 * collection.shouldHaveStandardDeviation(BigDecimal("0.82"), 2)      // Assertion passes214 * collection.shouldHaveStandardDeviation(BigDecimal("0.8165"))       // Assertion passes215 *216 * collection.shouldHaveStandardDeviation(BigDecimal("0.82"))         // Assertion fails217 * collection.shouldHaveStandardDeviation(BigDecimal("0.8165"), 5)    // Assertion fails218 * ```219 *220 * @param value - expected standard deviation value221 * @param precision - precision, by default - 4 digits after decimal point222 *223 */224fun <T : Number> Collection<T>.shouldHaveStandardDeviation(value: BigDecimal, precision: Int = 4) = this should haveStandardDeviation<T>(value, precision)225/**226 * Asserts that standard deviation of the Collection elements equals to [value] with default227 * or specific [precision]. Default precision equals 4 digits after decimal point.228 *229 * Opposite of [shouldNotHaveStandardDeviation]230 *231 * Example:232 * ```233 *234 * val collection = listOf(1, 2, 3)235 *236 * collection.shouldHaveStandardDeviation(0.82, 2)      // Assertion passes237 * collection.shouldHaveStandardDeviation(0.8165)       // Assertion passes238 *239 * collection.shouldHaveStandardDeviation(0.82)         // Assertion fails240 * collection.shouldHaveStandardDeviation(0.8165, 5)    // Assertion fails241 * ```242 *243 * @param value - expected standard deviation value244 * @param precision - precision, by default - 4 digits after decimal point245 *246 */247fun <T : Number> Collection<T>.shouldHaveStandardDeviation(value: Double, precision: Int = 4) = this should haveStandardDeviation<T>(value, precision)248/**249 * Asserts that standard deviation of the Collection elements doesn't equal to [value] with default250 * or specific [precision]. Default precision equals 4 digits after decimal point.251 *252 * Opposite of [shouldHaveStandardDeviation]253 *254 * Example:255 * ```256 *257 * val collection = listOf(1, 2, 3)258 *259 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.8165"), 5)    // Assertion passes260 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.8333"))       // Assertion passes261 *262 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.8165"))       // Assertion fails263 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.82"), 2)      // Assertion fails264 * ```265 *266 * @param value - not expected standard deviation value267 * @param precision - precision, by default - 4 digits after decimal point268 *269 */270fun <T : Number> Collection<T>.shouldNotHaveStandardDeviation(value: BigDecimal, precision: Int = 4) = this shouldNot haveStandardDeviation<T>(value, precision)271/**272 * Asserts that standard deviation of the Collection elements doesn't equal to [value] with default273 * or specific [precision]. Default precision equals 4 digits after decimal point.274 *275 * Opposite of [shouldHaveStandardDeviation]276 *277 * Example:278 * ```279 *280 * val collection = listOf(1, 2, 3)281 *282 * collection.shouldNotHaveStandardDeviation(0.8165, 5)    // Assertion passes283 * collection.shouldNotHaveStandardDeviation(0.8333)       // Assertion passes284 *285 * collection.shouldNotHaveStandardDeviation(0.8165)       // Assertion fails286 * collection.shouldNotHaveStandardDeviation(0.82, 2)      // Assertion fails287 * ```288 *289 * @param value - not expected standard deviation value290 * @param precision - precision, by default - 4 digits after decimal point291 *292 */293fun <T : Number> Collection<T>.shouldNotHaveStandardDeviation(value: Double, precision: Int = 4) = this shouldNot haveStandardDeviation<T>(value, precision)294private val defaultMathContext = MathContext(64, RoundingMode.HALF_UP)295private fun BigDecimal.round(precision: Int): BigDecimal = this.setScale(precision, RoundingMode.HALF_UP).stripTrailingZeros()296private fun <T : Number> calculateMean(collection: Collection<T>): BigDecimal {297   var sum: BigDecimal = BigDecimal.ZERO298   for (elem in collection) {299      sum += BigDecimal(elem.toString())300   }301   return sum.divide(BigDecimal(collection.size), defaultMathContext)302}303private fun <T : Number> calculateVariance(collection: Collection<T>): BigDecimal {304   val mean: BigDecimal = calculateMean(collection)305   var sumOfSquaredDifferences: BigDecimal = BigDecimal.ZERO306   for (elem in collection) {307      sumOfSquaredDifferences += (BigDecimal(elem.toString()) - mean).pow(2)308   }309   return sumOfSquaredDifferences.divide(BigDecimal(collection.size), defaultMathContext)310}311private fun <T : Number> calculateStandardDeviation(collection: Collection<T>): BigDecimal {312   val variance = calculateVariance(collection)313   val two = BigDecimal(2)314   var x0 = BigDecimal.ZERO315   var x1 = BigDecimal(sqrt(variance.toDouble()))316   while (x0 != x1) {317      x0 = x1318      x1 = variance.divide(x0, defaultMathContext)319      x1 = x1.add(x0)320      x1 = x1.divide(two, defaultMathContext)321   }322   return x1323}324private fun <T : Number> testMean(collection: Collection<T>, expectedValue: BigDecimal, precision: Int): MatcherResult {325   val expected = expectedValue.stripTrailingZeros()326   val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateMean(collection).round(precision)327   return MatcherResult(328      expected.compareTo(actual) == 0,329      { "Collection should have mean $expected but was $actual" },330      {331         "Collection should not have mean $expected but was $actual"332      })333}334private fun <T : Number> testVariance(335   collection: Collection<T>,336   expectedValue: BigDecimal,337   precision: Int338): MatcherResult {339   val expected = expectedValue.stripTrailingZeros()340   val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateVariance(collection).round(precision)341   return MatcherResult(342      expected.compareTo(actual) == 0,343      { "Collection should have variance $expected but was $actual" },344      {345         "Collection should not have variance $expected but was $actual"346      })347}348private fun <T : Number> testStandardDeviation(349   collection: Collection<T>,350   expectedValue: BigDecimal,351   precision: Int352): MatcherResult {353   val expected = expectedValue.stripTrailingZeros()354   val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateStandardDeviation(collection).round(precision)355   return MatcherResult(356      expected.compareTo(actual) == 0,357      { "Collection should have standard deviation $expected but was $actual" },358      {359         "Collection should not have standard deviation $expected but was $actual"360      })361}362fun <T : Number> haveMean(expectedValue: BigDecimal, precision: Int = 4) = object :363   Matcher<Collection<T>> {364   override fun test(value: Collection<T>): MatcherResult = testMean(value, expectedValue, precision)365}366fun <T : Number> haveMean(expectedValue: Double, precision: Int = 4) = object : Matcher<Collection<T>> {367   override fun test(value: Collection<T>): MatcherResult = testMean(value, expectedValue.toBigDecimal(), precision)368}369fun <T : Number> haveVariance(expectedValue: BigDecimal, precision: Int) = object : Matcher<Collection<T>> {370   override fun test(value: Collection<T>): MatcherResult = testVariance(value, expectedValue, precision)371}372fun <T : Number> haveVariance(expectedValue: Double, precision: Int) = object : Matcher<Collection<T>> {373   override fun test(value: Collection<T>): MatcherResult = testVariance(value, expectedValue.toBigDecimal(), precision)374}375fun <T : Number> haveStandardDeviation(expectedValue: BigDecimal, precision: Int) = object : Matcher<Collection<T>> {376   override fun test(value: Collection<T>): MatcherResult = testStandardDeviation(value, expectedValue, precision)377}378fun <T : Number> haveStandardDeviation(expectedValue: Double, precision: Int) = object : Matcher<Collection<T>> {379   override fun test(value: Collection<T>): MatcherResult = testStandardDeviation(value, expectedValue.toBigDecimal(), precision)380}...

Full Screen

Full Screen

Collection.shouldHaveMean

Using AI Code Generation

copy

Full Screen

1    shouldHaveMean(2.0)2    shouldHaveMedian(2.0)3    shouldHaveStandardDeviation(0.0)4    shouldHaveVariance(0.0)5    shouldHaveRange(0.0..4.0)6    shouldHaveInterquartileRange(0.0..4.0)7    shouldHaveMode(2.0)8    shouldHaveSkewness(0.0)9    shouldHaveKurtosis(0.0)10    shouldHaveSum(10.0)11    shouldHaveSumOfSquares(20.0)12    shouldHaveSumOfLogs(2.302585092994046)13    shouldHaveSumOfSquaresOfDifferences(20.0)14    shouldHaveGeometricMean(2.0)

Full Screen

Full Screen

Collection.shouldHaveMean

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.shouldBe2import io.kotest.matchers.stats.matchers.shouldHaveMean3val data = listOf(1.0, 2.0, 3.0, 4.0, 5.0)4import io.kotest.matchers.shouldBe5import io.kotest.matchers.stats.matchers.shouldHaveMedian6val data = listOf(1.0, 2.0, 3.0, 4.0, 5.0)7import io.kotest.matchers.shouldBe8import io.kotest.matchers.stats.matchers.shouldHaveMode9val data = listOf(1.0, 2.0, 3.0, 4.0, 5.0)10import io.kotest.matchers.shouldBe11import io.kotest.matchers.stats.matchers.shouldHaveRange12val data = listOf(1.0, 2.0, 3.0, 4.0, 5.0)13import io.kotest.matchers.shouldBe14import io.kotest.matchers.stats.matchers.shouldHaveVariance15val data = listOf(1.0, 2.0, 3.0, 4.0, 5.0)

Full Screen

Full Screen

Collection.shouldHaveMean

Using AI Code Generation

copy

Full Screen

1val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )2 list . shouldHaveMean ( 3.0 )3val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )4 list . shouldHaveMedian ( 3.0 )5val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )6 list . shouldHaveMode ( 3.0 )7val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )8 list . shouldHaveStandardDeviation ( 1.6 )9val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )10 list . shouldHaveVariance ( 2.5 )11val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )12 list . shouldHaveRange ( 4.0 )13val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )14 list . shouldHaveSum ( 15.0 )15val  list  =  listOf ( 1 ,  2 ,  3 ,  4 ,  5 )16 list . shouldHaveSumOfSquares ( 55.0 )17val  list  =  listOf ( 1 ,

Full Screen

Full Screen

Collection.shouldHaveMean

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.shouldBe2import io.kotest.matchers.stats.matchers.shouldHaveMean3val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)4numbers.shouldHaveMean(5.5)5import io.kotest.matchers.shouldBe6import io.kotest.matchers.stats.matchers.shouldHaveMedian7val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)8numbers.shouldHaveMedian(5.5)9import io.kotest.matchers.shouldBe10import io.kotest.matchers.stats.matchers.shouldHaveMode11val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)12numbers.shouldHaveMode(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))13import io.kotest.matchers.shouldBe14import io.kotest.matchers.stats.matchers.shouldHaveRange15val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)16numbers.shouldHaveRange(1..10)17import io.kotest.matchers.shouldBe18import io.kotest.matchers.stats.matchers.shouldHaveStandardDeviation19val numbers = listOf(1, 2,

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful