diff --git a/README.md b/README.md index 292e3e0..2ad243f 100644 --- a/README.md +++ b/README.md @@ -434,7 +434,6 @@ func main() { func Contain[T comparable](slice []T, value T) bool //check if the value is in the slice or not func ContainSubSlice[T comparable](slice, subslice []T) bool //check if the slice contain subslice or not func Chunk[T any](slice []T, size int) [][]T //creates an slice of elements split into groups the length of size. -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //convert originalSlice to newSliceType func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice func DeleteByIndex[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1 func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0 diff --git a/README_zh-CN.md b/README_zh-CN.md index d8eb16b..f703d34 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -434,7 +434,6 @@ func main() { func Contain[T comparable](slice []T, value T) bool //判断slice是否包含value func ContainSubSlice[T comparable](slice, subslice []T) bool //判断slice是否包含subslice func Chunk[T any](slice []T, size int) [][]T //均分slice -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //将originalSlice转换为 newSliceType func Difference[T comparable](slice1, slice2 []T) []T //返回切片,其元素在slice1中,不在slice2中 func DeleteByIndex[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end) func Drop[T any](slice []T, n int) []T //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 diff --git a/internal/assert_test.go b/internal/assert_test.go index 185dd79..ae0bcfa 100644 --- a/internal/assert_test.go +++ b/internal/assert_test.go @@ -8,11 +8,31 @@ func TestAssert(t *testing.T) { assert := NewAssert(t, "TestAssert") assert.Equal(0, 0) assert.NotEqual(1, 0) + + assert.NotEqual("1", 1) + var uInt1 uint + var uInt2 uint + var uInt8 uint8 + var uInt16 uint16 + var uInt32 uint32 + var uInt64 uint64 + assert.NotEqual(uInt1, uInt8) + assert.NotEqual(uInt8, uInt16) + assert.NotEqual(uInt16, uInt32) + assert.NotEqual(uInt32, uInt64) + + assert.Equal(uInt1, uInt2) + + uInt1 = 1 + uInt2 = 2 + assert.Less(uInt1, uInt2) + assert.Greater(1, 0) assert.GreaterOrEqual(1, 1) assert.Less(0, 1) assert.LessOrEqual(0, 0) + assert.Equal(0.1, 0.1) assert.Greater(1.1, 0.1) assert.Less(0.1, 1.1) diff --git a/slice/slice.go b/slice/slice.go index 34896a2..4cb12af 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -10,7 +10,6 @@ import ( "math/rand" "reflect" "sort" - "unsafe" ) // Contain check if the value is in the iterable type or not @@ -345,26 +344,6 @@ func IntSlice(slice interface{}) []int { return out } -// ConvertSlice convert original slice to new data type element of slice. -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} { - sv := sliceValue(originalSlice) - if newSliceType.Kind() != reflect.Slice { - panic(fmt.Sprintf("Invalid newSliceType(non-slice type of type %T)", newSliceType)) - } - - newSlice := reflect.New(newSliceType) - - hdr := (*reflect.SliceHeader)(unsafe.Pointer(newSlice.Pointer())) - - var newElemSize = int(sv.Type().Elem().Size()) / int(newSliceType.Elem().Size()) - - hdr.Cap = sv.Cap() * newElemSize - hdr.Len = sv.Len() * newElemSize - hdr.Data = sv.Pointer() - - return newSlice.Elem().Interface() -} - // DeleteByIndex delete the element of slice from start index to end index - 1. func DeleteByIndex[T any](slice []T, start int, end ...int) []T { size := len(slice) diff --git a/slice/slice_test.go b/slice/slice_test.go index 0f3bd95..66aab40 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -45,19 +45,6 @@ func TestChunk(t *testing.T) { assert.Equal(r5, Chunk(arr, 5)) } -func TestConvertSlice(t *testing.T) { - //t1 := []string{"1","2"} - //aInt, _ := strconv.ParseInt("1", 10, 64) - //bInt, _ := strconv.ParseInt("2", 10, 64) - //expected :=[]int64{aInt, bInt} - // - //a := ConvertSlice(t1, reflect.TypeOf(expected)) - //if !reflect.DeepEqual(a, expected) { - // utils.LogFailedTestInfo(t, "ConvertSlice", t1, expected, a) - // t.FailNow() - //} -} - func TestEvery(t *testing.T) { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { @@ -367,6 +354,7 @@ func TestIntersection(t *testing.T) { for i := 0; i < len(res); i++ { assert.Equal(expected[i], res[i]) } + // assert.IsNil(Intersection()) } func TestReverse(t *testing.T) { diff --git a/system/os_test.go b/system/os_test.go index 6e04008..1d5ac1f 100644 --- a/system/os_test.go +++ b/system/os_test.go @@ -1,11 +1,24 @@ package system import ( + "strings" "testing" "github.com/duke-git/lancet/internal" ) +func TestOsDetection(t *testing.T) { + assert := internal.NewAssert(t, "TestOsJudgment") + + osType, _, _ := ExecCommand("echo $OSTYPE") + if strings.Index(osType, "linux") != -1 { + assert.Equal(true, IsLinux()) + } + if strings.Index(osType, "darwin") != -1 { + assert.Equal(true, IsMac()) + } +} + func TestOsEnvOperation(t *testing.T) { assert := internal.NewAssert(t, "TestOsEnvOperation") @@ -18,6 +31,14 @@ func TestOsEnvOperation(t *testing.T) { assert.Equal(true, CompareOsEnv("foo", "foo_value")) assert.Equal(false, CompareOsEnv("foo", "abc")) + assert.Equal(false, CompareOsEnv("abc", "abc")) + assert.Equal(false, CompareOsEnv("abc", "abc")) + + err := RemoveOsEnv("foo") + if err != nil { + t.Fail() + } + assert.Equal(false, CompareOsEnv("foo", "foo_value")) } func TestExecCommand(t *testing.T) {