Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric refined types for Byte, Short, BigInt and BigDecimal. #567

Merged
merged 6 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.timepit.refined.types

/** Module for all predefined refined types. */
object all extends AllTypes
object all extends AllTypes with AllTypesBinCompat1

trait AllTypes
extends CharTypes
Expand All @@ -10,3 +10,5 @@ trait AllTypes
with NumericTypes
with StringTypes
with TimeTypes

trait AllTypesBinCompat1 extends NumericTypesBinCompat1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,46 @@ import eu.timepit.refined.numeric.{Negative, NonNegative, NonPositive, Positive}
/** Module for numeric refined types. */
object numeric {

/** A `Byte` in the range from 1 to `Byte.MaxValue`. */
type PosByte = Byte Refined Positive

object PosByte extends RefinedTypeOps.Numeric[PosByte, Byte]

/** A `Byte` in the range from 0 to `Byte.MaxValue`. */
type NonNegByte = Byte Refined NonNegative

object NonNegByte extends RefinedTypeOps.Numeric[NonNegByte, Byte]

/** A `Byte` in the range from `Byte.MinValue` to -1. */
type NegByte = Byte Refined Negative

object NegByte extends RefinedTypeOps.Numeric[NegByte, Byte]

/** A `Byte` in the range from `Byte.MinValue` to 0. */
type NonPosByte = Byte Refined NonPositive

object NonPosByte extends RefinedTypeOps.Numeric[NonPosByte, Byte]

/** A `Short` in the range from 1 to `Short.MaxValue`. */
type PosShort = Short Refined Positive

object PosShort extends RefinedTypeOps.Numeric[PosShort, Short]

/** A `Short` in the range from 0 to `Short.MaxValue`. */
type NonNegShort = Short Refined NonNegative

object NonNegShort extends RefinedTypeOps.Numeric[NonNegShort, Short]

/** A `Short` in the range from `Short.MinValue` to -1. */
type NegShort = Short Refined Negative

object NegShort extends RefinedTypeOps.Numeric[NegShort, Short]

/** A `Short` in the range from `Short.MinValue` to 0. */
type NonPosShort = Short Refined NonPositive

object NonPosShort extends RefinedTypeOps.Numeric[NonPosShort, Short]

/** An `Int` in the range from 1 to `Int.MaxValue`. */
type PosInt = Int Refined Positive

Expand Down Expand Up @@ -46,6 +86,26 @@ object numeric {

object NonPosLong extends RefinedTypeOps.Numeric[NonPosLong, Long]

/** A `BigInt` greater than 0. */
type PosBigInt = BigInt Refined Positive

object PosBigInt extends RefinedTypeOps[PosBigInt, BigInt]

/** A `BigInt` greater than or equal to 0. */
type NonNegBigInt = BigInt Refined NonNegative

object NonNegBigInt extends RefinedTypeOps[NonNegBigInt, BigInt]

/** A `BigInt` less than or equal to 0. */
type NegBigInt = BigInt Refined Negative

object NegBigInt extends RefinedTypeOps[NegBigInt, BigInt]

/** A `BigInt` less than or equal to 0. */
type NonPosBigInt = BigInt Refined NonPositive

object NonPosBigInt extends RefinedTypeOps[NonPosBigInt, BigInt]

/** A `Float` greater than 0. */
type PosFloat = Float Refined Positive

Expand Down Expand Up @@ -85,6 +145,26 @@ object numeric {
type NonPosDouble = Double Refined NonPositive

object NonPosDouble extends RefinedTypeOps.Numeric[NonPosDouble, Double]

/** A `BigDecimal` greater than 0. */
type PosBigDecimal = BigDecimal Refined Positive

object PosBigDecimal extends RefinedTypeOps[PosBigDecimal, BigDecimal]

/** A `BigDecimal` greater than or equal to 0. */
type NonNegBigDecimal = BigDecimal Refined NonNegative

object NonNegBigDecimal extends RefinedTypeOps[NonNegBigDecimal, BigDecimal]

/** A `BigDecimal` less than 0. */
type NegBigDecimal = BigDecimal Refined Negative

object NegBigDecimal extends RefinedTypeOps[NegBigDecimal, BigDecimal]

/** A `BigDecimal` less than or equal to 0. */
type NonPosBigDecimal = BigDecimal Refined NonPositive

object NonPosBigDecimal extends RefinedTypeOps[NonPosBigDecimal, BigDecimal]
}

trait NumericTypes {
Expand Down Expand Up @@ -136,3 +216,53 @@ trait NumericTypes {
final type NonPosDouble = numeric.NonPosDouble
final val NonPosDouble = numeric.NonPosDouble
}

trait NumericTypesBinCompat1 {
final type PosByte = numeric.PosByte
final val PosByte = numeric.PosByte

final type NonNegByte = numeric.NonNegByte
final val NonNegByte = numeric.NonNegByte

final type NegByte = numeric.NegByte
final val NegByte = numeric.NegByte

final type NonPosByte = numeric.NonPosByte
final val NonPosByte = numeric.NonPosByte

final type PosShort = numeric.PosShort
final val PosShort = numeric.PosShort

final type NonNegShort = numeric.NonNegShort
final val NonNegShort = numeric.NonNegShort

final type NegShort = numeric.NegShort
final val NegShort = numeric.NegShort

final type NonPosShort = numeric.NonPosShort
final val NonPosShort = numeric.NonPosShort

final type PosBigInt = numeric.PosBigInt
final val PosBigInt = numeric.PosBigInt

final type NonNegBigInt = numeric.NonNegBigInt
final val NonNegBigInt = numeric.NonNegBigInt

final type NegBigInt = numeric.NegBigInt
final val NegBigInt = numeric.NegBigInt

final type NonPosBigInt = numeric.NonPosBigInt
final val NonPosBigInt = numeric.NonPosBigInt

final type PosBigDecimal = numeric.PosBigDecimal
final val PosBigDecimal = numeric.PosBigDecimal

final type NonNegBigDecimal = numeric.NonNegBigDecimal
final val NonNegBigDecimal = numeric.NonNegBigDecimal

final type NegBigDecimal = numeric.NegBigDecimal
final val NegBigDecimal = numeric.NegBigDecimal

final type NonPosBigDecimal = numeric.NonPosBigDecimal
final val NonPosBigDecimal = numeric.NonPosBigDecimal
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,70 @@ import org.scalacheck.Properties

class NumericTypesSpec extends Properties("NumericTypes") {

property("PosByte.from(1)") = secure {
PosByte.from(1).isRight
}

property("PosByte.from(0)") = secure {
PosByte.from(0) ?= Left("Predicate failed: (0 > 0).")
}

property("NonNegByte.from(0)") = secure {
NonNegByte.from(0).isRight
}

property("NonNegByte.from(-1)") = secure {
NonNegByte.from(-1) ?= Left("Predicate (-1 < 0) did not fail.")
}

property("NegByte.from(-1)") = secure {
NegByte.from(-1).isRight
}

property("NegByte.from(0)") = secure {
NegByte.from(0) ?= Left("Predicate failed: (0 < 0).")
}

property("NonPosByte.from(0)") = secure {
NonPosByte.from(0).isRight
}

property("NonPosByte.from(1)") = secure {
NonPosByte.from(1) ?= Left("Predicate (1 > 0) did not fail.")
}

property("PosShort.from(1)") = secure {
PosShort.from(1).isRight
}

property("PosShort.from(0)") = secure {
PosShort.from(0) ?= Left("Predicate failed: (0 > 0).")
}

property("NonNegShort.from(0)") = secure {
NonNegShort.from(0).isRight
}

property("NonNegShort.from(-1)") = secure {
NonNegShort.from(-1) ?= Left("Predicate (-1 < 0) did not fail.")
}

property("NegShort.from(-1)") = secure {
NegShort.from(-1).isRight
}

property("NegShort.from(0)") = secure {
NegShort.from(0) ?= Left("Predicate failed: (0 < 0).")
}

property("NonPosShort.from(0)") = secure {
NonPosShort.from(0).isRight
}

property("NonPosShort.from(1)") = secure {
NonPosShort.from(1) ?= Left("Predicate (1 > 0) did not fail.")
}

property("PosInt.from(1)") = secure {
PosInt.from(1).isRight
}
Expand Down Expand Up @@ -70,6 +134,38 @@ class NumericTypesSpec extends Properties("NumericTypes") {
NonPosLong.from(1L) ?= Left("Predicate (1 > 0) did not fail.")
}

property("PosBigInt.from(BigInt(1))") = secure {
PosBigInt.from(BigInt(1)).isRight
}

property("PosBigInt.from(BigInt(0))") = secure {
PosBigInt.from(BigInt(0)) ?= Left("Predicate failed: (0 > 0).")
}

property("NonNegBigInt.from(BigInt(0))") = secure {
NonNegBigInt.from(BigInt(0)).isRight
}

property("NonNegBigInt.from(BigInt(-1))") = secure {
NonNegBigInt.from(BigInt(-1)) ?= Left("Predicate (-1 < 0) did not fail.")
}

property("NegBigInt.from(BigInt(-1))") = secure {
NegBigInt.from(BigInt(-1)).isRight
}

property("NegBigInt.from(BigInt(0))") = secure {
NegBigInt.from(BigInt(0)) ?= Left("Predicate failed: (0 < 0).")
}

property("NonPosBigInt.from(BigInt(0))") = secure {
NonPosBigInt.from(BigInt(0)).isRight
}

property("NonPosBigInt.from(BigInt(1))") = secure {
NonPosBigInt.from(BigInt(1)) ?= Left("Predicate (1 > 0) did not fail.")
}

property("PosFloat.from(0.1F)") = secure {
PosFloat.from(0.1f).isRight
}
Expand Down Expand Up @@ -133,4 +229,36 @@ class NumericTypesSpec extends Properties("NumericTypes") {
property("NonPosDouble.from(0.1)") = secure {
NonPosDouble.from(0.1).isLeft
}

property("PosBigDecimal.from(BigDecimal(0.1))") = secure {
PosBigDecimal.from(BigDecimal(0.1)).isRight
}

property("PosBigDecimal.from(BigDecimal(0.0))") = secure {
PosBigDecimal.from(BigDecimal(0.0)).isLeft
}

property("NonNegBigDecimal.from(BigDecimal(0.0))") = secure {
NonNegBigDecimal.from(BigDecimal(0.0)).isRight
}

property("NonNegBigDecimal.from(BigDecimal(-0.1))") = secure {
NonNegBigDecimal.from(BigDecimal(-0.1)).isLeft
}

property("NegBigDecimal.from(BigDecimal(-0.1))") = secure {
NegBigDecimal.from(BigDecimal(-0.1)).isRight
}

property("NegBigDecimal.from(BigDecimal(0.0))") = secure {
NegBigDecimal.from(BigDecimal(0.0)).isLeft
}

property("NonPosBigDecimal.from(BigDecimal(0.0))") = secure {
NonPosBigDecimal.from(BigDecimal(0.0)).isRight
}

property("NonPosBigDecimal.from(BigDecimal(0.1))") = secure {
NonPosBigDecimal.from(BigDecimal(0.1)).isLeft
}
}