1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00

merge main

This commit is contained in:
dudaodong
2022-01-19 20:47:47 +08:00
6 changed files with 42 additions and 36 deletions

View File

@@ -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

View File

@@ -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个元素

View File

@@ -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)

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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) {