github.com/DFWallet/tendermint-cosmos@v0.0.2/types/time/time_test.go (about) 1 package time 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestWeightedMedian(t *testing.T) { 11 m := make([]*WeightedTime, 3) 12 13 t1 := Now() 14 t2 := t1.Add(5 * time.Second) 15 t3 := t1.Add(10 * time.Second) 16 17 m[2] = NewWeightedTime(t1, 33) // faulty processes 18 m[0] = NewWeightedTime(t2, 40) // correct processes 19 m[1] = NewWeightedTime(t3, 27) // correct processes 20 totalVotingPower := int64(100) 21 22 median := WeightedMedian(m, totalVotingPower) 23 assert.Equal(t, t2, median) 24 // median always returns value between values of correct processes 25 assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) && 26 (median.Before(t3) || median.Equal(t3))) 27 28 m[1] = NewWeightedTime(t1, 40) // correct processes 29 m[2] = NewWeightedTime(t2, 27) // correct processes 30 m[0] = NewWeightedTime(t3, 33) // faulty processes 31 totalVotingPower = int64(100) 32 33 median = WeightedMedian(m, totalVotingPower) 34 assert.Equal(t, t2, median) 35 // median always returns value between values of correct processes 36 assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) && 37 (median.Before(t2) || median.Equal(t2))) 38 39 m = make([]*WeightedTime, 8) 40 t4 := t1.Add(15 * time.Second) 41 t5 := t1.Add(60 * time.Second) 42 43 m[3] = NewWeightedTime(t1, 10) // correct processes 44 m[1] = NewWeightedTime(t2, 10) // correct processes 45 m[5] = NewWeightedTime(t2, 10) // correct processes 46 m[4] = NewWeightedTime(t3, 23) // faulty processes 47 m[0] = NewWeightedTime(t4, 20) // correct processes 48 m[7] = NewWeightedTime(t5, 10) // faulty processes 49 totalVotingPower = int64(83) 50 51 median = WeightedMedian(m, totalVotingPower) 52 assert.Equal(t, t3, median) 53 // median always returns value between values of correct processes 54 assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) && 55 (median.Before(t4) || median.Equal(t4))) 56 }