# How to use haveMean method of io.kotest.matchers.stats.matchers class

Best Kotest code snippet using io.kotest.matchers.stats.matchers.haveMean

matchers.kt

Source:matchers.kt

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

haveMean

Using AI Code Generation

`1haveMean(0.0)2haveMedian(0.0)3haveMode(0.0)4haveStandardDeviation(0.0)5haveVariance(0.0)6haveCoefficientOfVariation(0.0)7haveRange(0.0)8haveInterquartileRange(0.0)9haveMAD(0.0)10haveSkewness(0.0)11haveKurtosis(0.0)12haveSampleEntropy(0.0)13haveSampleSkewness(0.0)14haveSampleKurtosis(0.0)15haveSum(0.0)16haveProduct(0.0)17haveGeometricMean(0.0)18haveHarmonicMean(0`

haveMean

Using AI Code Generation

`1haveMean(1.0) shouldBe true2haveMedian(1.0) shouldBe true3haveMode(1) shouldBe true4haveVariance(1.0) shouldBe true5haveStandardDeviation(1.0) shouldBe true6haveSkewness(1.0) shouldBe true7haveKurtosis(1.0) shouldBe true8haveRange(1.0) shouldBe true9haveInterQuartileRange(1.0) shouldBe true10haveSum(1.0) shouldBe true11haveSumOfSquares(1.0) shouldBe true12haveSumOfLogs(1.0) shouldBe true13haveSumOfSquaresOfDifferences(1.0) shouldBe true14haveGeometricMean(1.0) shouldBe true15haveHarmonicMean(1.0) shouldBe true16haveMeanAbsoluteDeviation(1.0)`

haveMean

Using AI Code Generation

`1 data class Person(val name: String, val age: Int, val height: Double)2 val people = listOf(Person("Alice", 30, 1.7), Person("Bob", 25, 1.8))3 people should haveMean(Person::height, 1.75)4 data class Person(val name: String, val age: Int, val height: Double)5 val people = listOf(Person("Alice", 30, 1.7), Person("Bob", 25, 1.8))6 people should haveMedian(Person::height, 1.75)7 data class Person(val name: String, val age: Int, val height: Double)8 val people = listOf(Person("Alice", 30, 1.7), Person("Bob", 25, 1.8))9 people should haveMode(Person::height, 1.75)10 data class Person(val name: String, val age: Int, val height: Double)11 val people = listOf(Person("Alice", 30, 1.7), Person("Bob", 25, 1.8))12 people should haveVariance(Person::height, 1.75)13 data class Person(val name: String, val age: Int, val height: Double)14 val people = listOf(Person("Alice", 30, 1.7), Person("Bob", 25, 1.8))15 people should haveStandardDeviation(Person::height, 1.75)16 data class Person(val name: String, val age: Int, val height: Double)17 val people = listOf(Person("Alice", 30, 1.7), Person("Bob", 25, 1.8))18 people should haveSkewness(Person::height, 1.75)`

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