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

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

matchers.kt

Source: matchers.kt Github

copy

Full Screen

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

haveVariance

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

haveVariance

Using AI Code Generation

copy

Full Screen

1haveVariance ( 0.05 ) should match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 ))2haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))3haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))4haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))5haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))6haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))7haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))8haveVariance ( 0.05 ) shouldNot match ( listOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ))

Full Screen

Full Screen

haveVariance

Using AI Code Generation

copy

Full Screen

1haveVariance ( 0.01 ) . should ( "have variance" ) { listOf ( 1 , 2 , 3 , 4 , 5 ) }2haveVariance ( 0.01 ) . should ( "have variance" ) { intArrayOf ( 1 , 2 , 3 , 4 , 5 ) }3haveVariance ( 0.01 ) . should ( "have variance" ) { floatArrayOf ( 1.0f , 2.0f , 3.0f , 4.0f , 5.0f ) }4haveVariance ( 0.01 ) . should ( "have variance" ) { doubleArrayOf ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 ) }5haveVariance ( 0.01 ) . should ( "have variance" ) { longArrayOf ( 1L , 2L , 3L , 4L , 5L ) }6haveVariance ( 0.01 ) . should ( "have variance" ) { shortArrayOf ( 1 , 2 , 3 , 4 , 5 ) }7haveVariance ( 0.01 ) . should ( "have variance" ) { byteArrayOf ( 1 , 2 , 3 , 4 , 5 ) }8haveVariance ( 0.01 ) . should ( "have variance" ) { charArrayOf ( 'a' , 'b' , 'c' , 'd' , 'e' ) }9haveVariance ( 0.01

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Are Agile Self-Managing Teams Realistic with Layered Management?

Agile software development stems from a philosophy that being agile means creating and responding to change swiftly. Agile means having the ability to adapt and respond to change without dissolving into chaos. Being Agile involves teamwork built on diverse capabilities, skills, and talents. Team members include both the business and software development sides working together to produce working software that meets or exceeds customer expectations continuously.

How To Refresh Page Using Selenium C# [Complete Tutorial]

When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.

An Interactive Guide To CSS Hover Effects

Building a website is all about keeping the user experience in mind. Ultimately, it’s about providing visitors with a mind-blowing experience so they’ll keep coming back. One way to ensure visitors have a great time on your site is to add some eye-catching text or image animations.

How To Handle Multiple Windows In Selenium Python

Automating testing is a crucial step in the development pipeline of a software product. In an agile development environment, where there is continuous development, deployment, and maintenance of software products, automation testing ensures that the end software products delivered are error-free.

A Complete Guide To CSS Houdini

As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????

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