Skip to content

Commit

Permalink
Add missing plusExact/minusExact overloads (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikc5000 authored Apr 27, 2021
1 parent 8c42e06 commit 82832fe
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/ExactMath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ package dev.erikchristensen.javamath2kmp
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

infix fun Int.plusExact(other: Long): Long = this.toLong() plusExact other
infix fun Int.minusExact(other: Long): Long = this.toLong() minusExact other

infix fun Long.plusExact(other: Int): Long = this plusExact other.toLong()
infix fun Long.minusExact(other: Int): Long = this minusExact other.toLong()
115 changes: 86 additions & 29 deletions src/commonTest/kotlin/MathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,116 @@ class MathTest {
}

@Test
fun plusExact_throwsWhenAddingLongsCausesOverflow() {
fun plusExact_IntInt_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { Int.MAX_VALUE plusExact 1 }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE plusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE plusExact -1 }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE plusExact Int.MIN_VALUE }
}

@Test
fun plusExact_IntInt_addsWhenResultFits() {
assertEquals(Int.MAX_VALUE, Int.MAX_VALUE - 1 plusExact 1)
assertEquals(Int.MIN_VALUE, Int.MIN_VALUE + 1 plusExact -1)
}

@Test
fun plusExact_IntLong_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { 1 plusExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE plusExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { -1 plusExact Long.MIN_VALUE }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE plusExact Long.MIN_VALUE }
}

@Test
fun plusExact_IntLong_addsWhenResultFits() {
assertEquals(Int.MAX_VALUE.toLong(), Int.MAX_VALUE - 1 plusExact 1L)
assertEquals(Int.MIN_VALUE.toLong(), Int.MIN_VALUE + 1 plusExact -1L)
}

@Test
fun plusExact_LongInt_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { Long.MAX_VALUE plusExact 1 }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE plusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE plusExact -1 }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE plusExact Int.MIN_VALUE }
}

@Test
fun plusExact_LongInt_addsWhenResultFits() {
assertEquals(Long.MAX_VALUE, Long.MAX_VALUE - 1 plusExact 1)
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE + 1 plusExact -1)
}

@Test
fun plusExact_LongLong_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { Long.MAX_VALUE plusExact 1L }
assertFailsWith<ArithmeticException> { 1L plusExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE plusExact -1L }
assertFailsWith<ArithmeticException> { -1L plusExact Long.MIN_VALUE }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE plusExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE plusExact (-1) }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE plusExact Long.MIN_VALUE }
}

@Test
fun plusExact_addsLongsWhenResultFits() {
assertEquals(Long.MAX_VALUE, Long.MAX_VALUE - 1 plusExact 1)
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE + 1 plusExact (-1))
fun plusExact_LongLong_addsWhenResultFits() {
assertEquals(Long.MAX_VALUE, Long.MAX_VALUE - 1 plusExact 1L)
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE + 1 plusExact -1L)
}

@Test
fun plusExact_throwsWhenAddingIntsCausesOverflow() {
assertFailsWith<ArithmeticException> { Int.MAX_VALUE plusExact 1 }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE plusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE plusExact (-1) }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE plusExact Int.MIN_VALUE }
fun minusExact_IntInt_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { Int.MIN_VALUE minusExact 1 }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE minusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE minusExact -1 }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE minusExact Int.MIN_VALUE }
}

@Test
fun plusExact_addsIntsWhenResultFits() {
assertEquals(Int.MAX_VALUE, Int.MAX_VALUE - 1 plusExact 1)
assertEquals(Int.MIN_VALUE, Int.MIN_VALUE + 1 plusExact (-1))
fun minusExact_IntInt_subtractsWhenResultFits() {
assertEquals(Int.MIN_VALUE, Int.MIN_VALUE + 1 minusExact 1)
assertEquals(Int.MAX_VALUE, Int.MAX_VALUE - 1 minusExact -1)
}

@Test
fun minusExact_IntLong_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { 0 minusExact Long.MIN_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE minusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact -1 }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact Int.MIN_VALUE }
}

@Test
fun minusExact_throwsWhenSubtractingLongsCausesOverflow() {
fun minusExact_IntLong_subtractsWhenResultFits() {
assertEquals(Long.MIN_VALUE, -1 minusExact Long.MAX_VALUE)
}

@Test
fun minusExact_LongInt_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { Long.MIN_VALUE minusExact 1 }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE minusExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact (-1) }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact Long.MIN_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE minusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact -1 }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact Int.MIN_VALUE }
}

@Test
fun minusExact_subtractsLongsWhenResultFits() {
fun minusExact_LongInt_subtractsWhenResultFits() {
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE + 1 minusExact 1)
assertEquals(Long.MAX_VALUE, Long.MAX_VALUE - 1 minusExact (-1))
assertEquals(Long.MAX_VALUE, Long.MAX_VALUE - 1 minusExact -1)
}

@Test
fun minusExact_throwsWhenSubtractingIntsCausesOverflow() {
assertFailsWith<ArithmeticException> { Int.MIN_VALUE minusExact 1 }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE minusExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE minusExact (-1) }
assertFailsWith<ArithmeticException> { Int.MAX_VALUE minusExact Int.MIN_VALUE }
fun minusExact_LongLong_throwsOnOverflow() {
assertFailsWith<ArithmeticException> { Long.MIN_VALUE minusExact 1L }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE minusExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact -1L }
assertFailsWith<ArithmeticException> { Long.MAX_VALUE minusExact Long.MIN_VALUE }
}

@Test
fun minusExact_subtractsIntsWhenResultFits() {
assertEquals(Int.MIN_VALUE, Int.MIN_VALUE + 1 minusExact 1)
assertEquals(Int.MAX_VALUE, Int.MAX_VALUE - 1 minusExact (-1))
fun minusExact_LongLong_subtractsWhenResultFits() {
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE + 1 minusExact 1L)
assertEquals(Long.MAX_VALUE, Long.MAX_VALUE - 1 minusExact -1L)
}

@Test
Expand All @@ -110,7 +167,7 @@ class MathTest {
assertFailsWith<ArithmeticException> { Long.MAX_VALUE timesExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE timesExact Long.MIN_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE timesExact Long.MAX_VALUE }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE timesExact (-1) }
assertFailsWith<ArithmeticException> { Long.MIN_VALUE timesExact -1 }
}

@Test
Expand All @@ -127,7 +184,7 @@ class MathTest {
assertFailsWith<ArithmeticException> { Int.MAX_VALUE timesExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE timesExact Int.MIN_VALUE }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE timesExact Int.MAX_VALUE }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE timesExact (-1) }
assertFailsWith<ArithmeticException> { Int.MIN_VALUE timesExact -1 }
}

@Test
Expand Down

0 comments on commit 82832fe

Please sign in to comment.