From a08930c66ff4d418df644930fffaa9226ec3857b Mon Sep 17 00:00:00 2001 From: Sachin Shah Date: Wed, 26 Jul 2017 16:45:18 -0700 Subject: [PATCH] Override toString() to return unsigned value --- src/main/kotlin/unsigned/Ubyte.kt | 2 ++ src/main/kotlin/unsigned/Uint.kt | 2 ++ src/main/kotlin/unsigned/Ulong.kt | 2 ++ src/main/kotlin/unsigned/Ushort.kt | 2 ++ src/test/kotlin/unsigned/test.kt | 11 +++++++++++ 5 files changed, 19 insertions(+) diff --git a/src/main/kotlin/unsigned/Ubyte.kt b/src/main/kotlin/unsigned/Ubyte.kt index bb3df2c..d145f39 100644 --- a/src/main/kotlin/unsigned/Ubyte.kt +++ b/src/main/kotlin/unsigned/Ubyte.kt @@ -94,4 +94,6 @@ data class Ubyte(var v: Byte = 0) : Number() { operator fun compareTo(b: Ubyte) = toInt() compareUnsigned b.toInt() operator fun compareTo(b: Byte) = toInt() compareUnsigned b.toUInt() operator fun compareTo(b: Int) = toInt() compareUnsigned b + + override fun toString() = toInt().toString() } diff --git a/src/main/kotlin/unsigned/Uint.kt b/src/main/kotlin/unsigned/Uint.kt index 3c740fb..445598d 100644 --- a/src/main/kotlin/unsigned/Uint.kt +++ b/src/main/kotlin/unsigned/Uint.kt @@ -77,5 +77,7 @@ data class Uint(var v: Int = 0) : Number() { operator fun compareTo(b: Uint) = v compareUnsigned b.toInt() operator fun compareTo(b: Int) = v compareUnsigned b + override fun toString() = toLong().toString() + // TODO long? } \ No newline at end of file diff --git a/src/main/kotlin/unsigned/Ulong.kt b/src/main/kotlin/unsigned/Ulong.kt index fbbab84..9365e65 100644 --- a/src/main/kotlin/unsigned/Ulong.kt +++ b/src/main/kotlin/unsigned/Ulong.kt @@ -73,6 +73,8 @@ data class Ulong(var v: Long = 0) : Number(), Comparable { // TODO others + override fun toString() = toBigInt().toString() + operator fun rangeTo(b: Ulong) = UlongRange(this, b) class UlongRange(override val start: Ulong, override val endInclusive: Ulong) : ClosedRange, Iterable { diff --git a/src/main/kotlin/unsigned/Ushort.kt b/src/main/kotlin/unsigned/Ushort.kt index 7ff6838..58e96fa 100644 --- a/src/main/kotlin/unsigned/Ushort.kt +++ b/src/main/kotlin/unsigned/Ushort.kt @@ -91,4 +91,6 @@ data class Ushort(var v: Short = 0) : Number() { operator fun compareTo(b: Ushort) = toInt() compareUnsigned b.toInt() operator fun compareTo(b: Short) = toInt() compareUnsigned b.toUInt() operator fun compareTo(b: Int) = toInt() compareUnsigned b + + override fun toString() = toInt().toString() } \ No newline at end of file diff --git a/src/test/kotlin/unsigned/test.kt b/src/test/kotlin/unsigned/test.kt index 8f81b69..b5ffe10 100644 --- a/src/test/kotlin/unsigned/test.kt +++ b/src/test/kotlin/unsigned/test.kt @@ -567,5 +567,16 @@ class Unsigned : StringSpec() { ((65_500 ucmp Ushort(65_499)) > 0) shouldBe true (65_500 ucmp Ushort(65_500)) shouldBe 0 } + + "string format" { + Ubyte(0xff).v.toString() shouldBe "-1" + Ubyte(0xff).toString() shouldBe "255" + Ushort(0xffff).v.toString() shouldBe "-1" + Ushort(0xffff).toString() shouldBe "65535" + Uint(0xffff_ffff).v.toString() shouldBe "-1" + Uint(0xffff_ffff).toString() shouldBe "4294967295" + Ulong(Ulong.MAX_VALUE).v.toString() shouldBe "-1" + Ulong(Ulong.MAX_VALUE).toString() shouldBe "18446744073709551615" + } } }