From e476d19d5e9e2611c57f0f9d47bc0f426433eae6 Mon Sep 17 00:00:00 2001 From: David Stainton <dstainton@elixxir.io> Date: Tue, 30 Aug 2022 18:16:02 -0400 Subject: [PATCH] Add test TestMutateTimestampDeltaAverage --- channels/mutateTimestamp_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/channels/mutateTimestamp_test.go b/channels/mutateTimestamp_test.go index 843757c3e..ba58728c4 100644 --- a/channels/mutateTimestamp_test.go +++ b/channels/mutateTimestamp_test.go @@ -1,7 +1,11 @@ package channels import ( + "crypto/rand" + "testing" "time" + + "gitlab.com/elixxir/crypto/channel" ) // withinMutationWindow is a utility test function to check if a mutated @@ -12,3 +16,30 @@ func withinMutationWindow(raw, mutated time.Time) bool { return mutated.After(lowerBound) && mutated.Before(upperBound) } + +func abs(n int64) int64 { + if n < 0 { + return -n + } + return n +} + +func TestMutateTimestampDeltaAverage(t *testing.T) { + samples := 10000 + t1 := time.Now() + sum := int64(0) + + for i := 0; i < samples; i++ { + var msgID channel.MessageID + rand.Read(msgID[:]) + t2 := mutateTimestamp(t1, msgID) + delta := t2.Sub(t1) + sum += abs(int64(delta)) + } + + avg := sum / int64(samples) + diff := abs(avg - 2502865) + if diff > 30000 { + t.Fatal() + } +} -- GitLab