Skip to content

Commit

Permalink
feat: Support Multiple/Divide constraints for BigInt/BigDecimal (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore authored Jul 9, 2023
1 parent 1f255ae commit bd424f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions main/src/io/github/iltotore/iron/constraint/numeric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ object numeric:
inline given [V <: NumConstant]: GreaterConstraint[Double, V] with
override inline def test(value: Double): Boolean = value > doubleValue[V]

inline given bigDecimalDouble[V <: Float | Double]: GreaterConstraint[BigDecimal, V] with
inline given bigDecimalDouble[V <: NumConstant]: GreaterConstraint[BigDecimal, V] with
override inline def test(value: BigDecimal): Boolean = value > BigDecimal(doubleValue[V])

inline given bigDecimalLong[V <: Int | Long]: GreaterConstraint[BigDecimal, V] with
Expand Down Expand Up @@ -170,7 +170,7 @@ object numeric:
inline given [V <: NumConstant]: LessConstraint[Double, V] with
override inline def test(value: Double): Boolean = value < doubleValue[V]

inline given bigDecimalDouble[V <: Float | Double]: LessConstraint[BigDecimal, V] with
inline given bigDecimalDouble[V <: NumConstant]: LessConstraint[BigDecimal, V] with
override inline def test(value: BigDecimal): Boolean = value < BigDecimal(doubleValue[V])

inline given bigDecimalLong[V <: Int | Long]: LessConstraint[BigDecimal, V] with
Expand Down Expand Up @@ -205,6 +205,14 @@ object numeric:
inline given [V <: NumConstant]: MultipleConstraint[Double, V] with
override inline def test(value: Double): Boolean = value % doubleValue[V] == 0

inline given [V <: Int | Long]: MultipleConstraint[BigInt, V] with

override inline def test(value: BigInt): Boolean = value % BigInt(longValue[V]) == 0

inline given[V <: NumConstant]: MultipleConstraint[BigDecimal, V] with

override inline def test(value: BigDecimal): Boolean = value % BigDecimal(doubleValue[V]) == 0

given [A, V1 <: A, V2 <: A](using V1 % V2 =:= Zero[A]): (Multiple[V1] ==> Multiple[V2]) = Implication()

object Divide:
Expand All @@ -223,6 +231,12 @@ object numeric:
inline given [V <: NumConstant]: DivideConstraint[Double, V] with
override inline def test(value: Double): Boolean = doubleValue[V] % value == 0

inline given [V <: Int | Long]: DivideConstraint[BigInt, V] with
override inline def test(value: BigInt): Boolean = BigInt(longValue[V]) % value == 0

inline given[V <: NumConstant]: DivideConstraint[BigDecimal, V] with
override inline def test(value: BigDecimal): Boolean = BigDecimal(doubleValue[V]) % value == 0

object NaN:
private trait NaNConstraint[A] extends Constraint[A, NaN]:
override inline def message: String = "Should be an unrepresentable number"
Expand Down
10 changes: 10 additions & 0 deletions main/test/src/io/github/iltotore/iron/testing/NumericSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,22 @@ object NumericSuite extends TestSuite:
test("multiple") {
test - 1.assertNotRefine[Multiple[2]]
test - 2.assertRefine[Multiple[2]]
test - BigInt(1).assertNotRefine[Multiple[2]]
test - BigInt(2).assertRefine[Multiple[2]]
test - BigDecimal(1).assertNotRefine[Multiple[2]]
test - BigDecimal(2).assertRefine[Multiple[2]]
}

test("divide") {
test - 1.assertRefine[Divide[2]]
test - 2.assertRefine[Divide[2]]
test - 3.assertNotRefine[Divide[2]]
test - BigInt(1).assertRefine[Divide[2]]
test - BigInt(2).assertRefine[Divide[2]]
test - BigInt(3).assertNotRefine[Divide[2]]
test - BigDecimal(1).assertRefine[Divide[2]]
test - BigDecimal(2).assertRefine[Divide[2]]
test - BigDecimal(3).assertNotRefine[Divide[2]]
}

test("nan") {
Expand Down

0 comments on commit bd424f6

Please sign in to comment.