test for remove-extremes
@@ -3,6 +3,7 @@ package timesync
import (
"fmt"
"github.com/spacemeshos/go-spacemesh/assert"
+ "github.com/spacemeshos/go-spacemesh/crypto"
"testing"
"time"
)
@@ -30,4 +31,28 @@ func TestNtpPacket_Time(t *testing.T) {
assert.True(t, p.Time().Year() == sysTime.Year(), "Converted and system time should be the same year")
}
+func generateRandomDurations() (sortableDurations, error) {
+ sd := sortableDurations{}
+ rand := 0
+ for rand < 10 {
+ rand = int(crypto.GetRandomUInt32(50))
+ }
+ for i := 0; i < rand; i++ {
+ randomSecs := crypto.GetRandomUInt32(100)
+ sd = append(sd, time.Duration(time.Second*time.Duration(randomSecs)))
+ }
+ return sd, nil
+}
+
+func TestSortableDurations_RemoveExtremes(t *testing.T) {
+ // Generate slice of len (random) of random durations (with max 100 seconds)
+ durations, err := generateRandomDurations()
+ if err != nil {
+ t.Error("could'nt create durations")
+ }
+ baseLen := len(durations)
+ durations.RemoveExtremes()
+ assert.True(t, len(durations) == baseLen-2, "Slice length should be reduced by two ")
+}
+
// TODO : add more tests for rest of the functions
