Skip to content

Commit

Permalink
feat(#31): add times operations for AnyInt
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Mar 15, 2023
1 parent e1f0256 commit 17201cd
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`, `minus`) for the `AnyInt` hierarchy (issue
- Binary operations (`plus`, `minus`, `times`) 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 @@ -47,6 +47,18 @@ public operator fun AnyInt.minus(other: Int): Int = toInt() - other
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.minus(other: AnyInt): Int = toInt() - other

/** Multiplies this integer by the [other] one. */
@SinceKotoolsTypes("4.1")
public operator fun Int.times(other: AnyInt): Int = this * other.toInt()

/** Multiplies this integer by the [other] one. */
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.times(other: Int): Int = toInt() * other

/** Multiplies this integer by the [other] one. */
@SinceKotoolsTypes("4.1")
public operator fun AnyInt.times(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 @@ -57,6 +57,30 @@ class AnyIntTest {
val result: Int = x - y
result shouldEqual x.toInt() - y.toInt()
}

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

@Test
fun times_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 times_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 17201cd

Please sign in to comment.