Skip to content

Commit

Permalink
Switch to munit
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Quiroz <carlos.m.quiroz@gmail.com>
  • Loading branch information
cquiroz committed Jan 7, 2021
1 parent c645142 commit 19b69a2
Show file tree
Hide file tree
Showing 122 changed files with 5,126 additions and 5,258 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
matrix:
os: [ubuntu-latest]
scala: [2.12.12, 2.13.4]
java: [adopt@1.8]
java: [adopt@1.8, adopt@1.11, adopt@1.15]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ TAGS
project/metals.sbt
.metals/
.bloop/
.bsp/
project/project/
4 changes: 2 additions & 2 deletions .jvmopts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
-XX:+TieredCompilation
-XX:-UseGCOverheadLimit
# effectively adds GC to Perm space
-XX:+CMSClassUnloadingEnabled
#-XX:+CMSClassUnloadingEnabled
# must be enabled for CMSClassUnloadingEnabled to work
-XX:+UseConcMarkSweepGC
#-XX:+UseConcMarkSweepGC
102 changes: 17 additions & 85 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import sbtcrossproject.{CrossType, crossProject}
lazy val scalaCollectionCompatVersion = "2.3.2"

lazy val scalaCheckVersion = "1.15.2"
lazy val scalaTestVersion = "3.2.3"
lazy val scalaTestPlusVersion = "3.2.2.0"

lazy val munit = "0.7.20"
lazy val munitDiscipline = "1.0.4"

lazy val shapelessVersion = "2.3.3"
lazy val disciplineScalaTestVersion = "2.1.1"
lazy val algebraVersion = "2.1.1"

lazy val apfloatVersion = "1.9.1"
Expand All @@ -27,7 +27,9 @@ ThisBuild / scalaVersion := Scala213
ThisBuild / githubWorkflowArtifactUpload := false

ThisBuild / githubWorkflowPublishTargetBranches := Seq()
ThisBuild / githubWorkflowJavaVersions := Seq("adopt@1.8", "adopt@1.11", "adopt@1.15")

Global / onChangedBuildSource := ReloadOnSourceChanges
// Projects

lazy val spire = project.in(file("."))
Expand Down Expand Up @@ -73,7 +75,7 @@ lazy val macros = crossProject(JSPlatform, JVMPlatform).crossType(CrossType.Pure
.settings(moduleName := "spire-macros")
.settings(spireSettings:_*)
.settings(scalaCheckSettings:_*)
.settings(scalaTestSettings:_*)
.settings(munitSettings: _*)
.settings(crossVersionSharedSources:_*)
.jvmSettings(commonJvmSettings:_*)
.jsSettings(commonJsSettings:_*)
Expand Down Expand Up @@ -165,7 +167,6 @@ lazy val laws = crossProject(JSPlatform, JVMPlatform).crossType(CrossType.Pure)
.settings(moduleName := "spire-laws")
.settings(spireSettings:_*)
.settings(libraryDependencies ++= Seq(
"org.typelevel" %%% "discipline-scalatest" % disciplineScalaTestVersion,
"org.typelevel" %%% "algebra-laws" % algebraVersion,
"org.scalacheck" %%% "scalacheck" % scalaCheckVersion
))
Expand All @@ -176,84 +177,13 @@ lazy val laws = crossProject(JSPlatform, JVMPlatform).crossType(CrossType.Pure)
lazy val lawsJVM = laws.jvm
lazy val lawsJS = laws.js

// Todo: As all tests in this list are commented out, no tests in testJS are run - but they are compiled.
// This list is TEMPORARY as tests are migrated to scala-js
lazy val jsTests = List(
/* "spire.PartialOrderSyntaxTest",
"spire.PartialSyntaxTest",
"spire.SyntaxTest",
//"spire.algebra.GCDTest",
"spire.algebra.NRootTest",
"spire.algebra.PartialOrderTest",
"spire.algebra.RingTest","*/
"spire.algebra.SignedTest"/*,
"spire.algebra.TrigTest",
"spire.laws.LawTests",
"spire.math.AlgebraicTest",
"spire.math.BinaryMergeCheck",
"spire.math.BitStringCheck",
"spire.math.BitStringTest",
"spire.math.ComplexCheck",
"spire.math.ComplexCheck2",
"spire.math.ComplexTest",
"spire.math.ContinuousIntervalTest",
"spire.math.CooperativeEqualityTest",
"spire.math.FastComplexCheck",
"spire.math.FixedPointCheck",
"spire.math.FpFilterTest",
"spire.math.IntervalCheck",
"spire.math.IntervalGeometricPartialOrderTest",
"spire.math.IntervalIteratorCheck",
"spire.math.IntervalReciprocalTest",
"spire.math.IntervalSubsetPartialOrderTest",
"spire.math.IntervalTest",
"spire.math.JetTest",
"spire.math.LinearSelectTest",
"spire.math.LiteralsTest",
"spire.math.MergingTest",
"spire.math.NaturalTest",
"spire.math.NumberPropertiesTest",
"spire.math.NumberTest",
"spire.math.NumericTest",
"spire.math.PackageCheck",
"spire.math.PackageTest",
"spire.math.PolynomialCheck",
"spire.math.PolynomialSamplingCheck",
"spire.math.PolynomialTest",
"spire.math.QuaternionCheck",
"spire.math.QuickSelectTest",
"spire.math.RationalCheck",
"spire.math.RationalTest",
"spire.math.RealCheck",
"spire.math.RingIntervalTest",
"spire.math.SafeLongTest",
"spire.math.SearchTest",
"spire.math.SortingTest",
"spire.math.TrileanCheck",
"spire.math.UByteTest",
"spire.math.UIntTest",
"spire.math.ULongTest",
"spire.math.UShortTest",
"spire.math.prime.FactorHeapCheck",
"spire.math.prime.FactorsCheck",
"spire.random.GaussianTest",
"spire.random.GeneratorTest",
"spire.random.SamplingTest",
"spire.random.ShufflingTest",
"spire.syntax.CforTest",
"spire.util.OptCheck",
"spire.util.PackCheck",
"test.scala.spire.math.TypeclassExistenceTest"
*/
)

lazy val tests = crossProject(JSPlatform, JVMPlatform).crossType(CrossType.Pure)
lazy val tests = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Full)
.settings(moduleName := "spire-tests")
.settings(spireSettings:_*)
.settings(scalaTestSettings:_*)
.settings(munitSettings:_*)
.settings(noPublishSettings:_*)
.jvmSettings(commonJvmSettings:_*)
.jsSettings(testOptions in Test := Seq(Tests.Filter(s => jsTests.contains(s))))
.jsSettings(commonJsSettings:_*)
.dependsOn(core, data, legacy, extras, laws)

Expand Down Expand Up @@ -312,7 +242,8 @@ lazy val commonSettings = Seq(

lazy val commonJsSettings = Seq(
scalaJSStage in Global := FastOptStage,
parallelExecution in Test := false
parallelExecution in Test := false,
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) }
)

lazy val commonJvmSettings = Seq(
Expand All @@ -322,7 +253,6 @@ lazy val commonJvmSettings = Seq(
case Some((2, scalaMajor)) if scalaMajor <= 11 => Seq("-optimize")
case _ => Seq.empty
}),
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-oDF")
)

lazy val docsMappingsAPIDir = settingKey[String]("Name of subdirectory in site target directory for api docs")
Expand Down Expand Up @@ -456,10 +386,12 @@ lazy val genProductTypes = TaskKey[Seq[File]]("gen-product-types", "Generates se

lazy val scalaCheckSettings = Seq(libraryDependencies += "org.scalacheck" %%% "scalacheck" % scalaCheckVersion % Test)

lazy val scalaTestSettings = Seq(
libraryDependencies += "org.scalatest" %%% "scalatest" % scalaTestVersion % Test,
libraryDependencies += "org.scalatestplus" %%% "scalacheck-1-14" % scalaTestPlusVersion % Test,
libraryDependencies += "com.chuusai" %% "shapeless" % shapelessVersion % Test
lazy val munitSettings = Seq(
libraryDependencies ++= Seq(
"org.scalameta" %%% "munit" % munit,
"org.typelevel" %%% "discipline-munit" % munitDiscipline
),
testFrameworks += new TestFramework("munit.Framework")
)

lazy val spireSettings = buildSettings ++ commonSettings ++ commonDeps ++ publishSettings ++ scoverageSettings
Expand Down
166 changes: 166 additions & 0 deletions macros/src/test/scala/spire/macros/CheckedScalaCheckSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package spire
package macros

import org.scalacheck.Arbitrary
import org.scalacheck.Prop._

class CheckedScalaCheckSuite extends munit.ScalaCheckSuite {
import Checked.checked
import Arbitrary.arbitrary

case class NotZero[A](value: A)
implicit def arbNotZeroLong = Arbitrary(arbitrary[Long] filter (_ != 0L) map (NotZero(_)))
implicit def arbNotZeroInt = Arbitrary(arbitrary[Int] filter (_ != 0L) map (NotZero(_)))

def checkForLongOverflow(value: BigInt, check: => Long): Unit = {
if (value.isValidLong) {
assertEquals(check, value.toLong)
} else {
intercept[ArithmeticException] { check }
}
}

def checkForIntOverflow(value: BigInt, check: => Int): Unit = {
if (value.isValidInt) {
assertEquals(check, value.toInt)
} else {
intercept[ArithmeticException] { check }
}
}

test("Negate of Int.MinValue overflows") {
val x = Int.MinValue
intercept[ArithmeticException] { checked(-x) }
}

property("Int negate overflow throws arithmetic exception") {
forAll { (x: Int) =>
checkForIntOverflow(-BigInt(x), checked(-x))
}
}

property("Int addition overflow throws arithmetic exception") {
forAll { (x: Int, y: Int) =>
checkForIntOverflow(BigInt(x) + BigInt(y), checked(x + y))
}
}

property("Int subtraction overflow throws arithmetic exception") {
forAll { (x: Int, y: Int) =>
checkForIntOverflow(BigInt(x) - BigInt(y), checked(x - y))
}
}

property("Int multiplication overflow throws arithmetic exception") {
forAll { (x: Int, y: Int) =>
checkForIntOverflow(BigInt(x) * BigInt(y), checked(x * y))
}
}

property("Int division overflow throws arithmetic exception") {
forAll { (x: Int, y: NotZero[Int]) =>
checkForIntOverflow(BigInt(x) / BigInt(y.value), checked(x / y.value))
}
}

def distSq(x: Long, y: Long): BigInt = BigInt(x) * BigInt(x) + BigInt(y) * BigInt(y)

property("Int euclidean square distance overflow throws arithmetic exception") {
forAll { (x: Int, y: Int) =>
checkForIntOverflow(distSq(x, y), checked(x * x + y * y))
}
}

test("Negate of Long.MinValue overflows") {
val x = Long.MinValue
intercept[ArithmeticException] { checked(-x) }
}

property("Long negate overflow throws arithmetic exception") {
forAll { (x: Long) =>
checkForLongOverflow(-BigInt(x), checked(-x))
}
}

property("Long addition overflow throws arithmetic exception") {
forAll { (x: Long, y: Long) =>
checkForLongOverflow(BigInt(x) + BigInt(y), checked(x + y))
}
}

property("Long subtraction overflow throws arithmetic exception") {
forAll { (x: Long, y: Long) =>
checkForLongOverflow(BigInt(x) - BigInt(y), checked(x - y))
}
}

property("Long multiplication overflow throws arithmetic exception") {
forAll { (x: Long, y: Long) =>
checkForLongOverflow(BigInt(x) * BigInt(y), checked(x * y))
}
}

property("Long division overflow throws arithmetic exception") {
forAll { (x: Long, y: NotZero[Long]) =>
checkForLongOverflow(BigInt(x) / BigInt(y.value), checked(x / y.value))
}
}

property("Long euclidean square distance overflow throws arithmetic exception") {
forAll { (x: Long, y: Long) =>
checkForLongOverflow(distSq(x, y), checked(x * x + y * y))
}
}

test("Int upgrades to Long for overflow checks when mixed in binary op") {
assertEquals(Checked.option {
val x = 2L
val y = Int.MaxValue
x + y
}, Some(Int.MaxValue.toLong + 2))

assertEquals(Checked.option {
val x = 2L
val y = Int.MaxValue
y + x
}, Some(Int.MaxValue.toLong + 2))

intercept[ArithmeticException] (checked {
val x = Long.MaxValue
val y = 2
x * y
})

intercept[ArithmeticException] (checked {
val x = Long.MaxValue
val y = 2
y * x
})
}

test("Byte and Short upgrade to Int when mixed") {
intercept[ArithmeticException] (checked {
val x = Int.MaxValue
val y = (2: Byte)
x * y
})

intercept[ArithmeticException] (checked {
val x = Int.MaxValue
val y = (2: Byte)
y * x
})

intercept[ArithmeticException] (checked {
val x = Int.MaxValue
val y = (2: Short)
x * y
})

intercept[ArithmeticException] (checked {
val x = Int.MaxValue
val y = (2: Short)
y * x
})
}
}
Loading

0 comments on commit 19b69a2

Please sign in to comment.