diff --git a/util/grand/grand.go b/util/grand/grand.go index 34554a2f5..90fd93e69 100644 --- a/util/grand/grand.go +++ b/util/grand/grand.go @@ -62,14 +62,11 @@ func N(min, max int) int { if min >= 0 { return Intn(max-min+1) + min } - if min < 0 { - // As `Intn` dose not support negative number, - // so we should first shift the value to right, - // then call `Intn` to produce the random number, - // and finally shift the result back to left. - return Intn(max+(0-min)+1) - (0 - min) - } - return 0 + // As `Intn` dose not support negative number, + // so we should first shift the value to right, + // then call `Intn` to produce the random number, + // and finally shift the result back to left. + return Intn(max+(0-min)+1) - (0 - min) } // S returns a random string which contains digits and letters, and its length is `n`. diff --git a/util/grand/grand_z_unit_test.go b/util/grand/grand_z_unit_test.go index 1a4a095ad..d3ea2d713 100644 --- a/util/grand/grand_z_unit_test.go +++ b/util/grand/grand_z_unit_test.go @@ -9,6 +9,7 @@ package grand_test import ( + "strings" "testing" "time" @@ -119,6 +120,9 @@ func Test_S(t *testing.T) { t.Assert(len(grand.S(5, true)), 5) } }) + gtest.C(t, func(t *gtest.T) { + t.Assert(len(grand.S(0)), 0) + }) } func Test_B(t *testing.T) { @@ -129,6 +133,10 @@ func Test_B(t *testing.T) { t.AssertNE(b, make([]byte, 5)) } }) + gtest.C(t, func(t *gtest.T) { + b := grand.B(0) + t.AssertNil(b) + }) } func Test_Str(t *testing.T) { @@ -156,6 +164,21 @@ func Test_RandStr(t *testing.T) { t.Assert(gstr.Contains(s, "w"), false) } }) + gtest.C(t, func(t *gtest.T) { + t.Assert(grand.Str(str, 0), "") + }) + gtest.C(t, func(t *gtest.T) { + list := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} + str := "" + for _, s := range list { + tmp := "" + for i := 0; i < 15; i++ { + tmp += tmp + s + } + str += tmp + } + t.Assert(len(grand.Str(str, 300)), 300) + }) } func Test_Digits(t *testing.T) { @@ -172,6 +195,9 @@ func Test_RandDigits(t *testing.T) { t.Assert(len(grand.Digits(5)), 5) } }) + gtest.C(t, func(t *gtest.T) { + t.Assert(len(grand.Digits(0)), 0) + }) } func Test_Letters(t *testing.T) { @@ -188,6 +214,9 @@ func Test_RandLetters(t *testing.T) { t.Assert(len(grand.Letters(5)), 5) } }) + gtest.C(t, func(t *gtest.T) { + t.Assert(len(grand.Letters(0)), 0) + }) } func Test_Perm(t *testing.T) { @@ -197,3 +226,18 @@ func Test_Perm(t *testing.T) { } }) } + +func Test_Symbols(t *testing.T) { + symbols := "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" + gtest.C(t, func(t *gtest.T) { + for i := 0; i < 100; i++ { + syms := []byte(grand.Symbols(5)) + for _, sym := range syms { + t.AssertNE(strings.Index(symbols, string(sym)), -1) + } + } + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(grand.Symbols(0), "") + }) +} diff --git a/util/gutil/gutil_z_unit_dump_test.go b/util/gutil/gutil_z_unit_dump_test.go index 336e74fb4..4ec32d37c 100755 --- a/util/gutil/gutil_z_unit_dump_test.go +++ b/util/gutil/gutil_z_unit_dump_test.go @@ -8,6 +8,7 @@ package gutil_test import ( "bytes" + "github.com/gogf/gf/v2/container/gtype" "testing" "github.com/gogf/gf/v2/frame/g" @@ -70,6 +71,11 @@ func Test_Dump(t *testing.T) { 100: 100, }) gutil.Dump(req) + gutil.Dump(true, false) + gutil.Dump(make(chan int)) + gutil.Dump(func() {}) + gutil.Dump(nil) + gutil.Dump(gtype.NewInt(1)) }) } diff --git a/util/gutil/gutil_z_unit_list_test.go b/util/gutil/gutil_z_unit_list_test.go index 7b731cd14..42061adcf 100755 --- a/util/gutil/gutil_z_unit_list_test.go +++ b/util/gutil/gutil_z_unit_list_test.go @@ -22,6 +22,7 @@ func Test_ListItemValues_Map(t *testing.T) { g.Map{"id": 3, "score": 99}, } t.Assert(gutil.ListItemValues(listMap, "id"), g.Slice{1, 2, 3}) + t.Assert(gutil.ListItemValues(&listMap, "id"), g.Slice{1, 2, 3}) t.Assert(gutil.ListItemValues(listMap, "score"), g.Slice{100, 99, 99}) }) gtest.C(t, func(t *gtest.T) { @@ -33,6 +34,10 @@ func Test_ListItemValues_Map(t *testing.T) { t.Assert(gutil.ListItemValues(listMap, "id"), g.Slice{1, 2, 3}) t.Assert(gutil.ListItemValues(listMap, "score"), g.Slice{100, nil, 0}) }) + gtest.C(t, func(t *gtest.T) { + listMap := g.List{} + t.Assert(len(gutil.ListItemValues(listMap, "id")), 0) + }) } func Test_ListItemValues_Map_SubKey(t *testing.T) { diff --git a/util/gutil/gutil_z_unit_map_test.go b/util/gutil/gutil_z_unit_map_test.go index b51939e43..29ad92693 100755 --- a/util/gutil/gutil_z_unit_map_test.go +++ b/util/gutil/gutil_z_unit_map_test.go @@ -37,6 +37,20 @@ func Test_MapContains(t *testing.T) { t.Assert(gutil.MapContains(m1, "k1"), true) t.Assert(gutil.MapContains(m1, "K1"), false) t.Assert(gutil.MapContains(m1, "k2"), false) + m2 := g.Map{} + t.Assert(gutil.MapContains(m2, "k1"), false) + }) +} + +func Test_MapDelete(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + m1 := g.Map{ + "k1": "v1", + } + gutil.MapDelete(m1, "k1") + gutil.MapDelete(m1, "K1") + m2 := g.Map{} + gutil.MapDelete(m2, "k1") }) } @@ -57,6 +71,7 @@ func Test_MapMerge(t *testing.T) { t.Assert(m1["k3"], "v3") t.Assert(m2["k1"], nil) t.Assert(m3["k1"], nil) + gutil.MapMerge(nil) }) } @@ -132,6 +147,9 @@ func Test_MapOmitEmpty(t *testing.T) { t.Assert(len(m), 2) t.AssertNE(m["k1"], nil) t.AssertNE(m["k2"], nil) + m1 := g.Map{} + gutil.MapOmitEmpty(m1) + t.Assert(len(m1), 0) }) } @@ -147,6 +165,12 @@ func Test_MapToSlice(t *testing.T) { t.AssertIN(s[1], g.Slice{"k1", "k2", "v1", "v2"}) t.AssertIN(s[2], g.Slice{"k1", "k2", "v1", "v2"}) t.AssertIN(s[3], g.Slice{"k1", "k2", "v1", "v2"}) + s1 := gutil.MapToSlice(&m) + t.Assert(len(s1), 4) + t.AssertIN(s1[0], g.Slice{"k1", "k2", "v1", "v2"}) + t.AssertIN(s1[1], g.Slice{"k1", "k2", "v1", "v2"}) + t.AssertIN(s1[2], g.Slice{"k1", "k2", "v1", "v2"}) + t.AssertIN(s1[3], g.Slice{"k1", "k2", "v1", "v2"}) }) gtest.C(t, func(t *gtest.T) { m := g.MapStrStr{ diff --git a/util/gutil/gutil_z_unit_slice_test.go b/util/gutil/gutil_z_unit_slice_test.go index 08c37d480..4d27c46ee 100755 --- a/util/gutil/gutil_z_unit_slice_test.go +++ b/util/gutil/gutil_z_unit_slice_test.go @@ -14,6 +14,28 @@ import ( "github.com/gogf/gf/v2/util/gutil" ) +func Test_SliceCopy(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := g.Slice{ + "K1", "v1", "K2", "v2", + } + s1 := gutil.SliceCopy(s) + t.Assert(s, s1) + }) +} + +func Test_SliceDelete(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := g.Slice{ + "K1", "v1", "K2", "v2", + } + t.Assert(gutil.SliceDelete(s, 0), g.Slice{ + "v1", "K2", "v2", + }) + t.Assert(gutil.SliceDelete(s, 5), s) + }) +} + func Test_SliceToMap(t *testing.T) { gtest.C(t, func(t *gtest.T) { s := g.Slice{ @@ -25,6 +47,13 @@ func Test_SliceToMap(t *testing.T) { "K1": "v1", "K2": "v2", }) + + m1 := gutil.SliceToMap(&s) + t.Assert(len(m1), 2) + t.Assert(m1, g.Map{ + "K1": "v1", + "K2": "v2", + }) }) gtest.C(t, func(t *gtest.T) { s := g.Slice{ @@ -34,6 +63,11 @@ func Test_SliceToMap(t *testing.T) { t.Assert(len(m), 0) t.Assert(m, nil) }) + gtest.C(t, func(t *gtest.T) { + m := gutil.SliceToMap(1) + t.Assert(len(m), 0) + t.Assert(m, nil) + }) } func Test_SliceToMapWithColumnAsKey(t *testing.T) { @@ -46,6 +80,12 @@ func Test_SliceToMapWithColumnAsKey(t *testing.T) { "v1": m1, "v2": m2, }) + + n := gutil.SliceToMapWithColumnAsKey(&s, "K1") + t.Assert(n, g.MapAnyAny{ + "v1": m1, + "v2": m2, + }) }) gtest.C(t, func(t *gtest.T) { m := gutil.SliceToMapWithColumnAsKey(s, "K2") diff --git a/util/gutil/gutil_z_unit_test.go b/util/gutil/gutil_z_unit_test.go index c47c63ffd..d36983bdf 100755 --- a/util/gutil/gutil_z_unit_test.go +++ b/util/gutil/gutil_z_unit_test.go @@ -8,6 +8,8 @@ package gutil_test import ( "context" + "github.com/gogf/gf/v2/errors/gerror" + "reflect" "testing" "github.com/gogf/gf/v2/frame/g" @@ -26,6 +28,12 @@ func Test_Try(t *testing.T) { panic(s) }), s) }) + gtest.C(t, func(t *gtest.T) { + s := `gutil Try test` + t.Assert(gutil.Try(ctx, func(ctx context.Context) { + panic(gerror.New(s)) + }), s) + }) } func Test_TryCatch(t *testing.T) { @@ -43,6 +51,15 @@ func Test_TryCatch(t *testing.T) { t.Assert(err, "gutil TryCatch test") }) }) + + gtest.C(t, func(t *gtest.T) { + gutil.TryCatch(ctx, func(ctx context.Context) { + panic(gerror.New("gutil TryCatch test")) + + }, func(ctx context.Context, err error) { + t.Assert(err, "gutil TryCatch test") + }) + }) } func Test_IsEmpty(t *testing.T) { @@ -62,6 +79,12 @@ func Test_Throw(t *testing.T) { } func Test_Keys(t *testing.T) { + // not support int + gtest.C(t, func(t *gtest.T) { + var val int = 1 + keys := gutil.Keys(reflect.ValueOf(val)) + t.AssertEQ(len(keys), 0) + }) // map gtest.C(t, func(t *gtest.T) { keys := gutil.Keys(map[int]int{ @@ -70,6 +93,13 @@ func Test_Keys(t *testing.T) { }) t.AssertIN("1", keys) t.AssertIN("2", keys) + + strKeys := gutil.Keys(map[string]interface{}{ + "key1": 1, + "key2": 2, + }) + t.AssertIN("key1", strKeys) + t.AssertIN("key2", strKeys) }) // *map gtest.C(t, func(t *gtest.T) { @@ -112,15 +142,38 @@ func Test_Keys(t *testing.T) { } func Test_Values(t *testing.T) { + // not support int gtest.C(t, func(t *gtest.T) { - values := gutil.Keys(map[int]int{ + var val int = 1 + keys := gutil.Values(reflect.ValueOf(val)) + t.AssertEQ(len(keys), 0) + }) + // map + gtest.C(t, func(t *gtest.T) { + values := gutil.Values(map[int]int{ 1: 10, 2: 20, }) - t.AssertIN("1", values) - t.AssertIN("2", values) - }) + t.AssertIN(10, values) + t.AssertIN(20, values) + values = gutil.Values(map[string]interface{}{ + "key1": 10, + "key2": 20, + }) + t.AssertIN(10, values) + t.AssertIN(20, values) + }) + // *map + gtest.C(t, func(t *gtest.T) { + keys := gutil.Values(&map[int]int{ + 1: 10, + 2: 20, + }) + t.AssertIN(10, keys) + t.AssertIN(20, keys) + }) + // struct gtest.C(t, func(t *gtest.T) { type T struct { A string @@ -133,3 +186,13 @@ func Test_Values(t *testing.T) { t.Assert(keys, g.Slice{"1", 2}) }) } + +func TestListToMapByKey(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + listMap := []map[string]interface{}{ + {"key1": 1, "key2": 2}, + {"key3": 3, "key4": 4}, + } + t.Assert(gutil.ListToMapByKey(listMap, "key1"), "{\"1\":{\"key1\":1,\"key2\":2}}") + }) +}