From 25e26a23881d95cfe97c8f15eb365630e367a402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lamarque?= Date: Mon, 20 Feb 2023 17:15:46 +0400 Subject: [PATCH] feat(#31): add minus operations for AnyInt --- changelog.md | 2 +- .../kotlin/kotools/types/number/AnyInt.kt | 12 ++++++++++ .../kotlin/kotools/types/number/AnyIntTest.kt | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index ab020323f..23fd4e7b2 100644 --- a/changelog.md +++ b/changelog.md @@ -32,7 +32,7 @@ class NotEmptyList : NotEmptyCollection class NotEmptySet : NotEmptyCollection ``` -- 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 diff --git a/src/commonMain/kotlin/kotools/types/number/AnyInt.kt b/src/commonMain/kotlin/kotools/types/number/AnyInt.kt index 0eec407aa..ac517cebc 100644 --- a/src/commonMain/kotlin/kotools/types/number/AnyInt.kt +++ b/src/commonMain/kotlin/kotools/types/number/AnyInt.kt @@ -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 : KSerializer { val serialName: Result diff --git a/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt b/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt index 056f6a94f..6459e56a7 100644 --- a/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt +++ b/src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt @@ -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 {