Add test for winnerProba
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
parent
9c98cf8bc1
commit
8bca545cab
@ -1,6 +1,11 @@
|
|||||||
package messagepool
|
package messagepool
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func TestBlockProbability(t *testing.T) {
|
func TestBlockProbability(t *testing.T) {
|
||||||
mp := &MessagePool{}
|
mp := &MessagePool{}
|
||||||
@ -13,3 +18,26 @@ func TestBlockProbability(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWinnerProba(t *testing.T) {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
const N = 1000000
|
||||||
|
winnerProba := noWinnersProb()
|
||||||
|
sum := 0
|
||||||
|
for i := 0; i < N; i++ {
|
||||||
|
minersRand := rand.Float64()
|
||||||
|
j := 0
|
||||||
|
for ; j < MaxBlocks; j++ {
|
||||||
|
minersRand -= winnerProba[j]
|
||||||
|
if minersRand < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sum += j
|
||||||
|
}
|
||||||
|
|
||||||
|
if avg := float64(sum) / N; math.Abs(avg-5) > 0.01 {
|
||||||
|
t.Fatalf("avg too far off: %f", avg)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user