From 32b2da3f28937a7b1320245dcdd97f0a83f9767c Mon Sep 17 00:00:00 2001 From: Tom McCabe Date: Wed, 6 Jul 2016 15:04:50 -0500 Subject: [PATCH] add respective generators and specs for ScriptNumber, Block, BlockHeader, CompactSizeUInt --- .../org/bitcoins/core/number/UInt32Spec.scala | 1 - .../core/protocol/CompactSizeUIntSpec.scala | 5 ++ .../protocol/blockchain/BlockHeaderSpec.scala | 14 +++++ .../script/constant/ScriptNumberSpec.scala | 61 +++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/test/scala/org/bitcoins/core/protocol/blockchain/BlockHeaderSpec.scala create mode 100644 src/test/scala/org/bitcoins/core/script/constant/ScriptNumberSpec.scala diff --git a/src/test/scala/org/bitcoins/core/number/UInt32Spec.scala b/src/test/scala/org/bitcoins/core/number/UInt32Spec.scala index 5a16e28533..b6096b18f2 100644 --- a/src/test/scala/org/bitcoins/core/number/UInt32Spec.scala +++ b/src/test/scala/org/bitcoins/core/number/UInt32Spec.scala @@ -80,7 +80,6 @@ class UInt32Spec extends Properties("UInt32") with BitcoinSLogger { } } - property("== & !=") = { Prop.forAll(NumberGenerator.uInt32s, NumberGenerator.uInt32s) { (num1 : UInt32, num2 : UInt32) => if (num1.underlying == num2.underlying) num1 == num2 diff --git a/src/test/scala/org/bitcoins/core/protocol/CompactSizeUIntSpec.scala b/src/test/scala/org/bitcoins/core/protocol/CompactSizeUIntSpec.scala index 77100d9ede..06d4e090fb 100644 --- a/src/test/scala/org/bitcoins/core/protocol/CompactSizeUIntSpec.scala +++ b/src/test/scala/org/bitcoins/core/protocol/CompactSizeUIntSpec.scala @@ -13,4 +13,9 @@ class CompactSizeUIntSpec extends Properties("CompactSizeUIntSpec") { Prop.forAll(NumberGenerator.positiveLongs) { num : Long => CompactSizeUInt.parseCompactSizeUInt(CompactSizeUInt(num).hex).num == num } + //:TODO Remove one of these + property("Serialization symmetry with separate generator") = + Prop.forAll(NumberGenerator.compactSizeUInts) { compact : CompactSizeUInt => + CompactSizeUInt(compact.num, compact.size) == compact + } } diff --git a/src/test/scala/org/bitcoins/core/protocol/blockchain/BlockHeaderSpec.scala b/src/test/scala/org/bitcoins/core/protocol/blockchain/BlockHeaderSpec.scala new file mode 100644 index 0000000000..b2f02a2cd7 --- /dev/null +++ b/src/test/scala/org/bitcoins/core/protocol/blockchain/BlockHeaderSpec.scala @@ -0,0 +1,14 @@ +package org.bitcoins.core.protocol.blockchain + +import org.bitcoins.core.gen.BlockchainElementsGenerator +import org.scalacheck.{Prop, Properties} + +/** + * Created by tom on 7/6/16. + */ +class BlockHeaderSpec extends Properties("BlockHeaderSpec") { + property("serialization symmetry") = + Prop.forAll(BlockchainElementsGenerator.blockHeader) { header => + BlockHeader(header.version, header.previousBlockHash, header.merkleRootHash, header.time, header.nBits, header.nonce) == header + } +} diff --git a/src/test/scala/org/bitcoins/core/script/constant/ScriptNumberSpec.scala b/src/test/scala/org/bitcoins/core/script/constant/ScriptNumberSpec.scala new file mode 100644 index 0000000000..c22fed9998 --- /dev/null +++ b/src/test/scala/org/bitcoins/core/script/constant/ScriptNumberSpec.scala @@ -0,0 +1,61 @@ +package org.bitcoins.core.script.constant + +import com.oracle.webservices.internal.api.message.PropertySet.Property +import org.bitcoins.core.gen.NumberGenerator +import org.bitcoins.core.number.UInt32 +import org.bitcoins.core.util.BitcoinSLogger +import org.scalacheck.{Prop, Properties} + +/** + * Created by tom on 7/5/16. + */ +class ScriptNumberSpec extends Properties("ScriptNumberSpec") with BitcoinSLogger { + property("Additive identity") = + Prop.forAll(NumberGenerator.scriptNumbers) { num : ScriptNumber => + num + ScriptNumber.zero == num + } + property("Subtraction identity") = + Prop.forAll(NumberGenerator.scriptNumbers) { num : ScriptNumber => + num - ScriptNumber.zero == num + } + property("Multiplicative identity") = + Prop.forAll(NumberGenerator.scriptNumbers) { num : ScriptNumber => + num * ScriptNumber.one == num + } + property("< >=") = + Prop.forAll(NumberGenerator.scriptNumbers, NumberGenerator.scriptNumbers) { (num1 : ScriptNumber, num2 : ScriptNumber) => + if (num1.underlying < num2.underlying) num1 < num2 + else num1 >= num2 + } + property("> <=") = + Prop.forAll(NumberGenerator.scriptNumbers, NumberGenerator.scriptNumbers) { (num1 : ScriptNumber, num2 : ScriptNumber) => + if (num1.underlying > num2.underlying) num1 > num2 + else num1 <= num2 + } + property("== & !=") = { + Prop.forAll(NumberGenerator.scriptNumbers, NumberGenerator.scriptNumbers) { (num1 : ScriptNumber, num2 : ScriptNumber) => + if (num1.underlying == num2.underlying) num1 == num2 + else num1 != num2 + } + } + property("add two script numbers") = + Prop.forAll(NumberGenerator.scriptNumbers, NumberGenerator.scriptNumbers) { (num1 : ScriptNumber, num2: ScriptNumber) => + num1 + num2 == ScriptNumber(num1.underlying + num2.underlying) + } + property("subtract a script number from another script number") = + Prop.forAll(NumberGenerator.scriptNumbers, NumberGenerator.scriptNumbers) { (num1 : ScriptNumber, num2: ScriptNumber) => + num1 - num2 == ScriptNumber(num1.underlying - num2.underlying) + } + property("multiply two script numbers") = + Prop.forAll(NumberGenerator.scriptNumbers, NumberGenerator.scriptNumbers) { (num1 : ScriptNumber, num2: ScriptNumber) => + num1 * num2 == ScriptNumber(num1.underlying * num2.underlying) + } + property("multiply a script number by zero should return zero") = + Prop.forAll(NumberGenerator.scriptNumbers) { (num1 : ScriptNumber) => + num1 * ScriptNumber.zero == ScriptNumber.zero + } + + + + +}