[unit-testing] Add test for schedule.go (#1938)

* Fix wrong date in release schedule map
	* Fix panic when PercentageForTimeStamp receives time before first bucket & time after last bucket
pull/1941/head
janet-harmony 5 years ago committed by Edgar Aroutiounian
parent 448a611c1f
commit bca505d297
  1. 10
      consensus/reward/schedule.go
  2. 31
      consensus/reward/schedule_test.go

@ -96,7 +96,7 @@ var (
mustParse("2025-Feb-28"): numeric.MustNewDecFromStr("0.9941"), mustParse("2025-Feb-28"): numeric.MustNewDecFromStr("0.9941"),
mustParse("2025-Mar-31"): numeric.MustNewDecFromStr("0.9961"), mustParse("2025-Mar-31"): numeric.MustNewDecFromStr("0.9961"),
mustParse("2025-Apr-30"): numeric.MustNewDecFromStr("0.9980"), mustParse("2025-Apr-30"): numeric.MustNewDecFromStr("0.9980"),
mustParse("2025-May-30"): numeric.MustNewDecFromStr("1.0000"), mustParse("2025-May-31"): numeric.MustNewDecFromStr("1.0000"),
} }
sorted = func() []pair { sorted = func() []pair {
s := []pair{} s := []pair{}
@ -126,8 +126,12 @@ func PercentageForTimeStamp(ts int64) numeric.Dec {
i, j := 0, 1 i, j := 0, 1
for range sorted { for range sorted {
if i == len(sorted) { if i == (len(sorted) - 1) {
bucket = sorted[i-1] if ts < sorted[0].ts {
bucket = sorted[0]
} else {
bucket = sorted[i]
}
break break
} }
if ts >= sorted[i].ts && ts < sorted[j].ts { if ts >= sorted[i].ts && ts < sorted[j].ts {

@ -0,0 +1,31 @@
package reward
import (
"testing"
"github.com/harmony-one/harmony/numeric"
)
func TestPercentageForTimeStamp(t *testing.T) {
testCases := []struct {
time string
expected string
}{
{"2019-Jan-01", "0.2429"},
{"2019-May-31", "0.2429"},
{"2021-Nov-30", "0.8561"},
{"2023-Apr-29", "0.9488"},
{"2023-Apr-30", "0.9507"},
{"2025-May-31", "1.0000"},
{"2026-Jan-01", "1.0000"},
}
for _, tc := range testCases {
result := PercentageForTimeStamp(mustParse(tc.time))
expect := numeric.MustNewDecFromStr(tc.expected)
if !result.Equal(expect) {
t.Errorf("Time: %s, Chosen bucket percent: %s, Expected: %s",
tc.time, result, expect)
}
}
}
Loading…
Cancel
Save