github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/is_test.go (about) 1 package ztype 2 3 import ( 4 "testing" 5 6 "github.com/sohaha/zlsgo" 7 ) 8 9 func TestIs(t *testing.T) { 10 T := zlsgo.NewTest(t) 11 var i int 12 tIsInt := IsInt(i) 13 T.Equal(true, tIsInt) 14 15 var i8 int8 16 tIsInt8 := IsInt8(i8) 17 T.Equal(true, tIsInt8) 18 19 var i16 int16 20 tIsInt16 := IsInt16(i16) 21 T.Equal(true, tIsInt16) 22 23 var i32 int32 24 tIsInt32 := IsInt32(i32) 25 T.Equal(true, tIsInt32) 26 27 var i64 int64 28 tIsInt64 := IsInt64(i64) 29 T.Equal(true, tIsInt64) 30 31 var ui uint 32 tIsUint := IsUint(ui) 33 T.Equal(true, tIsUint) 34 35 var ui8 uint8 36 tIsUint8 := IsUint8(ui8) 37 T.Equal(true, tIsUint8) 38 39 var ui16 uint16 40 tIsUint16 := IsUint16(ui16) 41 T.Equal(true, tIsUint16) 42 43 var ui32 uint32 44 tIsUint32 := IsUint32(ui32) 45 T.Equal(true, tIsUint32) 46 47 var ui64 uint64 48 tIsUint64 := IsUint64(ui64) 49 T.Equal(true, tIsUint64) 50 51 var f32 float32 52 tIsFloat32 := IsFloat32(f32) 53 T.Equal(true, tIsFloat32) 54 55 var f64 float64 56 tIsFloat64 := IsFloat64(f64) 57 T.Equal(true, tIsFloat64) 58 59 var bo bool 60 tIsBool := IsBool(bo) 61 T.Equal(true, tIsBool) 62 63 var str string 64 tIsString := IsString(str) 65 T.Equal(true, tIsString) 66 67 var by []byte 68 tIsByte := IsByte(by) 69 T.Equal(true, tIsByte) 70 71 type inTest interface { 72 } 73 74 type sutTest struct { 75 test string 76 } 77 78 var in inTest 79 tIsInterface := IsInterface(in) 80 T.Equal(true, tIsInterface) 81 82 sut := sutTest{test: "T"} 83 T.Equal(true, IsStruct(sut)) 84 T.Equal(true, IsStruct(&sut)) 85 T.Equal("ztype.sutTest", GetType(sut)) 86 T.Equal("*ztype.sutTest", GetType(&sut)) 87 88 m := map[string]string{} 89 m["test"] = "testValue" 90 tGetType1 := GetType(m) 91 T.Equal("map[string]string", tGetType1) 92 93 var m2 map[string]interface{} 94 tGetType2 := GetType(m2) 95 T.Equal("map[string]interface {}", tGetType2) 96 97 var n chan int 98 tGetType3 := GetType(n) 99 T.Equal("chan int", tGetType3) 100 } 101 102 func BenchmarkIsStr(b *testing.B) { 103 for i := 0; i < b.N; i++ { 104 _ = isStr("sss").(string) 105 } 106 } 107 108 func BenchmarkZIsStr(b *testing.B) { 109 for i := 0; i < b.N; i++ { 110 _ = IsString(isStr("sss")) 111 } 112 } 113 114 func BenchmarkGetType(b *testing.B) { 115 for i := 0; i < b.N; i++ { 116 _ = GetType(isStr("sss")) 117 } 118 } 119 120 func isStr(s interface{}) interface{} { 121 return s 122 } 123 124 func TestIsEmpty(T *testing.T) { 125 t := zlsgo.NewTest(T) 126 t.EqualTrue(IsEmpty("")) 127 t.EqualTrue(IsEmpty(0)) 128 t.EqualTrue(IsEmpty([]byte(""))) 129 t.EqualTrue(IsEmpty(int(0))) 130 t.EqualTrue(IsEmpty(int8(0))) 131 t.EqualTrue(IsEmpty(int16(0))) 132 t.EqualTrue(IsEmpty(int32(0))) 133 t.EqualTrue(IsEmpty(int64(0))) 134 t.EqualTrue(IsEmpty(uint(0))) 135 t.EqualTrue(IsEmpty(uint8(0))) 136 t.EqualTrue(IsEmpty(uint16(0))) 137 t.EqualTrue(IsEmpty(uint32(0))) 138 t.EqualTrue(IsEmpty(uint64(0))) 139 t.EqualTrue(IsEmpty(float32(0))) 140 t.EqualTrue(IsEmpty(float64(0))) 141 t.EqualTrue(IsEmpty(false)) 142 t.EqualTrue(IsEmpty([]string{})) 143 var s interface{} 144 t.EqualTrue(IsEmpty(s)) 145 }