How to use haveStandardDeviation method of io.kotest.matchers.stats.matchers class

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

matchers.kt

Source:matchers.kt Github

copy

Full Screen

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

haveStandardDeviation

Using AI Code Generation

copy

Full Screen

1haveStandardDeviation(1.0) shouldBe true2haveVariance(1.0) shouldBe true3haveMean(1.0) shouldBe true4haveMedian(1.0) shouldBe true5haveMode(1.0) shouldBe true6haveKurtosis(1.0) shouldBe true7haveSkewness(1.0) shouldBe true8haveRange(1.0, 2.0) shouldBe true9haveRange(1.0..2.0) shouldBe true10haveCoefficientOfVariation(1.0) shouldBe true11haveInterQuartileRange(1.0) shouldBe true12haveRange(1.0, 2.0) shouldBe true13haveRange(1.0..2.0) shouldBe true14haveCoefficientOfVariation(1.0) shouldBe true15haveInterQuartileRange(1.0) shouldBe true

Full Screen

Full Screen

haveStandardDeviation

Using AI Code Generation

copy

Full Screen

1haveStandardDeviation(0.0)2haveStandardDeviation(0.0, 0.0)3haveStandardDeviation(0.0, 0.0, 0.0)4haveStandardDeviation(0.0, 0.0, 0.0, 0.0)5haveStandardDeviation(0.0, 0.0, 0.0, 0.0, 0.0)6haveStandardDeviation(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)7haveStandardDeviation(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)

Full Screen

Full Screen

haveStandardDeviation

Using AI Code Generation

copy

Full Screen

1haveStandardDeviation(0.0) shouldBe true2shouldHaveStandardDeviation(0.0) shouldBe true3shouldNotHaveStandardDeviation(0.0) shouldBe true4shouldNotHaveStandardDeviation(0.0) shouldBe true5shouldHaveStandardDeviation(0.0) shouldBe true6shouldNotHaveStandardDeviation(0.0) shouldBe true7shouldNotHaveStandardDeviation(0.0) shouldBe true8shouldHaveStandardDeviation(0.0) shouldBe true9shouldNotHaveStandardDeviation(0.0) shouldBe true10shouldNotHaveStandardDeviation(0.0) shouldBe true11shouldHaveStandardDeviation(0.0) shouldBe true12shouldNotHaveStandardDeviation(0.0) shouldBe true13shouldNotHaveStandardDeviation(0.0) shouldBe true14shouldHaveStandardDeviation(0.0) shouldBe true

Full Screen

Full Screen

haveStandardDeviation

Using AI Code Generation

copy

Full Screen

1haveStandardDeviation(1.0)2haveStandardDeviation(1.0, 0.1)3haveStandardDeviation(1.0, 0.1, 0.1)4haveStandardDeviation(1.0, 0.1, 0.1, 0.1)5haveStandardDeviation(1.0, 0.1, 0.1, 0.1, 0.1)6haveStandardDeviation(1.0, 0.1, 0.1, 0.1, 0.1, 0.1)7haveStandardDeviation(1.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)

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