本文整理汇总了Scala中org.scalacheck.Prop.forAll类的典型用法代码示例。如果您正苦于以下问题:Scala forAll类的具体用法?Scala forAll怎么用?Scala forAll使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了forAll类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: FailureSpec
//设置package包名称以及导入依赖的类
package roc
package types
import org.scalacheck.Prop.forAll
import org.specs2._
import roc.types.failures._
final class FailureSpec extends Specification with ScalaCheck { def is = s2"""
BinaryDecodingUnsupportedFailure
must have correct error message $testBinaryDecodingFailureErrMsg
TextDecodingUnsupportedFailure
must have correct error message $testTextDecodingFailureErrMsg
NullDecodedFailure
must have correct error message $testNullDecodedFailureErrMsg
ElementDecodingFailure
must have correct error message $testElementDecodingFailureErrMsg
"""
val testBinaryDecodingFailureErrMsg = forAll { s: String =>
val failure = new BinaryDecodingUnsupportedFailure(s)
failure.getMessage() must_== s"Binary decoding of type $s is currently unsupported."
}
val testTextDecodingFailureErrMsg = forAll { s: String =>
val failure = new TextDecodingUnsupportedFailure(s)
failure.getMessage() must_== s"Text decoding of type $s is currently unsupported."
}
val testNullDecodedFailureErrMsg = forAll { s: String =>
val failure = new NullDecodedFailure(s)
failure.getMessage() must_==
s"A NULL value was decoded for type $s. Hint: use the Option[$s] decoder, or ensure that Postgres cannot return NULL for the requested value."
}
val testElementDecodingFailureErrMsg = forAll { (s: String, t: Throwable) =>
val failure = new ElementDecodingFailure(s, t)
failure.getMessage() must_== s"Failure to decode $s. ${t.getMessage()}"
}
}
示例2: MessagesSpec
//设置package包名称以及导入依赖的类
package roc
package postgresql
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen}
import org.specs2._
final class MessagesSpec extends Specification with ScalaCheck { def is = s2"""
PasswordMessage
should MD5 encrypt a password with given salt $pmEncrypt
"""
val pmEncrypt = forAll { (user: String, pm: PasswordMessage, salt: Array[Byte]) =>
val md = MessageDigest.getInstance("MD5")
md.update((pm.password+ user).getBytes(StandardCharsets.UTF_8))
val unsaltedHexStr = md.digest().map(x => "%02x".format(x.byteValue)).foldLeft("")(_ + _)
val saltedBytes = unsaltedHexStr.getBytes ++ salt
md.reset()
md.update(saltedBytes)
val passwd = md.digest().map(x => "%02x".format(x.byteValue)).foldLeft("md5")(_ + _)
passwd must_== PasswordMessage.encryptMD5Passwd(user, pm.password, salt)
}
lazy val genByte: Gen[Byte] = arbitrary[Byte]
lazy val genSalt: Gen[Array[Byte]] = Gen.containerOfN[Array, Byte](4, genByte)
lazy val genPasswordMessage: Gen[PasswordMessage] = for {
password <- arbitrary[String]
} yield new PasswordMessage(password)
implicit lazy val implicitPasswordMessage: Arbitrary[PasswordMessage] =
Arbitrary(genPasswordMessage)
}
示例3: ResultsSpec
//设置package包名称以及导入依赖的类
package roc
package postgresql
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen}
import org.specs2._
import org.specs2.mock.Mockito
import org.specs2.specification.core._
import roc.postgresql.failures.ElementNotFoundFailure
final class ResultsSpec extends Specification with ScalaCheck with Mockito { def is = s2"""
Row
get(column) must throw ElementNotFound failure for unknown column name $columnNotFound
"""
val columnNotFound = forAll { sym: Symbol =>
val row = new Row(List.empty[Element])
row.get(sym) must throwA[ElementNotFoundFailure]
}
lazy val genSymbol: Gen[Symbol] = for {
str <- arbitrary[String]
} yield Symbol(str)
implicit lazy val arbitrarySymbol: Arbitrary[Symbol] =
Arbitrary(genSymbol)
}
示例4: NativeExampleSuite
//设置package包名称以及导入依赖的类
package com.highperformancespark.examples.ffi
import com.holdenkarau.spark.testing._
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Prop.forAll
import org.scalatest.FunSuite
import org.scalatest.prop.Checkers
import org.scalatest.Matchers._
class NativeExampleSuite extends FunSuite
with SharedSparkContext with Checkers with RDDComparisons {
test("local sum") {
val input = Array(1, 2, 3)
val sumMagic = new SumJNI()
val result = sumMagic.sum(input)
val expected = 6
assert(result === expected)
}
test("super simple test") {
val input = sc.parallelize(List(("hi", Array(1, 2, 3))))
val result = NativeExample.jniSum(input).collect()
val expected = List(("hi", 6))
assert(result === expected)
}
test("native call should find sum correctly") {
val property = forAll(
RDDGenerator.genRDD[(String, Array[Int])](sc)(
Arbitrary.arbitrary[(String, Array[Int])])) {
rdd =>
val expected = rdd.mapValues(_.sum)
val result = NativeExample.jniSum(rdd)
compareRDDWithOrder(expected, result).isEmpty
}
check(property)
}
test("JNA support") {
val input = Array(1, 2, 3)
assert(6 === SumJNA.sum(input, input.size))
}
test("JNA Fortran support") {
val input = Array(1, 2, 3)
assert(6 === SumFJNA.easySum(input.size, input))
}
}
示例5: StringSpecification
//设置package包名称以及导入依赖的类
package example
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
object StringSpecification extends Properties("String") {
property("startsWith") = forAll { (a: String, b: String) =>
(a+b).startsWith(a)
}
property("concatenate") = forAll { (a: String, b: String) =>
(a+b).length >= a.length && (a+b).length >= b.length
}
property("substring") = forAll { (a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
}
//TODO see https://github.com/rickynils/scalacheck/blob/master/doc/UserGuide.md
}
示例6: maxBounded
//设置package包名称以及导入依赖的类
package newts.internal
import cats.kernel.Eq
import cats.kernel.laws.OrderLaws
import cats.laws._
import cats.laws.discipline._
import cats.syntax.order._
import org.scalacheck.{Arbitrary, Cogen}
import org.scalacheck.Prop.forAll
trait BoundedTests[A] extends OrderLaws[A] {
def maxBounded(implicit maxBounded: MaxBounded[A]): RuleSet = new OrderProperties(
name = "MaxBounded",
parent = Some(order),
"maxValue is the maximum" -> forAll((a: A) => (maxBounded.maxValue max a) <-> maxBounded.maxValue)
)
def minBounded(implicit minBounded: MinBounded[A]): RuleSet = new OrderProperties(
name = "MaxBounded",
parent = Some(order),
"minValue is the minimum" -> forAll((a: A) => (minBounded.minValue min a) <-> minBounded.minValue)
)
}
object BoundedTests {
def apply[A: Eq: Arbitrary: Cogen]: BoundedTests[A] =
new BoundedTests[A] {
def Equ = Eq[A]
def Arb = implicitly[Arbitrary[A]]
def Cog = implicitly[Cogen[A]]
}
}
示例7: Generators
//设置package包名称以及导入依赖的类
package better.testing.palindrome
import Generators._
import org.scalacheck._
import org.scalacheck.Prop.forAll
import org.scalacheck.Arbitrary.arbitrary
import org.scalatest.prop.PropertyChecks
import org.scalatest.PropSpec
object Generators {
val palindromeGen: Gen[String] = for {
s <- arbitrary[String]
} yield s
}
object PalindromeCheck extends Properties("Palindrome") with PalindromeCheckers {
property("checkReverse") = forAll(palindromeGen) { s =>
checkReverse(s)
}
}
class PalindromeSpecifications extends PropSpec with PropertyChecks with PalindromeCheckers {
import org.scalatest._
import Matchers._
property("checkReverse") { forAll(palindromeGen) { s =>
checkReverse(s) shouldBe true
}}
}
示例8: Generators
//设置package包名称以及导入依赖的类
package better.testing.palindrome
import Generators._
import org.scalacheck._
import org.scalacheck.Prop.forAll
import org.scalacheck.Arbitrary.arbitrary
import org.scalatest.prop.PropertyChecks
import org.scalatest.PropSpec
object Generators {
val palindromeGen: Gen[String] = for {
base <- arbitrary[String]
middle <- Gen.option(arbitrary[Char])
} yield base + middle.getOrElse("") + base.reverse
val maybePalindromeGen: Gen[String] = Gen.oneOf(palindromeGen,arbitrary[String])
}
object PalindromeCheck extends Properties("Palindrome") with PalindromeCheckers {
property("checkReverse") = forAll(palindromeGen) { s =>
checkReverse(s)
}
property("checkIndices") = forAll(maybePalindromeGen) { s =>
checkIndices(s) == checkReverse(s)
}
}
class PalindromeSpecifications extends PropSpec with PropertyChecks with PalindromeCheckers {
import org.scalatest._
import Matchers._
property("checkReverse") { forAll(palindromeGen) { s =>
checkReverse(s) shouldBe true
}}
property("checkIndices") { forAll(palindromeGen) { s =>
checkIndices(s) shouldBe true
}}
property("checkIndices returns false when not palindrome") { forAll(maybePalindromeGen) { s =>
checkIndices(s) shouldBe checkReverse(s)
}}
}
示例9: TensorFlowTypeSpec
//设置package包名称以及导入依赖的类
package shapeless.datatype.tensorflow
import java.net.URI
import org.joda.time.Instant
import org.scalacheck.Prop.{all, forAll}
import org.scalacheck.ScalacheckShapeless._
import org.scalacheck._
import org.tensorflow.example.{Feature, Int64List}
import shapeless._
import shapeless.datatype.record._
import scala.collection.JavaConverters._
object TensorFlowTypeSpec extends Properties("TensorFlowType") {
import shapeless.datatype.test.Records._
import shapeless.datatype.test.SerializableUtils._
implicit def compareByteArrays(x: Array[Byte], y: Array[Byte]) = java.util.Arrays.equals(x, y)
implicit def compareIntArrays(x: Array[Int], y: Array[Int]) = java.util.Arrays.equals(x, y)
implicit def compareDouble(x: Double, y: Double) = x.toFloat == y.toFloat
def roundTrip[A, L <: HList](m: A)
(implicit
gen: LabelledGeneric.Aux[A, L],
fromL: FromFeatures[L],
toL: ToFeatures[L],
mr: MatchRecord[L]): Prop = {
val t = ensureSerializable(TensorFlowType[A])
val rm = RecordMatcher[A]
all(
t.fromExample(t.toExample(m)).exists(rm(_, m)),
t.fromExampleBuilder(t.toExampleBuilder(m)).exists(rm(_, m)))
}
implicit val timestampTensorFlowMappableType = TensorFlowType.at[Instant](
TensorFlowType.toLongs(_).map(new Instant(_)),
xs => TensorFlowType.fromLongs(xs.map(_.getMillis)))
property("required") = forAll { m: Required => roundTrip(m) }
property("optional") = forAll { m: Optional => roundTrip(m) }
property("repeated") = forAll { m: Repeated => roundTrip(m) }
property("mixed") = forAll { m: Mixed => roundTrip(m) }
property("seqs") = forAll { m: Seqs => roundTrip(m) }
implicit val uriTensorFlowType = TensorFlowType.at[URI](
TensorFlowType.toStrings(_).map(URI.create),
xs => TensorFlowType.fromStrings(xs.map(_.toString)))
property("custom") = forAll { m: Custom => roundTrip(m)}
}
示例10: DatastoreTypeSpec
//设置package包名称以及导入依赖的类
package shapeless.datatype.datastore
import java.net.URI
import com.google.datastore.v1.client.DatastoreHelper._
import org.scalacheck.Prop.{all, forAll}
import org.scalacheck.ScalacheckShapeless._
import org.scalacheck._
import shapeless._
import shapeless.datatype.record._
object DatastoreTypeSpec extends Properties("DatastoreType") {
import shapeless.datatype.test.Records._
import shapeless.datatype.test.SerializableUtils._
implicit def compareByteArrays(x: Array[Byte], y: Array[Byte]) = java.util.Arrays.equals(x, y)
implicit def compareIntArrays(x: Array[Int], y: Array[Int]) = java.util.Arrays.equals(x, y)
def roundTrip[A, L <: HList](m: A)
(implicit
gen: LabelledGeneric.Aux[A, L],
fromL: FromEntity[L],
toL: ToEntity[L],
mr: MatchRecord[L]): Prop = {
val t = ensureSerializable(DatastoreType[A])
val rm = RecordMatcher[A]
all(
t.fromEntity(t.toEntity(m)).exists(rm(_, m)),
t.fromEntityBuilder(t.toEntityBuilder(m)).exists(rm(_, m)))
}
property("required") = forAll { m: Required => roundTrip(m) }
property("optional") = forAll { m: Optional => roundTrip(m) }
property("repeated") = forAll { m: Repeated => roundTrip(m) }
property("mixed") = forAll { m: Mixed => roundTrip(m) }
property("nested") = forAll { m: Nested => roundTrip(m) }
property("seqs") = forAll { m: Seqs => roundTrip(m) }
implicit val uriDatastoreType = DatastoreType.at[URI](
v => URI.create(v.getStringValue), u => makeValue(u.toString).build())
property("custom") = forAll { m: Custom => roundTrip(m)}
}
示例11: AtomicBitSetSpec
//设置package包名称以及导入依赖的类
package uk.co.odinconsultants.bitset
import org.junit.runner.RunWith
import org.scalacheck.Gen.choose
import org.scalacheck.Prop.forAll
import org.scalatest.junit.JUnitRunner
import org.scalatest.{Matchers, WordSpec}
@RunWith(classOf[JUnitRunner])
class AtomicBitSetSpec extends WordSpec with Matchers {
val smallInteger = choose(0,1000)
"bitset" should {
"indicate when everything is set" in {
val propSmallInteger = forAll(smallInteger) { n =>
val toTest = new AtomicBitSet(n)
for (i <- 0 to n) toTest.set(i.toLong)
toTest.isEverythingSet
}
}
}
}
示例12: TimeValidationSpec
//设置package包名称以及导入依赖的类
package com.redbubble.hawk.validate
import com.redbubble.hawk.params._
import com.redbubble.hawk.spec.Generators
import com.redbubble.hawk.util.Time
import com.redbubble.hawk.util.Time._
import com.redbubble.hawk.validate.TimeValidation.{acceptableTimeDelta, validate}
import com.redbubble.hawk.{HeaderValidationMethod, _}
import com.redbubble.util.spec.SpecHelper
import org.scalacheck.Prop.forAll
import org.scalacheck.Properties
import org.specs2.mutable.Specification
final class TimeValidationSpec extends Specification with SpecHelper with Generators {
val credentials = Credentials(KeyId("fred"), Key("d0p1h1n5"), Sha256)
val timestamps = new Properties("Timestamps") {
property("are valid if within the interval") = forAll { (time: Time) =>
val delta = nowUtc.minus(time).getStandardSeconds
if (delta > acceptableTimeDelta.getStandardSeconds) {
validate(credentials, context(time), HeaderValidationMethod) must beLeft
} else {
validate(credentials, context(time), HeaderValidationMethod) must beRight
}
}
}
s2"Validating timestamps$timestamps"
private def context(time: Time): ValidatableRequestContext = {
val header = RequestAuthorisationHeader(
KeyId("fred"), time, Nonce(Base64Encoded("nonce")), None, Some(ExtendedData("data")), MAC(Base64Encoded("base64")))
ValidatableRequestContext(RequestContext(Get, Host("example.com"), Port(80), UriPath("/"), None), header)
}
}
示例13: WindowedMaxTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.mux
import org.junit.runner.RunWith
import org.scalacheck.Gen
import org.scalacheck.Prop.forAll
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.Checkers
@RunWith(classOf[JUnitRunner])
class WindowedMaxTest extends FunSuite with Checkers {
test("return max") {
check {
forAll { ary: Array[Long] =>
forAll(Gen.posNum[Int]) { window: Int =>
val w = new WindowedMax(window)
for (v <- ary) w.add(v)
val expected =
if (ary.isEmpty) Long.MinValue
else if (window > ary.length) ary.max
else ary.takeRight(window).max
expected == w.get
}
}
}
}
}
示例14: Converter1Test2
//设置package包名称以及导入依赖的类
package com.stulsoft.pscalacheck.myclasses
import org.scalacheck.Gen
import org.scalacheck.Prop.forAll
import org.scalatest.FlatSpec
import org.scalatest.prop.Checkers
class Converter1Test2 extends FlatSpec with Checkers {
behavior of "Converter1"
"toInt" should "produce length of names for all symbols" in {
// ScalaTest style
check((x: String) => Converter1(x).toInt == x.length)
}
it should "produce length for ASCII symbols" in {
// ScalaCheck style
val usAsciiStringGen = Gen.containerOf[Array, Char](Gen.choose[Char](0, 127)).map(_.mkString)
forAll(usAsciiStringGen) { x: String => Converter1(x).toInt == x.length }
}
}
示例15: MonadLawsCheck
//设置package包名称以及导入依赖的类
import MonadLaws._
import org.scalacheck._
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
import Arbitrary.arbitrary
import scala.language.higherKinds
sealed abstract class MonadLawsCheck[M[_]](name: String)(
implicit MO: Monad[M],
AMI: Arbitrary[M[Int]],
AMS: Arbitrary[M[String]],
AMB: Arbitrary[M[Boolean]]
) extends Properties(s"$name Monad Laws Check") {
property(" Left identity") = forAll {
(a: Int, f: Int => M[Int]) => Laws.leftIdentity(MO)(a)(f)
}
property(" Right identity") = forAll {
(ma: M[Int]) => Laws.rightIdentity(MO)(ma)
}
property(" Associativity") = forAll {
(ma: M[Int], f: Int => M[String], g: String => M[Boolean]) => Laws.associativity(MO)(ma)(f)(g)
}
property(" Left identity No Infix") = forAll {
(a: Int, f: Int => M[Int]) => LawsNoInfix.leftIdentity(MO)(a)(f)
}
property(" Right identity No Infix") = forAll {
(ma: M[Int]) => LawsNoInfix.rightIdentity(MO)(ma)
}
property(" Associativity No Infix") = forAll {
(ma: M[Int], f: Int => M[String], g: String => M[Boolean]) => LawsNoInfix.associativity(MO)(ma)(f)(g)
}
}
object IdMonadLawsCheck extends MonadLawsCheck[Id]("Id")
object ListMonadLawsCheck extends MonadLawsCheck[List]("List")
object OptionMonadLawsCheck extends MonadLawsCheck[Option]("Option")