You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.8 KiB

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