Skip to content

Commit

Permalink
Merge pull request #950 from satorg/add-scalafmt
Browse files Browse the repository at this point in the history
Add scalafmt to the repository
  • Loading branch information
rossabaker committed Jun 16, 2023
2 parents 70aa6f2 + 97d1eb1 commit 88311e4
Show file tree
Hide file tree
Showing 53 changed files with 2,856 additions and 2,504 deletions.
6 changes: 5 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Header whitespace cleanup
# Header organizing (PR: #959, #963)
b8564609df3c849b3a258a411e0de645d4e8c7a0
0b26b5e6dbcf3e6beeca806297f848f41dde899b

# Code formatting (PR: #950)
ccd98e22a078e1282dbb2c1acfb40fe3afee01b6

2 changes: 0 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ fileOverride {
project.excludeFilters = [
"scalafix/*"
]

project.includePaths = [] # temporarily disable formatting
63 changes: 25 additions & 38 deletions bench/src/main/scala/org/scalacheck/bench/GenBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,31 @@ import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalacheck.rng.Seed

/**
* Generator benchmarks
*
* Since generators may run very quickly (or very slowly) depending on
* the seed and size parameter used, we want to make sure these are
* held constant across runs. Otherwise, we might believe a particular
* change made a given generator faster (or slower) when in fact we
* just got lucky (or unlucky).
*
* We use `seedCount` to choose how many seeds we will benchmark with.
* For each seed, we run the given generator. So if `seedCount` is
* 100, the average time we get is the time to generate values for the
* 100 seeds we started with.
*
* The size parameter also plays a major role in how long generators
* take. ScalaCheck's default parameters start with 100. You can
* benchmark additional size parameters to see how changes to this
* parameter effect generator run time.
*
* Finally, a generator's `apply` method may fail to generate a value.
* This might allow a slow generator that fails 50% of the time to
* appear faster than other generators that don't fail. Since we
* expect all generators to be able to succeed, we benchmark using
* `pureApply`, which will retry up to 100 times before crashing. This
* way, there's no chance that a flaky generator gets an advantage
* during benchmarking.
*
* In theory, we should do as much work as possible outside the
* benchmark methods to avoid benchmarking construction time. However,
* since many generators are constructed inside of `flatMap` calls
* during generation we do want these constructors to also run as
* quickly as possible. For that reason, we mostly choose to assemble
* our generators inside the benchmark methods.
*
* Prefer to add new benchmarks instead of modifying existing ones.
* This will enable us to compare the results for a given benchmark
* over time.
*/
/** Generator benchmarks
*
* Since generators may run very quickly (or very slowly) depending on the seed and size parameter used, we want to
* make sure these are held constant across runs. Otherwise, we might believe a particular change made a given
* generator faster (or slower) when in fact we just got lucky (or unlucky).
*
* We use `seedCount` to choose how many seeds we will benchmark with. For each seed, we run the given generator. So if
* `seedCount` is 100, the average time we get is the time to generate values for the 100 seeds we started with.
*
* The size parameter also plays a major role in how long generators take. ScalaCheck's default parameters start with
* 100. You can benchmark additional size parameters to see how changes to this parameter effect generator run time.
*
* Finally, a generator's `apply` method may fail to generate a value. This might allow a slow generator that fails 50%
* of the time to appear faster than other generators that don't fail. Since we expect all generators to be able to
* succeed, we benchmark using `pureApply`, which will retry up to 100 times before crashing. This way, there's no
* chance that a flaky generator gets an advantage during benchmarking.
*
* In theory, we should do as much work as possible outside the benchmark methods to avoid benchmarking construction
* time. However, since many generators are constructed inside of `flatMap` calls during generation we do want these
* constructors to also run as quickly as possible. For that reason, we mostly choose to assemble our generators inside
* the benchmark methods.
*
* Prefer to add new benchmarks instead of modifying existing ones. This will enable us to compare the results for a
* given benchmark over time.
*/
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
Expand Down
18 changes: 10 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ ThisBuild / homepage := Some(url("http://www.scalacheck.org"))
ThisBuild / licenses := Seq("BSD-3-Clause" -> url("https://opensource.org/licenses/BSD-3-Clause"))
ThisBuild / developers := List(
Developer(
id = "rickynils",
name = "Rickard Nilsson",
id = "rickynils",
name = "Rickard Nilsson",
email = "[email protected]",
url = url("https://github.com/rickynils")
url = url("https://github.com/rickynils")
)
)

Expand Down Expand Up @@ -40,8 +40,8 @@ ThisBuild / githubWorkflowAddedJobs ++= Seq(
"for d in */ ; do cd \"$d\" && sbt test:compile && cd ../ ; done"),
name = Some("Build examples"))),
javas = List(Java8),
scalas = List((ThisBuild / scalaVersion).value)),

scalas = List((ThisBuild / scalaVersion).value)
),
WorkflowJob(
"bench",
"Bench",
Expand All @@ -50,7 +50,9 @@ ThisBuild / githubWorkflowAddedJobs ++= Seq(
List("bench/jmh:run -p genSize=0 -p seedCount=0 -bs 1 -wi 0 -i 1 -f 0 -t 1 -r 0 org.scalacheck.bench.GenBench"),
name = Some("Build benchmark suite"))),
javas = List(Java8),
scalas = List((ThisBuild / scalaVersion).value)))
scalas = List((ThisBuild / scalaVersion).value)
)
)

ThisBuild / tlBaseVersion := "1.17"
ThisBuild / tlMimaPreviousVersions ++= Set(
Expand All @@ -63,7 +65,7 @@ ThisBuild / tlMimaPreviousVersions ++= Set(
"1.15.1",
"1.15.2",
"1.15.3",
"1.15.4",
"1.15.4"
)
ThisBuild / tlVersionIntroduced := Map("3" -> "1.15.3")

Expand Down Expand Up @@ -115,6 +117,6 @@ lazy val bench = project.in(file("bench"))
.dependsOn(core.jvm)
.settings(
name := "scalacheck-bench",
fork := true,
fork := true
)
.enablePlugins(NoPublishPlugin, JmhPlugin)
10 changes: 5 additions & 5 deletions core/js/src/main/scala/org/scalacheck/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

package org.scalacheck

import Test._

import scala.annotation.nowarn
import scala.scalajs.reflect.Reflect

import Test._

private[scalacheck] object Platform {

@nowarn("msg=is never used")
def runWorkers(
params: Parameters,
workerFun: Int => Result,
stop: () => Unit
params: Parameters,
workerFun: Int => Result,
stop: () => Unit
): Result = {
workerFun(0)
}
Expand Down
35 changes: 18 additions & 17 deletions core/jvm/src/main/scala/org/scalacheck/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,41 @@ private[scalacheck] object Platform {
import util.FreqMap

def runWorkers(
params: Parameters,
workerFun: Int => Result,
stop: () => Unit
params: Parameters,
workerFun: Int => Result,
stop: () => Unit
): Result = {
import params._

def mergeResults(r1: Result, r2: Result): Result = {
val Result(st1, s1, d1, fm1, _) = r1
val Result(st2, s2, d2, fm2, _) = r2
if (st1 != Passed && st1 != Exhausted)
Result(st1, s1+s2, d1+d2, fm1++fm2)
Result(st1, s1 + s2, d1 + d2, fm1 ++ fm2)
else if (st2 != Passed && st2 != Exhausted)
Result(st2, s1+s2, d1+d2, fm1++fm2)
Result(st2, s1 + s2, d1 + d2, fm1 ++ fm2)
else {
if (s1+s2 >= minSuccessfulTests && maxDiscardRatio*(s1+s2) >= (d1+d2))
Result(Passed, s1+s2, d1+d2, fm1++fm2)
if (s1 + s2 >= minSuccessfulTests && maxDiscardRatio * (s1 + s2) >= (d1 + d2))
Result(Passed, s1 + s2, d1 + d2, fm1 ++ fm2)
else
Result(Exhausted, s1+s2, d1+d2, fm1++fm2)
Result(Exhausted, s1 + s2, d1 + d2, fm1 ++ fm2)
}
}

if(workers < 2) workerFun(0)
if (workers < 2) workerFun(0)
else {
import concurrent._
val tp = java.util.concurrent.Executors.newFixedThreadPool(workers)
implicit val ec: ExecutionContextExecutor = ExecutionContext.fromExecutor(tp)
try {
val fs = List.range(0,workers) map (idx => Future {
params.customClassLoader.foreach(
Thread.currentThread.setContextClassLoader(_)
)
blocking { workerFun(idx) }
})
val zeroRes = Result(Passed,0,0,FreqMap.empty[Set[Any]])
val fs = List.range(0, workers) map (idx =>
Future {
params.customClassLoader.foreach(
Thread.currentThread.setContextClassLoader(_)
)
blocking { workerFun(idx) }
})
val zeroRes = Result(Passed, 0, 0, FreqMap.empty[Set[Any]])
val res =
if (fs.isEmpty) Future.successful(zeroRes)
else Future.sequence(fs).map(_.foldLeft(zeroRes)(mergeResults))
Expand All @@ -65,7 +66,7 @@ private[scalacheck] object Platform {

@nowarn("msg=is never used")
def newInstance(name: String, loader: ClassLoader, paramTypes: Seq[Class[_]])(args: Seq[AnyRef]): AnyRef =
if(args.nonEmpty) ???
if (args.nonEmpty) ???
else Class.forName(name, true, loader).getDeclaredConstructor().newInstance().asInstanceOf[AnyRef]

def loadModule(name: String, loader: ClassLoader): AnyRef =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object ArbitrarySpecification extends Properties("Arbitrary") {

property("arbChar") =
Prop.forAll { (c: Char) =>
0x0000 <= c && c <= 0xD7FF || 0xE000 <= c && c <= 0xFFFD
0x0000 <= c && c <= 0xd7ff || 0xe000 <= c && c <= 0xfffd
}

property("arbString") =
Expand Down
18 changes: 9 additions & 9 deletions core/jvm/src/test/scala/org/scalacheck/ChooseSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

package org.scalacheck

import Gen._

import java.time._
import java.time.temporal.ChronoUnit
import java.util.Date
import scala.util.{Try, Success, Failure}
import scala.util.Failure
import scala.util.Success
import scala.util.Try

import Gen._

object ChooseSpecification extends Properties("Choose") with time.OrderingVersionSpecific {

Expand Down Expand Up @@ -58,9 +60,8 @@ object ChooseSpecification extends Properties("Choose") with time.OrderingVersio

property("choose[ZonedDateTime]") = chooseProp[ZonedDateTime]

/** Generate a duration which is at least 1 second smaller than the max
* duration the type can support. We use this to avoid the incredibly
* unlikely event of overflowing in the handle-min-nanos-duration test.
/** Generate a duration which is at least 1 second smaller than the max duration the type can support. We use this to
* avoid the incredibly unlikely event of overflowing in the handle-min-nanos-duration test.
*/
lazy val genOneSecondLessThanMaxDuration: Gen[Duration] =
Gen.choose(Duration.ofSeconds(Long.MinValue), Duration.ofSeconds(Long.MaxValue - 1L, 999999999L))
Expand All @@ -78,9 +79,8 @@ object ChooseSpecification extends Properties("Choose") with time.OrderingVersio
checkChoose(min, max)
}

/** Generate an Instant which is at least 1 second smaller than the max
* Instant the type can support. We use this to avoid the incredibly
* unlikely event of overflowing in the handle-min-nanos-instant test.
/** Generate an Instant which is at least 1 second smaller than the max Instant the type can support. We use this to
* avoid the incredibly unlikely event of overflowing in the handle-min-nanos-instant test.
*/
lazy val genOneSecondLessThanMaxInstant: Gen[Instant] =
Gen.choose(Instant.MIN, Instant.MAX.minusSeconds(1L))
Expand Down
30 changes: 16 additions & 14 deletions core/jvm/src/test/scala/org/scalacheck/CogenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import org.scalacheck.Gen.listOfN
import org.scalacheck.GenSpecification.arbSeed
import org.scalacheck.Prop.forAll
import org.scalacheck.rng.Seed
import ScalaVersionSpecific._

import java.time._
import java.util.UUID
import scala.collection.immutable.SortedMap
import scala.collection.immutable.SortedSet
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.concurrent.duration.Duration
import scala.concurrent.duration.FiniteDuration
import scala.util.Try

import java.time._
import java.util.UUID
import ScalaVersionSpecific._

object CogenSpecification extends Properties("Cogen") {

Expand Down Expand Up @@ -82,7 +82,7 @@ object CogenSpecification extends Properties("Cogen") {
implicit def arbFunction0[A: Arbitrary]: Arbitrary[() => A] =
Arbitrary(arbitrary[A].map(() => _))

implicit def arbCogen[A: Arbitrary : Cogen]: Arbitrary[Cogen[A]] =
implicit def arbCogen[A: Arbitrary: Cogen]: Arbitrary[Cogen[A]] =
Arbitrary(arbitrary[A => A].map(Cogen[A].contramap(_)))

// Cogens should follow these laws.
Expand All @@ -94,19 +94,19 @@ object CogenSpecification extends Properties("Cogen") {
In particular, if the space of the input is larger than the space of the seed (2^256) there is a
possibility of legitimate collisions, but this is vanishingly small for the sample sizes we are using.
*/
def uniqueness[A: Equal : Arbitrary : Cogen]: Prop =
def uniqueness[A: Equal: Arbitrary: Cogen]: Prop =
forAll { (seed: Seed, as: List[A]) =>
as.map(Cogen[A].perturb(seed, _)).toSet.size == distinct(as).size
}

// A Cogen should always generate the same output for a given seed and input.
def consistency[A: Arbitrary : Cogen]: Prop =
def consistency[A: Arbitrary: Cogen]: Prop =
forAll { (seed: Seed, a: A) =>
Cogen[A].perturb(seed, a) == Cogen[A].perturb(seed, a)
}
}

def cogenLaws[A: Equal : Arbitrary : Cogen]: Properties =
def cogenLaws[A: Equal: Arbitrary: Cogen]: Properties =
new Properties("cogenLaws") {
property("uniqueness") = CogenLaws.uniqueness[A]
property("consistency") = CogenLaws.consistency[A]
Expand All @@ -116,16 +116,18 @@ object CogenSpecification extends Properties("Cogen") {
object ContravariantLaws {

// Contramapping over a Cogen with the identity function should return the Cogen unchanged.
def identity[A: Equal : Arbitrary : Cogen]: Prop =
def identity[A: Equal: Arbitrary: Cogen]: Prop =
forAll { (fa: Cogen[A]) =>
Equal[Cogen[A]].equal(fa.contramap(a => a), fa)
}

// Contramapping with f and g is the same as contramapping with the composition of f and g.
def composition[A, B, C](implicit eq: Equal[Cogen[C]],
arb1: Arbitrary[Cogen[A]],
arb2: Arbitrary[B => A],
arb3: Arbitrary[C => B]): Prop =
def composition[A, B, C](implicit
eq: Equal[Cogen[C]],
arb1: Arbitrary[Cogen[A]],
arb2: Arbitrary[B => A],
arb3: Arbitrary[C => B]
): Prop =
forAll { (fa: Cogen[A], f: B => A, g: C => B) =>
Equal[Cogen[C]].equal(fa.contramap(f).contramap(g), fa.contramap(f compose g))
}
Expand Down
Loading

0 comments on commit 88311e4

Please sign in to comment.