Best Kotest code snippet using io.kotest.matchers.stats.matchers.haveVariance
matchers.kt
Source:matchers.kt
...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}...
haveVariance
Using AI Code Generation
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)
haveVariance
Using AI Code Generation
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 ))
haveVariance
Using AI Code Generation
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
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!