From 38557869052b4d3fcc77e7ec2ce34bbe9f5daf6b Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Tue, 11 Jun 2019 17:49:29 +0800 Subject: [PATCH 1/3] gtype unit test --- g/container/gtype/gtype_z_unit_test.go | 291 +++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 g/container/gtype/gtype_z_unit_test.go diff --git a/g/container/gtype/gtype_z_unit_test.go b/g/container/gtype/gtype_z_unit_test.go new file mode 100644 index 000000000..3b7f0a5e1 --- /dev/null +++ b/g/container/gtype/gtype_z_unit_test.go @@ -0,0 +1,291 @@ +package gtype_test + +import ( + "sync" + "testing" + + "github.com/gogf/gf/g/container/gtype" + "github.com/gogf/gf/g/test/gtest" +) + +type Temp struct { + Name string + Age int +} + +func Test_Bool(t *testing.T) { + gtest.Case(t, func() { + i := gtype.NewBool(true) + iClone := i.Clone() + gtest.Assert(iClone.Set(false), true) + gtest.Assert(iClone.Val(), false) + + i1 := gtype.NewBool(false) + iClone1 := i1.Clone() + gtest.Assert(iClone1.Set(true), false) + gtest.Assert(iClone1.Val(), true) + + //空参测试 + i2 := gtype.NewBool() + gtest.Assert(i2.Val(), false) + }) +} + +func Test_Byte(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 127 + i := gtype.NewByte(byte(0)) + iClone := i.Clone() + gtest.Assert(iClone.Set(byte(1)), byte(0)) + gtest.Assert(iClone.Val(), byte(1)) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(byte(addTimes), i.Val()) + + //空参测试 + i1 := gtype.NewByte() + gtest.Assert(i1.Val(), byte(0)) + }) +} + +func Test_Bytes(t *testing.T) { + gtest.Case(t, func() { + i := gtype.NewBytes([]byte("abc")) + iClone := i.Clone() + gtest.Assert(iClone.Set([]byte("123")), []byte("abc")) + gtest.Assert(iClone.Val(), []byte("123")) + + //空参测试 + i1 := gtype.NewBytes() + gtest.Assert(i1.Val(), nil) + }) +} + +func Test_String(t *testing.T) { + gtest.Case(t, func() { + i := gtype.NewString("abc") + iClone := i.Clone() + gtest.Assert(iClone.Set("123"), "abc") + gtest.Assert(iClone.Val(), "123") + + //空参测试 + i1 := gtype.NewString() + gtest.Assert(i1.Val(), "") + }) +} + +func Test_Interface(t *testing.T) { + gtest.Case(t, func() { + t := Temp{Name: "gf", Age: 18} + t1 := Temp{Name: "gf", Age: 19} + i := gtype.New(t) + iClone := i.Clone() + gtest.Assert(iClone.Set(t1), t) + gtest.Assert(iClone.Val().(Temp), t1) + + //空参测试 + i1 := gtype.New() + gtest.Assert(i1.Val(), nil) + }) +} + +func Test_Float32(t *testing.T) { + gtest.Case(t, func() { + //var wg sync.WaitGroup + //addTimes := 100 + i := gtype.NewFloat32(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(0.1), 0.0) + gtest.Assert(iClone.Val(), 0.1) + // for index := 0; index < addTimes; index++ { + // wg.Add(1) + // go func() { + // defer wg.Done() + // i.Add(0.2) + // fmt.Println(i.Val()) + // }() + // } + // wg.Wait() + // gtest.Assert(100.0, i.Val()) + + //空参测试 + i1 := gtype.NewFloat32() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Float64(t *testing.T) { + gtest.Case(t, func() { + //var wg sync.WaitGroup + //addTimes := 100 + i := gtype.NewFloat64(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(0.1), 0.0) + gtest.Assert(iClone.Val(), 0.1) + // for index := 0; index < addTimes; index++ { + // wg.Add(1) + // go func() { + // defer wg.Done() + // i.Add(0.1) + // fmt.Println(i.Val()) + // }() + // } + // wg.Wait() + // gtest.Assert(100.0, i.Val()) + + //空参测试 + i1 := gtype.NewFloat64() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Int(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 1000 + i := gtype.NewInt(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(1), 0) + gtest.Assert(iClone.Val(), 1) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(addTimes, i.Val()) + + //空参测试 + i1 := gtype.NewInt() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Int32(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 1000 + i := gtype.NewInt32(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(1), 0) + gtest.Assert(iClone.Val(), 1) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(addTimes, i.Val()) + + //空参测试 + i1 := gtype.NewInt32() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Int64(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 1000 + i := gtype.NewInt64(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(1), 0) + gtest.Assert(iClone.Val(), 1) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(addTimes, i.Val()) + + //空参测试 + i1 := gtype.NewInt64() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Uint(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 1000 + i := gtype.NewUint(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(1), 0) + gtest.Assert(iClone.Val(), 1) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(addTimes, i.Val()) + + //空参测试 + i1 := gtype.NewUint() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Uint32(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 1000 + i := gtype.NewUint32(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(1), 0) + gtest.Assert(iClone.Val(), 1) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(addTimes, i.Val()) + + //空参测试 + i1 := gtype.NewUint32() + gtest.Assert(i1.Val(), 0) + }) +} + +func Test_Uint64(t *testing.T) { + gtest.Case(t, func() { + var wg sync.WaitGroup + addTimes := 1000 + i := gtype.NewUint64(0) + iClone := i.Clone() + gtest.Assert(iClone.Set(1), 0) + gtest.Assert(iClone.Val(), 1) + for index := 0; index < addTimes; index++ { + wg.Add(1) + go func() { + defer wg.Done() + i.Add(1) + }() + } + wg.Wait() + gtest.Assert(addTimes, i.Val()) + + //空参测试 + i1 := gtype.NewUint64() + gtest.Assert(i1.Val(), 0) + }) +} From 165330ec6879131aab89b3e4096e26416872be79 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Wed, 12 Jun 2019 11:21:10 +0800 Subject: [PATCH 2/3] gchan unit test --- g/container/gchan/gchan.go | 44 +++++++++++++----------- g/container/gchan/gchan_z_unit_test.go | 47 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 g/container/gchan/gchan_z_unit_test.go diff --git a/g/container/gchan/gchan.go b/g/container/gchan/gchan.go index 9e762162f..eeba2e153 100644 --- a/g/container/gchan/gchan.go +++ b/g/container/gchan/gchan.go @@ -10,55 +10,61 @@ package gchan import ( - "errors" - "github.com/gogf/gf/g/container/gtype" + "errors" + + "github.com/gogf/gf/g/container/gtype" ) // Graceful channel. type Chan struct { - channel chan interface{} - closed *gtype.Bool + channel chan interface{} + closed *gtype.Bool } // New creates a graceful channel with given . func New(limit int) *Chan { - return &Chan { - channel : make(chan interface{}, limit), - closed : gtype.NewBool(), - } + return &Chan{ + channel: make(chan interface{}, limit), + closed: gtype.NewBool(), + } } // Push pushes to channel. // It is safe to be called repeatedly. func (c *Chan) Push(value interface{}) error { - if c.closed.Val() { - return errors.New("channel is closed") - } - c.channel <- value - return nil + if c.closed.Val() { + return errors.New("channel is closed") + } + c.channel <- value + return nil } // Pop pops value from channel. // If there's no value in channel, it would block to wait. // If the channel is closed, it will return a nil value immediately. func (c *Chan) Pop() interface{} { - return <- c.channel + return <-c.channel } // Close closes the channel. // It is safe to be called repeatedly. func (c *Chan) Close() { - if !c.closed.Set(true) { - close(c.channel) - } + if !c.closed.Set(true) { + close(c.channel) + } } // See Len. func (c *Chan) Size() int { - return c.Len() + return c.Len() } // Len returns the length of the channel. func (c *Chan) Len() int { return len(c.channel) -} \ No newline at end of file +} + +// Cap returns the capacity of the channel. +func (c *Chan) Cap() int { + return cap(c.channel) +} diff --git a/g/container/gchan/gchan_z_unit_test.go b/g/container/gchan/gchan_z_unit_test.go new file mode 100644 index 000000000..057113627 --- /dev/null +++ b/g/container/gchan/gchan_z_unit_test.go @@ -0,0 +1,47 @@ +package gchan_test + +import ( + "errors" + "testing" + + "github.com/gogf/gf/g/container/gchan" + "github.com/gogf/gf/g/test/gtest" +) + +func Test_Gchan(t *testing.T) { + gtest.Case(t, func() { + ch := gchan.New(10) + + gtest.Assert(ch.Cap(), 10) + gtest.Assert(ch.Push(1), nil) + gtest.Assert(ch.Len(), 1) + gtest.Assert(ch.Size(), 1) + ch.Pop() + gtest.Assert(ch.Len(), 0) + gtest.Assert(ch.Size(), 0) + ch.Close() + gtest.Assert(ch.Push(1), errors.New("channel is closed")) + + ch = gchan.New(0) + ch1 := gchan.New(0) + go func() { + var i = 0 + for { + v := ch.Pop() + if v == nil { + ch1.Push(i) + break + } + gtest.Assert(v, i) + i++ + } + }() + + for index := 0; index < 10; index++ { + ch.Push(index) + } + ch.Close() + gtest.Assert(ch1.Pop(), 10) + ch1.Close() + }) +} From abdf8e696c1bca8ec2cb0760f1c37fe4e07c267c Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Wed, 12 Jun 2019 17:52:30 +0800 Subject: [PATCH 3/3] gtype unit test use AssertEQ replace Assert. --- g/container/gtype/gtype_z_unit_test.go | 100 ++++++++++++------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/g/container/gtype/gtype_z_unit_test.go b/g/container/gtype/gtype_z_unit_test.go index 3b7f0a5e1..9870ed8c6 100644 --- a/g/container/gtype/gtype_z_unit_test.go +++ b/g/container/gtype/gtype_z_unit_test.go @@ -17,17 +17,17 @@ func Test_Bool(t *testing.T) { gtest.Case(t, func() { i := gtype.NewBool(true) iClone := i.Clone() - gtest.Assert(iClone.Set(false), true) - gtest.Assert(iClone.Val(), false) + gtest.AssertEQ(iClone.Set(false), true) + gtest.AssertEQ(iClone.Val(), false) i1 := gtype.NewBool(false) iClone1 := i1.Clone() - gtest.Assert(iClone1.Set(true), false) - gtest.Assert(iClone1.Val(), true) + gtest.AssertEQ(iClone1.Set(true), false) + gtest.AssertEQ(iClone1.Val(), true) //空参测试 i2 := gtype.NewBool() - gtest.Assert(i2.Val(), false) + gtest.AssertEQ(i2.Val(), false) }) } @@ -37,8 +37,8 @@ func Test_Byte(t *testing.T) { addTimes := 127 i := gtype.NewByte(byte(0)) iClone := i.Clone() - gtest.Assert(iClone.Set(byte(1)), byte(0)) - gtest.Assert(iClone.Val(), byte(1)) + gtest.AssertEQ(iClone.Set(byte(1)), byte(0)) + gtest.AssertEQ(iClone.Val(), byte(1)) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -47,11 +47,11 @@ func Test_Byte(t *testing.T) { }() } wg.Wait() - gtest.Assert(byte(addTimes), i.Val()) + gtest.AssertEQ(byte(addTimes), i.Val()) //空参测试 i1 := gtype.NewByte() - gtest.Assert(i1.Val(), byte(0)) + gtest.AssertEQ(i1.Val(), byte(0)) }) } @@ -59,12 +59,12 @@ func Test_Bytes(t *testing.T) { gtest.Case(t, func() { i := gtype.NewBytes([]byte("abc")) iClone := i.Clone() - gtest.Assert(iClone.Set([]byte("123")), []byte("abc")) - gtest.Assert(iClone.Val(), []byte("123")) + gtest.AssertEQ(iClone.Set([]byte("123")), []byte("abc")) + gtest.AssertEQ(iClone.Val(), []byte("123")) //空参测试 i1 := gtype.NewBytes() - gtest.Assert(i1.Val(), nil) + gtest.AssertEQ(i1.Val(), nil) }) } @@ -72,12 +72,12 @@ func Test_String(t *testing.T) { gtest.Case(t, func() { i := gtype.NewString("abc") iClone := i.Clone() - gtest.Assert(iClone.Set("123"), "abc") - gtest.Assert(iClone.Val(), "123") + gtest.AssertEQ(iClone.Set("123"), "abc") + gtest.AssertEQ(iClone.Val(), "123") //空参测试 i1 := gtype.NewString() - gtest.Assert(i1.Val(), "") + gtest.AssertEQ(i1.Val(), "") }) } @@ -87,12 +87,12 @@ func Test_Interface(t *testing.T) { t1 := Temp{Name: "gf", Age: 19} i := gtype.New(t) iClone := i.Clone() - gtest.Assert(iClone.Set(t1), t) - gtest.Assert(iClone.Val().(Temp), t1) + gtest.AssertEQ(iClone.Set(t1), t) + gtest.AssertEQ(iClone.Val().(Temp), t1) //空参测试 i1 := gtype.New() - gtest.Assert(i1.Val(), nil) + gtest.AssertEQ(i1.Val(), nil) }) } @@ -102,8 +102,8 @@ func Test_Float32(t *testing.T) { //addTimes := 100 i := gtype.NewFloat32(0) iClone := i.Clone() - gtest.Assert(iClone.Set(0.1), 0.0) - gtest.Assert(iClone.Val(), 0.1) + gtest.AssertEQ(iClone.Set(0.1), float32(0)) + gtest.AssertEQ(iClone.Val(), float32(0.1)) // for index := 0; index < addTimes; index++ { // wg.Add(1) // go func() { @@ -113,11 +113,11 @@ func Test_Float32(t *testing.T) { // }() // } // wg.Wait() - // gtest.Assert(100.0, i.Val()) + // gtest.AssertEQ(100.0, i.Val()) //空参测试 i1 := gtype.NewFloat32() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), float32(0)) }) } @@ -127,8 +127,8 @@ func Test_Float64(t *testing.T) { //addTimes := 100 i := gtype.NewFloat64(0) iClone := i.Clone() - gtest.Assert(iClone.Set(0.1), 0.0) - gtest.Assert(iClone.Val(), 0.1) + gtest.AssertEQ(iClone.Set(0.1), float64(0)) + gtest.AssertEQ(iClone.Val(), float64(0.1)) // for index := 0; index < addTimes; index++ { // wg.Add(1) // go func() { @@ -138,11 +138,11 @@ func Test_Float64(t *testing.T) { // }() // } // wg.Wait() - // gtest.Assert(100.0, i.Val()) + // gtest.AssertEQ(100.0, i.Val()) //空参测试 i1 := gtype.NewFloat64() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), float64(0)) }) } @@ -152,8 +152,8 @@ func Test_Int(t *testing.T) { addTimes := 1000 i := gtype.NewInt(0) iClone := i.Clone() - gtest.Assert(iClone.Set(1), 0) - gtest.Assert(iClone.Val(), 1) + gtest.AssertEQ(iClone.Set(1), 0) + gtest.AssertEQ(iClone.Val(), 1) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -162,11 +162,11 @@ func Test_Int(t *testing.T) { }() } wg.Wait() - gtest.Assert(addTimes, i.Val()) + gtest.AssertEQ(addTimes, i.Val()) //空参测试 i1 := gtype.NewInt() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), 0) }) } @@ -176,8 +176,8 @@ func Test_Int32(t *testing.T) { addTimes := 1000 i := gtype.NewInt32(0) iClone := i.Clone() - gtest.Assert(iClone.Set(1), 0) - gtest.Assert(iClone.Val(), 1) + gtest.AssertEQ(iClone.Set(1), int32(0)) + gtest.AssertEQ(iClone.Val(), int32(1)) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -186,11 +186,11 @@ func Test_Int32(t *testing.T) { }() } wg.Wait() - gtest.Assert(addTimes, i.Val()) + gtest.AssertEQ(int32(addTimes), i.Val()) //空参测试 i1 := gtype.NewInt32() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), int32(0)) }) } @@ -200,8 +200,8 @@ func Test_Int64(t *testing.T) { addTimes := 1000 i := gtype.NewInt64(0) iClone := i.Clone() - gtest.Assert(iClone.Set(1), 0) - gtest.Assert(iClone.Val(), 1) + gtest.AssertEQ(iClone.Set(1), int64(0)) + gtest.AssertEQ(iClone.Val(), int64(1)) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -210,11 +210,11 @@ func Test_Int64(t *testing.T) { }() } wg.Wait() - gtest.Assert(addTimes, i.Val()) + gtest.AssertEQ(int64(addTimes), i.Val()) //空参测试 i1 := gtype.NewInt64() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), int64(0)) }) } @@ -224,8 +224,8 @@ func Test_Uint(t *testing.T) { addTimes := 1000 i := gtype.NewUint(0) iClone := i.Clone() - gtest.Assert(iClone.Set(1), 0) - gtest.Assert(iClone.Val(), 1) + gtest.AssertEQ(iClone.Set(1), uint(0)) + gtest.AssertEQ(iClone.Val(), uint(1)) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -234,11 +234,11 @@ func Test_Uint(t *testing.T) { }() } wg.Wait() - gtest.Assert(addTimes, i.Val()) + gtest.AssertEQ(uint(addTimes), i.Val()) //空参测试 i1 := gtype.NewUint() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), uint(0)) }) } @@ -248,8 +248,8 @@ func Test_Uint32(t *testing.T) { addTimes := 1000 i := gtype.NewUint32(0) iClone := i.Clone() - gtest.Assert(iClone.Set(1), 0) - gtest.Assert(iClone.Val(), 1) + gtest.AssertEQ(iClone.Set(1), uint32(0)) + gtest.AssertEQ(iClone.Val(), uint32(1)) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -258,11 +258,11 @@ func Test_Uint32(t *testing.T) { }() } wg.Wait() - gtest.Assert(addTimes, i.Val()) + gtest.AssertEQ(uint32(addTimes), i.Val()) //空参测试 i1 := gtype.NewUint32() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), uint32(0)) }) } @@ -272,8 +272,8 @@ func Test_Uint64(t *testing.T) { addTimes := 1000 i := gtype.NewUint64(0) iClone := i.Clone() - gtest.Assert(iClone.Set(1), 0) - gtest.Assert(iClone.Val(), 1) + gtest.AssertEQ(iClone.Set(1), uint64(0)) + gtest.AssertEQ(iClone.Val(), uint64(1)) for index := 0; index < addTimes; index++ { wg.Add(1) go func() { @@ -282,10 +282,10 @@ func Test_Uint64(t *testing.T) { }() } wg.Wait() - gtest.Assert(addTimes, i.Val()) + gtest.AssertEQ(uint64(addTimes), i.Val()) //空参测试 i1 := gtype.NewUint64() - gtest.Assert(i1.Val(), 0) + gtest.AssertEQ(i1.Val(), uint64(0)) }) }