diff --git a/g/internal/empty/empty_test.go b/g/internal/empty/empty_test.go new file mode 100644 index 000000000..0241a8b2c --- /dev/null +++ b/g/internal/empty/empty_test.go @@ -0,0 +1,90 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package empty_test + +import ( + "github.com/gogf/gf/g" + "github.com/gogf/gf/g/internal/empty" + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" + "testing" +) + +type TestPerson interface { + Say() string +} +type TestWoman struct { +} + +func (woman TestWoman) Say() string { + return "nice" +} + +func TestIsEmpty(t *testing.T) { + gtest.Case(t, func() { + tmpT1 := "0" + tmpT2 := func() {} + tmpT2 = nil + tmpT3 := make(chan int, 0) + var tmpT4 TestPerson = nil + var tmpT5 *TestPerson = nil + tmpF1 := "1" + tmpF2 := func(a string) string { return "1" } + tmpF3 := make(chan int, 1) + tmpF3 <- 1 + var tmpF4 TestPerson = TestWoman{} + tmpF5 := &tmpF4 + // true + gtest.Assert(empty.IsEmpty(nil), true) + gtest.Assert(empty.IsEmpty(gconv.Int(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int8(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int16(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int32(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Int64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint16(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint32(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Float32(tmpT1)), true) + gtest.Assert(empty.IsEmpty(gconv.Float64(tmpT1)), true) + gtest.Assert(empty.IsEmpty(false), true) + gtest.Assert(empty.IsEmpty([]byte("")), true) + gtest.Assert(empty.IsEmpty(""), true) + gtest.Assert(empty.IsEmpty(g.Map{}), true) + gtest.Assert(empty.IsEmpty(g.Slice{}), true) + gtest.Assert(empty.IsEmpty(g.Array{}), true) + gtest.Assert(empty.IsEmpty(tmpT2), true) + gtest.Assert(empty.IsEmpty(tmpT3), true) + gtest.Assert(empty.IsEmpty(tmpT3), true) + gtest.Assert(empty.IsEmpty(tmpT4), true) + gtest.Assert(empty.IsEmpty(tmpT5), true) + // false + gtest.Assert(empty.IsEmpty(gconv.Int(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int8(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int16(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int32(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Int64(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint8(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint16(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint32(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Float32(tmpF1)), false) + gtest.Assert(empty.IsEmpty(gconv.Float64(tmpF1)), false) + gtest.Assert(empty.IsEmpty(true), false) + gtest.Assert(empty.IsEmpty(tmpT1), false) + gtest.Assert(empty.IsEmpty([]byte("1")), false) + gtest.Assert(empty.IsEmpty(g.Map{"a": 1}), false) + gtest.Assert(empty.IsEmpty(g.Slice{"1"}), false) + gtest.Assert(empty.IsEmpty(g.Array{"1"}), false) + gtest.Assert(empty.IsEmpty(tmpF2), false) + gtest.Assert(empty.IsEmpty(tmpF3), false) + gtest.Assert(empty.IsEmpty(tmpF4), false) + gtest.Assert(empty.IsEmpty(tmpF5), false) + }) +} diff --git a/g/internal/mutex/mutex_z_unit_test.go b/g/internal/mutex/mutex_z_unit_test.go new file mode 100644 index 000000000..ca7f0c368 --- /dev/null +++ b/g/internal/mutex/mutex_z_unit_test.go @@ -0,0 +1,99 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package mutex_test + +import ( + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/internal/mutex" + "github.com/gogf/gf/g/test/gtest" + "testing" + "time" +) + +func TestMutexIsSafe(t *testing.T) { + gtest.Case(t, func() { + lock := mutex.New() + gtest.Assert(lock.IsSafe(), true) + + lock = mutex.New(false) + gtest.Assert(lock.IsSafe(), true) + + lock = mutex.New(false, false) + gtest.Assert(lock.IsSafe(), true) + + lock = mutex.New(true, false) + gtest.Assert(lock.IsSafe(), false) + + lock = mutex.New(true, true) + gtest.Assert(lock.IsSafe(), false) + + lock = mutex.New(true) + gtest.Assert(lock.IsSafe(), false) + }) +} + +func TestSafeMutex(t *testing.T) { + gtest.Case(t, func() { + safeLock := mutex.New(false) + array := garray.New() + + go func() { + safeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + safeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(80 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} + +func TestUnsafeMutex(t *testing.T) { + gtest.Case(t, func() { + unsafeLock := mutex.New(true) + array := garray.New() + + go func() { + unsafeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + unsafeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 2) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} diff --git a/g/internal/rwmutex/rwmutex_z_unit_test.go b/g/internal/rwmutex/rwmutex_z_unit_test.go new file mode 100644 index 000000000..2c5cccbc1 --- /dev/null +++ b/g/internal/rwmutex/rwmutex_z_unit_test.go @@ -0,0 +1,138 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package rwmutex_test + +import ( + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/internal/rwmutex" + "github.com/gogf/gf/g/test/gtest" + "testing" + "time" +) + +func TestRwmutexIsSafe(t *testing.T) { + gtest.Case(t, func() { + lock := rwmutex.New() + gtest.Assert(lock.IsSafe(), true) + + lock = rwmutex.New(false) + gtest.Assert(lock.IsSafe(), true) + + lock = rwmutex.New(false, false) + gtest.Assert(lock.IsSafe(), true) + + lock = rwmutex.New(true, false) + gtest.Assert(lock.IsSafe(), false) + + lock = rwmutex.New(true, true) + gtest.Assert(lock.IsSafe(), false) + + lock = rwmutex.New(true) + gtest.Assert(lock.IsSafe(), false) + }) +} + +func TestSafeRwmutex(t *testing.T) { + gtest.Case(t, func() { + safeLock := rwmutex.New() + array := garray.New() + + go func() { + safeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + safeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + safeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 1) + time.Sleep(80 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} + +func TestSafeReaderRwmutex(t *testing.T) { + gtest.Case(t, func() { + safeLock := rwmutex.New() + array := garray.New() + + go func() { + safeLock.RLock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.RUnlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + safeLock.RLock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + safeLock.RUnlock() + }() + go func() { + time.Sleep(50 * time.Millisecond) + safeLock.Lock() + array.Append(1) + safeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 2) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 6) + }) +} + +func TestUnsafeRwmutex(t *testing.T) { + gtest.Case(t, func() { + unsafeLock := rwmutex.New(true) + array := garray.New() + + go func() { + unsafeLock.Lock() + array.Append(1) + time.Sleep(100 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + go func() { + time.Sleep(10 * time.Millisecond) + unsafeLock.Lock() + array.Append(1) + time.Sleep(200 * time.Millisecond) + array.Append(1) + unsafeLock.Unlock() + }() + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 2) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(50 * time.Millisecond) + gtest.Assert(array.Len(), 3) + time.Sleep(100 * time.Millisecond) + gtest.Assert(array.Len(), 4) + }) +} diff --git a/g/os/gcmd/gcmd_z_unit_test.go b/g/os/gcmd/gcmd_z_unit_test.go index bd9e05a84..be548c46a 100644 --- a/g/os/gcmd/gcmd_z_unit_test.go +++ b/g/os/gcmd/gcmd_z_unit_test.go @@ -14,7 +14,6 @@ import ( "testing" ) - func Test_ValueAndOption(t *testing.T) { os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"} doInit() @@ -23,6 +22,31 @@ func Test_ValueAndOption(t *testing.T) { gtest.Assert(Value.Get(0), "v1") gtest.Assert(Value.Get(1), "v2") gtest.Assert(Value.Get(2), "") + gtest.Assert(Value.Get(2, "1"), "1") + gtest.Assert(Value.GetVar(1, "1").String(), "v2") + gtest.Assert(Value.GetVar(2, "1").String(), "1") + + gtest.Assert(Option.GetAll(), map[string]string{"o1": "111", "o2": "222"}) + gtest.Assert(Option.Get("o1"), "111") + gtest.Assert(Option.Get("o2"), "222") + gtest.Assert(Option.Get("o3", "1"), "1") + gtest.Assert(Option.GetVar("o2", "1").String(), "222") + gtest.Assert(Option.GetVar("o3", "1").String(), "1") + }) } +func Test_Handle(t *testing.T) { + os.Args = []string{"gf", "gf"} + doInit() + gtest.Case(t, func() { + num := 1 + BindHandle("gf", func() { + num += 1 + }) + RunHandle("gf") + gtest.AssertEQ(num, 2) + AutoRun() + gtest.AssertEQ(num, 3) + }) +}