Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and optimize NumberAbbreviator and its tests #2045

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,34 +37,6 @@ 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"
}
val endTime = System.nanoTime()

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

"completes large numbers quickly" {
val iterations = 10000
val startTime = System.nanoTime()
repeat(iterations) {
NumberAbbreviator.abbreviate(mockContext, 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) }
})
Loading