Skip to content

Commit

Permalink
Correct conversion between bytes and other units (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyarnd committed Oct 4, 2022
1 parent e848290 commit 5d7035b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Expand Up @@ -22,7 +22,7 @@ data class SizeInBytes(val size: Long) {
fun gigabytes() = convert(InformationUnit.Gigabytes)
fun gibibytes() = convert(InformationUnit.Gibibytes)

fun convert(unit: InformationUnit): Long = (unit.ratioToPrimary / size).toLong()
fun convert(unit: InformationUnit): Long = (size / unit.ratioToPrimary).toLong()

companion object {
fun parse(input: String): SizeInBytes? {
Expand Down
@@ -0,0 +1,24 @@
package com.sksamuel.hoplite.decoder

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class SizeInBytesDecoderTest : FunSpec({
val units = listOf(
InformationUnit.Kilobytes,
InformationUnit.Megabytes,
InformationUnit.Gigabytes,

InformationUnit.Octets,
InformationUnit.Kibibytes,
InformationUnit.Mebibytes,
InformationUnit.Gibibytes
)

test("conversion between bytes and other units") {
val size = 8
for (unit in units) {
SizeInBytes((size * unit.ratioToPrimary).toLong()).convert(unit) shouldBe size
}
}
})

0 comments on commit 5d7035b

Please sign in to comment.