From 9abda1c91f3b4e0999c856031c5e61d5b61aa661 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Fri, 13 May 2016 14:55:42 +0200 Subject: [PATCH] cleanup and improve tests of utils package --- utils/log.go | 14 ++++++--- utils/log_test.go | 57 +++++++++++++++++++++++++++++++++++++ utils/minmax.go | 67 ++++++++++++++++++++++++++++++++++++++++++++ utils/minmax_test.go | 54 +++++++++++++++++++++++++++++++++++ utils/utils.go | 65 ------------------------------------------ utils/utils_test.go | 38 ------------------------- 6 files changed, 188 insertions(+), 107 deletions(-) create mode 100644 utils/log_test.go create mode 100644 utils/minmax.go create mode 100644 utils/minmax_test.go diff --git a/utils/log.go b/utils/log.go index d2b483a7..5a919df8 100644 --- a/utils/log.go +++ b/utils/log.go @@ -1,6 +1,12 @@ package utils -import "fmt" +import ( + "fmt" + "io" + "os" +) + +var out io.Writer = os.Stdout // LogLevel of quic-go type LogLevel uint8 @@ -26,20 +32,20 @@ func SetLogLevel(level LogLevel) { // Debugf logs something func Debugf(format string, args ...interface{}) { if logLevel == LogLevelDebug { - fmt.Printf(format+"\n", args...) + fmt.Fprintf(out, format+"\n", args...) } } // Infof logs something func Infof(format string, args ...interface{}) { if logLevel <= LogLevelInfo { - fmt.Printf(format+"\n", args...) + fmt.Fprintf(out, format+"\n", args...) } } // Errorf logs something func Errorf(format string, args ...interface{}) { if logLevel <= LogLevelError { - fmt.Printf(format+"\n", args...) + fmt.Fprintf(out, format+"\n", args...) } } diff --git a/utils/log_test.go b/utils/log_test.go new file mode 100644 index 00000000..5ee1108f --- /dev/null +++ b/utils/log_test.go @@ -0,0 +1,57 @@ +package utils + +import ( + "bytes" + "os" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Log", func() { + var ( + b *bytes.Buffer + ) + + BeforeEach(func() { + b = bytes.NewBuffer([]byte{}) + out = b + }) + + AfterEach(func() { + out = os.Stdout + SetLogLevel(LogLevelNothing) + }) + + It("log level nothing", func() { + SetLogLevel(LogLevelNothing) + Debugf("debug") + Infof("info") + Errorf("err") + Expect(b.Bytes()).To(Equal([]byte(""))) + }) + + It("log level err", func() { + SetLogLevel(LogLevelError) + Debugf("debug") + Infof("info") + Errorf("err") + Expect(b.Bytes()).To(Equal([]byte("err\n"))) + }) + + It("log level info", func() { + SetLogLevel(LogLevelInfo) + Debugf("debug") + Infof("info") + Errorf("err") + Expect(b.Bytes()).To(Equal([]byte("info\nerr\n"))) + }) + + It("log level debug", func() { + SetLogLevel(LogLevelDebug) + Debugf("debug") + Infof("info") + Errorf("err") + Expect(b.Bytes()).To(Equal([]byte("debug\ninfo\nerr\n"))) + }) +}) diff --git a/utils/minmax.go b/utils/minmax.go new file mode 100644 index 00000000..e3650d82 --- /dev/null +++ b/utils/minmax.go @@ -0,0 +1,67 @@ +package utils + +import "time" + +// Max returns the maximum of two Ints +func Max(a, b int) int { + if a < b { + return b + } + return a +} + +// MaxUint32 returns the maximum of two uint32 +func MaxUint32(a, b uint32) uint32 { + if a < b { + return b + } + return a +} + +// MaxUint64 returns the maximum of two uint64 +func MaxUint64(a, b uint64) uint64 { + if a < b { + return b + } + return a +} + +// Min returns the minimum of two Ints +func Min(a, b int) int { + if a < b { + return a + } + return b +} + +// MinInt64 returns the minimum of two int64 +func MinInt64(a, b int64) int64 { + if a < b { + return a + } + return b +} + +// MaxInt64 returns the minimum of two int64 +func MaxInt64(a, b int64) int64 { + if a > b { + return a + } + return b +} + +// MaxDuration returns the max duration +func MaxDuration(a, b time.Duration) time.Duration { + if a > b { + return a + } + return b +} + +// AbsDuration returns the absolute value of a time duration +func AbsDuration(d time.Duration) time.Duration { + if d >= 0 { + return d + } + return -d +} diff --git a/utils/minmax_test.go b/utils/minmax_test.go new file mode 100644 index 00000000..d3144b0c --- /dev/null +++ b/utils/minmax_test.go @@ -0,0 +1,54 @@ +package utils + +import ( + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Min / Max", func() { + Context("Max", func() { + It("returns the maximum", func() { + Expect(Max(5, 7)).To(Equal(7)) + Expect(Max(7, 5)).To(Equal(7)) + }) + + It("returns the maximum uint32", func() { + Expect(MaxUint32(5, 7)).To(Equal(uint32(7))) + Expect(MaxUint32(7, 5)).To(Equal(uint32(7))) + }) + + It("returns the maximum uint64", func() { + Expect(MaxUint64(5, 7)).To(Equal(uint64(7))) + Expect(MaxUint64(7, 5)).To(Equal(uint64(7))) + }) + + It("returns the maximum int64", func() { + Expect(MaxInt64(5, 7)).To(Equal(int64(7))) + Expect(MaxInt64(7, 5)).To(Equal(int64(7))) + }) + + It("returns the maximum duration", func() { + Expect(MaxDuration(time.Microsecond, time.Nanosecond)).To(Equal(time.Microsecond)) + Expect(MaxDuration(time.Nanosecond, time.Microsecond)).To(Equal(time.Microsecond)) + }) + }) + + Context("Min", func() { + It("returns the minimum", func() { + Expect(Min(5, 7)).To(Equal(5)) + Expect(Min(7, 5)).To(Equal(5)) + }) + + It("returns the minimum int64", func() { + Expect(MinInt64(7, 5)).To(Equal(int64(5))) + Expect(MinInt64(5, 7)).To(Equal(int64(5))) + }) + }) + + It("returns the abs time", func() { + Expect(AbsDuration(time.Microsecond)).To(Equal(time.Microsecond)) + Expect(AbsDuration(-time.Microsecond)).To(Equal(time.Microsecond)) + }) +}) diff --git a/utils/utils.go b/utils/utils.go index d5a6bdc0..af17da2b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/rand" "io" - "time" ) // ReadStream is the read part of a QUIC stream @@ -160,70 +159,6 @@ func WriteUint16(b *bytes.Buffer, i uint16) { b.WriteByte(uint8((i >> 8) & 0xff)) } -// Max returns the maximum of two Ints -func Max(a, b int) int { - if a < b { - return b - } - return a -} - -// MaxUint32 returns the maximum of two uint32 -func MaxUint32(a, b uint32) uint32 { - if a < b { - return b - } - return a -} - -// MaxUint64 returns the maximum of two uint64 -func MaxUint64(a, b uint64) uint64 { - if a < b { - return b - } - return a -} - -// Min returns the minimum of two Ints -func Min(a, b int) int { - if a < b { - return a - } - return b -} - -// MinInt64 returns the minimum of two int64 -func MinInt64(a, b int64) int64 { - if a < b { - return a - } - return b -} - -// MaxInt64 returns the minimum of two int64 -func MaxInt64(a, b int64) int64 { - if a > b { - return a - } - return b -} - -// MaxDuration returns the max duration -func MaxDuration(a, b time.Duration) time.Duration { - if a > b { - return a - } - return b -} - -// AbsDuration returns the absolute value of a time duration -func AbsDuration(d time.Duration) time.Duration { - if d >= 0 { - return d - } - return -d -} - // RandomBit returns a cryptographically secure random bit (encoded as true / false) func RandomBit() (bool, error) { // ToDo: it's probably more efficient to read a bigger slice of random numbers at once and to cache them somewhere diff --git a/utils/utils_test.go b/utils/utils_test.go index 7e0fc0a5..2a51f5cb 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -2,7 +2,6 @@ package utils import ( "bytes" - "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -167,43 +166,6 @@ var _ = Describe("Utils", func() { }) }) - Context("Max", func() { - It("returns the maximum", func() { - Expect(Max(5, 7)).To(Equal(7)) - }) - - It("returns the maximum uint32", func() { - Expect(MaxUint32(5, 7)).To(Equal(uint32(7))) - }) - - It("returns the maximum uint64", func() { - Expect(MaxUint64(5, 7)).To(Equal(uint64(7))) - }) - - It("returns the maximum int64", func() { - Expect(MaxInt64(5, 7)).To(Equal(int64(7))) - }) - - It("returns the maximum duration", func() { - Expect(MaxDuration(time.Microsecond, time.Nanosecond)).To(Equal(time.Microsecond)) - }) - }) - - It("returns the abs time", func() { - Expect(AbsDuration(time.Microsecond)).To(Equal(time.Microsecond)) - Expect(AbsDuration(-time.Microsecond)).To(Equal(time.Microsecond)) - }) - - Context("Min", func() { - It("returns the minimum", func() { - Expect(Min(5, 7)).To(Equal(5)) - }) - - It("returns the minimum int64", func() { - Expect(MinInt64(5, 7)).To(Equal(int64(5))) - }) - }) - Context("Rand", func() { It("returns either true or false", func() { val, err := RandomBit()