Skip to content

Commit

Permalink
feat(#31): add minus operations for AnyInt
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Mar 14, 2023
1 parent 2a7c8ac commit 25e26a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NotEmptyList<out E> : NotEmptyCollection<E>
class NotEmptySet<out E> : NotEmptyCollection<E>
```

- Binary operations (`plus`) for the `AnyInt` hierarchy (issue
- Binary operations (`plus`, `minus`) for the `AnyInt` hierarchy (issue
[#31](https://github.com/kotools/types/issues/31)).

```kotlin
Expand Down
12 changes: 12 additions & 0 deletions src/commonMain/kotlin/kotools/types/number/AnyInt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public operator fun AnyInt.plus(other: Int): Int = toInt() + other
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.plus(other: AnyInt): Int = toInt() + other

/** Subtracts the [other] integer from this one. */
@SinceKotoolsTypes("4.1")
public operator fun Int.minus(other: AnyInt): Int = this - other.toInt()

/** Subtracts the [other] integer from this one. */
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.minus(other: Int): Int = toInt() - other

/** Subtracts the [other] integer from this one. */
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.minus(other: AnyInt): Int = toInt() - other

internal sealed interface AnyIntSerializer<I : AnyInt> : KSerializer<I> {
val serialName: Result<NotBlankString>

Expand Down
24 changes: 24 additions & 0 deletions src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ class AnyIntTest {
val result: Int = x + y
result shouldEqual x.toInt() + y.toInt()
}

@Test
fun int_minus_should_pass() {
val x: Int = Random.nextInt()
val y: AnyInt = NonZeroInt.random()
val result: Int = x - y
result shouldEqual x - y.toInt()
}

@Test
fun minus_should_pass_with_an_Int() {
val x: AnyInt = PositiveInt.random()
val y: Int = Random.nextInt()
val result: Int = x - y
result shouldEqual x.toInt() - y
}

@Test
fun minus_should_pass_with_an_AnyInt() {
val x: AnyInt = PositiveInt.random()
val y: AnyInt = NegativeInt.random()
val result: Int = x - y
result shouldEqual x.toInt() - y.toInt()
}
}

class AnyIntSerializerTest {
Expand Down

0 comments on commit 25e26a2

Please sign in to comment.