From 26c040537f19c9c24c940f0d1a2a994a1567baac Mon Sep 17 00:00:00 2001 From: V2Ray Date: Thu, 17 Sep 2015 00:15:06 +0200 Subject: [PATCH] test cases for rand --- math/rand_test.go | 20 +++++++++++++ testing/unit/assertions.go | 4 +++ testing/unit/int64subject.go | 54 ++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 math/rand_test.go create mode 100644 testing/unit/int64subject.go diff --git a/math/rand_test.go b/math/rand_test.go new file mode 100644 index 000000000..1d31e81ea --- /dev/null +++ b/math/rand_test.go @@ -0,0 +1,20 @@ +package math + +import ( + "testing" + "time" + + "github.com/v2ray/v2ray-core/testing/unit" +) + +func TestGenerateRandomInt64InRange(t *testing.T) { + assert := unit.Assert(t) + base := time.Now().Unix() + delta := 100 + + for i := 0; i < 100; i++ { + v := GenerateRandomInt64InRange(base, delta) + assert.Int64(v).AtMost(base + int64(delta)) + assert.Int64(v).AtLeast(base - int64(delta)) + } +} diff --git a/testing/unit/assertions.go b/testing/unit/assertions.go index bb85507a7..fe5b2cfc4 100644 --- a/testing/unit/assertions.go +++ b/testing/unit/assertions.go @@ -16,6 +16,10 @@ func Assert(t *testing.T) *Assertion { return assert } +func (a *Assertion) Int64(value int64) *Int64Subject { + return NewInt64Subject(NewSubject(a), value) +} + func (a *Assertion) Int(value int) *IntSubject { return NewIntSubject(NewSubject(a), value) } diff --git a/testing/unit/int64subject.go b/testing/unit/int64subject.go new file mode 100644 index 000000000..59c3d9c8b --- /dev/null +++ b/testing/unit/int64subject.go @@ -0,0 +1,54 @@ +package unit + +import ( + "strconv" +) + +type Int64Subject struct { + *Subject + value int64 +} + +func NewInt64Subject(base *Subject, value int64) *Int64Subject { + return &Int64Subject{ + Subject: base, + value: value, + } +} + +func (subject *Int64Subject) Named(name string) *Int64Subject { + subject.Subject.Named(name) + return subject +} + +func (subject *Int64Subject) Fail(verb string, other int64) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.FormatInt(other, 10) + ">.") +} + +func (subject *Int64Subject) DisplayString() string { + return subject.Subject.DisplayString(strconv.FormatInt(subject.value, 10)) +} + +func (subject *Int64Subject) Equals(expectation int64) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *Int64Subject) GreaterThan(expectation int64) { + if subject.value <= expectation { + subject.Fail("is greater than", expectation) + } +} + +func (subject *Int64Subject) AtMost(expectation int64) { + if subject.value > expectation { + subject.Fail("is at most", expectation) + } +} + +func (subject *Int64Subject) AtLeast(expectation int64) { + if subject.value < expectation { + subject.Fail("is at least", expectation) + } +}