Skip to content

Commit

Permalink
add tests for getLowBits
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent 96f32ee commit 11b81de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Difficulty {
case len => "0" * (64 - len) + hex
}

def hashLessThanTarget(hash: String, target: String): Boolean = ???
// def hashLessThanTarget(hash: String, target: String): Boolean = ???

def difficultyOf(target: BigInt, negative: Boolean, overflow: Boolean): Double = {
if (target == 0 || negative || overflow) 0.0
Expand Down
30 changes: 20 additions & 10 deletions src/test/scala/com/fluency03/blockchain/core/DifficultyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,34 @@ class DifficultyTest extends FlatSpec with Matchers {
targetOfBits("05009234".hex2Long) shouldEqual "92340000".hex2Long
targetOfBits("04923456".hex2Long) shouldEqual - "12345600".hex2Long
targetOfBits("04123456".hex2Long) shouldEqual "12345600".hex2Long
// println("1d00ffff".hex)
// println(bitsOfTarget(t1, n1))
// println(n1, o1)
// println(bitsOfTarget(t2, n2))
// println(n2, o2)
// println(bitsOfTarget(t3, n3))
// println(n3, o3)
}

"padHexTarget" should "add enough zeros (0) in front of a hex fot making it 64 bytes." in {
padHexTarget(t1.toString(16)) shouldEqual "00000000ffff0000000000000000000000000000000000000000000000000000"
padHexTarget(t2.toString(16)) shouldEqual "0000000000012dcd000000000000000000000000000000000000000000000000"
difficultyOf(t1, n1, o1) shouldEqual 1.0
difficultyOf(t2, n2, o2) shouldEqual 55589.518126868665
// targetOfBits(bitsOfTarget(t1, n1)) shouldEqual t1
// targetOfBits(bitsOfTarget(t2, n2)) shouldEqual t2
difficultyOf(0, n2, o2) shouldEqual 0
difficultyOf(t1, true, o1) shouldEqual 0
difficultyOf(t1, n1, true) shouldEqual 0
}


"getLowBits" should "obtain the N lower bits of a BigInt." in {
getLowBits(BigInt(1000), 2) shouldEqual 0
getLowBits(BigInt(1001), 2) shouldEqual 1
getLowBits(BigInt(1002), 2) shouldEqual 2
getLowBits(BigInt(1003), 2) shouldEqual 3
getLowBits(BigInt(1004), 2) shouldEqual 0
getLowBits(BigInt(1005), 2) shouldEqual 1

getLowBits(BigInt(1000), 4) shouldEqual 8
getLowBits(BigInt(1001), 4) shouldEqual 9
getLowBits(BigInt(1002), 4) shouldEqual 10
getLowBits(BigInt(1003), 4) shouldEqual 11
getLowBits(BigInt(1004), 4) shouldEqual 12
getLowBits(BigInt(1005), 4) shouldEqual 13

getLowBits(BigInt(1000), 8) shouldEqual 232
}

}

0 comments on commit 11b81de

Please sign in to comment.