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


Scala PropertyChecks类代码示例

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


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

示例1: Demo2Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FunSpec, Matchers}

// DEMO 2 - use assume with an ensuring that compares with math.sqrt
class Demo2Spec extends FunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRootB function") {
    it("should compute the square root") {
      forAll { (x: Double) =>
        whenever (x >= 0.0 && !x.isPosInfinity) {
          noException should be thrownBy { squareRootB(x) }
        }
      }
    }
    it("should should throw IAE on negative input") {
      an [AssertionError] should be thrownBy {
        squareRootB(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [AssertionError] should be thrownBy {
        squareRootB(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:33,代码来源:Demo2Spec.scala

示例2: Demo11Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers}

// (Demo 10 - PosInt in the REPL)

// DEMO 11 - Use PosZInt to weaken the require?
// Only input is changed to PosZDouble so far.
class Demo11Spec extends LogicFunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: PosZDouble) =>
        whenever (!x.isPosInfinity) {
          squareRoot2(x) should === (math.sqrt(x))
        }
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] should be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] should be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:37,代码来源:Demo11Spec.scala

示例3: ODateTimeSpec

//设置package包名称以及导入依赖的类
package net.fosdal.oslo.odatetime

import org.joda.time.DateTime
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, WordSpec}

import scala.concurrent.duration._

class ODateTimeSpec extends WordSpec with Matchers with PropertyChecks {

  "ordering order collections correctly" in new Fixture {
    Seq(now, tomorrow, yesterday).sorted(ordering) shouldBe Seq(yesterday, now, tomorrow)
  }

  "comparisons" in new Fixture {
    yesterday < now  shouldBe true
    yesterday > now  shouldBe false
    sameAsNow <= now shouldBe true
    sameAsNow >= now shouldBe true
  }

  "Adding FiniteDuration to DateTime" in new Fixture {
    forAll { millis: Int =>
      now + millis.milliseconds shouldBe now.plusMillis(millis)
    }
  }

  "Subtracting FiniteDuration from DateTime" in new Fixture {
    forAll { millis: Int =>
      now - millis.milliseconds shouldBe now.minusMillis(millis)
    }
  }

  "Subtracting DateTime from DateTime" in new Fixture {
    tomorrow - yesterday shouldBe 2.days
    yesterday - tomorrow shouldBe -2.days
  }

  trait Fixture {
    val now       = DateTime.now
    val sameAsNow = DateTime.now
    val tomorrow  = now.plusDays(1)
    val yesterday = now.minusDays(1)
  }

} 
开发者ID:sfosdal,项目名称:oslo,代码行数:47,代码来源:ODateTimeSpec.scala

示例4: BarcoderTest

//设置package包名称以及导入依赖的类
package kokellab.utils.misc

import java.io.{ByteArrayInputStream, ByteArrayOutputStream}

import com.google.zxing.BarcodeFormat
import org.scalacheck.Gen
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}

class BarcoderTest extends PropSpec with GeneratorDrivenPropertyChecks with Matchers {

		def fakeEncodeDecode(text: String, barcodeFormat: BarcodeFormat, dimensions: (Int, Int), imageFormat: String): String =
			if (text.isEmpty) text else encodeDecode(text.toUpperCase, barcodeFormat, dimensions, imageFormat)

		def genBoundedList[T](maxSize: Int, gen: Gen[T]): Gen[List[T]] =
			Gen.choose(0, maxSize) flatMap (size => Gen.listOfN(size, gen))

		def genBoundedString(maxSize: Int, gen: Gen[Char]): Gen[String] =
			Gen.choose(0, maxSize) flatMap (size => Gen.listOfN(size, gen) map (_.mkString))

		def encodeDecode(text: String, codeFormat: BarcodeFormat, dimensions: (Int, Int), imageFormat: String): String = {
			val barcoder = new Barcoder(codeFormat, imageFormat, dimensions._1, dimensions._2)
			val os = new ByteArrayOutputStream()
			barcoder.encode(text, os)
			val is = new ByteArrayInputStream(os.toByteArray)
			barcoder.decode(is)
		}

		val imageFormatGen = Gen.oneOf("png", "jpg", "gif")
		def test(barcodeFormat: BarcodeFormat, dimensionsGen: Gen[(Int, Int)], stringGen: Gen[String]) = {
			property(s"Decoding an encoded string should yield the original string for ${barcodeFormat.name} codes") {
				forAll(imageFormatGen, stringGen, dimensionsGen) { (imageFormat: String, text: String, dimensions: (Int, Int)) =>
					fakeEncodeDecode(text, barcodeFormat, dimensions, imageFormat) should equal (text.toUpperCase)
				}
			}
		}

		val rectangularGen: Gen[(Int, Int)] = for {
			width <- Gen.choose(20, 100)
			height <- Gen.choose(20, 100)
		} yield (width, height)

		val squareGen: Gen[(Int, Int)] = for {
			size <- Gen.choose(20, 100)
		} yield (size, size)

		val code39And93Gen: Gen[String] = genBoundedString(48, Gen.frequency((36, Gen.alphaNumChar), (7, Gen.oneOf('-', '.', '$', '/', '+', '%', ' '))))
		test(BarcodeFormat.CODE_39, rectangularGen, code39And93Gen)
		test(BarcodeFormat.CODE_93, rectangularGen, code39And93Gen)

		// TODO this fails due to https://github.com/zxing/zxing/issues/716
		// there's nothing I can do now
//		test(BarcodeFormat.CODE_128, rectangularGen, genBoundedString(48, Gen.choose[Char](0x20, 127)))

		// TODO QR codes break; also not my fault
//		test(BarcodeFormat.QR_CODE, squareGen, genBoundedString(4296, Gen.frequency((36, Gen.alphaNumChar), (8, Gen.oneOf('-', '.', '$', '/', '+', '%', ' ', ':')))))

} 
开发者ID:kokellab,项目名称:kl-common-scala,代码行数:59,代码来源:BarcoderTest.scala

示例5: FileHasherTest

//设置package包名称以及导入依赖的类
package kokellab.utils.misc

import java.io.ByteArrayInputStream
import java.nio.file.Paths

import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}

import scala.io.Source

class FileHasherTest extends PropSpec with GeneratorDrivenPropertyChecks with Matchers {

	val hasher = new FileHasher("SHA-256")
	println(hasher.hash("aef46c1e5d854fcfd20ab427b599c86102b2fc6252fcdfe9be84dafdb58d6ffb".getBytes()))

	val anyByteArray: Gen[List[Byte]] = Gen.listOf(Arbitrary.arbByte.arbitrary)
	property("Correct hashes should validate") {
		forAll(anyByteArray) { (array: List[Byte]) =>
			val hex = hasher.hash(array)
			hasher.validate(array, hex)
		}
	}

	property("Incorrect hashes should not validate") {
		// this is a bit weird, but if we generated a byte array and a string hash hex, we couldn't guarantee that the hashes don't match
		forAll(anyByteArray, anyByteArray) { (arrayA: List[Byte], arrayB: List[Byte]) =>
			if (arrayA != arrayB) {
				a [ValidationFailedException] should be thrownBy {
					val hexB = hasher.hash(arrayB)
					hasher.validate(arrayA, hexB)
				}
			}
		}
	}

	property("Can hash from stream") {
		forAll(anyByteArray) { (array: List[Byte]) =>
			val hex = hasher.hash(new ByteArrayInputStream(array.toArray))
			hasher.validate(new ByteArrayInputStream(array.toArray), hex)
		}
	}

	property("Can hash from file") {
		val archive = Paths.get(this.getClass.getResource("test.7z").toURI)
		val file = Paths.get(this.getClass.getResource("expected_file_to_hash.txt").toURI)
		hasher.hash(archive) should equal ("afa411f16a1e7943cb07a57516c593384c097e8521f840b2112d2680877a2b04")
		hasher.hash(file) should equal ("aef46c1e5d854fcfd20ab427b599c86102b2fc6252fcdfe9be84dafdb58d6ffb")
	}

} 
开发者ID:kokellab,项目名称:kl-common-scala,代码行数:53,代码来源:FileHasherTest.scala

示例6: AlgosTests

//设置package包名称以及导入依赖的类
package org.ergoplatform.settings

import org.ergoplatform.ErgoGenerators
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}

class AlgosTests extends PropSpec
  with PropertyChecks
  with GeneratorDrivenPropertyChecks
  with Matchers
  with ErgoGenerators with scorex.testkit.SerializationTests {


  property("blockIdDifficulty should be > 0") {
    forAll(genBytesList(32)) { id: Array[Byte] =>
      assert(Algos.blockIdDifficulty(Algos.hash(id)) > 0)

    }
  }
} 
开发者ID:ergoplatform,项目名称:ergo,代码行数:21,代码来源:AlgosTests.scala

示例7: ErgoMemPoolTest

//设置package包名称以及导入依赖的类
package org.ergoplatform.nodeView.mempool

import org.ergoplatform.ErgoGenerators
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}

import scala.concurrent.Await
import scala.concurrent.duration._

class ErgoMemPoolTest extends PropSpec
  with PropertyChecks
  with GeneratorDrivenPropertyChecks
  with Matchers
  with ErgoGenerators {
  property("wait for the appearance of transactions") {
    forAll(blockTransactionsGen) { blockTransactions =>
      val memPool = ErgoMemPool.empty
      val ids = blockTransactions.txs.map(_.id)
      val transactionsFuture = memPool.waitForAll(ids)

      memPool.put(blockTransactions.txs)

      val transactionsFromMempool = Await.result(transactionsFuture, 5.seconds)
      transactionsFromMempool should contain theSameElementsAs blockTransactions.txs
      memPool.waitedForAssembly shouldBe 'empty
    }
  }
} 
开发者ID:ergoplatform,项目名称:ergo,代码行数:29,代码来源:ErgoMemPoolTest.scala

示例8: RulesSpec

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

import state._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.BeforeAndAfter
import org.junit.runner.RunWith
import org.scalatest.OneInstancePerTest
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.junit.JUnitRunner
import hanabi._
import hanabi.ai._
import org.scalatest.prop.PropertyChecks

@RunWith(classOf[JUnitRunner])
class RulesSpec extends FlatSpec with Matchers with MockitoSugar with OneInstancePerTest with BeforeAndAfter
    with PropertyChecks with HanabiDomain {
  import SimpleRules._
  import Cards._

  "simple rules" should "have proper number of cards" in {
    forAll { c: Card =>
      whenever(c.level == 1) {
        count(c) shouldBe 3
      }
    }
    forAll { c: Card =>
      whenever(c.level == 5) {
        count(c) shouldBe 1
      }
    }
    forAll { c: Card =>
      whenever(c.level > 1 && c.level < 5) {
        count(c) shouldBe 2
      }
    }
  }

  implicit override val generatorDrivenConfig = PropertyCheckConfiguration(
    minSuccessful = 100,
    maxDiscardedFactor = 15)
} 
开发者ID:wl-seclin-hashcode,项目名称:hanabi,代码行数:43,代码来源:RulesSpec.scala

示例9: ComplexProperties

//设置package包名称以及导入依赖的类
// src/main/scala/progscala2/toolslibs/toolslibs/ComplexProperties.scala
package progscala2.toolslibs
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class ComplexProperties extends FunSuite with PropertyChecks {

  def additionTest(a: Complex, b: Complex) = {
    assert( (a + b).real === (a.real + b.real) )
    assert( (a + b).imaginary === (a.imaginary + b.imaginary) )
  }

  def subtractionTest(a: Complex, b: Complex) = {
    assert( (a - b).real === (a.real - b.real) )
    assert( (a - b).imaginary === (a.imaginary - b.imaginary) )
  }

  val zero = Complex(0.0, 0.0)

  test ("Complex addition with the identity element (zero)") {
    forAll { (real: Double, imag: Double) =>
      val c = Complex(real, imag)
      additionTest(zero, c)
      additionTest(c, zero)
    }
  }

  test ("Complex subtraction with the identity element (zero)") {
    forAll { (real: Double, imag: Double) =>
      val c = Complex(real, imag)
      subtractionTest(zero, c)
      subtractionTest(c, zero)
    }
  }

  test ("Complex addition with two values") {
    forAll { (real1: Double, imag1: Double, real2: Double, imag2: Double) =>
      val c1 = Complex(real1, imag1)
      val c2 = Complex(real2, imag2)
      additionTest(c1, c2)
    }
  }

  test ("Complex subtraction with two values") {
    forAll { (real1: Double, imag1: Double, real2: Double, imag2: Double) =>
      val c1 = Complex(real1, imag1)
      val c2 = Complex(real2, imag2)
      subtractionTest(c1, c2)
    }
  }
} 
开发者ID:vr1090,项目名称:scala-ing,代码行数:52,代码来源:ComplexProperties.scala

示例10: Demo17Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, WillMatchers}

// Demo 17 - use implies with q and q
class Demo17Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: PosZDouble) =>
        val p = x will !== (PosZDouble.PositiveInfinity)
        val q = squareRoot3(x).value will === (math.sqrt(x))
        p implies q
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:34,代码来源:Demo17Spec.scala

示例11: Demo13Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers}

// (Demo 12 PosInt.ensuringValid in the REPL)

// DEMO 13 - Use PosZDouble as result type
class Demo13Spec extends LogicFunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: PosZDouble) =>
        whenever (!x.isPosInfinity) {
          squareRoot3(x).value should === (math.sqrt(x))
        }
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] should be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] should be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:36,代码来源:Demo13Spec.scala

示例12: Demo16Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, WillMatchers}

// DEMO 16 use implies
class Demo16Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: PosZDouble) =>
        (x will !== (PosZDouble.PositiveInfinity)) implies {
          squareRoot3(x).value will === (math.sqrt(x))
        }
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:34,代码来源:Demo16Spec.scala

示例13: Demo15Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers, WillMatchers}

// (Demo 14 was REPL will and expect)

// DEMO 15 use Expectations with whenever
class Demo15Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: PosZDouble) =>
        whenever (!x.isPosInfinity) {
          squareRoot3(x).value will === (math.sqrt(x))
        }
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:36,代码来源:Demo15Spec.scala

示例14: Demo18Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Expectation, Fact, LogicFunSpec, WillMatchers}

// Demo 18 - use implies with Booleans
class Demo18Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: PosZDouble) =>
        x != PosZDouble.PositiveInfinity implies squareRoot3(x).value == math.sqrt(x)
      }
    }
    it("should compute the square root (version 2)") {
      forAll { (x: PosZDouble) =>
        val p: Fact = x != PosZDouble.PositiveInfinity
        val q: Fact = squareRoot3(x).value == math.sqrt(x)
        p implies q
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] will be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:39,代码来源:Demo18Spec.scala

示例15: Demo9Spec

//设置package包名称以及导入依赖的类
package org.scalatest.examples

// Type-constructor polymorphism

import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers}

// DEMO 9 - Our forAll in a LogicSpec
class Demo9Spec extends LogicFunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {

  describe("The squareRoot1 function") {
    it("should compute the square root") {
      forAll { (x: Double) =>
        whenever (x >= 0.0 && !x.isPosInfinity) {
          squareRoot1(x) should === (math.sqrt(x))
        }
      }
    }
    it("should should throw IAE on negative input") {
      an [IllegalArgumentException] should be thrownBy {
        squareRoot1(-1.0)
      }
    }
    it("should should throw IAE on positive infinity input") {
      an [IllegalArgumentException] should be thrownBy {
        squareRoot1(Double.PositiveInfinity)
      }
    }
  }
} 
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:33,代码来源:Demo9Spec.scala


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