Skip to content

Commit

Permalink
refactor(#12)!: remove inheritance between AnyInt and Comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Jan 3, 2023
1 parent e920dc5 commit c543824
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 36 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Expand Up @@ -26,7 +26,7 @@ Introduce a new `AnyInt` hierarchy representing integers (issue
[#132](https://github.com/kotools/libraries/issues/132) in [kotools/libraries]).

```kotlin
interface AnyInt : Comparable<AnyInt>
interface AnyInt
interface NonZeroInt : AnyInt
interface PositiveInt : AnyInt
interface NegativeInt : AnyInt
Expand Down
11 changes: 1 addition & 10 deletions src/commonMain/kotlin/kotools/types/number/AnyInt.kt
Expand Up @@ -15,16 +15,7 @@ import kotools.types.text.toNotBlankString
/** Representation of all integers. */
@Serializable(AnyIntSerializerImplementation::class)
@SinceKotoolsTypes("4.0")
public sealed interface AnyInt : Comparable<AnyInt> {
/**
* Compares this integer with the [other] one for order.
* Returns zero if this integer equals the [other] one, a negative number if
* it's less than the [other] one, or a positive number if it's greater than
* the [other] one.
*/
override fun compareTo(other: AnyInt): Int = toInt()
.compareTo(other.toInt())

public sealed interface AnyInt {
/** Returns this integer as an [Int]. */
public fun toInt(): Int

Expand Down
25 changes: 0 additions & 25 deletions src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt
Expand Up @@ -8,31 +8,6 @@ import kotools.types.Package
import kotools.types.shouldEqual
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertTrue

class AnyIntTest {
@Test
fun compareTo_should_return_zero_with_another_AnyInt_having_the_same_value() {
val result: Int = ZeroInt.compareTo(ZeroInt)
result shouldEqual ZeroInt.toInt()
}

@Test
fun compareTo_should_return_a_negative_Int_with_another_AnyInt_having_a_greater_value() {
val x: AnyInt = StrictlyNegativeInt.random()
val y: AnyInt = StrictlyPositiveInt.random()
val result: Int = x.compareTo(y)
assertTrue { result < ZeroInt.toInt() }
}

@Test
fun compareTo_should_return_a_positive_Int_with_another_AnyInt_having_a_lower_value() {
val x: AnyInt = StrictlyPositiveInt.random()
val y: AnyInt = StrictlyNegativeInt.random()
val result: Int = x.compareTo(y)
assertTrue { result > ZeroInt.toInt() }
}
}

class AnyIntSerializerTest {
private val serializer: KSerializer<AnyInt> = AnyIntSerializerImplementation
Expand Down

0 comments on commit c543824

Please sign in to comment.