embracing-nondeterminism-code/src/test/scala/green/thisfieldwas/embracingnondeterminism/stdlib/SetSpec.scala

37 lines
1.0 KiB
Scala

package green.thisfieldwas.embracingnondeterminism.stdlib
import green.thisfieldwas.embracingnondeterminism.data.{Semigroup, SemigroupLaws}
import green.thisfieldwas.embracingnondeterminism.util.Laws
import org.scalacheck.{Arbitrary, Gen}
import Arbitrary.arbitrary
/** Proves that Scala's Set conforms to a Semigroup.
*/
class SetSpec extends Laws with SemigroupLaws {
/** Non-empty Sets specifically form Semigroups.
*
* @tparam A
* Any type held by a Set.
* @return
* The Arbitrary generator for a non-empty Set.
*/
implicit def arbitraryNonEmptySet[A: Arbitrary]: Arbitrary[Set[A]] = Arbitrary {
for {
length <- Gen.sized(n => Gen.choose(1, n.max(1)))
set <- Gen.listOfN(length, arbitrary[A]).map(_.toSet)
} yield set
}
/** Set forms a Semigroup under union.
*
* @tparam A
* Any type held by the Set.
* @return
* The Semigroup instance for Set.
*/
implicit def setSemigroup[A]: Semigroup[Set[A]] = _.union(_)
checkSemigroupLaws[Set[Int]]()
}