当前位置: 首页>>代码示例>>Scala>>正文


Scala Prop类代码示例

本文整理汇总了Scala中org.scalacheck.Prop的典型用法代码示例。如果您正苦于以下问题:Scala Prop类的具体用法?Scala Prop怎么用?Scala Prop使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Prop类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: QuickCheckHeap

//设置package包名称以及导入依赖的类
package quickcheck

import common._
import org.scalacheck.{Prop, _}
import Arbitrary._
import Gen._
import Prop._

abstract class QuickCheckHeap extends Properties("Heap") with IntHeap {

  lazy val genHeap: Gen[H] = for {
    n <- arbitrary[Int]
    h <- oneOf(const(empty), genHeap)
  } yield insert(n, h)

  implicit lazy val arbHeap: Arbitrary[H] = Arbitrary(genHeap)

  def toList(h:H):List[Int] = if (isEmpty(h)) Nil else findMin(h) :: toList(deleteMin(h))

  property("min1") = forAll { a: Int =>
    val h = insert(a, empty)
    findMin(h) == a
  }

  property("order") = forAll { (a:Int, b: Int) =>
    val h = insert(a, insert(b, empty))
    findMin(h) == (if (a > b) b else a)
  }

  property("empty") = forAll { a:Int =>
    val h = insert(a, empty)
    deleteMin(h) == empty
  }

  property("melding") = forAll { (a:Int, b: Int) =>
    val h = insert(a, empty)
    val i = insert(b, empty)
    val merged = meld(h, i)

    findMin(merged) ==  Math.min(a, b)
  }

  property("melding heaps") = forAll { (h:H, i:H) =>
    val minH = findMin(h)
    val minI = findMin(i)
    val merged = meld(h, i)
    findMin(merged) == Math.min(minH, minI)
  }

  property("associative meld") = forAll { (h:H, i:H, j:H) =>
    val a = meld(meld(h, i), j)
    val b = meld(h, meld(i, j))
    toList(a) == toList(b)
  }

  property("order of mins") = forAll { (h:H) =>
    toList(h).zip(toList(h).drop(1)).forall {
      case (x, y) => x <= y
    }
  }
} 
开发者ID:alvsanand,项目名称:scala_specialization_coursera,代码行数:62,代码来源:QuickCheck.scala

示例2: QuickCheckBinomialHeap

//设置package包名称以及导入依赖的类
package quickcheck

import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

import org.scalatest.prop.Checkers
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop
import org.scalacheck.Prop._

import org.scalatest.exceptions.TestFailedException

object QuickCheckBinomialHeap extends QuickCheckHeap with BinomialHeap

@RunWith(classOf[JUnitRunner])
class QuickCheckSuite extends FunSuite with Checkers {
  def checkBogus(p: Prop) {
    var ok = false
    try {
      check(p)
    } catch {
      case e: TestFailedException =>
        ok = true
    }
    assert(ok, "A bogus heap should NOT satisfy all properties. Try to find the bug!")
  }

  test("Binomial heap satisfies properties.") {
    check(new QuickCheckHeap with quickcheck.test.BinomialHeap)
  }

  test("Bogus (1) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with quickcheck.test.Bogus1BinomialHeap)
  }

  test("Bogus (2) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with quickcheck.test.Bogus2BinomialHeap)
  }

  test("Bogus (3) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with quickcheck.test.Bogus3BinomialHeap)
  }

  test("Bogus (4) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with quickcheck.test.Bogus4BinomialHeap)
  }

  test("Bogus (5) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with quickcheck.test.Bogus5BinomialHeap)
  }
} 
开发者ID:vincenzobaz,项目名称:Functional-Programming-in-Scala,代码行数:53,代码来源:QuickCheckSuite.scala

示例3: ListeMonoidNonIso

//设置package包名称以及导入依赖的类
package psug201607.annexe.liste_monoid_non_iso

import org.scalatest.FunSuite
import org.scalacheck.{Prop, Test}
import org.scalacheck.Shapeless._
import org.scalacheck.Prop._

class ListeMonoidNonIso extends FunSuite with org.typelevel.discipline.scalatest.Discipline {

  val lconcat = new ListeOptConcat[Int]
  val labsorb = new ListeOptAbsorb[Int]

  checkAll("ListeCCMonoid1" , lconcat.laws.monoid)
  checkAll("ListeCCMonoid2" , labsorb.laws.monoid)
  checkAll("Eq Liste"       , EqListe(lconcat, labsorb).eqliste)

  test("Contre exemple") {
    check(Prop {
      val gauche = Cons(None   , Vide)
      val droit  = Cons(Some(0), Vide)

      lconcat.plus(gauche, droit) != labsorb.plus(gauche, droit)
    })
  }
} 
开发者ID:christophe-calves,项目名称:psug-2016.7,代码行数:26,代码来源:ListeMonoidNonIso.scala

示例4: QuickCheckBinomialHeap

//设置package包名称以及导入依赖的类
package quickcheck

import org.scalatest.FunSuite

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

import org.scalatest.prop.Checkers
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop
import org.scalacheck.Prop._

import org.scalatest.exceptions.TestFailedException

object QuickCheckBinomialHeap extends QuickCheckHeap with BinomialHeap

@RunWith(classOf[JUnitRunner])
class QuickCheckSuite extends FunSuite with Checkers {
  def checkBogus(p: Prop) {
    var ok = false
    try {
      check(p)
    } catch {
      case e: TestFailedException =>
        ok = true
    }
    assert(ok, "A bogus heap should NOT satisfy all properties. Try to find the bug!")
  }

  test("Binomial heap satisfies properties.") {
    check(new QuickCheckHeap with BinomialHeap)
  }

  test("Bogus (1) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with Bogus1BinomialHeap)
  }

  test("Bogus (2) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with Bogus2BinomialHeap)
  }

  test("Bogus (3) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with Bogus3BinomialHeap)
  }

  test("Bogus (4) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with Bogus4BinomialHeap)
  }

  test("Bogus (5) binomial heap does not satisfy properties.") {
    checkBogus(new QuickCheckHeap with Bogus5BinomialHeap)
  }
} 
开发者ID:zearom32,项目名称:Courses,代码行数:54,代码来源:QuickCheckSuite.scala

示例5: SessionManagerBasicEncoderTest

//设置package包名称以及导入依赖的类
package com.softwaremill.session

import org.scalacheck.{Gen, Prop, Properties}

object SessionManagerBasicEncoderTest extends Properties("SessionManagerBasicEncoder") {

  import Prop._

  val secretGen = Gen.choose(64, 256).flatMap(size => Gen.listOfN(size, Gen.alphaNumChar).map(_.mkString))

  property("encode+decode") = forAllNoShrink(secretGen) { (secret: String) =>
    forAll { (encrypt: Boolean, useMaxAgeSeconds: Boolean, data: Map[String, String]) =>
      val config = SessionConfig.default(secret)
        .copy(sessionEncryptData = encrypt)
        .copy(sessionMaxAgeSeconds = if (useMaxAgeSeconds) Some(3600L) else None)
      val manager = new SessionManager[Map[String, String]](config).clientSessionManager

      manager.decode(manager.encode(data)) == SessionResult.Decoded(data)
    }
  }

  property("doesn't decode expired session") = forAllNoShrink(secretGen) { (secret: String) =>
    forAll { (encrypt: Boolean, data: Map[String, String]) =>
      val config = SessionConfig.default(secret)
        .copy(sessionEncryptData = encrypt)
        .copy(sessionMaxAgeSeconds = Some(20L)) // expires after 20s
      val managerPast = new SessionManager[Map[String, String]](config) {
        override def nowMillis = 8172L * 1000L
      }.clientSessionManager
      val managerFuture = new SessionManager[Map[String, String]](config) {
        override def nowMillis = (8172L + 600L) * 1000L // 600s later
      }.clientSessionManager

      managerFuture.decode(managerPast.encode(data)) == SessionResult.Expired
    }
  }
} 
开发者ID:adamw,项目名称:testpr,代码行数:38,代码来源:SessionManagerBasicEncoderTest.scala

示例6: PropForEach

//设置package包名称以及导入依赖的类
package prisoners_dilemma

import org.scalacheck.Prop

object PropForEach {

  def constantly[I, R](result: R): I => R = (i:I) => result
  def emptyString: Any => String = constantly("")

  def forEach[T](thing: Seq[T], property: T => Prop,
    message: T => String = emptyString): Prop = {
    Prop.all(thing.map{a =>
        val p = property(a)
        if (message(a).nonEmpty)
          p :| message(a)
        else p} :_*)
  }
} 
开发者ID:Mharlin,项目名称:better-testing-workshop,代码行数:19,代码来源:PropForEach.scala

示例7: ApplicativeSpec

//设置package包名称以及导入依赖的类
package uscala.cats.result

import cats.std.all._
import cats.syntax.cartesian._
import org.specs2.{ScalaCheck, Specification}
import uscala.cats.syntax.result._
import applicative._
import org.scalacheck.{Gen, Prop}
import uscala.result.Result
import uscala.result.specs2.ResultMatchers

class ApplicativeSpec extends Specification with ResultMatchers with ScalaCheck  {
  import ApplicativeSpec._

  override def is =
    s2"""
        Can collect multiple failures on the left of a result $test
      """

  def test = Prop.forAllNoShrink(list)(errs =>
    errs.foldLeft(ResultNel.ok[String, String](new String))((acc, v) =>
      (acc |@| Result.fail[String, String](v).toResultNel).map(_ + _)
    ) must beFail.like { case a => a.unwrap must containTheSameElementsAs(errs) }
  )
}

object ApplicativeSpec {
  val stringGen = Gen.alphaStr.suchThat(_.nonEmpty)
  val list = Gen.listOf(stringGen).suchThat(_.nonEmpty)
} 
开发者ID:janstenpickle,项目名称:uscala-cats,代码行数:31,代码来源:ApplicativeSpec.scala

示例8: check

//设置package包名称以及导入依赖的类
package iotatests

import cats._
import org.scalacheck.Prop
import org.scalacheck.Prop._
import shapeless.{ Id => _, _ }
import shapeless.ops.hlist.{ ToList => HListToList }

import scala.reflect.runtime.universe._

sealed trait TypeEqv[A] {
  def check(x: A, y: A): Prop
}

object TypeEqv extends TypeEqvInstances0 {
  object syntax {
    final implicit class TypeEqvOps[A](x: A)(implicit eqv: TypeEqv[A]) {
      def ?=:=(y: A): Prop = eqv.check(x, y)
    }
  }
}

sealed class TypeEqvInstances0 extends TypeEqvInstances1 {
  implicit def idTypeEqv[A <: Type]: TypeEqv[A] = new TypeEqv[A] {
    def check(x: A, y: A): Prop =
      Prop(x =:= y) :| s"$x was not =:= to $y"
  }

  implicit def eitherTypeEqv[A, B](
    implicit eqv: TypeEqv[B]
  ): TypeEqv[Either[A, B]] = new TypeEqv[Either[A, B]] {
    def check(ex: Either[A, B], ey: Either[A, B]): Prop =
      (ex, ey) match {
        case (Right(bx), Right(by)) => eqv.check(bx, by)
        case _                      => ex ?= ey
      }
  }
}

sealed class TypeEqvInstances1 {
  implicit def foldableTypeEqv[F[_], A](
    implicit F: Foldable[F], eqv: TypeEqv[A]
  ): TypeEqv[F[A]] = new TypeEqv[F[A]] {
    def check(fx: F[A], fy: F[A]): Prop =
      (F.toList(fx) zip F.toList(fy)).foldLeft(proved)((acc, vv) =>
        acc && eqv.check(vv._1, vv._2))
  }

  implicit def genericTypeEqv[P, L <: HList, A](
    implicit
      gen: Generic.Aux[P, L],
      toList: HListToList[L, A],
      eqv: TypeEqv[List[A]]
  ): TypeEqv[P] = new TypeEqv[P] {
    def check(x: P, y: P): Prop =
      eqv.check(toList(gen.to(x)), toList(gen.to(y)))
  }
} 
开发者ID:frees-io,项目名称:iota,代码行数:59,代码来源:TypeEqv.scala

示例9: Test

//设置package包名称以及导入依赖的类
package scalachecklib

import shapeless._
import shapeless.ops.function._

import cats.implicits._
import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Gen
import Prop.forAll

import org.scalatest.exceptions._

import org.scalacheck.Shapeless._

object Test {

  def testSuccess[F, R, L <: HList](method: F, answer: L)(
      implicit A: Arbitrary[L],
      fntop: FnToProduct.Aux[F, L ? R]
  ): Prop = {
    val rightGen = genRightAnswer(answer)
    val rightProp = forAll(rightGen)({ p ?
      val result = Either.catchOnly[GeneratorDrivenPropertyCheckFailedException]({
        fntop(method)(p)
      })
      result match {
        case Left(exc) ?
          exc.cause match {
            case Some(originalException) ? throw originalException
            case _                       ? false
          }
        case _ ? true
      }
    })

    val wrongGen = genWrongAnswer(answer)
    val wrongProp = forAll(wrongGen)({ p ?
      Either.catchNonFatal({ fntop(method)(p) }).isLeft
    })

    Prop.all(rightProp, wrongProp)
  }

  def genRightAnswer[L <: HList](answer: L): Gen[L] =
    Gen.const(answer)

  def genWrongAnswer[L <: HList](l: L)(
      implicit A: Arbitrary[L]
  ): Gen[L] =
    A.arbitrary.suchThat(_ != l)
} 
开发者ID:scala-exercises,项目名称:exercises-scalacheck,代码行数:52,代码来源:Test.scala

示例10: DependencyParserProps

//设置package包名称以及导入依赖的类
package net.ssanj.dabble

import org.scalacheck.Properties
import org.scalacheck.{Prop, Gen}
import org.scalacheck.Gen.{posNum, negNum}
import scalaz._
import scalaz.std.list._

object DependencyParserProps extends Properties("DependencyParser") with DabbleProps {

 property("returns a valid dependency from a valid input") =
  Prop.forAll(genDependency) { inputs: Seq[String] =>
      val \/-(Seq(dep)) = DependencyParser.parseDependencies(inputs)
      dep match {
        case ScalaVersionSupplied(org, name, version, None) =>
          Seq(org, "%" , name, "%", version) == inputs
        case ScalaVersionSupplied(org, name, version, Some(config)) =>
          Seq(org, "%" , name, "%", version, "%", config) == inputs
        case ScalaVersionDerived (org, name, version, None) =>
          Seq(org, "%%", name, "%", version) == inputs
        case ScalaVersionDerived (org, name, version, Some(config)) =>
          Seq(org, "%%", name, "%", version, "%", config) == inputs
      }
  }

property("returns a valid list of dependencies from a valid list of inputs")=
  Prop.forAll(genDependencyList) { inputs: Seq[String] =>
    val \/-(deps) = DependencyParser.parseDependencies(inputs)
    val outputs = intersperse(deps.map {
      case ScalaVersionSupplied(org, name, version, None) =>
        Seq(org, "%" , name, "%", version)
      case ScalaVersionSupplied(org, name, version, Some(config)) =>
        Seq(org, "%" , name, "%", version, "%", config)
      case ScalaVersionDerived (org, name, version, None) =>
        Seq(org, "%%", name, "%", version)
      case ScalaVersionDerived (org, name, version, Some(config)) =>
        Seq(org, "%%", name, "%", version, "%", config)
    }.toList, Seq("+")).flatten

    inputs == outputs
  }

property("returns an empty list of dependencies if the input is invalid") =
  Prop.forAll(emptyInput) { inputs: Seq[String] =>
    val -\/(error) = DependencyParser.parseDependencies(inputs)
    error == s"unable to derive dependencies from: $inputs"
  }
} 
开发者ID:ssanj,项目名称:dabble,代码行数:49,代码来源:DependencyParserProps.scala

示例11: encode

//设置package包名称以及导入依赖的类
package io.rout

import algebra.Eq
import cats.laws._
import cats.laws.discipline._
import cats.std.AllInstances
import com.twitter.io.Buf
import org.scalacheck.{Prop, Arbitrary}
import org.typelevel.discipline.Laws
import shapeless.Witness

trait EncodeLaws[A, CT <: String] extends Laws with MissingInstances with AllInstances {

  def encode: Encode.Aux[A, CT]

  def roundTrip(a: A): IsEq[Buf] =
    encode(a) <-> Buf.Utf8(a.toString)

  def all(implicit A: Arbitrary[A], eq: Eq[A]): RuleSet = new DefaultRuleSet(
    name = "all",
    parent = None,
    "roundTrip" -> Prop.forAll { (a: A) => roundTrip(a) }
  )
}

object EncodeLaws {
  def texthtml[A: Encode.TextHtml]: EncodeLaws[A, Witness.`"text/html"`.T] =
    new EncodeLaws[A, Witness.`"text/html"`.T] {
      val encode: Encode.Aux[A, Witness.`"text/html"`.T] = implicitly[Encode.TextHtml[A]]
    }
} 
开发者ID:teodimoff,项目名称:rOut,代码行数:32,代码来源:EncodeLaws.scala

示例12: DataStructuresProperties

//设置package包名称以及导入依赖的类
import org.scalatest.FlatSpec
import org.scalacheck.{Prop, Properties}
import fpinscala.datastructures._


object DataStructuresProperties extends Properties("Chapter 3 Properties") {
  property("Tail of non empty list") = Prop.forAll {
    (h: Int, x: scala.List[Int]) =>
      List(x: _*) == List.tail(Cons(h, List(x: _*)))
  }

  property("Set head of tail") = Prop.forAll {
    (h: Int, newH: Int, x: scala.List[Int]) => {
      val l1 = Cons(h, List(x: _*))
      val l2 = List.setHead(l1, newH)
      (l1, l2) match {
        case (Cons(l1Head, l1Tail), Cons(l2Head, l2Tail)) =>
          (l1Head compareTo l2Head) == (h compareTo newH) &&
            l1Tail == l2Tail
        case _ => false
      }
    }
  }

  property("FoldLeft via foldRight") = Prop.forAll {
    (l: scala.List[Int]) => {
      val newL = List.apply(l: _*)
      val a = List.foldLeft(newL, Nil: List[Int])((acc, h) => Cons(h, acc))
      val b = List.foldLeftViaFoldRight(newL, Nil: List[Int])((acc, h) => Cons(h, acc))
      a == b
    }
  }
}

class DataStructuresTests extends FlatSpec {
  "Dropping an element from a list" should "result in a list shorter by that number" in {
    List.drop(List(1,2,3,4,5), 2) === List(3,4,5) &&
    List.drop(List(1,2,3,4,5), 0) === List(1,2,3,4,5)
  }

  "Dropwhile on predicate" should "result in a list missing the failed elements from the head" in {
    List.dropWhile[Int](List(1,2,3,4,5,6,7,8,9,10), _ < 6) === List(6,7,8,9,10)
  }

  "Init of short list" should "be list without the last element" in {
    List.init(List(1,2,3,4,5,6)) === List(1,2,3,4,5)
  }

  "Reverse of list" should "be reversed" in {
    List.reverse(List(1,2,3,4,5)) === List(5,4,3,2,1)
  }

  "Flatmap" should "flatten a mapping functions result" in {
    List.flatMap(List(1,2,3))(i => List(i,i)) === List(1,1,2,2,3,3)
  }

  "Filter" should "remove correct elements" in {
    List.filterViaFlatMap(List(1,2,3,4,5,6,7,8))(_ % 2 == 0) === List(2,4,6,8)
  }
} 
开发者ID:GarySGlover,项目名称:FPInScala,代码行数:61,代码来源:DataStructuresTests.scala

示例13: P28aCheck

//设置package包名称以及导入依赖的类
package jp.co.dwango.s99

import org.scalacheck.{Prop, Properties}

class P28aCheck extends Properties("P28a") {
  property("lsort()") = {
    Prop.forAll { (s: List[List[Int]]) =>
      val a = P28a.lsort(s)
      if (a.length < 2) {
        true
      } else {
        a.zip(a.tail).forall { case (l, r) => l.length <= r.length }
      }
    }
  }
} 
开发者ID:dwango,项目名称:S99,代码行数:17,代码来源:P28aCheck.scala

示例14: P26Check

//设置package包名称以及导入依赖的类
package jp.co.dwango.s99

import org.scalacheck.{Prop, Gen, Arbitrary, Properties}

class P26Check extends Properties("P26") {
  property("combinations()") = {
    val gen = for {
      n <- Gen.choose(0, 10)
      s <- Gen.listOfN(n + 5, implicitly[Arbitrary[Int]].arbitrary)
    } yield (s, n)

    Prop.forAll(gen) {
      case (s, n) =>
        val lc = P26.combinations(n, s).map { _.sorted }
        val rc = s.combinations(n).map { _.sorted }.toList
        lc.exists { l =>
          rc.contains(l)
        } && rc.exists { r =>
          lc.contains(r)
        }
    }
  }
} 
开发者ID:dwango,项目名称:S99,代码行数:24,代码来源:P26Check.scala

示例15: P27bCheck

//设置package包名称以及导入依赖的类
package jp.co.dwango.s99

import org.scalacheck.{Prop, Properties, Gen, Arbitrary}

class P27bCheck extends Properties("P27b") {
  property("group()") = {
    val gen = for {
      g1 <- Gen.listOfN(3, Gen.choose(1, 3)) // To avoid StackOverflowError, small numbers are chosen
      g2 <- Gen.listOfN(g1.sum, implicitly[Arbitrary[Int]].arbitrary)
      if g2.distinct.length == g2.length
    } yield (g1, g2)

    Prop.forAll(gen) {
      case (s1: List[Int], s2: List[Int]) =>
        val a: List[List[List[Int]]] = P27b.group(s1, s2)
        a.forall { b =>
          s1.length == b.length && b.zip(s1).forall {
            case (c, n) => c.length == n && c.distinct.length == c.length
          }
        }
    }
  }
} 
开发者ID:dwango,项目名称:S99,代码行数:24,代码来源:P27bCheck.scala


注:本文中的org.scalacheck.Prop类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。