Skip to content

Commit

Permalink
Optimize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgomezrico committed Oct 28, 2023
1 parent 7c1a682 commit 82e4d12
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package com.habitrpg.common.habitica.helpers

import android.content.Context
import kotlin.math.abs
import kotlin.math.min

object NumberAbbreviator {

fun abbreviate(context: Context?, number: Float, numberOfDecimals: Int = 2, minForAbbrevation: Int = 0): String {
return abbreviate(context, number.toDouble(), numberOfDecimals, minForAbbrevation)
}

fun abbreviate(context: Context?, number: Double, numberOfDecimals: Int = 2, minForAbbrevation: Int = 0): String {
fun abbreviate(number: Double, numberOfDecimals: Int = 2, minForAbbrevation: Int = 0): String {
val decimalCount = if (number != 0.0 && number > -1 && number < 1 && numberOfDecimals == 0) 2 else numberOfDecimals
val absNumber = abs(number)
var usedNumber = absNumber

var counter = 0
while (usedNumber >= 1000 && absNumber >= minForAbbrevation) {
counter++
usedNumber /= 1000
}

val parts = usedNumber.toString().split(".")
var result = parts[0]
if (parts.size == 2) {
Expand All @@ -29,9 +25,11 @@ object NumberAbbreviator {
result = "$result.$decimal"
}
}

if (number < 0) {
result = "-$result"
}

return result + abbreviationForCounter(counter)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ import io.mockk.mockk
import java.util.Locale

class NumberAbbreviatorTest : StringSpec({
val mockContext = mockk<Context>()
beforeSpec {
Locale.setDefault(Locale.US)
every { mockContext.getString(com.habitrpg.common.habitica.R.string.thousand_abbrev) } returns "k"
every { mockContext.getString(com.habitrpg.common.habitica.R.string.million_abbrev) } returns "m"
every { mockContext.getString(com.habitrpg.common.habitica.R.string.billion_abbrev) } returns "b"
every { mockContext.getString(com.habitrpg.common.habitica.R.string.trillion_abbrev) } returns "t"
every { mockContext.getString(com.habitrpg.common.habitica.R.string.quadrillion_abbrev) } returns "q"
}

withData(
Expand All @@ -43,14 +37,14 @@ class NumberAbbreviatorTest : StringSpec({
Triple(0.328, "0.32", 0),
Triple(-0.99, "-0.99", 0)
) { (input, output, decimals) ->
NumberAbbreviator.abbreviate(mockContext, input, decimals) shouldBe output
NumberAbbreviator.abbreviate(input, decimals) shouldBe output
}

"completes quickly" {
val iterations = 10000
val startTime = System.nanoTime()
repeat(iterations) {
NumberAbbreviator.abbreviate(mockContext, 201.5, 2) shouldBe "201.5"
NumberAbbreviator.abbreviate( 201.5, 2)
}
val endTime = System.nanoTime()

Expand All @@ -63,14 +57,12 @@ class NumberAbbreviatorTest : StringSpec({
val iterations = 10000
val startTime = System.nanoTime()
repeat(iterations) {
NumberAbbreviator.abbreviate(mockContext, 1.9943212354213233E30, 2) shouldBe "1.99"
NumberAbbreviator.abbreviate( 1.9943212354213233E30, 2) shouldBe "1.99"
}
val endTime = System.nanoTime()

val averageDuration = (endTime - startTime) / iterations
print("Average duration: $averageDuration")
averageDuration shouldBeLessThan 2000
}

afterSpec { clearMocks(mockContext) }
})

0 comments on commit 82e4d12

Please sign in to comment.