mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-09-27 03:15:55 +08:00
Feature/cover unit test (#81)
This commit is contained in:
1
Makefile
1
Makefile
@@ -16,6 +16,7 @@ bench: ## 运行基准测试
|
||||
|
||||
.PHONY:doc
|
||||
doc: ## 启动文档服务器
|
||||
# go install golang.org/x/tools/cmd/godoc@latest
|
||||
godoc -http=:6060 -play -index
|
||||
|
||||
.PHONY:cover
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
func TestToBool(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
var tests = []struct {
|
||||
input any
|
||||
want bool
|
||||
@@ -58,3 +57,61 @@ func TestToBool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToBool(t *testing.T) {
|
||||
iPtr := 90
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
want bool
|
||||
}{
|
||||
{"bool true", true, true},
|
||||
{"bool false", false, false},
|
||||
{"int -1", int(-1), true},
|
||||
{"int 1", int(1), true},
|
||||
{"int 0", int(0), false},
|
||||
{"int8 1", int8(1), true},
|
||||
{"int8 0", int8(0), false},
|
||||
{"int16 1", int16(1), true},
|
||||
{"int16 0", int16(0), false},
|
||||
{"int32 1", int32(1), true},
|
||||
{"int32 0", int32(0), false},
|
||||
{"int64 1", int64(1), true},
|
||||
{"int64 100", int64(100), true},
|
||||
{"int64 0", int64(0), false},
|
||||
{"uint 1", uint(1), true},
|
||||
{"uint 0", uint(0), false},
|
||||
{"uint8 1", uint8(1), true},
|
||||
{"uint8 0", uint8(0), false},
|
||||
{"uint16 1", uint16(1), true},
|
||||
{"uint16 0", uint16(0), false},
|
||||
{"uint32 1", uint32(1), true},
|
||||
{"uint32 0", uint32(0), false},
|
||||
{"uint64 1", uint64(1), true},
|
||||
{"uint64 0", uint64(0), false},
|
||||
{"float32 1.0", float32(1.0), true},
|
||||
{"float32 0.0", float32(0.0), false},
|
||||
{"float64 1.0", float64(1.0), true},
|
||||
{"float64 0.0", float64(0.0), false},
|
||||
{"string abc", "abc", true},
|
||||
{"string true", "true", true},
|
||||
{"string false", "false", false},
|
||||
{"empty string", "", false},
|
||||
{"nil value", nil, false},
|
||||
{"complex64 1+1i", complex64(1 + 1i), true},
|
||||
{"complex64 0+0i", complex64(0 + 0i), false},
|
||||
{"complex128 1+1i", complex128(1 + 1i), true},
|
||||
{"complex128 0+0i", complex128(0 + 0i), false},
|
||||
{"nil pointer", (*int)(nil), false},
|
||||
{"non-nil pointer", &iPtr, true},
|
||||
{"empty slice", []int{}, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := AnyToBool(tc.input); got != tc.want {
|
||||
t.Errorf("AnyToBool(%v) = %v; want %v", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,52 +1,52 @@
|
||||
package anyUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func TestAnyToFloat64Test(t *testing.T) {
|
||||
// fmt.Println(AnyToFloat64([]int{}))
|
||||
//}
|
||||
|
||||
func TestAnyToFloat32(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
want float32
|
||||
err error
|
||||
}{
|
||||
{nil, 0, nil},
|
||||
{float32(3.14), 3.14, nil},
|
||||
{float64(6.28), 6.28, nil},
|
||||
{int(42), 42, nil},
|
||||
{int8(8), 8, nil},
|
||||
{int16(16), 16, nil},
|
||||
{int32(32), 32, nil},
|
||||
{int64(64), 64, nil},
|
||||
{uint(24), 24, nil},
|
||||
{uint8(8), 8, nil},
|
||||
{uint16(16), 16, nil},
|
||||
{uint32(32), 32, nil},
|
||||
{uint64(64), 64, nil},
|
||||
{"3.14", 3.14, nil},
|
||||
{math.MaxFloat64, 0, ErrValOut},
|
||||
{make(chan int), 0, ErrType},
|
||||
{"nil", nil, 0, nil},
|
||||
{"float32", float32(3.14), 3.14, nil},
|
||||
{"float64", float64(6.28), 6.28, nil},
|
||||
{"int", int(42), 42, nil},
|
||||
{"int8", int8(8), 8, nil},
|
||||
{"int16", int16(16), 16, nil},
|
||||
{"int32", int32(32), 32, nil},
|
||||
{"int64", int64(64), 64, nil},
|
||||
{"uint", uint(24), 24, nil},
|
||||
{"uint8", uint8(8), 8, nil},
|
||||
{"uint16", uint16(16), 16, nil},
|
||||
{"uint32", uint32(32), 32, nil},
|
||||
{"uint64", uint64(64), 64, nil},
|
||||
{"string", "3.14", 3.14, nil},
|
||||
{"math.MaxFloat64", math.MaxFloat64, 0, ErrValOut},
|
||||
{"channel", make(chan int), 0, ErrType},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got, err := AnyToFloat32(test.input)
|
||||
if got != test.want {
|
||||
t.Errorf("AnyToFloat32(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
if err != test.err {
|
||||
if err != nil && test.err != nil {
|
||||
if err.Error() != test.err.Error() {
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := AnyToFloat32(test.input)
|
||||
if got != test.want {
|
||||
t.Errorf("AnyToFloat32(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
if !errors.Is(err, test.err) {
|
||||
if err != nil && test.err != nil {
|
||||
if err.Error() != test.err.Error() {
|
||||
t.Errorf("AnyToFloat32(%v) error = %v; want %v", test.input, err, test.err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("AnyToFloat32(%v) error = %v; want %v", test.input, err, test.err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("AnyToFloat32(%v) error = %v; want %v", test.input, err, test.err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,47 +54,49 @@ func TestAnyToFloat64(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input any
|
||||
want float64
|
||||
err error
|
||||
}{
|
||||
{nil, 0, nil},
|
||||
//{float32(3.14), 3.14, nil},
|
||||
{float64(6.28), 6.28, nil},
|
||||
{int(42), 42, nil},
|
||||
{int8(8), 8, nil},
|
||||
{int16(16), 16, nil},
|
||||
{int32(32), 32, nil},
|
||||
{int64(64), 64, nil},
|
||||
{uint(24), 24, nil},
|
||||
{uint8(8), 8, nil},
|
||||
{uint16(16), 16, nil},
|
||||
{uint32(32), 32, nil},
|
||||
{uint64(64), 64, nil},
|
||||
{"3.14", 3.14, nil},
|
||||
{true, 1, nil},
|
||||
{false, 0, nil},
|
||||
{complex64(1 + 2i), 1, nil},
|
||||
{complex128(1 + 2i), 1, nil},
|
||||
{&iPtr, 90, nil},
|
||||
{(*int)(nil), 0, nil},
|
||||
{"abc", 0, ErrSyntax},
|
||||
{[]int{}, 0, ErrType},
|
||||
{"nil", nil, 0, nil},
|
||||
{"float64", float64(6.28), 6.28, nil},
|
||||
{"int", int(42), 42, nil},
|
||||
{"int8", int8(8), 8, nil},
|
||||
{"int16", int16(16), 16, nil},
|
||||
{"int32", int32(32), 32, nil},
|
||||
{"int64", int64(64), 64, nil},
|
||||
{"uint", uint(24), 24, nil},
|
||||
{"uint8", uint8(8), 8, nil},
|
||||
{"uint16", uint16(16), 16, nil},
|
||||
{"uint32", uint32(32), 32, nil},
|
||||
{"uint64", uint64(64), 64, nil},
|
||||
{"string", "3.14", 3.14, nil},
|
||||
{"true", true, 1, nil},
|
||||
{"false", false, 0, nil},
|
||||
{"complex64", complex64(1 + 2i), 1, nil},
|
||||
{"complex128", complex128(1 + 2i), 1, nil},
|
||||
{"pointer", &iPtr, 90, nil},
|
||||
{"nil pointer", (*int)(nil), 0, nil},
|
||||
{"invalid string", "abc", 0, ErrSyntax},
|
||||
{"slice", []int{}, 0, ErrType},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got, err := AnyToFloat64(test.input)
|
||||
if got != test.want {
|
||||
t.Errorf("AnyToFloat64(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
if err != test.err {
|
||||
if err != nil && test.err != nil {
|
||||
if err.Error() != test.err.Error() {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := AnyToFloat64(test.input)
|
||||
if got != test.want {
|
||||
t.Errorf("AnyToFloat64(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
if err != test.err {
|
||||
if err != nil && test.err != nil {
|
||||
if err.Error() != test.err.Error() {
|
||||
t.Errorf("AnyToFloat64(%v) error = %v; want %v", test.input, err, test.err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("AnyToFloat64(%v) error = %v; want %v", test.input, err, test.err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("AnyToFloat64(%v) error = %v; want %v", test.input, err, test.err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -14,9 +14,9 @@ func AnyToInt(i any) (int, error) {
|
||||
}
|
||||
|
||||
// int 兼容32位和64位系统
|
||||
if int64(int(v)) != v {
|
||||
return 0, ErrValOut
|
||||
}
|
||||
//if int64(int(v)) != v {
|
||||
// return 0, ErrValOut
|
||||
//}
|
||||
|
||||
return int(v), nil
|
||||
}
|
||||
|
@@ -2,255 +2,226 @@ package anyUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func TestAnyToIntDemo(t *testing.T) {
|
||||
// fmt.Println(AnyToInt(math.MinInt32))
|
||||
//}
|
||||
|
||||
func TestAnyToInt(t *testing.T) {
|
||||
type testCase struct {
|
||||
iPtr := 90
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected int
|
||||
err error
|
||||
}{
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
{name: "iPtr", input: &iPtr, expected: 90, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 布尔
|
||||
{name: "true", input: true, expected: 1, err: nil},
|
||||
{name: "false", input: false, expected: 0, err: nil},
|
||||
|
||||
// 空指针
|
||||
{name: "nil point", input: (*int)(nil), expected: 0, err: nil},
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
{input: 123, expected: 123, err: nil},
|
||||
{input: int8(123), expected: 123, err: nil},
|
||||
{input: int16(123), expected: 123, err: nil},
|
||||
{input: int32(123), expected: 123, err: nil},
|
||||
{input: int64(123), expected: 123, err: nil},
|
||||
{input: uint(123), expected: 123, err: nil},
|
||||
{input: uint8(123), expected: 123, err: nil},
|
||||
{input: uint16(123), expected: 123, err: nil},
|
||||
{input: uint32(123), expected: 123, err: nil},
|
||||
{input: uint64(123), expected: 123, err: nil},
|
||||
{input: float32(123.45), expected: 123, err: nil},
|
||||
{input: float64(123.45), expected: 123, err: nil},
|
||||
{input: "123", expected: 123, err: nil},
|
||||
{input: "abc", expected: 0, err: ErrSyntax},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
actual, err := AnyToInt(tc.input)
|
||||
|
||||
if err != nil && tc.err == nil {
|
||||
t.Errorf("AnyToInt(%v) returned unexpected error: %v", tc.input, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err == nil && tc.err != nil {
|
||||
t.Errorf("AnyToInt(%v) expected error but got nil", tc.input)
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil && tc.err != nil && err.Error() != tc.err.Error() {
|
||||
t.Errorf("AnyToInt(%v) returned wrong error. Expected %v, but got %v", tc.input, tc.err, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if actual != tc.expected {
|
||||
t.Errorf("AnyToInt(%v) returned %v, expected %v", tc.input, actual, tc.expected)
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res, err := AnyToInt(test.input)
|
||||
if res != test.expected || !errors.Is(err, test.err) {
|
||||
t.Errorf("AnyToInt(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToInt8(t *testing.T) {
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected int8
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "positive integer",
|
||||
input: 42,
|
||||
expected: 42,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "negative integer",
|
||||
input: -42,
|
||||
expected: -42,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "positive integer out of range",
|
||||
input: 128,
|
||||
expected: 0,
|
||||
err: ErrValOut,
|
||||
},
|
||||
{
|
||||
name: "negative integer out of range",
|
||||
input: -129,
|
||||
expected: 0,
|
||||
err: ErrValOut,
|
||||
},
|
||||
{
|
||||
name: "float",
|
||||
input: 3.14,
|
||||
expected: 3,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
input: "42",
|
||||
expected: 42,
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "string out of range",
|
||||
input: "128",
|
||||
expected: 0,
|
||||
err: ErrValOut,
|
||||
},
|
||||
{
|
||||
name: "invalid string",
|
||||
input: "invalid",
|
||||
expected: 0,
|
||||
err: ErrSyntax,
|
||||
},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 溢出
|
||||
{name: "MinInt8", input: math.MinInt8 - 1, expected: 0, err: ErrValOut},
|
||||
{name: "MaxInt8", input: math.MaxInt8 + 1, expected: 0, err: ErrValOut},
|
||||
}
|
||||
|
||||
// Run test cases
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actual, err := AnyToInt8(tc.input)
|
||||
|
||||
// Check error
|
||||
if err != nil {
|
||||
if !errors.Is(err, tc.err) {
|
||||
t.Errorf("Expected error: %v, but got: %v", tc.err, err)
|
||||
}
|
||||
//if err.Error() != tc.err.Error() {
|
||||
// t.Errorf("Expected error: %v, but got: %v", tc.err, err)
|
||||
//}
|
||||
}
|
||||
|
||||
// Check result
|
||||
if actual != tc.expected {
|
||||
t.Errorf("Expected result: %v, but got: %v", tc.expected, actual)
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res, err := AnyToInt8(test.input)
|
||||
if res != test.expected || !errors.Is(err, test.err) {
|
||||
t.Errorf("AnyToInt8(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToInt16(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
want int16
|
||||
err error
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected int16
|
||||
err error
|
||||
}{
|
||||
{int(12345), 12345, nil},
|
||||
{int8(123), 123, nil},
|
||||
{int16(12345), 12345, nil},
|
||||
{int32(12345), 12345, nil},
|
||||
{int64(12345), 12345, nil},
|
||||
{uint(12345), 12345, nil},
|
||||
{uint8(123), 123, nil},
|
||||
{uint16(12345), 12345, nil},
|
||||
{uint32(12345), 12345, nil},
|
||||
{uint64(12345), 12345, nil},
|
||||
{float32(123.45), 123, nil},
|
||||
{float64(123.45), 123, nil},
|
||||
{"12345", 12345, nil},
|
||||
{math.MinInt16 - 1, 0, ErrValOut},
|
||||
{"not a number", 0, ErrSyntax},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 溢出
|
||||
{name: "MinInt16", input: math.MinInt16 - 1, expected: 0, err: ErrValOut},
|
||||
{name: "MaxInt16", input: math.MaxInt16 + 1, expected: 0, err: ErrValOut},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got, err := AnyToInt16(tt.input)
|
||||
|
||||
if got != tt.want {
|
||||
t.Errorf("AnyToInt16(%v) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
|
||||
if !errors.Is(err, tt.err) {
|
||||
t.Errorf("AnyToInt16(%v) error = %v, want error %v", tt.input, err, tt.err)
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res, err := AnyToInt16(test.input)
|
||||
if res != test.expected || !errors.Is(err, test.err) {
|
||||
t.Errorf("AnyToInt16(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToInt32(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input any
|
||||
expectedValue int32
|
||||
expectedError error
|
||||
name string
|
||||
input any
|
||||
expected int32
|
||||
err error
|
||||
}{
|
||||
{int(123), 123, nil},
|
||||
{int64(2147483647), 2147483647, nil},
|
||||
{int64(2147483648), 0, ErrValOut},
|
||||
{int64(-2147483648), -2147483648, nil},
|
||||
{int64(-2147483649), 0, ErrValOut},
|
||||
{float64(123.45), 123, nil},
|
||||
{"123", 123, nil},
|
||||
{"-2147483649", 0, ErrValOut},
|
||||
{struct{}{}, 0, ErrType},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 溢出
|
||||
{name: "MinInt32", input: math.MinInt32 - 1, expected: 0, err: ErrValOut},
|
||||
{name: "MaxInt32", input: math.MaxInt32 + 1, expected: 0, err: ErrValOut},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
actualValue, actualError := AnyToInt32(testCase.input)
|
||||
if actualValue != testCase.expectedValue {
|
||||
t.Errorf("Unexpected value. Input: %v, Expected: %d, Actual: %d", testCase.input, testCase.expectedValue, actualValue)
|
||||
}
|
||||
if (actualError == nil && testCase.expectedError != nil) || (actualError != nil && testCase.expectedError == nil) || (actualError != nil && actualError.Error() != testCase.expectedError.Error()) {
|
||||
t.Errorf("Unexpected error. Input: %v, Expected: %v, Actual: %v", testCase.input, testCase.expectedError, actualError)
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res, err := AnyToInt32(test.input)
|
||||
if res != test.expected || !errors.Is(err, test.err) {
|
||||
t.Errorf("AnyToInt32(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToInt64(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected int64
|
||||
err bool
|
||||
err error
|
||||
}{
|
||||
// test cases for int values
|
||||
{int(42), int64(42), false},
|
||||
{int8(42), int64(42), false},
|
||||
{int16(42), int64(42), false},
|
||||
{int32(42), int64(42), false},
|
||||
{int64(42), int64(42), false},
|
||||
// test cases for uint values
|
||||
{uint(42), int64(42), false},
|
||||
{uint8(42), int64(42), false},
|
||||
{uint16(42), int64(42), false},
|
||||
{uint32(42), int64(42), false},
|
||||
{uint64(42), int64(42), false},
|
||||
// test cases for float values
|
||||
{float32(42.42), int64(42), false},
|
||||
{float64(42.42), int64(42), false},
|
||||
// test cases for string values
|
||||
{"42", int64(42), false},
|
||||
{"-42", int64(-42), false},
|
||||
{"42.42", int64(0), true}, // invalid syntax
|
||||
// unsupported type
|
||||
{struct{}{}, int64(0), true},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 布尔
|
||||
{name: "true", input: true, expected: 1, err: nil},
|
||||
{name: "false", input: false, expected: 0, err: nil},
|
||||
|
||||
// 空指针
|
||||
{name: "nil point", input: (*int)(nil), expected: 0, err: nil},
|
||||
|
||||
// complex
|
||||
{complex64(1 + 2i), 1, false},
|
||||
{complex128(1 + 2i), 1, false},
|
||||
// point
|
||||
{(*int)(nil), 0, false},
|
||||
{nil, 0, false},
|
||||
{&iPtr, 90, false},
|
||||
// bool
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{name: "complex128", input: complex(3.14, 1.59), expected: 3, err: nil},
|
||||
{name: "complex64", input: complex(float32(2.71), float32(1.41)), expected: 2, err: nil},
|
||||
|
||||
// 其他
|
||||
{name: "struct", input: struct{ Name string }{Name: "test"}, expected: 0, err: ErrType},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
actual, err := AnyToInt64(testCase.input)
|
||||
if err != nil && !testCase.err {
|
||||
t.Errorf("unexpected error: %s", err)
|
||||
}
|
||||
if actual != testCase.expected {
|
||||
t.Errorf("expected %d but got %d", testCase.expected, actual)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := AnyToInt64(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -5,35 +5,38 @@ import "testing"
|
||||
func TestAnyToStr(t *testing.T) {
|
||||
iPtr := 90
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected string
|
||||
}{
|
||||
{"hello", "hello"},
|
||||
{123, "123"},
|
||||
{int8(8), "8"},
|
||||
{int16(16), "16"},
|
||||
{int32(32), "32"},
|
||||
{int64(64), "64"},
|
||||
{uint(10), "10"},
|
||||
{uint8(80), "80"},
|
||||
{uint16(160), "160"},
|
||||
{uint32(320), "320"},
|
||||
{uint64(640), "640"},
|
||||
{float32(3.14159), "3.14159"},
|
||||
{float64(2.71828), "2.71828"},
|
||||
{complex64(1 + 2i), "(1+2i)"},
|
||||
{complex128(3 + 4i), "(3+4i)"},
|
||||
{(*int)(nil), ""},
|
||||
{&iPtr, "90"},
|
||||
{true, "true"},
|
||||
{nil, ""},
|
||||
{make(chan int), ""},
|
||||
{"String", "hello", "hello"},
|
||||
{"Integer", 123, "123"},
|
||||
{"Int8", int8(8), "8"},
|
||||
{"Int16", int16(16), "16"},
|
||||
{"Int32", int32(32), "32"},
|
||||
{"Int64", int64(64), "64"},
|
||||
{"Uint", uint(10), "10"},
|
||||
{"Uint8", uint8(80), "80"},
|
||||
{"Uint16", uint16(160), "160"},
|
||||
{"Uint32", uint32(320), "320"},
|
||||
{"Uint64", uint64(640), "640"},
|
||||
{"Float32", float32(3.14159), "3.14159"},
|
||||
{"Float64", float64(2.71828), "2.71828"},
|
||||
{"Complex64", complex64(1 + 2i), "(1+2i)"},
|
||||
{"Complex128", complex128(3 + 4i), "(3+4i)"},
|
||||
{"NilPointer", (*int)(nil), ""},
|
||||
{"PointerToInt", &iPtr, "90"},
|
||||
{"Boolean", true, "true"},
|
||||
{"Nil", nil, ""},
|
||||
{"Channel", make(chan int), ""},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
result := AnyToStr(testCase.input)
|
||||
if result != testCase.expected {
|
||||
t.Errorf("Unexpected result. Expected: %v, but got: %v", testCase.expected, result)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := AnyToStr(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("Unexpected result. Expected: %v, but got: %v", tc.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -14,9 +14,9 @@ func AnyToUint(i any) (uint, error) {
|
||||
}
|
||||
|
||||
// uint 兼容32位和64位系统
|
||||
if uint64(uint(v)) != v {
|
||||
return 0, ErrValOut
|
||||
}
|
||||
//if uint64(uint(v)) != v {
|
||||
// return 0, ErrValOut
|
||||
//}
|
||||
|
||||
return uint(v), nil
|
||||
}
|
||||
|
@@ -1,273 +1,237 @@
|
||||
package anyUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAnyToUint(t *testing.T) {
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
input any
|
||||
output uint
|
||||
err error
|
||||
iPtr := 90
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected uint
|
||||
err error
|
||||
}{
|
||||
{10, 10, nil},
|
||||
{-5, 0, ErrUnsignedInt},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{2.5, 2, nil},
|
||||
{make(chan int), 0, ErrType},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
{name: "iPtr", input: &iPtr, expected: 90, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 布尔
|
||||
{name: "true", input: true, expected: 1, err: nil},
|
||||
{name: "false", input: false, expected: 0, err: nil},
|
||||
|
||||
// 空指针
|
||||
{name: "nil point", input: (*int)(nil), expected: 0, err: nil},
|
||||
|
||||
// complex
|
||||
{name: "complex128", input: complex(3.14, 1.59), expected: 3, err: nil},
|
||||
{name: "complex64", input: complex(float32(2.71), float32(1.41)), expected: 2, err: nil},
|
||||
|
||||
// 其他
|
||||
{name: "struct", input: struct{ Name string }{Name: "test"}, expected: 0, err: ErrType},
|
||||
|
||||
{
|
||||
name: "Value within uint range (32-bit)",
|
||||
input: uint32(4294967295), // Maximum value for uint32 (32-bit)
|
||||
expected: uint(4294967295),
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "Value within uint range (64-bit)",
|
||||
input: uint64(18446744073709551615), // Maximum value for uint64 (64-bit)
|
||||
expected: uint(18446744073709551615), // This is an edge case for a 64-bit system
|
||||
err: nil,
|
||||
},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, test := range tests {
|
||||
result, err := AnyToUint(test.input)
|
||||
if result != test.output || !reflect.DeepEqual(err, test.err) {
|
||||
t.Errorf("AnyToUint(%v) = (%v, %v), expected (%v, %v)",
|
||||
test.input, result, err, test.output, test.err)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := AnyToUint(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToUint8(t *testing.T) {
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
input any
|
||||
output uint8
|
||||
err error
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected uint8
|
||||
err error
|
||||
}{
|
||||
{10, 10, nil},
|
||||
{300, 0, ErrValOut},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, ErrType},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 溢出
|
||||
{name: "MaxUint8", input: math.MaxUint8 + 1, expected: 0, err: ErrValOut},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, test := range tests {
|
||||
result, err := AnyToUint8(test.input)
|
||||
if result != test.output || !reflect.DeepEqual(err, test.err) {
|
||||
t.Errorf("AnyToUint8(%v) = (%v, %v), expected (%v, %v)",
|
||||
test.input, result, err, test.output, test.err)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := AnyToUint8(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToUint16(t *testing.T) {
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
input any
|
||||
output uint16
|
||||
err error
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected uint16
|
||||
err error
|
||||
}{
|
||||
{10, 10, nil},
|
||||
{70000, 0, ErrValOut},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, ErrType},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 溢出
|
||||
{name: "MaxUint16", input: math.MaxUint16 + 1, expected: 0, err: ErrValOut},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, test := range tests {
|
||||
result, err := AnyToUint16(test.input)
|
||||
if result != test.output || !reflect.DeepEqual(err, test.err) {
|
||||
t.Errorf("AnyToUint16(%v) = (%v, %v), expected (%v, %v)",
|
||||
test.input, result, err, test.output, test.err)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := AnyToUint16(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToUint32(t *testing.T) {
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
input any
|
||||
output uint32
|
||||
err error
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected uint32
|
||||
err error
|
||||
}{
|
||||
{10, 10, nil},
|
||||
{5000000000, 0, ErrValOut},
|
||||
{"20", 20, nil},
|
||||
{1.5, 1, nil},
|
||||
{make(chan int), 0, ErrType},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 溢出
|
||||
{name: "MaxUint32", input: math.MaxUint32 + 1, expected: 0, err: ErrValOut},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, test := range tests {
|
||||
result, err := AnyToUint32(test.input)
|
||||
if result != test.output || !reflect.DeepEqual(err, test.err) {
|
||||
t.Errorf("AnyToUint32(%v) = (%v, %v), expected (%v, %v)",
|
||||
test.input, result, err, test.output, test.err)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := AnyToUint32(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyToUint64(t *testing.T) {
|
||||
iPtr := 90
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input any
|
||||
want uint64
|
||||
wantError error
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected uint64
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Test -float32",
|
||||
input: float32(-0.1),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test -float64",
|
||||
input: float64(-0.2),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test -int",
|
||||
input: int(-1),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test -int8",
|
||||
input: int8(-2),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test -int16",
|
||||
input: int16(-3),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test -int32",
|
||||
input: int32(-4),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test -int64",
|
||||
input: int64(-5),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test uint",
|
||||
input: uint(12),
|
||||
want: 12,
|
||||
},
|
||||
{
|
||||
name: "Test uint8",
|
||||
input: uint8(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test uint16",
|
||||
input: uint16(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test uint32",
|
||||
input: uint32(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test uint64",
|
||||
input: uint64(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int8",
|
||||
input: int8(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int16",
|
||||
input: int16(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int32",
|
||||
input: int32(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int64",
|
||||
input: int64(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test float32",
|
||||
input: float32(42.0),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test float64",
|
||||
input: float64(42.0),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test complex64",
|
||||
input: complex64(complex(42, 0)),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test complex128",
|
||||
input: complex128(complex(42, 0)),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test string",
|
||||
input: "42",
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test invalid string",
|
||||
input: "not a number",
|
||||
wantError: ErrSyntax,
|
||||
},
|
||||
{
|
||||
name: "Test nil pointer",
|
||||
input: (*int)(nil),
|
||||
want: 0,
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "Test bool true",
|
||||
input: true,
|
||||
want: 1,
|
||||
},
|
||||
{
|
||||
name: "Test bool false",
|
||||
input: false,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "Test int point",
|
||||
input: &iPtr,
|
||||
want: 90,
|
||||
},
|
||||
{
|
||||
name: "Test nil",
|
||||
input: nil,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "test -complex",
|
||||
input: complex(-1, -1),
|
||||
wantError: ErrUnsignedInt,
|
||||
},
|
||||
{
|
||||
name: "Test invalid type",
|
||||
input: make(chan int),
|
||||
wantError: ErrType,
|
||||
},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// complex
|
||||
{name: "complex128", input: complex(3.14, 1.59), expected: 3, err: nil},
|
||||
{name: "complex64", input: complex(float32(2.71), float32(1.41)), expected: 2, err: nil},
|
||||
|
||||
// 其他
|
||||
{name: "struct", input: struct{ Name string }{Name: "test"}, expected: 0, err: ErrType},
|
||||
|
||||
// 小于0
|
||||
{name: "int < 0", input: int(-8), expected: 0, err: ErrUnsignedInt},
|
||||
{name: "float < 0", input: float64(-8), expected: 0, err: ErrUnsignedInt},
|
||||
{name: "complex128", input: complex(-3.14, -1.59), expected: 0, err: ErrUnsignedInt},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := AnyToUint64(tt.input)
|
||||
if err != tt.wantError {
|
||||
t.Errorf("toUint64(%v) error = %v, wantError %v", tt.input, err, tt.wantError)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("toUint64(%v) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := AnyToUint64(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package byteUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBytesToStr(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -14,9 +17,7 @@ func TestBytesToStr(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := BytesToStr(tt.data)
|
||||
if got != tt.expected {
|
||||
t.Errorf("BytesToStr(%v) = %v, want %v", tt.data, got, tt.expected)
|
||||
}
|
||||
assert.Equal(t, tt.expected, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,25 @@
|
||||
package cryptoUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashSHA256(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"},
|
||||
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
|
||||
{"1234567890", "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646"},
|
||||
{"字符串", "hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"},
|
||||
{"空字符串", "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
|
||||
{"数字", "1234567890", "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
output := HashSHA256(tc.input)
|
||||
if output != tc.expected {
|
||||
t.Errorf("HashSHA256(%s) = %s; expected %s", tc.input, output, tc.expected)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := HashSHA256(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,32 +1,37 @@
|
||||
package cryptoUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMd5(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
output string
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
output: "d41d8cd98f00b204e9800998ecf8427e",
|
||||
name: "空字符串",
|
||||
input: "",
|
||||
expected: "d41d8cd98f00b204e9800998ecf8427e",
|
||||
},
|
||||
{
|
||||
input: "test",
|
||||
output: "098f6bcd4621d373cade4e832627b4f6",
|
||||
name: "字符串",
|
||||
input: "test",
|
||||
expected: "098f6bcd4621d373cade4e832627b4f6",
|
||||
},
|
||||
{
|
||||
input: "hello world",
|
||||
output: "5eb63bbbe01eeed093cb22bb8f5acdc3",
|
||||
name: "带空格的字符串",
|
||||
input: "hello world",
|
||||
expected: "5eb63bbbe01eeed093cb22bb8f5acdc3",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := Md5(tc.input)
|
||||
if result != tc.output {
|
||||
t.Errorf("Md5(%q) = %q; want %q", tc.input, result, tc.output)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Md5(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
1
cryptoUtil/rsa_example_test.go
Normal file
1
cryptoUtil/rsa_example_test.go
Normal file
@@ -0,0 +1 @@
|
||||
package cryptoUtil
|
@@ -1,12 +1,12 @@
|
||||
package cryptoUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -17,6 +17,37 @@ func (r *badRandomReader) Read([]byte) (int, error) {
|
||||
return 0, errors.New("fake error")
|
||||
}
|
||||
|
||||
func TestGenerateRSAKeys(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
before func(t *testing.T)
|
||||
after func(t *testing.T)
|
||||
wantPrivateKey bool
|
||||
wantPublicKey bool
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "生成成功",
|
||||
before: func(t *testing.T) {},
|
||||
after: func(t *testing.T) {},
|
||||
wantPrivateKey: true,
|
||||
wantPublicKey: true,
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
tc.before(t)
|
||||
|
||||
privateKeyPEM, publicKeyPEM, err := GenerateRSAKeys()
|
||||
assert.Equal(t, tc.wantErr, err)
|
||||
assert.Equal(t, tc.wantPrivateKey, privateKeyPEM != "")
|
||||
assert.Equal(t, tc.wantPublicKey, publicKeyPEM != "")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 测试 rsa.GenerateKey 生产失败
|
||||
func TestGenerateRSAKeysError(t *testing.T) {
|
||||
// 保存原始的 rand.Reader
|
||||
@@ -48,7 +79,7 @@ func TestGenerateRSAKeysError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateRSAKeys(t *testing.T) {
|
||||
func TestGenerateRSAKeys2(t *testing.T) {
|
||||
// 测试 GenerateRSAKeys 是否能够正常工作
|
||||
privateKeyPEM, publicKeyPEM, err := GenerateRSAKeys()
|
||||
if err != nil {
|
||||
@@ -101,88 +132,123 @@ func TestGenerateRSAKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptRSAError(t *testing.T) {
|
||||
// 测试 EncryptRSA 函数在提供无效的公钥时是否返回错误
|
||||
plaintext := []byte("Hello, RSA!")
|
||||
invalidPublicKey := "invalid_public_key"
|
||||
ciphertext, err := EncryptRSA(invalidPublicKey, plaintext)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got ciphertext: %v", ciphertext)
|
||||
func TestEncryptRSA(t *testing.T) {
|
||||
_, pubKeyStr, _ := GenerateRSAKeys()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
publicKeyStr string
|
||||
message []byte
|
||||
expectedErr bool
|
||||
}{
|
||||
{
|
||||
name: "正常加密",
|
||||
publicKeyStr: pubKeyStr,
|
||||
message: []byte("test message"),
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "无效公钥",
|
||||
publicKeyStr: "invalid public key",
|
||||
message: []byte("test message"),
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "无效公钥格式",
|
||||
publicKeyStr: "-----BEGIN PUBLIC KEY-----\nInvalidKey\n-----END PUBLIC KEY-----",
|
||||
message: []byte("test message"),
|
||||
expectedErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
// 测试 EncryptRSA 函数在解析公钥失败时是否返回错误
|
||||
invalidKeyPEM := "invalid_key_pem"
|
||||
_, err = EncryptRSA(invalidKeyPEM, plaintext)
|
||||
if err == nil {
|
||||
t.Error("Expected error but got nil")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := EncryptRSA(tc.publicKeyStr, tc.message)
|
||||
if tc.expectedErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 验证加密和解密后的数据是否一致
|
||||
func TestRSAEncryptionAndDecryption(t *testing.T) {
|
||||
// 生成 RSA 密钥对
|
||||
privateKeyPEM, publicKeyPEM, err := GenerateRSAKeys()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate RSA keys: %s", err)
|
||||
}
|
||||
|
||||
// 加密数据
|
||||
plaintext := []byte("Hello, RSA!")
|
||||
ciphertext, err := EncryptRSA(publicKeyPEM, plaintext)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to encrypt data: %s", err)
|
||||
}
|
||||
|
||||
// 解密数据
|
||||
decryptedText, err := DecryptRSA(privateKeyPEM, ciphertext)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to decrypt data: %s", err)
|
||||
}
|
||||
|
||||
// 检查解密后的数据是否与原始数据一致
|
||||
if !bytes.Equal(decryptedText, plaintext) {
|
||||
t.Fatal("Decrypted text does not match the original plaintext")
|
||||
}
|
||||
}
|
||||
|
||||
// 验证 DecryptRSA 方法
|
||||
func TestDecryptRSA(t *testing.T) {
|
||||
privateKeyPEM, _, _ := GenerateRSAKeys()
|
||||
privateKeyBlock, _ := pem.Decode([]byte(privateKeyPEM))
|
||||
if privateKeyBlock == nil {
|
||||
t.Fatal("Failed to parse private key")
|
||||
}
|
||||
privateKey, publicKey, _ := GenerateRSAKeys()
|
||||
|
||||
privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
|
||||
// 使用公钥加密一条消息,以便测试解密
|
||||
message := []byte("test message")
|
||||
ciphertext, err := EncryptRSA(publicKey, message)
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing private key: %s", err)
|
||||
t.Fatalf("failed to encrypt message: %v", err)
|
||||
}
|
||||
|
||||
message := []byte("Hello, World!")
|
||||
|
||||
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &privateKey.PublicKey, message)
|
||||
if err != nil {
|
||||
t.Fatalf("Error encrypting message: %s", err)
|
||||
testCases := []struct {
|
||||
name string
|
||||
privateKeyStr string
|
||||
ciphertext []byte
|
||||
expected []byte
|
||||
expectedErr bool
|
||||
}{
|
||||
{
|
||||
name: "正常解密",
|
||||
privateKeyStr: privateKey,
|
||||
ciphertext: ciphertext,
|
||||
expected: message,
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "无效私钥",
|
||||
privateKeyStr: "invalid private key",
|
||||
ciphertext: ciphertext,
|
||||
expected: nil,
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "无效私钥格式",
|
||||
privateKeyStr: "-----BEGIN RSA PRIVATE KEY-----\nInvalidKey\n-----END RSA PRIVATE KEY-----",
|
||||
ciphertext: ciphertext,
|
||||
expected: nil,
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "无效密文",
|
||||
privateKeyStr: privateKey,
|
||||
ciphertext: []byte("invalid ciphertext"),
|
||||
expected: nil,
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "无效私钥数据",
|
||||
privateKeyStr: "-----BEGIN RSA PRIVATE KEY-----\n" + "A" + "\n-----END RSA PRIVATE KEY-----",
|
||||
ciphertext: ciphertext,
|
||||
expected: nil,
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "不完整的私钥",
|
||||
privateKeyStr: "-----BEGIN RSA PRIVATE KEY-----\n" +
|
||||
"MIICWwIBAAKBgQDEkzKS0u5p6kwl9m0g3g4mMI09S8QOAbW5aBMbDWZ5R0pUtH5h" +
|
||||
"J9mQFt8Uu4FJ8Yc9C5ZiM5F9pV5J2V4SeKk3RbKjFG2iD6rzO/OMrMZ3/1H8n02" +
|
||||
"eZ/D14SvnPBNhYnb8Ysdd4kS8A==\n-----END RSA PRIVATE KEY-----",
|
||||
ciphertext: ciphertext,
|
||||
expected: nil,
|
||||
expectedErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
// Test decryption with correct private key
|
||||
plaintext, err := DecryptRSA(privateKeyPEM, ciphertext)
|
||||
if err != nil {
|
||||
t.Fatalf("Error decrypting ciphertext: %s", err)
|
||||
}
|
||||
if string(plaintext) != string(message) {
|
||||
t.Fatalf("Decrypted message doesn't match original message")
|
||||
}
|
||||
|
||||
// Test decryption with incorrect private key
|
||||
_, err = DecryptRSA("InvalidPrivateKey", ciphertext)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error decrypting with invalid private key")
|
||||
}
|
||||
|
||||
// Test decryption with incorrect ciphertext
|
||||
_, err = DecryptRSA(privateKeyPEM, []byte("InvalidCiphertext"))
|
||||
if err == nil {
|
||||
t.Fatal("Expected error decrypting invalid ciphertext")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := DecryptRSA(tc.privateKeyStr, tc.ciphertext)
|
||||
if tc.expectedErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,88 +1,51 @@
|
||||
package emojiUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodeEmojiUnicode(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"[\\u1F602]", "😂"},
|
||||
{"[\\u1F60A]", "😊"},
|
||||
{"[\\u1F60E]456", "😎456"},
|
||||
{"No emoji", "No emoji"},
|
||||
{"表情笑哭", "[\\u1F602]", "😂"},
|
||||
{"表情微笑", "[\\u1F60A]", "😊"},
|
||||
{"表情和数字", "[\\u1F60E]456", "😎456"},
|
||||
{"无意义的字符串", "No emoji", "No emoji"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
res := DecodeEmojiUnicode(tc.input)
|
||||
|
||||
// 转大写
|
||||
res = strings.ToUpper(res)
|
||||
expected := strings.ToUpper(tc.expected)
|
||||
|
||||
if res != expected {
|
||||
t.Errorf("Unexpected result - input: %s, expected: %s, got: %s",
|
||||
tc.input,
|
||||
expected,
|
||||
res,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeEmojiUnicode2(t *testing.T) {
|
||||
input := "[\\u1F602]"
|
||||
expected := "😂"
|
||||
result := DecodeEmojiUnicode(input)
|
||||
|
||||
// 转大写
|
||||
expected = strings.ToUpper(expected)
|
||||
result = strings.ToUpper(result)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("DecodeEmoji(%s) = %s; expected %s", input, result, expected)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := DecodeEmojiUnicode(tc.input)
|
||||
// 转大写
|
||||
res = strings.ToUpper(res)
|
||||
expected := strings.ToUpper(tc.expected)
|
||||
assert.Equal(t, expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeEmojiUnicode(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"Hello 😂", "Hello [\\u1F602]"},
|
||||
{"No emoji", "No emoji"},
|
||||
{"字符串和表情", "Hello 😂", "Hello [\\u1F602]"},
|
||||
{"无意义的字符串", "No emoji", "No emoji"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
actual := EncodeEmojiUnicode(tc.input)
|
||||
|
||||
// 转大写
|
||||
actual = strings.ToUpper(actual)
|
||||
expected := strings.ToUpper(tc.expected)
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected result - input: %s, expected: %s, got: %s",
|
||||
tc.input,
|
||||
expected,
|
||||
actual,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeEmojiUnicode2(t *testing.T) {
|
||||
input := "😂"
|
||||
expected := "[\\u1F602]"
|
||||
result := EncodeEmojiUnicode(input)
|
||||
|
||||
// 转大写
|
||||
expected = strings.ToUpper(expected)
|
||||
result = strings.ToUpper(result)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("EncodeEmoji(%s) = %s; expected %s", input, result, expected)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actual := EncodeEmojiUnicode(tc.input)
|
||||
// 转大写
|
||||
actual = strings.ToUpper(actual)
|
||||
expected := strings.ToUpper(tc.expected)
|
||||
assert.Equal(t, expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,42 +1,78 @@
|
||||
package floatUtil
|
||||
|
||||
import (
|
||||
"math"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFloat32ToStr(t *testing.T) {
|
||||
var f float32 = 3.14159
|
||||
want := "3.14159"
|
||||
got := Float32ToStr(f)
|
||||
if got != want {
|
||||
t.Errorf("Float32ToStr(%v) = %v; want %v", f, got, want)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float32
|
||||
expected string
|
||||
}{
|
||||
{"一位小数", float32(3.1), "3.1"},
|
||||
{"多位小数", float32(3.122), "3.122"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Float32ToStr(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64ToStr(t *testing.T) {
|
||||
var f float64 = 3.14159
|
||||
want := "3.14159"
|
||||
got := Float64ToStr(f)
|
||||
if got != want {
|
||||
t.Errorf("Float64ToStr(%v) = %v; want %v", f, got, want)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float64
|
||||
expected string
|
||||
}{
|
||||
{"一位小数", float64(3.1), "3.1"},
|
||||
{"多位小数", float64(3.122), "3.122"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Float64ToStr(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat32ToFloat64(t *testing.T) {
|
||||
var f float32 = 3.14159
|
||||
want := 3.14159
|
||||
got := Float32ToFloat64(f)
|
||||
if math.Abs(got-want) > 1e-6 {
|
||||
t.Errorf("Float32ToFloat64(%v) = %v; want %v", f, got, want)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float32
|
||||
expected float64
|
||||
}{
|
||||
{"一位小数", float32(3.1), float64(3.1)},
|
||||
{"多位小数", float32(3.122), float64(3.122)},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Float32ToFloat64(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64ToFloat32(t *testing.T) {
|
||||
var f float64 = 3.14159
|
||||
want := float32(3.14159)
|
||||
got := Float64ToFloat32(f)
|
||||
if math.Abs(float64(got)-float64(want)) > 1e-6 {
|
||||
t.Errorf("Float64ToFloat32(%v) = %v; want %v", f, got, want)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float64
|
||||
expected float32
|
||||
}{
|
||||
{"一位小数", float64(3.1), float32(3.1)},
|
||||
{"多位小数", float64(3.122), float32(3.122)},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Float64ToFloat32(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
7
go.mod
7
go.mod
@@ -1,3 +1,10 @@
|
||||
module github.com/jefferyjob/go-easy-utils/v2
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
9
go.sum
Normal file
9
go.sum
Normal file
@@ -0,0 +1,9 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
@@ -1,72 +1,9 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func toBool(i any) bool {
|
||||
if i == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return false
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case bool:
|
||||
return v
|
||||
case int:
|
||||
return v != 0
|
||||
case int8:
|
||||
return v != 0
|
||||
case int16:
|
||||
return v != 0
|
||||
case int32:
|
||||
return v != 0
|
||||
case int64:
|
||||
return v != 0
|
||||
case uint:
|
||||
return v != 0
|
||||
case uint8:
|
||||
return v != 0
|
||||
case uint16:
|
||||
return v != 0
|
||||
case uint32:
|
||||
return v != 0
|
||||
case uint64:
|
||||
return v != 0
|
||||
case uintptr:
|
||||
return v != 0
|
||||
case float32:
|
||||
return v != 0
|
||||
case float64:
|
||||
return v != 0
|
||||
case complex64:
|
||||
return v != 0
|
||||
case complex128:
|
||||
return v != 0
|
||||
case string:
|
||||
if v == "true" {
|
||||
return true
|
||||
} else if v == "false" {
|
||||
return false
|
||||
}
|
||||
return v != ""
|
||||
case fmt.Stringer:
|
||||
return v.String() != ""
|
||||
case interface{ IsNil() bool }:
|
||||
return !v.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func toBoolReflect(i any) bool {
|
||||
if i == nil {
|
||||
return false
|
||||
|
@@ -1,60 +1,62 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToBool(t *testing.T) {
|
||||
var iPtr = 90
|
||||
var tests = []struct {
|
||||
name string
|
||||
input any
|
||||
want bool
|
||||
}{
|
||||
{true, true},
|
||||
{false, false},
|
||||
{int(-1), true},
|
||||
{int(1), true},
|
||||
{int(0), false},
|
||||
{int8(1), true},
|
||||
{int8(0), false},
|
||||
{int16(1), true},
|
||||
{int16(0), false},
|
||||
{int32(1), true},
|
||||
{int32(0), false},
|
||||
{int64(1), true},
|
||||
{int64(0), false},
|
||||
{uint(1), true},
|
||||
{uint(0), false},
|
||||
{uint8(1), true},
|
||||
{uint8(0), false},
|
||||
{uint16(1), true},
|
||||
{uint16(0), false},
|
||||
{uint32(1), true},
|
||||
{uint32(0), false},
|
||||
{uint64(1), true},
|
||||
{uint64(0), false},
|
||||
{float32(1.0), true},
|
||||
{float32(0.0), false},
|
||||
{float64(1.0), true},
|
||||
{float64(0.0), false},
|
||||
{"abc", true},
|
||||
{"true", true},
|
||||
{"false", false},
|
||||
{"", false},
|
||||
{nil, false},
|
||||
{complex64(1 + 1i), true},
|
||||
{complex64(0 + 0i), false},
|
||||
{complex128(1 + 1i), true},
|
||||
{complex128(0 + 0i), false},
|
||||
{(*int)(nil), false},
|
||||
{make(chan int), false},
|
||||
{"布尔真", true, true},
|
||||
{"布尔假", false, false},
|
||||
{"负整数", int(-1), true},
|
||||
{"正整数", int(1), true},
|
||||
{"零整数", int(0), false},
|
||||
{"正int8", int8(1), true},
|
||||
{"零int8", int8(0), false},
|
||||
{"正int16", int16(1), true},
|
||||
{"零int16", int16(0), false},
|
||||
{"正int32", int32(1), true},
|
||||
{"零int32", int32(0), false},
|
||||
{"正int64", int64(1), true},
|
||||
{"零int64", int64(0), false},
|
||||
{"正uint", uint(1), true},
|
||||
{"零uint", uint(0), false},
|
||||
{"正uint8", uint8(1), true},
|
||||
{"零uint8", uint8(0), false},
|
||||
{"正uint16", uint16(1), true},
|
||||
{"零uint16", uint16(0), false},
|
||||
{"正uint32", uint32(1), true},
|
||||
{"零uint32", uint32(0), false},
|
||||
{"正uint64", uint64(1), true},
|
||||
{"零uint64", uint64(0), false},
|
||||
{"浮点1.0", float32(1.0), true},
|
||||
{"浮点0.0", float32(0.0), false},
|
||||
{"双精1.0", float64(1.0), true},
|
||||
{"双精0.0", float64(0.0), false},
|
||||
{"字符串", "abc", true},
|
||||
{"字符串真", "true", true},
|
||||
{"字符串假", "false", false},
|
||||
{"空字符串", "", false},
|
||||
{"空值", nil, false},
|
||||
{"非空指针", &iPtr, true},
|
||||
{"复数1+1i", complex64(1 + 1i), true},
|
||||
{"复数0+0i", complex64(0 + 0i), false},
|
||||
{"双复1+1i", complex128(1 + 1i), true},
|
||||
{"双复0+0i", complex128(0 + 0i), false},
|
||||
{"空指针", (*int)(nil), false},
|
||||
{"通道", make(chan int), false},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if got := toBool(test.input); got != test.want {
|
||||
t.Errorf("toBool(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
|
||||
if got := toBoolReflect(test.input); got != test.want {
|
||||
t.Errorf("toBool(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := toBoolReflect(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -5,105 +5,6 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func toFloat64(i any) (float64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return 0, nil
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case float32:
|
||||
return float64(v), nil
|
||||
case float64:
|
||||
return v, nil
|
||||
case string:
|
||||
floatValue, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return 0, ErrSyntax
|
||||
}
|
||||
return floatValue, nil
|
||||
case uint:
|
||||
return float64(v), nil
|
||||
case uint8:
|
||||
return float64(v), nil
|
||||
case uint16:
|
||||
return float64(v), nil
|
||||
case uint32:
|
||||
return float64(v), nil
|
||||
case uint64:
|
||||
return float64(v), nil
|
||||
case int:
|
||||
return float64(v), nil
|
||||
case int8:
|
||||
return float64(v), nil
|
||||
case int16:
|
||||
return float64(v), nil
|
||||
case int32:
|
||||
return float64(v), nil
|
||||
case int64:
|
||||
return float64(v), nil
|
||||
case complex64:
|
||||
return float64(real(v)), nil
|
||||
case complex128:
|
||||
return real(v), nil
|
||||
case bool:
|
||||
if v {
|
||||
return 1, nil
|
||||
} else {
|
||||
return 0, nil
|
||||
}
|
||||
//case *float32:
|
||||
// return float64(*v), nil
|
||||
//case *float64:
|
||||
// return *v, nil
|
||||
//case *string:
|
||||
// floatValue, err := strconv.ParseFloat(*v, 64)
|
||||
// if err != nil {
|
||||
// return 0, ErrSyntax
|
||||
// }
|
||||
// return floatValue, nil
|
||||
//case *uint:
|
||||
// return float64(*v), nil
|
||||
//case *uint8:
|
||||
// return float64(*v), nil
|
||||
//case *uint16:
|
||||
// return float64(*v), nil
|
||||
//case *uint32:
|
||||
// return float64(*v), nil
|
||||
//case *uint64:
|
||||
// return float64(*v), nil
|
||||
//case *int:
|
||||
// return float64(*v), nil
|
||||
//case *int8:
|
||||
// return float64(*v), nil
|
||||
//case *int16:
|
||||
// return float64(*v), nil
|
||||
//case *int32:
|
||||
// return float64(*v), nil
|
||||
//case *int64:
|
||||
// return float64(*v), nil
|
||||
//case *complex64:
|
||||
// return float64(real(*v)), nil
|
||||
//case *complex128:
|
||||
// return real(*v), nil
|
||||
//case *bool:
|
||||
// if v != nil && *v {
|
||||
// return 1, nil
|
||||
// } else {
|
||||
// return 0, nil
|
||||
// }
|
||||
default:
|
||||
return 0, ErrType
|
||||
}
|
||||
}
|
||||
|
||||
func toFloat64Reflect(i any) (float64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
|
@@ -1,280 +1,46 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func TestFloat(t *testing.T) {
|
||||
// var data1 any
|
||||
// data1 = 123
|
||||
// fmt.Println(toFloat64Reflect(data1))
|
||||
//
|
||||
// data2 := int16Ptr(80)
|
||||
// fmt.Println(toFloat64Reflect(data2))
|
||||
//
|
||||
// data3 := map[any]any{
|
||||
// "aaa": "aaa",
|
||||
// }
|
||||
// fmt.Println(toFloat64Reflect(data3))
|
||||
//}
|
||||
|
||||
func TestToFloat64(t *testing.T) {
|
||||
var iPtr = 90
|
||||
testCases := []struct {
|
||||
name string
|
||||
value any
|
||||
expected float64
|
||||
expectedErr error
|
||||
}{
|
||||
{nil, 0, nil},
|
||||
{float32(123.5), 123.5, nil},
|
||||
{"123.456", 123.456, nil},
|
||||
{uint(123), 123, nil},
|
||||
{uint8(123), 123, nil},
|
||||
{uint16(123), 123, nil},
|
||||
{uint32(123), 123, nil},
|
||||
{uint64(123), 123, nil},
|
||||
{int(123), 123, nil},
|
||||
{int8(123), 123, nil},
|
||||
{int16(123), 123, nil},
|
||||
{int32(123), 123, nil},
|
||||
{int64(123), 123, nil},
|
||||
{complex64(1 + 2i), 1, nil},
|
||||
{complex128(1 + 2i), 1, nil},
|
||||
{true, 1, nil},
|
||||
{false, 0, nil},
|
||||
{(*bool)(nil), 0, nil},
|
||||
{make(chan int), 0, ErrType},
|
||||
{"abc", 0, ErrSyntax},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
f, err := toFloat64(tc.value)
|
||||
if f != tc.expected || err != tc.expectedErr {
|
||||
t.Errorf("toFloat64(%v) = (%v, %v), expected (%v, %v)", tc.value, f, err, tc.expected, tc.expectedErr)
|
||||
}
|
||||
|
||||
f2, err2 := toFloat64Reflect(tc.value)
|
||||
if f != tc.expected || err2 != tc.expectedErr {
|
||||
t.Errorf("toFloat64Reflect(%v) = (%v, %v), expected (%v, %v)", tc.value, f2, err2, tc.expected, tc.expectedErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToFloat64Pointer(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expectedValue float64
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
name: "nil input",
|
||||
input: nil,
|
||||
expectedValue: 0,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "float32 pointer input",
|
||||
input: float32Ptr(12.2),
|
||||
expectedValue: 12.199999809265137,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "float64 pointer input",
|
||||
input: float64Ptr(56.78),
|
||||
expectedValue: 56.78,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "invalid string input",
|
||||
input: stringPtr("abc"),
|
||||
expectedValue: 0,
|
||||
expectedError: ErrSyntax,
|
||||
},
|
||||
{
|
||||
name: "valid string input",
|
||||
input: stringPtr("123.45"),
|
||||
expectedValue: 123.45,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "uint pointer input",
|
||||
input: uintPtr(10),
|
||||
expectedValue: 10,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "uint8 pointer input",
|
||||
input: uint8Ptr(20),
|
||||
expectedValue: 20,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "uint16 pointer input",
|
||||
input: uint16Ptr(30),
|
||||
expectedValue: 30,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "uint32 pointer input",
|
||||
input: uint32Ptr(40),
|
||||
expectedValue: 40,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "uint64 pointer input",
|
||||
input: uint64Ptr(50),
|
||||
expectedValue: 50,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "int pointer input",
|
||||
input: intPtr(-60),
|
||||
expectedValue: -60,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "int8 pointer input",
|
||||
input: int8Ptr(-70),
|
||||
expectedValue: -70,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "int16 pointer input",
|
||||
input: int16Ptr(-80),
|
||||
expectedValue: -80,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "int32 pointer input",
|
||||
input: int32Ptr(-90),
|
||||
expectedValue: -90,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "int64 pointer input",
|
||||
input: int64Ptr(-100),
|
||||
expectedValue: -100,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "complex64 pointer input",
|
||||
input: complex64Ptr(complex(1, 2)),
|
||||
expectedValue: 1,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "complex128 pointer input",
|
||||
input: complex128Ptr(complex(3, 4)),
|
||||
expectedValue: 3,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "bool pointer input - true",
|
||||
input: boolPtr(true),
|
||||
expectedValue: 1,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "bool pointer input - false",
|
||||
input: boolPtr(false),
|
||||
expectedValue: 0,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "unsupported input type",
|
||||
input: "unsupported",
|
||||
expectedValue: 0,
|
||||
expectedError: ErrSyntax,
|
||||
},
|
||||
{"空值", nil, 0, nil},
|
||||
{"浮点数", float32(123.5), 123.5, nil},
|
||||
{"字符串数", "123.456", 123.456, nil},
|
||||
{"无符整型", uint(123), 123, nil},
|
||||
{"无符uint8", uint8(123), 123, nil},
|
||||
{"无符uint16", uint16(123), 123, nil},
|
||||
{"无符uint32", uint32(123), 123, nil},
|
||||
{"无符uint64", uint64(123), 123, nil},
|
||||
{"有符整型", int(123), 123, nil},
|
||||
{"有符int8", int8(123), 123, nil},
|
||||
{"有符int16", int16(123), 123, nil},
|
||||
{"有符int32", int32(123), 123, nil},
|
||||
{"有符int64", int64(123), 123, nil},
|
||||
{"复数64", complex64(1 + 2i), 1, nil},
|
||||
{"复数128", complex128(1 + 2i), 1, nil},
|
||||
{"布尔真", true, 1, nil},
|
||||
{"布尔假", false, 0, nil},
|
||||
{"空布尔指针", (*bool)(nil), 0, nil},
|
||||
{"非空指针", &iPtr, 90, nil},
|
||||
{"通道", make(chan int), 0, ErrType},
|
||||
{"无效字符串", "abc", 0, ErrSyntax},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actualValue, actualError := toFloat64(tc.input)
|
||||
|
||||
if actualError != tc.expectedError {
|
||||
t.Errorf("Expected(name:%s) error: %v, but got: %v", tc.name, tc.expectedError, actualError)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actualValue, tc.expectedValue) {
|
||||
t.Errorf("Expected value: %v, but got: %v", tc.expectedValue, actualValue)
|
||||
}
|
||||
|
||||
// Reflect
|
||||
actualValue2, actualError2 := toFloat64Reflect(tc.input)
|
||||
|
||||
if actualError2 != tc.expectedError {
|
||||
t.Errorf("Expected(name:%s) error: %v, but got: %v", tc.name, tc.expectedError, actualError2)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actualValue2, tc.expectedValue) {
|
||||
t.Errorf("Expected value: %v, but got: %v", tc.expectedValue, actualValue2)
|
||||
}
|
||||
res, err := toFloat64Reflect(tc.value)
|
||||
assert.Equal(t, tc.expectedErr, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func float32Ptr(v float32) *float32 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func float64Ptr(v float64) *float64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func stringPtr(v string) *string {
|
||||
return &v
|
||||
}
|
||||
|
||||
func uintPtr(v uint) *uint {
|
||||
return &v
|
||||
}
|
||||
|
||||
func uint8Ptr(v uint8) *uint8 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func uint16Ptr(v uint16) *uint16 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func uint32Ptr(v uint32) *uint32 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func uint64Ptr(v uint64) *uint64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func intPtr(v int) *int {
|
||||
return &v
|
||||
}
|
||||
|
||||
func int8Ptr(v int8) *int8 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func int16Ptr(v int16) *int16 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func int32Ptr(v int32) *int32 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func int64Ptr(v int64) *int64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func complex64Ptr(v complex64) *complex64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func complex128Ptr(v complex128) *complex128 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
@@ -5,115 +5,6 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func toInt64(i any) (int64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return 0, nil
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case bool:
|
||||
if v {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case uint:
|
||||
return int64(v), nil
|
||||
case uint8:
|
||||
return int64(v), nil
|
||||
case uint16:
|
||||
return int64(v), nil
|
||||
case uint32:
|
||||
return int64(v), nil
|
||||
case uint64:
|
||||
return int64(v), nil
|
||||
case float32:
|
||||
return int64(v), nil
|
||||
case float64:
|
||||
return int64(v), nil
|
||||
case complex64:
|
||||
return int64(real(v)), nil
|
||||
case complex128:
|
||||
return int64(real(v)), nil
|
||||
case string:
|
||||
intValue, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, ErrSyntax
|
||||
}
|
||||
return intValue, nil
|
||||
case int:
|
||||
return int64(v), nil
|
||||
case int8:
|
||||
return int64(v), nil
|
||||
case int16:
|
||||
return int64(v), nil
|
||||
case int32:
|
||||
return int64(v), nil
|
||||
case int64:
|
||||
return v, nil
|
||||
//case *bool:
|
||||
// if v == nil {
|
||||
// return 0, nil
|
||||
// }
|
||||
// if *v {
|
||||
// return 1, nil
|
||||
// }
|
||||
// return 0, nil
|
||||
//case *uint:
|
||||
// return int64(*v), nil
|
||||
//case *uint8:
|
||||
// return int64(*v), nil
|
||||
//case *uint16:
|
||||
// return int64(*v), nil
|
||||
//case *uint32:
|
||||
// return int64(*v), nil
|
||||
//case *uint64:
|
||||
// return int64(*v), nil
|
||||
//case *float32:
|
||||
// return int64(*v), nil
|
||||
//case *float64:
|
||||
// return int64(*v), nil
|
||||
//case *complex64:
|
||||
// if v == nil {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return int64(real(*v)), nil
|
||||
//case *complex128:
|
||||
// if v == nil {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return int64(real(*v)), nil
|
||||
//case *string:
|
||||
// if v == nil {
|
||||
// return 0, nil
|
||||
// }
|
||||
// intValue, err := strconv.ParseInt(*v, 10, 64)
|
||||
// if err != nil {
|
||||
// return 0, ErrSyntax
|
||||
// }
|
||||
// return intValue, nil
|
||||
//case *int:
|
||||
// return int64(*v), nil
|
||||
//case *int8:
|
||||
// return int64(*v), nil
|
||||
//case *int16:
|
||||
// return int64(*v), nil
|
||||
//case *int32:
|
||||
// return int64(*v), nil
|
||||
//case *int64:
|
||||
// return *v, nil
|
||||
default:
|
||||
return 0, ErrType
|
||||
}
|
||||
}
|
||||
|
||||
func toInt64Reflect(i any) (int64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
|
@@ -1,54 +1,58 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToInt64(t *testing.T) {
|
||||
var tests = []struct {
|
||||
func TestAnyToInt64(t *testing.T) {
|
||||
var iPtr = 90
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expected int64
|
||||
err error
|
||||
}{
|
||||
{"123", 123, nil},
|
||||
{"-234", -234, nil},
|
||||
{345, 345, nil},
|
||||
{-456, -456, nil},
|
||||
{int8(12), 12, nil},
|
||||
{int16(1238), 1238, nil},
|
||||
{int32(1239), 1239, nil},
|
||||
{int64(1230), 1230, nil},
|
||||
{uint(1231), 1231, nil},
|
||||
{uint8(123), 123, nil},
|
||||
{uint16(1232), 1232, nil},
|
||||
{uint32(1233), 1233, nil},
|
||||
{uint64(1234), 1234, nil},
|
||||
{float32(12.45), 12, nil},
|
||||
{float64(123.45), 123, nil},
|
||||
{true, 1, nil},
|
||||
{false, 0, nil},
|
||||
{complex64(1 + 2i), 1, nil},
|
||||
{complex128(1 + 2i), 1, nil},
|
||||
{nil, 0, nil},
|
||||
{"not a number", 0, ErrSyntax},
|
||||
{make(chan int), 0, ErrType},
|
||||
// 测试整数输入
|
||||
{name: "number", input: 42, expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: int64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: int32(42), expected: 42, err: nil},
|
||||
{name: "int", input: int(42), expected: 42, err: nil},
|
||||
{name: "int64", input: uint64(42), expected: 42, err: nil},
|
||||
{name: "int32", input: uint32(42), expected: 42, err: nil},
|
||||
{name: "uint", input: uint(42), expected: 42, err: nil},
|
||||
|
||||
// 测试浮点数输入
|
||||
{name: "float64", input: float64(42.0), expected: 42, err: nil},
|
||||
{name: "float32", input: float32(42.0), expected: 42, err: nil},
|
||||
{name: "float64_小数点", input: float64(42.5), expected: 42, err: nil},
|
||||
|
||||
// 测试非数值类型
|
||||
{name: "string", input: "rand string", expected: 0, err: ErrSyntax},
|
||||
{name: "nil", input: nil, expected: 0, err: nil},
|
||||
|
||||
// 布尔
|
||||
{name: "true", input: true, expected: 1, err: nil},
|
||||
{name: "false", input: false, expected: 0, err: nil},
|
||||
|
||||
// 空指针
|
||||
{name: "nil point", input: (*int)(nil), expected: 0, err: nil},
|
||||
{name: "point", input: &iPtr, expected: 90, err: nil},
|
||||
|
||||
// complex
|
||||
{name: "complex128", input: complex(3.14, 1.59), expected: 3, err: nil},
|
||||
{name: "complex64", input: complex(float32(2.71), float32(1.41)), expected: 2, err: nil},
|
||||
|
||||
// 其他
|
||||
{name: "struct", input: struct{ Name string }{Name: "test"}, expected: 0, err: ErrType},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
actual, err := toInt64(tt.input)
|
||||
if err != tt.err {
|
||||
t.Errorf("toInt64(%v): expected error %v, actual error %v", tt.input, tt.err, err)
|
||||
}
|
||||
if actual != tt.expected {
|
||||
t.Errorf("toInt64(%v): expected %v, actual %v", tt.input, tt.expected, actual)
|
||||
}
|
||||
|
||||
actual, err = toInt64Reflect(tt.input)
|
||||
if err != tt.err {
|
||||
t.Errorf("toInt64(%v): expected error %v, actual error %v", tt.input, tt.err, err)
|
||||
}
|
||||
if actual != tt.expected {
|
||||
t.Errorf("toInt64(%v): expected %v, actual %v", tt.input, tt.expected, actual)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := toInt64Reflect(tc.input)
|
||||
assert.Equal(t, tc.err, err)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -6,137 +6,6 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func toString(i any) string {
|
||||
if i == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return ""
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case string:
|
||||
return v
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int32:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10)
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(v, 10)
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(v), 'f', -1, 32)
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64)
|
||||
case complex64:
|
||||
return fmt.Sprintf("(%g+%gi)", real(v), imag(v))
|
||||
case complex128:
|
||||
return fmt.Sprintf("(%g+%gi)", real(v), imag(v))
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
//case *string:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return *v
|
||||
//case *int:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.Itoa(*v)
|
||||
//case *int8:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatInt(int64(*v), 10)
|
||||
//case *int16:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatInt(int64(*v), 10)
|
||||
//case *int32:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatInt(int64(*v), 10)
|
||||
//case *int64:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatInt(*v, 10)
|
||||
//case *uint:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatUint(uint64(*v), 10)
|
||||
//case *uint8:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatUint(uint64(*v), 10)
|
||||
//case *uint16:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatUint(uint64(*v), 10)
|
||||
//case *uint32:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatUint(uint64(*v), 10)
|
||||
//case *uint64:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatUint(*v, 10)
|
||||
//case *float32:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatFloat(float64(*v), 'f', -1, 32)
|
||||
//case *float64:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatFloat(*v, 'f', -1, 64)
|
||||
//case *complex64:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return fmt.Sprintf("(%g+%gi)", real(*v), imag(*v))
|
||||
//case *complex128:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return fmt.Sprintf("(%g+%gi)", real(*v), imag(*v))
|
||||
//case *bool:
|
||||
// if v == nil {
|
||||
// return ""
|
||||
// }
|
||||
// return strconv.FormatBool(*v)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func toStringReflect(i any) string {
|
||||
if i == nil {
|
||||
return ""
|
||||
|
@@ -1,8 +1,12 @@
|
||||
package jsonUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToString(t *testing.T) {
|
||||
var iPar = "point"
|
||||
tests := []struct {
|
||||
name string
|
||||
value any
|
||||
@@ -24,19 +28,16 @@ func TestToString(t *testing.T) {
|
||||
{"float64", 3.14159, "3.14159"},
|
||||
{"bool-true", true, "true"},
|
||||
{"bool-false", false, "false"},
|
||||
{"point", &iPar, "point"},
|
||||
{"complex64", complex64(1 + 2i), "(1+2i)"},
|
||||
{"complex128", complex128(3 + 4i), "(3+4i)"},
|
||||
{"chan", make(chan int), ""},
|
||||
{"int nil", (*int)(nil), ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := toString(tt.value); got != tt.want {
|
||||
t.Errorf("ToString() = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
if got := toStringReflect(tt.value); got != tt.want {
|
||||
t.Errorf("ToString() = %v, want %v", got, tt.want)
|
||||
}
|
||||
got := toStringReflect(tt.value)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -5,159 +5,6 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func toUint64(i any) (uint64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return 0, nil
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case float32:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
case float64:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
case complex64:
|
||||
if real(v) < 0 || imag(v) < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(real(v)), nil
|
||||
case complex128:
|
||||
if real(v) < 0 || imag(v) < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(real(v)), nil
|
||||
case string:
|
||||
intValue, err := strconv.ParseUint(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, ErrSyntax
|
||||
}
|
||||
return intValue, nil
|
||||
case bool:
|
||||
if v {
|
||||
return 1, nil
|
||||
} else {
|
||||
return 0, nil
|
||||
}
|
||||
case uint:
|
||||
return uint64(v), nil
|
||||
case uint8:
|
||||
return uint64(v), nil
|
||||
case uint16:
|
||||
return uint64(v), nil
|
||||
case uint32:
|
||||
return uint64(v), nil
|
||||
case uint64:
|
||||
return v, nil
|
||||
case int:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
case int8:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
case int16:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
case int32:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
case int64:
|
||||
if v < 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return uint64(v), nil
|
||||
//case *float32:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
//case *float64:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
//case *complex64:
|
||||
// if real(*v) < 0 || imag(*v) < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(real(*v)), nil
|
||||
//case *complex128:
|
||||
// if real(*v) < 0 || imag(*v) < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(real(*v)), nil
|
||||
//case *string:
|
||||
// intValue, err := strconv.ParseUint(*v, 10, 64)
|
||||
// if err != nil {
|
||||
// return 0, ErrSyntax
|
||||
// }
|
||||
// return intValue, nil
|
||||
//case *bool:
|
||||
// if *v {
|
||||
// return 1, nil
|
||||
// } else {
|
||||
// return 0, nil
|
||||
// }
|
||||
//case *uint:
|
||||
// return uint64(*v), nil
|
||||
//case *uint8:
|
||||
// return uint64(*v), nil
|
||||
//case *uint16:
|
||||
// return uint64(*v), nil
|
||||
//case *uint32:
|
||||
// return uint64(*v), nil
|
||||
//case *uint64:
|
||||
// return *v, nil
|
||||
//case *int:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
//case *int8:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
//case *int16:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
//case *int32:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
//case *int64:
|
||||
// if *v < 0 {
|
||||
// return 0, nil
|
||||
// }
|
||||
// return uint64(*v), nil
|
||||
default:
|
||||
return 0, ErrType
|
||||
}
|
||||
}
|
||||
|
||||
func toUint64Reflect(i any) (uint64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToUint64(t *testing.T) {
|
||||
var iPtr = 90
|
||||
tests := []struct {
|
||||
name string
|
||||
input any
|
||||
@@ -147,26 +149,23 @@ func TestToUint64(t *testing.T) {
|
||||
input: make(chan int),
|
||||
wantError: ErrType,
|
||||
},
|
||||
{
|
||||
name: "Test point",
|
||||
input: &iPtr,
|
||||
want: 90,
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
input: nil,
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := toUint64(tt.input)
|
||||
if err != tt.wantError {
|
||||
t.Errorf("toUint64(%v) error = %v, wantError %v", tt.input, err, tt.wantError)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("toUint64(%v) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
|
||||
// Reflect
|
||||
got2, err2 := toUint64Reflect(tt.input)
|
||||
if err2 != tt.wantError {
|
||||
t.Errorf("toUint64Reflect(%v) error = %v, wantError %v", tt.input, err2, tt.wantError)
|
||||
}
|
||||
if got2 != tt.want {
|
||||
t.Errorf("toUint64Reflect(%v) = %v, want %v", tt.input, got2, tt.want)
|
||||
}
|
||||
got, err := toUint64Reflect(tt.input)
|
||||
assert.Equal(t, tt.wantError, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -12,10 +12,8 @@ var (
|
||||
ErrNotMap = errors.New("cannot parse map, value is not a map")
|
||||
// ErrNotSlice 不是Slice类型
|
||||
ErrNotSlice = errors.New("cannot parse slice, value is not a slice")
|
||||
// ErrSyntax indicates that a value does not have the right syntax for the target type
|
||||
// 指示值不具有目标类型的正确语法
|
||||
// ErrSyntax 指示值不具有目标类型的正确语法
|
||||
ErrSyntax = strconv.ErrSyntax
|
||||
// ErrType indicates that a value does not have the right syntax for the target type
|
||||
// 指示值不具有目标类型的正确语法
|
||||
// ErrType 指示值不具有目标类型的正确语法
|
||||
ErrType = errors.New("unsupported type")
|
||||
)
|
||||
|
@@ -1,36 +1,76 @@
|
||||
package mapUtil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMapValueExists(t *testing.T) {
|
||||
m := map[string]string{
|
||||
"foo": "bar",
|
||||
"baz": "123",
|
||||
}
|
||||
if !MapValueExists(m, "bar") {
|
||||
t.Errorf("expected MapValueExists to return true for value 'bar'")
|
||||
}
|
||||
if !MapValueExists(m, "123") {
|
||||
t.Errorf("expected MapValueExists to return true for value 123")
|
||||
}
|
||||
if MapValueExists(m, "non-existent") {
|
||||
t.Errorf("expected MapValueExists to return false for value 'non-existent'")
|
||||
}
|
||||
}
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMapKeyExists(t *testing.T) {
|
||||
m := map[string]any{
|
||||
"foo": "bar",
|
||||
"baz": 123,
|
||||
"qux": []int{1, 2, 3},
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputMap map[string]string
|
||||
inputKey string
|
||||
wantRes bool
|
||||
}{
|
||||
{
|
||||
name: "存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputKey: "for",
|
||||
wantRes: true,
|
||||
},
|
||||
{
|
||||
name: "不存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputKey: "tom",
|
||||
wantRes: false,
|
||||
},
|
||||
}
|
||||
if !MapKeyExists(m, "foo") {
|
||||
t.Errorf("expected MapKeyExists to return true for key 'foo'")
|
||||
}
|
||||
if !MapKeyExists(m, "baz") {
|
||||
t.Errorf("expected MapKeyExists to return true for key 'baz'")
|
||||
}
|
||||
if MapKeyExists(m, "non-existent") {
|
||||
t.Errorf("expected MapKeyExists to return false for key 'non-existent'")
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := MapKeyExists(tc.inputMap, tc.inputKey)
|
||||
assert.Equal(t, tc.wantRes, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapValueExists(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputMap map[string]string
|
||||
inputValue string
|
||||
wantRes bool
|
||||
}{
|
||||
{
|
||||
name: "存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputValue: "jack",
|
||||
wantRes: true,
|
||||
},
|
||||
{
|
||||
name: "不存在",
|
||||
inputMap: map[string]string{
|
||||
"for": "jack",
|
||||
"bar": "123",
|
||||
},
|
||||
inputValue: "tom",
|
||||
wantRes: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := MapValueExists(tc.inputMap, tc.inputValue)
|
||||
assert.Equal(t, tc.wantRes, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,48 @@
|
||||
package mathUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAbsInt64(t *testing.T) {
|
||||
var input int64 = -12
|
||||
var expected int64 = 12
|
||||
res := Abs(input)
|
||||
if res != expected {
|
||||
t.Errorf("abs error")
|
||||
testCases := []struct {
|
||||
name string
|
||||
input int64
|
||||
expected int64
|
||||
}{
|
||||
{
|
||||
name: "负数转绝对值",
|
||||
input: -12,
|
||||
expected: 12,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Abs(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsFloat32(t *testing.T) {
|
||||
var input float32 = -12.4
|
||||
var expected float32 = 12.4
|
||||
res := Abs(input)
|
||||
if res != expected {
|
||||
t.Errorf("abs error")
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float32
|
||||
expected float32
|
||||
}{
|
||||
{
|
||||
name: "负数转绝对值",
|
||||
input: -12.4,
|
||||
expected: 12.4,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Abs(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,48 @@
|
||||
package mathUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCeilFloat32(t *testing.T) {
|
||||
var input float32 = -12.4
|
||||
var expected int = -12
|
||||
res := Ceil(input)
|
||||
if res != expected {
|
||||
t.Errorf("ceil error")
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float32
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "负浮点数取整",
|
||||
input: -12.4,
|
||||
expected: -12,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Ceil(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCeilFloat64(t *testing.T) {
|
||||
var input float64 = 12.4
|
||||
var expected int = 13
|
||||
res := Ceil(input)
|
||||
if res != expected {
|
||||
t.Errorf("ceil error")
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float64
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "正浮点数取整",
|
||||
input: 12.4,
|
||||
expected: 13,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Ceil(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,48 @@
|
||||
package mathUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFloorFloat32(t *testing.T) {
|
||||
var input float32 = -12.4
|
||||
var expected int = -13
|
||||
res := Floor(input)
|
||||
if res != expected {
|
||||
t.Errorf("floor error %d", res)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float32
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "负浮点数取整",
|
||||
input: -12.4,
|
||||
expected: -13,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Floor(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloorFloat64(t *testing.T) {
|
||||
var input float64 = 12.4
|
||||
var expected int = 12
|
||||
res := Floor(input)
|
||||
if res != expected {
|
||||
t.Errorf("floor error %d", res)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float64
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "正浮点数取整",
|
||||
input: 12.4,
|
||||
expected: 12,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Floor(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,32 @@
|
||||
package mathUtil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMaxEmpty(t *testing.T) {
|
||||
s := []int{}
|
||||
var expected int = 0
|
||||
res := Max(s)
|
||||
if res != expected {
|
||||
t.Errorf("max error %d", res)
|
||||
}
|
||||
}
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
s := []int{3, 7, 1, 9, 3, 0, 2, 2}
|
||||
var expected int = 9
|
||||
res := Max(s)
|
||||
if res != expected {
|
||||
t.Errorf("max error %d", res)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input []int
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "空切片",
|
||||
input: []int{},
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "普通切片",
|
||||
input: []int{3, 7, 1, 9, 3, 0, 2, 2},
|
||||
expected: 9,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Max(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,32 @@
|
||||
package mathUtil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMinEmpty(t *testing.T) {
|
||||
s := []int{}
|
||||
var expected int = 0
|
||||
res := Min(s)
|
||||
if res != expected {
|
||||
t.Errorf("min error %d", res)
|
||||
}
|
||||
}
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMin(t *testing.T) {
|
||||
s := []int{3, 7, 1, 9, 3, 0, 2, 2}
|
||||
var expected int = 0
|
||||
res := Min(s)
|
||||
if res != expected {
|
||||
t.Errorf("min error %d", res)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input []int
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "空切片",
|
||||
input: []int{},
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "普通切片",
|
||||
input: []int{3, 7, 1, 9, 3, 0, 2, 2},
|
||||
expected: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Min(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -3,12 +3,26 @@ package mathUtil
|
||||
import "testing"
|
||||
|
||||
func TestRand(t *testing.T) {
|
||||
minNum := 1
|
||||
maxNum := 100
|
||||
for i := 0; i < 1000; i++ {
|
||||
num := Rand(minNum, maxNum)
|
||||
if num < minNum || num > maxNum {
|
||||
t.Errorf("randRange() returned %d, which is outside the range of [%d, %d]", num, minNum, maxNum)
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
minNum int
|
||||
maxNum int
|
||||
}{
|
||||
{
|
||||
name: "测试随机数在范围内",
|
||||
minNum: 1,
|
||||
maxNum: 100,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
for i := 0; i < 1000; i++ {
|
||||
num := Rand(tc.minNum, tc.maxNum)
|
||||
if num < tc.minNum || num > tc.maxNum {
|
||||
t.Errorf("Rand(%d, %d) returned %d, which is outside the range of [%d, %d]", tc.minNum, tc.maxNum, num, tc.minNum, tc.maxNum)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,48 @@
|
||||
package mathUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRoundFloat32(t *testing.T) {
|
||||
var input float32 = -12.4
|
||||
var expected int = -12
|
||||
res := Round(input)
|
||||
if res != expected {
|
||||
t.Errorf("Round error %d", res)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float32
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "负数四舍五入",
|
||||
input: -12.4,
|
||||
expected: -12,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Round(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundFloat64(t *testing.T) {
|
||||
var input float64 = 12.5
|
||||
var expected int = 13
|
||||
res := Round(input)
|
||||
if res != expected {
|
||||
t.Errorf("Round error %d", res)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input float64
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "正数四舍五入",
|
||||
input: 12.5,
|
||||
expected: 13,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := Round(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,156 +1,271 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChunk(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []any
|
||||
size int
|
||||
want [][]any
|
||||
}{
|
||||
{
|
||||
name: "empty slice",
|
||||
slice: []any{},
|
||||
size: 3,
|
||||
want: [][]any{},
|
||||
},
|
||||
{
|
||||
name: "slice with less than chunk size",
|
||||
name: "切片小于块大小",
|
||||
slice: []any{1, 2},
|
||||
size: 3,
|
||||
want: [][]any{{1, 2}},
|
||||
},
|
||||
{
|
||||
name: "slice with exact chunk size",
|
||||
name: "切片等于块大小",
|
||||
slice: []any{1, 2, 3, 4, 5, 6},
|
||||
size: 3,
|
||||
want: [][]any{{1, 2, 3}, {4, 5, 6}},
|
||||
},
|
||||
{
|
||||
name: "slice with more than chunk size",
|
||||
name: "切片大于块大小",
|
||||
slice: []any{1, 2, 3, 4, 5, 6, 7},
|
||||
size: 3,
|
||||
want: [][]any{{1, 2, 3}, {4, 5, 6}, {7}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ChunkSlice(tt.slice, tt.size)
|
||||
if len(got) != len(tt.want) {
|
||||
t.Errorf("Chunk() = %+v, want %+v", got, tt.want)
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if !reflect.DeepEqual(got[i], tt.want[i]) {
|
||||
t.Errorf("Chunk() = %+v, want %+v", got, tt.want)
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkStr(t *testing.T) {
|
||||
slice := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
|
||||
size := 3
|
||||
expected := [][]string{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k"}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkStr(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []string
|
||||
size int
|
||||
expected [][]string
|
||||
}{
|
||||
{
|
||||
name: "切片大小为3",
|
||||
slice: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"},
|
||||
size: 3,
|
||||
expected: [][]string{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k"}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkInt(t *testing.T) {
|
||||
slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkInt(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int
|
||||
size int
|
||||
expected [][]int
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkInt8(t *testing.T) {
|
||||
slice := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]int8{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkInt8(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int8
|
||||
size int
|
||||
expected [][]int8
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []int8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]int8{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkInt32(t *testing.T) {
|
||||
slice := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]int32{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkInt32(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int32
|
||||
size int
|
||||
expected [][]int32
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]int32{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkInt64(t *testing.T) {
|
||||
slice := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]int64{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkInt64(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int64
|
||||
size int
|
||||
expected [][]int64
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]int64{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkUint(t *testing.T) {
|
||||
slice := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]uint{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkUint(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint
|
||||
size int
|
||||
expected [][]uint
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]uint{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkUint8(t *testing.T) {
|
||||
slice := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]uint8{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkUint8(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint8
|
||||
size int
|
||||
expected [][]uint8
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]uint8{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkUint16(t *testing.T) {
|
||||
slice := []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]uint16{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkUint16(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint16
|
||||
size int
|
||||
expected [][]uint16
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]uint16{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkUint32(t *testing.T) {
|
||||
slice := []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]uint32{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkUint32(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint32
|
||||
size int
|
||||
expected [][]uint32
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]uint32{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkUint64(t *testing.T) {
|
||||
slice := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
size := 4
|
||||
expected := [][]uint64{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}}
|
||||
result := ChunkSlice(slice, size)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("ChunkUint64(%v, %d) = %v; expected %v", slice, size, result, expected)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint64
|
||||
size int
|
||||
expected [][]uint64
|
||||
}{
|
||||
{
|
||||
name: "切片大小为4",
|
||||
slice: []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
size: 4,
|
||||
expected: [][]uint64{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ChunkSlice(tc.slice, tc.size)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,26 +1,10 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func TestColumnSliceDemo(t *testing.T) {
|
||||
// type Person struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// }
|
||||
// people := []Person{
|
||||
// {"Alice", 18},
|
||||
// {"Bob", 20},
|
||||
// {"Charlie", 22},
|
||||
// }
|
||||
//
|
||||
// // 获取年龄列
|
||||
// ages := ColumnSlice(people, "Age")
|
||||
// fmt.Println(ages) // 输出:[18 20 22]
|
||||
//}
|
||||
|
||||
func TestColumnSlice(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string
|
||||
@@ -28,31 +12,29 @@ func TestColumnSlice(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input []Person
|
||||
column string
|
||||
output []any
|
||||
name string
|
||||
input []Person
|
||||
inputColumn string
|
||||
want []any
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
column: "Age",
|
||||
output: []any{18, 20, 22},
|
||||
name: "success",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
inputColumn: "Age",
|
||||
want: []any{18, 20, 22},
|
||||
},
|
||||
{
|
||||
name: "column not found",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
column: "Gender",
|
||||
output: []any{},
|
||||
name: "column not found",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
inputColumn: "Gender",
|
||||
want: []any{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
output := ColumnSlice(tc.input, tc.column)
|
||||
if !reflect.DeepEqual(output, tc.output) {
|
||||
t.Errorf("expected %v, but got %v", tc.output, output)
|
||||
}
|
||||
res := ColumnSlice(tc.input, tc.inputColumn)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -5,48 +5,38 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractKeysInline(t *testing.T) {
|
||||
// 使用匿名结构体定义测试数据
|
||||
persons := []struct {
|
||||
func TestExtractKeys(t *testing.T) {
|
||||
type person struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputSlices []person
|
||||
inputFunc func(p person) int
|
||||
want []int
|
||||
}{
|
||||
{1, "Alice"},
|
||||
{2, "Bob"},
|
||||
{3, "Charlie"},
|
||||
{
|
||||
name: "获取ID",
|
||||
inputSlices: []person{
|
||||
{1, "Alice"},
|
||||
{2, "Bob"},
|
||||
{3, "Charlie"},
|
||||
},
|
||||
inputFunc: func(p person) int {
|
||||
return p.ID
|
||||
},
|
||||
want: []int{1, 2, 3},
|
||||
},
|
||||
}
|
||||
|
||||
// 使用函数字面量定义 keyFunc 函数
|
||||
keys := ExtractKeys(persons, func(p struct {
|
||||
ID int
|
||||
Name string
|
||||
}) int {
|
||||
return p.ID
|
||||
})
|
||||
|
||||
expectedKeys := []int{1, 2, 3}
|
||||
if !reflect.DeepEqual(keys, expectedKeys) {
|
||||
t.Errorf("Expected keys %v, but got %v", expectedKeys, keys)
|
||||
}
|
||||
|
||||
// 另一个例子:使用不同的结构体和 keyFunc 函数
|
||||
animals := []struct {
|
||||
ID string
|
||||
Name string
|
||||
}{
|
||||
{"cat001", "Cat"},
|
||||
{"dog002", "Dog"},
|
||||
}
|
||||
|
||||
animalKeys := ExtractKeys(animals, func(a struct {
|
||||
ID string
|
||||
Name string
|
||||
}) string {
|
||||
return a.Name
|
||||
})
|
||||
|
||||
expectedAnimalKeys := []string{"Cat", "Dog"}
|
||||
if !reflect.DeepEqual(animalKeys, expectedAnimalKeys) {
|
||||
t.Errorf("Expected animal keys %v, but got %v", expectedAnimalKeys, animalKeys)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := ExtractKeys(tc.inputSlices, tc.inputFunc)
|
||||
if !reflect.DeepEqual(tc.want, res) {
|
||||
t.Errorf("Expected keys %v, but got %v", tc.want, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,128 +1,331 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInStrSlices(t *testing.T) {
|
||||
slices := []string{"apple", "banana", "cherry"}
|
||||
if !InSlice("apple", slices) {
|
||||
t.Errorf("apple should be in slices %v", slices)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []string
|
||||
element string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "apple in slice",
|
||||
slice: []string{"apple", "banana", "cherry"},
|
||||
element: "apple",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "banana in slice",
|
||||
slice: []string{"apple", "banana", "cherry"},
|
||||
element: "banana",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "orange not in slice",
|
||||
slice: []string{"apple", "banana", "cherry"},
|
||||
element: "orange",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
if !InSlice("banana", slices) {
|
||||
t.Errorf("banana should be in slices %v", slices)
|
||||
}
|
||||
if InSlice("orange", slices) {
|
||||
t.Errorf("orange should not be in slices %v", slices)
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInIntSlices(t *testing.T) {
|
||||
slices := []int{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int
|
||||
element int
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []int{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []int{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInInt8Slices(t *testing.T) {
|
||||
slices := []int8{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int8
|
||||
element int8
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []int8{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []int8{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInInt16Slices(t *testing.T) {
|
||||
slices := []int16{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int16
|
||||
element int16
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []int16{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []int16{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInInt32Slices(t *testing.T) {
|
||||
slices := []int32{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int32
|
||||
element int32
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []int32{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []int32{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInInt64Slices(t *testing.T) {
|
||||
slices := []int64{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []int64
|
||||
element int64
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []int64{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []int64{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInUintSlices(t *testing.T) {
|
||||
slices := []uint{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint
|
||||
element uint
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []uint{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []uint{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInUint8Slices(t *testing.T) {
|
||||
slices := []uint8{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint8
|
||||
element uint8
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []uint8{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []uint8{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInUint16Slices(t *testing.T) {
|
||||
slices := []uint16{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint16
|
||||
element uint16
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []uint16{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []uint16{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInUint32Slices(t *testing.T) {
|
||||
slices := []uint32{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint32
|
||||
element uint32
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []uint32{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []uint32{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInUint64Slices(t *testing.T) {
|
||||
slices := []uint64{1, 2, 3, 4, 5}
|
||||
if !InSlice(3, slices) {
|
||||
t.Errorf("Expected true, but got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
slice []uint64
|
||||
element uint64
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "3 in slice",
|
||||
slice: []uint64{1, 2, 3, 4, 5},
|
||||
element: 3,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "6 not in slice",
|
||||
slice: []uint64{1, 2, 3, 4, 5},
|
||||
element: 6,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
if InSlice(6, slices) {
|
||||
t.Errorf("Expected false, but got true")
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := InSlice(tc.element, tc.slice)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +1,50 @@
|
||||
package sliceUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsSlice(t *testing.T) {
|
||||
var tests = []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
want bool
|
||||
}{
|
||||
{[]int{1, 1, 3}, true},
|
||||
{[]any{1, 2, "a"}, true},
|
||||
{[]map[string]any{
|
||||
{"1": 1},
|
||||
{"c": 89},
|
||||
}, true},
|
||||
{"1234", false},
|
||||
{make(chan int), false},
|
||||
{
|
||||
name: "slice of int",
|
||||
input: []int{1, 1, 3},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "slice of any",
|
||||
input: []any{1, 2, "a"},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "slice of map",
|
||||
input: []map[string]any{
|
||||
{"1": 1},
|
||||
{"c": 89},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
input: "1234",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "channel",
|
||||
input: make(chan int),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if got := IsSlice(test.input); got != test.want {
|
||||
t.Errorf("IsSlice(%v) = %v; want %v", test.input, got, test.want)
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := IsSlice(tc.input)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,160 +1,305 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 测试MergeSlices
|
||||
func TestMergeSlices(t *testing.T) {
|
||||
slice1 := []any{1, 2, 3}
|
||||
slice2 := []any{4, 5, 6}
|
||||
slice3 := []any{7, 8, 9}
|
||||
expected := []any{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]any
|
||||
expected []any
|
||||
}{
|
||||
{
|
||||
name: "merge multiple slices",
|
||||
slices: [][]any{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 测试MergeStrSlices
|
||||
func TestMergeStrSlices(t *testing.T) {
|
||||
slice1 := []string{"a", "b", "c"}
|
||||
slice2 := []string{"d", "e", "f"}
|
||||
slice3 := []string{"g", "h", "i"}
|
||||
expected := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"}
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "merge multiple string slices",
|
||||
slices: [][]string{
|
||||
{"a", "b", "c"},
|
||||
{"d", "e", "f"},
|
||||
{"g", "h", "i"},
|
||||
},
|
||||
expected: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeIntSlices(t *testing.T) {
|
||||
slice1 := []int{1, 2, 3}
|
||||
slice2 := []int{4, 5, 6}
|
||||
slice3 := []int{7, 8, 9}
|
||||
expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]int
|
||||
expected []int
|
||||
}{
|
||||
{
|
||||
name: "merge multiple int slices",
|
||||
slices: [][]int{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeInt8Slices(t *testing.T) {
|
||||
slice1 := []int8{1, 2, 3}
|
||||
slice2 := []int8{4, 5, 6}
|
||||
slice3 := []int8{7, 8, 9}
|
||||
expected := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]int8
|
||||
expected []int8
|
||||
}{
|
||||
{
|
||||
name: "merge multiple int8 slices",
|
||||
slices: [][]int8{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []int8{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeInt16Slices(t *testing.T) {
|
||||
slice1 := []int16{1, 2, 3}
|
||||
slice2 := []int16{4, 5, 6}
|
||||
slice3 := []int16{7, 8, 9}
|
||||
expected := []int16{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]int16
|
||||
expected []int16
|
||||
}{
|
||||
{
|
||||
name: "merge multiple int16 slices",
|
||||
slices: [][]int16{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []int16{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeInt32Slices(t *testing.T) {
|
||||
slice1 := []int32{1, 2, 3}
|
||||
slice2 := []int32{4, 5, 6}
|
||||
slice3 := []int32{7, 8, 9}
|
||||
expected := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]int32
|
||||
expected []int32
|
||||
}{
|
||||
{
|
||||
name: "merge multiple int32 slices",
|
||||
slices: [][]int32{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []int32{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeInt64Slices(t *testing.T) {
|
||||
slice1 := []int64{1, 2, 3}
|
||||
slice2 := []int64{4, 5, 6}
|
||||
slice3 := []int64{7, 8, 9}
|
||||
expected := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]int64
|
||||
expected []int64
|
||||
}{
|
||||
{
|
||||
name: "merge multiple int64 slices",
|
||||
slices: [][]int64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeUintSlices(t *testing.T) {
|
||||
slice1 := []uint{1, 2, 3}
|
||||
slice2 := []uint{4, 5, 6}
|
||||
slice3 := []uint{7, 8, 9}
|
||||
expected := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]uint
|
||||
expected []uint
|
||||
}{
|
||||
{
|
||||
name: "merge multiple uint slices",
|
||||
slices: [][]uint{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []uint{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeUint8Slices(t *testing.T) {
|
||||
slice1 := []uint8{1, 2, 3}
|
||||
slice2 := []uint8{4, 5, 6}
|
||||
slice3 := []uint8{7, 8, 9}
|
||||
expected := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]uint8
|
||||
expected []uint8
|
||||
}{
|
||||
{
|
||||
name: "merge multiple uint8 slices",
|
||||
slices: [][]uint8{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeUint16Slices(t *testing.T) {
|
||||
slice1 := []uint16{1, 2, 3}
|
||||
slice2 := []uint16{4, 5, 6}
|
||||
slice3 := []uint16{7, 8, 9}
|
||||
expected := []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]uint16
|
||||
expected []uint16
|
||||
}{
|
||||
{
|
||||
name: "merge multiple uint16 slices",
|
||||
slices: [][]uint16{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeUint32Slices(t *testing.T) {
|
||||
slice1 := []uint32{1, 2, 3}
|
||||
slice2 := []uint32{4, 5, 6}
|
||||
slice3 := []uint32{7, 8, 9}
|
||||
expected := []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]uint32
|
||||
expected []uint32
|
||||
}{
|
||||
{
|
||||
name: "merge multiple uint32 slices",
|
||||
slices: [][]uint32{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeUint64Slices(t *testing.T) {
|
||||
slice1 := []uint64{1, 2, 3}
|
||||
slice2 := []uint64{4, 5, 6}
|
||||
slice3 := []uint64{7, 8, 9}
|
||||
expected := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
testCases := []struct {
|
||||
name string
|
||||
slices [][]uint64
|
||||
expected []uint64
|
||||
}{
|
||||
{
|
||||
name: "merge multiple uint64 slices",
|
||||
slices: [][]uint64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
},
|
||||
expected: []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
||||
},
|
||||
}
|
||||
|
||||
result := MergeSlice(slice1, slice2, slice3)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v but got %v", expected, result)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := MergeSlice(tc.slices...)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -6,57 +6,41 @@ import (
|
||||
)
|
||||
|
||||
func TestSliceToMap(t *testing.T) {
|
||||
// Test case 1: Test with a slice of Person structs
|
||||
type Person struct {
|
||||
type person struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
people := []Person{
|
||||
{ID: 1, Name: "Alice"},
|
||||
{ID: 2, Name: "Bob"},
|
||||
{ID: 3, Name: "Charlie"},
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputSlices []person
|
||||
inputFunc func(p person) int
|
||||
want map[int]person
|
||||
}{
|
||||
{
|
||||
name: "以id为map的key",
|
||||
inputSlices: []person{
|
||||
{ID: 1, Name: "Alice"},
|
||||
{ID: 2, Name: "Bob"},
|
||||
{ID: 3, Name: "Charlie"},
|
||||
},
|
||||
inputFunc: func(p person) int {
|
||||
return p.ID
|
||||
},
|
||||
want: map[int]person{
|
||||
1: {ID: 1, Name: "Alice"},
|
||||
2: {ID: 2, Name: "Bob"},
|
||||
3: {ID: 3, Name: "Charlie"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
keyFunc := func(p Person) int {
|
||||
return p.ID
|
||||
}
|
||||
|
||||
expected := map[int]Person{
|
||||
1: {ID: 1, Name: "Alice"},
|
||||
2: {ID: 2, Name: "Bob"},
|
||||
3: {ID: 3, Name: "Charlie"},
|
||||
}
|
||||
|
||||
result := SliceToMap(people, keyFunc)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Test case 1 failed. Expected %v, got %v", expected, result)
|
||||
}
|
||||
|
||||
// Test case 2: Test with an empty slice
|
||||
emptySlice := []Person{}
|
||||
expectedEmpty := map[int]Person{}
|
||||
|
||||
resultEmpty := SliceToMap(emptySlice, keyFunc)
|
||||
|
||||
if !reflect.DeepEqual(resultEmpty, expectedEmpty) {
|
||||
t.Errorf("Test case 2 failed. Expected %v, got %v", expectedEmpty, resultEmpty)
|
||||
}
|
||||
|
||||
// Test case 3: Test with a slice containing elements with the same key
|
||||
duplicatePeople := []Person{
|
||||
{ID: 1, Name: "Alice"},
|
||||
{ID: 1, Name: "Bob"},
|
||||
}
|
||||
|
||||
expectedDuplicate := map[int]Person{
|
||||
1: {ID: 1, Name: "Bob"}, // The last occurrence of the duplicate key will overwrite the previous one
|
||||
}
|
||||
|
||||
resultDuplicate := SliceToMap(duplicatePeople, keyFunc)
|
||||
|
||||
if !reflect.DeepEqual(resultDuplicate, expectedDuplicate) {
|
||||
t.Errorf("Test case 3 failed. Expected %v, got %v", expectedDuplicate, resultDuplicate)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := SliceToMap(tc.inputSlices, tc.inputFunc)
|
||||
if !reflect.DeepEqual(tc.want, res) {
|
||||
t.Errorf("failed. Expected %v, got %v", tc.want, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -25,14 +26,27 @@ func TestSumIntSlice(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4, 5},
|
||||
expected: 15,
|
||||
},
|
||||
{
|
||||
name: "Negative Numbers",
|
||||
input: []int{-1, -2, -3},
|
||||
expected: -6,
|
||||
},
|
||||
{
|
||||
name: "Mix of Positive and Negative",
|
||||
input: []int{1, -2, 3, -4, 5},
|
||||
expected: 3,
|
||||
},
|
||||
{
|
||||
name: "Large Numbers",
|
||||
input: []int{1000000000, 2000000000, 3000000000},
|
||||
expected: 6000000000,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := SumSlice(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("Expected %d but got %d", tc.expected, result)
|
||||
}
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -63,9 +77,7 @@ func TestSumInt8Slice(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := SumSlice(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("Expected %d but got %d", tc.expected, result)
|
||||
}
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -82,12 +94,10 @@ func TestSumInt16Slice(t *testing.T) {
|
||||
{"multiple element slice", []int16{1, 2, 3}, 6},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := SumSlice(tt.slice)
|
||||
if got != tt.want {
|
||||
t.Errorf("SumInt16Slice(%v) = %v, want %v", tt.slice, got, tt.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := SumSlice(tc.slice)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -106,10 +116,8 @@ func TestSumInt32Slice(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := SumSlice(tt.slice)
|
||||
if got != tt.want {
|
||||
t.Errorf("SumInt32Slice(%v) = %v, want %v", tt.slice, got, tt.want)
|
||||
}
|
||||
res := SumSlice(tt.slice)
|
||||
assert.Equal(t, tt.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -128,10 +136,8 @@ func TestSumInt64Slice(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := SumSlice(tt.slice)
|
||||
if got != tt.want {
|
||||
t.Errorf("SumInt64Slice(%v) = %v, want %v", tt.slice, got, tt.want)
|
||||
}
|
||||
res := SumSlice(tt.slice)
|
||||
assert.Equal(t, tt.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -150,10 +156,8 @@ func TestSumFloat32Slice(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := SumSlice(tt.slice)
|
||||
if got != tt.want {
|
||||
t.Errorf("SumFloat32Slice(%v) = %v, want %v", tt.slice, got, tt.want)
|
||||
}
|
||||
res := SumSlice(tt.slice)
|
||||
assert.Equal(t, tt.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -172,10 +176,8 @@ func TestSumFloat64Slice(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := SumSlice(tt.slice)
|
||||
if got != tt.want {
|
||||
t.Errorf("SumFloat64Slice(%v) = %v, want %v", tt.slice, got, tt.want)
|
||||
}
|
||||
res := SumSlice(tt.slice)
|
||||
assert.Equal(t, tt.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,39 +1,47 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUniqueSlice(t *testing.T) {
|
||||
// 测试整型切片去重
|
||||
s1 := []int{1, 2, 3, 2, 4, 4, 5}
|
||||
s1 = UniqueSlice(s1)
|
||||
if !reflect.DeepEqual(s1, []int{1, 2, 3, 4, 5}) {
|
||||
t.Errorf("UniqueSlice(%v) = %v, expected %v", s1, s1, []int{1, 2, 3, 4, 5})
|
||||
func TestUniqueSliceString(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
in []string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "字符串切片去重",
|
||||
in: []string{"a", "b", "c", "b", "d", "d", "e"},
|
||||
want: []string{"a", "b", "c", "d", "e"},
|
||||
},
|
||||
}
|
||||
|
||||
// 测试字符串切片去重
|
||||
s2 := []string{"a", "b", "c", "b", "d", "d", "e"}
|
||||
s2 = UniqueSlice(s2)
|
||||
if !reflect.DeepEqual(s2, []string{"a", "b", "c", "d", "e"}) {
|
||||
t.Errorf("UniqueSlice(%v) = %v, expected %v", s2, s2, []string{"a", "b", "c", "d", "e"})
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := UniqueSlice(tc.in)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniqueSliceInt(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, want []int
|
||||
testCases := []struct {
|
||||
name string
|
||||
in []int
|
||||
want []int
|
||||
}{
|
||||
{[]int{1, 2, 3, 2, 1}, []int{1, 2, 3}},
|
||||
{[]int{1, 1, 1, 1}, []int{1}},
|
||||
{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
|
||||
{[]int{}, []int{}},
|
||||
{name: "重复元素", in: []int{1, 2, 3, 2, 1}, want: []int{1, 2, 3}},
|
||||
{name: "所有元素相同", in: []int{1, 1, 1, 1}, want: []int{1}},
|
||||
{name: "无重复元素", in: []int{1, 2, 3, 4}, want: []int{1, 2, 3, 4}},
|
||||
{name: "空切片", in: []int{}, want: []int{}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := UniqueSlice(c.in)
|
||||
if !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("UniqueSliceInt(%v) == %v, want %v", c.in, got, c.want)
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := UniqueSlice(tc.in)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,23 @@
|
||||
package strUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStrToBytes(t *testing.T) {
|
||||
expected := []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f}
|
||||
result := StrToBytes("hello")
|
||||
if !bytes.Equal(result, expected) {
|
||||
t.Errorf("StrToBytes test failed, expected %v but got %v", expected, result)
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []byte
|
||||
}{
|
||||
{"转换hello", "hello", []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f}},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToBytes(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,131 +1,112 @@
|
||||
package strUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStrToInt(t *testing.T) {
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected int
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
{"零", "0", 0},
|
||||
{"正数", "1", 1},
|
||||
{"负数", "-1", -1},
|
||||
{"无效输入", "invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToInt(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt8(t *testing.T) {
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected int8
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
{"零", "0", 0},
|
||||
{"正数", "1", 1},
|
||||
{"负数", "-1", -1},
|
||||
{"无效输入", "invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt8(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt8(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToInt8(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt16(t *testing.T) {
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected int16
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
{"零", "0", 0},
|
||||
{"正数", "1", 1},
|
||||
{"负数", "-1", -1},
|
||||
{"无效输入", "invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt16(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt16(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToInt16(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt32(t *testing.T) {
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected int32
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"invalid", 0},
|
||||
{"零", "0", 0},
|
||||
{"正数", "1", 1},
|
||||
{"负数", "-1", -1},
|
||||
{"无效输入", "invalid", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt32(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt32(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToInt32(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt64(t *testing.T) {
|
||||
// Test cases
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected int64
|
||||
}{
|
||||
{"0", 0},
|
||||
{"1", 1},
|
||||
{"-1", -1},
|
||||
{"9223372036854775807", 9223372036854775807},
|
||||
{"-9223372036854775808", -9223372036854775808},
|
||||
{"2147483647", 2147483647},
|
||||
{"-2147483648", -2147483648},
|
||||
{"invalid", 0},
|
||||
{"9223372036854775808", 0},
|
||||
{"-9223372036854775809", 0},
|
||||
{"零", "0", 0},
|
||||
{"正数", "1", 1},
|
||||
{"负数", "-1", -1},
|
||||
{"最大正数", "9223372036854775807", 9223372036854775807},
|
||||
{"最小负数", "-9223372036854775808", -9223372036854775808},
|
||||
{"32位最大正数", "2147483647", 2147483647},
|
||||
{"32位最小负数", "-2147483648", -2147483648},
|
||||
{"无效输入", "invalid", 0},
|
||||
{"超出最大值", "9223372036854775808", 0},
|
||||
{"超出最小值", "-9223372036854775809", 0},
|
||||
}
|
||||
|
||||
// Test loop
|
||||
for _, tc := range testCases {
|
||||
// Call the function
|
||||
got := StrToInt64(tc.input)
|
||||
|
||||
// Check the result
|
||||
if got != tc.expected {
|
||||
t.Errorf("Unexpected result: StrToInt64(%v) = %v, expected %v", tc.input, got, tc.expected)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToInt64(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,98 +1,106 @@
|
||||
package strUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStrToUint(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected uint
|
||||
}{
|
||||
{"123", 123},
|
||||
{"0", 0},
|
||||
{"-1", 0},
|
||||
{"正数", "123", 123},
|
||||
{"零", "0", 0},
|
||||
{"负数", "-1", 0},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := StrToUint(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("StrToUint(%s) = %d; expected %d", test.input, result, test.expected)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToUint(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint8(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected uint8
|
||||
}{
|
||||
{"123", 123},
|
||||
{"0", 0},
|
||||
{"-1", 0},
|
||||
{"256", 0},
|
||||
{"正常值", "123", 123},
|
||||
{"零", "0", 0},
|
||||
{"负值", "-1", 0},
|
||||
{"超出范围", "256", 0},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := StrToUint8(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("StrToUint8(%s) = %d; expected %d", test.input, result, test.expected)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToUint8(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint16(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected uint16
|
||||
}{
|
||||
{"12345", 12345},
|
||||
{"0", 0},
|
||||
{"-1", 0},
|
||||
{"65536", 0},
|
||||
{"正常值", "12345", 12345},
|
||||
{"零", "0", 0},
|
||||
{"负值", "-1", 0},
|
||||
{"超出范围", "65536", 0},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := StrToUint16(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("StrToUint16(%s) = %d; expected %d", test.input, result, test.expected)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToUint16(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint32(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected uint32
|
||||
}{
|
||||
{"1234567", 1234567},
|
||||
{"0", 0},
|
||||
{"-1", 0},
|
||||
{"4294967296", 0},
|
||||
{"正常值", "1234567", 1234567},
|
||||
{"零", "0", 0},
|
||||
{"负值", "-1", 0},
|
||||
{"超出范围", "4294967296", 0},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := StrToUint32(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("StrToUint32(%s) = %d; expected %d", test.input, result, test.expected)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToUint32(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint64(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expectedOutput uint64
|
||||
}{
|
||||
{"12345", 12345},
|
||||
{"0", 0},
|
||||
{"18446744073709551615", 18446744073709551615},
|
||||
{"-1", 0},
|
||||
{"not a number", 0},
|
||||
{"正常值", "12345", 12345},
|
||||
{"零", "0", 0},
|
||||
{"最大值", "18446744073709551615", 18446744073709551615},
|
||||
{"负值", "-1", 0},
|
||||
{"非数字", "not a number", 0},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
output := StrToUint64(tc.input)
|
||||
if output != tc.expectedOutput {
|
||||
t.Errorf("StrToUint64(%q) = %d; want %d", tc.input, output, tc.expectedOutput)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := StrToUint64(tc.input)
|
||||
assert.Equal(t, tc.expectedOutput, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,25 +1,29 @@
|
||||
package validUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsEmail(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"test@example.com", true},
|
||||
{"abc@def.com", true},
|
||||
{"123@456.com", true},
|
||||
{"test@.com", false},
|
||||
{"test@com", false},
|
||||
{"test@example", false},
|
||||
{"合法邮箱", "test@example.com", true},
|
||||
{"合法邮箱", "abc@def.com", true},
|
||||
{"合法邮箱", "123@456.com", true},
|
||||
{"非法邮箱", "test@.com", false},
|
||||
{"非法邮箱", "test@com", false},
|
||||
{"非法邮箱", "test@example", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := IsEmail(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("IsEmail(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsEmail(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,9 +39,8 @@ func TestIsJSON(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if result := IsJSON(tc.input); result != tc.expected {
|
||||
t.Errorf("expected %v, but got %v", tc.expected, result)
|
||||
}
|
||||
res := IsJSON(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -58,11 +61,10 @@ func TestIsQQ(t *testing.T) {
|
||||
{"test8", "a1234567", false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if output := IsQQ(test.input); output != test.expected {
|
||||
t.Errorf("Input: %s, Expected: %v, Output: %v", test.input, test.expected, output)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsQQ(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -82,11 +84,10 @@ func TestIsWeChat(t *testing.T) {
|
||||
{"test7", "a-b-c-d-e-f-g-h-i-j", true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if output := IsWeChat(test.input); output != test.expected {
|
||||
t.Errorf("Input: %s, Expected: %v, Output: %v", test.input, test.expected, output)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsWeChat(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -108,42 +109,42 @@ func TestIsWeibo(t *testing.T) {
|
||||
{"test9", "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_", false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if output := IsWeibo(test.input); output != test.expected {
|
||||
t.Errorf("Input: %s, Expected: %v, Output: %v", test.input, test.expected, output)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsWeibo(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPassword(t *testing.T) {
|
||||
testCases := []struct {
|
||||
password string
|
||||
name string
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"", false},
|
||||
{"aBc12", false},
|
||||
{"abcdef", false},
|
||||
{"123456", false},
|
||||
{"ab@1", false},
|
||||
{"abcdefg", false},
|
||||
{"ab1", false},
|
||||
{"aBc12#", true},
|
||||
{"Abc123!@#", true},
|
||||
{"1234567Abcdefghijk#", true},
|
||||
{"_abc_123", true},
|
||||
{"_abc_123!", true},
|
||||
{"!@#$%^&*()", false},
|
||||
{"!@#$%^&*()Aa123", true},
|
||||
{"12345678901234567890", false},
|
||||
{"1234567890123456789A", false},
|
||||
{"空密码", "", false},
|
||||
{"短混合", "aBc12", false},
|
||||
{"全小写", "abcdef", false},
|
||||
{"全数字", "123456", false},
|
||||
{"短特殊", "ab@1", false},
|
||||
{"仅字母", "abcdefg", false},
|
||||
{"仅数字", "ab1", false},
|
||||
{"有效短", "aBc12#", true},
|
||||
{"有效长", "Abc123!@#", true},
|
||||
{"长有效", "1234567Abcdefghijk#", true},
|
||||
{"下划线数", "_abc_123", true},
|
||||
{"下划线特", "_abc_123!", true},
|
||||
{"全特殊", "!@#$%^&*()", false},
|
||||
{"特大小写", "!@#$%^&*()Aa123", true},
|
||||
{"超长无大", "12345678901234567890", false},
|
||||
{"超长混合", "1234567890123456789A", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := IsPassword(tc.password)
|
||||
if result != tc.expected {
|
||||
t.Errorf("Expected %t for password '%s', but got %t", tc.expected, tc.password, result)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsPassword(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,50 +1,35 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 测试IsBankCardNo函数
|
||||
func TestIsBankCardNo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cardNo string
|
||||
wantRes bool
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"empty cardNo", "", false},
|
||||
{"less than 16 digits", "123456789012345", false},
|
||||
{"more than 19 digits", "12345678901234567890", false},
|
||||
{"contains non-numeric characters", "1234567a890123456", false},
|
||||
{"valid cardNo with 16 digits", "6222020123456789", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := IsBankCardNo(tt.cardNo)
|
||||
if got != tt.wantRes {
|
||||
t.Errorf("IsBankCardNo(%s) = %t, want %t", tt.cardNo, got, tt.wantRes)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
cardNum string
|
||||
expected bool
|
||||
}{
|
||||
{"case 1", "1234567890123456", false},
|
||||
{"case 2", "6217000010081698261", true},
|
||||
{"case 3", "6227000010081698261", false},
|
||||
{"case 4", "621700001008169826", false},
|
||||
{"case 5", "62170000100816982612", false},
|
||||
|
||||
{"存在字母的银行卡号", "A217000010081698261", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := IsBankCardNo(tc.cardNum)
|
||||
if result != tc.expected {
|
||||
t.Errorf("expected %v, but got %v", tc.expected, result)
|
||||
}
|
||||
res := IsBankCardNo(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,71 +1,82 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsTime(t *testing.T) {
|
||||
if !IsTime("12:34:56") {
|
||||
t.Error("Expected true, got false")
|
||||
}
|
||||
if IsTime("12:34") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsTime("25:34:56") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsTime("12:60:56") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsTime("12:34:60") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDateDemo(t *testing.T) {
|
||||
fmt.Println(IsDate("2022-04-01"))
|
||||
}
|
||||
|
||||
func TestIsDate(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"2021-01-01", true},
|
||||
{"2021/01/01", false},
|
||||
{"2021-20-01", false},
|
||||
{"2021-02-29", false},
|
||||
{"2020-02-29", true},
|
||||
{"2020-02-25", true},
|
||||
{"2023-04-30", true},
|
||||
{"合法日期", "2021-01-01", true},
|
||||
{"非法分隔符", "2021/01/01", false},
|
||||
{"非法月份", "2021-20-01", false},
|
||||
{"非闰年2月29", "2021-02-29", false},
|
||||
{"闰年2月29", "2020-02-29", true},
|
||||
{"合法2月25", "2020-02-25", true},
|
||||
{"合法4月30", "2023-04-30", true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
result := IsDate(testCase.input)
|
||||
if result != testCase.expected {
|
||||
t.Errorf("IsDate(%v) = %v, expected %v", testCase.input, result, testCase.expected)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsDate(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDateTime(t *testing.T) {
|
||||
if !IsDateTime("2023-03-11 12:34:56") {
|
||||
t.Error("Expected true, got false")
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"合法日期时间", "2023-03-11 12:34:56", true},
|
||||
{"无效闰日", "2023-02-29 12:34:56", false},
|
||||
{"无效日期", "2023-04-31 12:34:56", false},
|
||||
{"无效月份", "2023-13-01 12:34:56", false},
|
||||
{"仅日期", "2023-03-11", false},
|
||||
{"仅时间", "12:34:56", false},
|
||||
}
|
||||
if IsDateTime("2023-02-29 12:34:56") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsDateTime("2023-04-31 12:34:56") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsDateTime("2023-13-01 12:34:56") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsDateTime("2023-03-11") {
|
||||
t.Error("Expected false, got true")
|
||||
}
|
||||
if IsDateTime("12:34:56") {
|
||||
t.Error("Expected false, got true")
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsDateTime(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidDay(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
day int
|
||||
month int
|
||||
year int
|
||||
want bool
|
||||
}{
|
||||
{"Valid day in Jan", 31, 1, 2024, true},
|
||||
{"Invalid day in Jan", 32, 1, 2024, false},
|
||||
{"Valid day in Apr", 30, 4, 2024, true},
|
||||
{"Invalid day in Apr", 31, 4, 2024, false},
|
||||
{"Valid day in Feb (Leap year)", 29, 2, 2024, true},
|
||||
{"Invalid day in Feb (Leap year)", 30, 2, 2024, false},
|
||||
{"Valid day in Feb (Non-leap year)", 28, 2, 2023, true},
|
||||
{"Invalid day in Feb (Non-leap year)", 29, 2, 2023, false},
|
||||
{"Valid day in Dec", 31, 12, 2024, true},
|
||||
{"Invalid day in Dec", 32, 12, 2024, false},
|
||||
{"Valid day at edge case", 1, 1, 2024, true},
|
||||
{"Invalid day at edge case", 0, 1, 2024, false},
|
||||
{"Invalid month", 15, 13, 2024, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := isValidDay(tc.day, tc.month, tc.year)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,28 @@
|
||||
package validUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsDecimal(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"1.23", true},
|
||||
{"123", true},
|
||||
{"123.4", true},
|
||||
{"123.456", false},
|
||||
{"1.2.3", false},
|
||||
{"abc", false},
|
||||
{"有效小数", "1.23", true},
|
||||
{"整数", "123", true},
|
||||
{"有效小数", "123.4", true},
|
||||
{"超长小数", "123.456", false},
|
||||
{"多个点", "1.2.3", false},
|
||||
{"非数字", "abc", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
got := IsDecimal(tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("IsDecimal(%q) = %v; want %v", tc.input, got, tc.want)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsDecimal(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// IsIDCard 验证身份证号(18或15位)
|
||||
@@ -20,34 +20,25 @@ func IsIDCard(str string) bool {
|
||||
|
||||
// IsIDCard18 验证18位身份证号
|
||||
func IsIDCard18(id string) bool {
|
||||
// 18位身份证号码正则表达式,根据规则来编写
|
||||
regExp := "^[1-9]\\d{5}(19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$"
|
||||
// 利用正则表达式匹配身份证号码
|
||||
match, err := regexp.MatchString(regExp, id)
|
||||
if err != nil {
|
||||
// 匹配过程出错,返回false
|
||||
return false
|
||||
}
|
||||
if !match {
|
||||
// 身份证号码不符合规则,返回false
|
||||
return false
|
||||
}
|
||||
// 解析身份证号码中的年、月、日
|
||||
year, _ := strconv.Atoi(id[6:10])
|
||||
month, _ := strconv.Atoi(id[10:12])
|
||||
day, _ := strconv.Atoi(id[12:14])
|
||||
// 判断年份是否合法
|
||||
if year < 1900 || year > time.Now().Year() {
|
||||
return false
|
||||
}
|
||||
// 判断月份是否合法
|
||||
if month < 1 || month > 12 {
|
||||
return false
|
||||
}
|
||||
// 判断日期是否合法
|
||||
if day < 1 || day > 31 {
|
||||
if match, _ := regexp.MatchString(regExp, id); !match {
|
||||
return false
|
||||
}
|
||||
|
||||
// 判断身份证中 年、月、日 是否合法
|
||||
//year, _ := strconv.Atoi(id[6:10])
|
||||
//month, _ := strconv.Atoi(id[10:12])
|
||||
//day, _ := strconv.Atoi(id[12:14])
|
||||
//if year < 1900 || year > time.Now().Year() {
|
||||
// return false
|
||||
//}
|
||||
//if month < 1 || month > 12 {
|
||||
// return false
|
||||
//}
|
||||
//if day < 1 || day > 31 {
|
||||
// return false
|
||||
//}
|
||||
|
||||
// 对身份证号码的最后一位进行校验
|
||||
// 根据身份证号码的规则,最后一位可能是数字0-9,也可能是字符X(表示10)
|
||||
// 将字符X转换成数字10进行校验
|
||||
@@ -96,6 +87,7 @@ func IsIDCard15(idCard string) bool {
|
||||
|
||||
// 将身份证号前两位转换成省份代码
|
||||
provinceCode, err := strconv.Atoi(idCard[:2])
|
||||
fmt.Println(provinceCode)
|
||||
if err != nil || provinceCode < 11 || provinceCode > 91 {
|
||||
return false
|
||||
}
|
||||
|
@@ -1,76 +1,79 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func TestIsIDCard18Demo(t *testing.T) {
|
||||
// fmt.Println(IsIDCard18("120103199001015953"))
|
||||
// fmt.Println(IsIDCard18("44080319861221348X"))
|
||||
//}
|
||||
|
||||
func TestIsIDCard(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"110102197809193026", true},
|
||||
{"142629680611101", true},
|
||||
{"空字符串", "", false},
|
||||
{"有效身份证18位", "110102197809193026", true},
|
||||
{"有效身份证15位", "142629680611101", true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := IsIDCard(c.input)
|
||||
if got != c.want {
|
||||
t.Errorf("IsIDCard(%q) == %v, want %v", c.input, got, c.want)
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsIDCard(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIDCard18(t *testing.T) {
|
||||
// 18位身份证号码测试
|
||||
cases18 := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"120103109001015953", false}, // 年份非法
|
||||
{"120103199018015953", false}, // 月份非法
|
||||
{"120103199001505953", false}, // 日期非法
|
||||
{"123456789012345678", false},
|
||||
{"12345678901234567X", false},
|
||||
{"12345678901234567x", false},
|
||||
{"110102197809193026", true},
|
||||
{"210381199503166219", true},
|
||||
{"64010219940313212X", true},
|
||||
{"120103199001015953", true},
|
||||
{"44080319861221348X", true},
|
||||
{"空字符串", "", false},
|
||||
{"年份小于1900错误", "120103180001015953", false},
|
||||
{"年份非法", "120103109001015953", false},
|
||||
{"月份非法", "120103199018015953", false},
|
||||
{"月份非法", "622826199018015953", false},
|
||||
{"日期非法", "120103199001505953", false},
|
||||
{"无效号码1", "123456789012345678", false},
|
||||
{"无效号码2", "12345678901234567X", false},
|
||||
{"无效号码3", "12345678901234567x", false},
|
||||
{"有效号码1", "110102197809193026", true},
|
||||
{"有效号码2", "210381199503166219", true},
|
||||
{"有效号码3", "64010219940313212X", true},
|
||||
{"有效号码4", "120103199001015953", true},
|
||||
{"有效号码5", "44080319861221348X", true},
|
||||
}
|
||||
for _, c := range cases18 {
|
||||
got := IsIDCard18(c.input)
|
||||
if got != c.want {
|
||||
t.Errorf("IsIDCard18(%q) == %v, want %v", c.input, got, c.want)
|
||||
}
|
||||
|
||||
for _, tc := range cases18 {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsIDCard18(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIDCard15(t *testing.T) {
|
||||
// 15位身份证号码测试
|
||||
cases15 := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"", false},
|
||||
{"142629680611101", true},
|
||||
{"610104620927690", true},
|
||||
{"142629601611101", false}, // 年份非法
|
||||
{"01345678901234", false},
|
||||
{"1234567890123X", false},
|
||||
{"9994567890123X", false},
|
||||
{"空字符串", "", false},
|
||||
{"有效号码1", "142629680611101", true},
|
||||
{"有效号码2", "610104620927690", true},
|
||||
{"年份非法", "142629601611101", false},
|
||||
{"无效号码1", "01345678901234", false},
|
||||
{"无效号码2", "1234567890123X", false},
|
||||
{"无效号码3", "9994567890123X", false},
|
||||
{"无效号码4", "102629680611101", false},
|
||||
}
|
||||
for _, c := range cases15 {
|
||||
got := IsIDCard15(c.input)
|
||||
if got != c.want {
|
||||
t.Errorf("IsIDCard15(%q) == %v, want %v", c.input, got, c.want)
|
||||
}
|
||||
|
||||
for _, tc := range cases15 {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsIDCard15(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,45 +1,50 @@
|
||||
package validUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsIPv4(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"192.168.0.1", true},
|
||||
{"10.0.0.1", true},
|
||||
{"172.16.0.1", true},
|
||||
{"1.2.3.4", true},
|
||||
{"1.2.3.4.5", false},
|
||||
{"1.2.3", false},
|
||||
{"abc.def.ghi.jkl", false},
|
||||
{"有效地址1", "192.168.0.1", true},
|
||||
{"有效地址2", "10.0.0.1", true},
|
||||
{"有效地址3", "172.16.0.1", true},
|
||||
{"有效地址4", "1.2.3.4", true},
|
||||
{"无效地址1", "1.2.3.4.5", false},
|
||||
{"无效地址2", "1.2.3", false},
|
||||
{"无效地址3", "abc.def.ghi.jkl", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := IsIPv4(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("IsIPv4(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsIPv4(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIPv6(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"CDCD:910A:2222:5498:8475:1111:3900:2020", true},
|
||||
{"1030::C9B4:FF12:48AA:1A2B", true},
|
||||
{"2000:0:0:0:0:0:0:1", true},
|
||||
{"127.0.0.1", false},
|
||||
{"::ffff:127.0.0.1", false},
|
||||
{"有效地址1", "CDCD:910A:2222:5498:8475:1111:3900:2020", true},
|
||||
{"有效地址2", "1030::C9B4:FF12:48AA:1A2B", true},
|
||||
{"有效地址3", "2000:0:0:0:0:0:0:1", true},
|
||||
{"无效地址1", "127.0.0.1", false},
|
||||
{"无效地址2", "::ffff:127.0.0.1", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := IsIPv6(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("Expected IsIPv6(%q) to be %v, but got %v", tc.input, tc.expected, result)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsIPv6(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,42 +1,47 @@
|
||||
package validUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsAllChinese(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"中国人", true},
|
||||
{"abc", false},
|
||||
{"中abc", false},
|
||||
{"", true},
|
||||
{"全中文", "中国人", true},
|
||||
{"全英文", "abc", false},
|
||||
{"中英文混合", "中abc", false},
|
||||
{"空字符串", "", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := IsAllChinese(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("IsAllChinese(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsAllChinese(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsContainChinese(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"中国人", true},
|
||||
{"abc", false},
|
||||
{"中abc", true},
|
||||
{"", false},
|
||||
{"全中文", "中国人", true},
|
||||
{"全英文", "abc", false},
|
||||
{"中英文混合", "中abc", true},
|
||||
{"空字符串", "", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := IsContainChinese(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("IsContainChinese(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsContainChinese(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,9 +58,8 @@ func TestIsChineseName(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if result := IsChineseName(tc.input); result != tc.expected {
|
||||
t.Errorf("expected %v, but got %v", tc.expected, result)
|
||||
}
|
||||
res := IsChineseName(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -72,9 +76,8 @@ func TestIsEnglishName(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if result := IsEnglishName(tc.input); result != tc.expected {
|
||||
t.Errorf("expected %v, but got %v", tc.expected, result)
|
||||
}
|
||||
res := IsEnglishName(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +1,27 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsNumber(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"123", true},
|
||||
{"1a2b3", false},
|
||||
{"0", true},
|
||||
{"", false},
|
||||
{" ", false},
|
||||
{"纯数字", "123", true},
|
||||
{"含字母", "1a2b3", false},
|
||||
{"单个数字", "0", true},
|
||||
{"空字符串", "", false},
|
||||
{"空格", " ", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
got := IsNumber(tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("IsNumber(%q) = %v; want %v", tc.input, got, tc.want)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsNumber(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,28 @@
|
||||
package validUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsPostalCode(t *testing.T) {
|
||||
// Test valid postal codes
|
||||
validCodes := []string{"100000", "200000", "999999"}
|
||||
for _, code := range validCodes {
|
||||
if !IsPostalCode(code) {
|
||||
t.Errorf("%s should be a valid postal code", code)
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
code string
|
||||
want bool
|
||||
}{
|
||||
{"有效邮政编码1", "100000", true},
|
||||
{"有效邮政编码2", "200000", true},
|
||||
{"有效邮政编码3", "999999", true},
|
||||
{"无效邮政编码1", "1234567", false},
|
||||
{"无效邮政编码2", "2000000", false},
|
||||
{"无效邮政编码3", "123a56", false},
|
||||
}
|
||||
|
||||
// Test invalid postal codes
|
||||
invalidCodes := []string{"1234567", "2000000", "123a56"}
|
||||
for _, code := range invalidCodes {
|
||||
if IsPostalCode(code) {
|
||||
t.Errorf("%s should be an invalid postal code", code)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsPostalCode(tc.code)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,47 +1,55 @@
|
||||
package validUtil
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsMobile(t *testing.T) {
|
||||
type test struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}
|
||||
|
||||
tests := []test{
|
||||
{input: "15812345678", want: true},
|
||||
{input: "15912345678", want: true},
|
||||
{input: "17012345678", want: true},
|
||||
{input: "17112345678", want: true},
|
||||
{input: "17212345678", want: true},
|
||||
{input: "18912345678", want: true},
|
||||
{input: "29012345678", want: false},
|
||||
{input: "11111111111", want: false},
|
||||
{input: "09212345678", want: false},
|
||||
{"有效号码1", "15812345678", true},
|
||||
{"有效号码2", "15912345678", true},
|
||||
{"有效号码3", "17012345678", true},
|
||||
{"有效号码4", "17112345678", true},
|
||||
{"有效号码5", "17212345678", true},
|
||||
{"有效号码6", "18912345678", true},
|
||||
{"无效号码1", "29012345678", false},
|
||||
{"无效号码2", "11111111111", false},
|
||||
{"无效号码3", "09212345678", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
if got := IsMobile(tt.input); got != tt.want {
|
||||
t.Errorf("IsMobile(%s) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsMobile(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTelephone(t *testing.T) {
|
||||
// Test valid telephone numbers
|
||||
validNumbers := []string{"010-12345678", "02112345678", "075512345678"}
|
||||
for _, num := range validNumbers {
|
||||
if !IsTelephone(num) {
|
||||
t.Errorf("%s should be a valid telephone number", num)
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"有效号码1", "010-12345678", true},
|
||||
{"有效号码2", "02112345678", true},
|
||||
{"有效号码3", "075512345678", true},
|
||||
{"无效号码1", "12345678", false},
|
||||
{"无效号码2", "010-1234-5678", false},
|
||||
{"无效号码3", "0515123456a", false},
|
||||
}
|
||||
|
||||
// Test invalid telephone numbers
|
||||
invalidNumbers := []string{"12345678", "010-1234-5678", "0515123456a"}
|
||||
for _, num := range invalidNumbers {
|
||||
if IsTelephone(num) {
|
||||
t.Errorf("%s should be an invalid telephone number", num)
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsTelephone(tc.input)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,28 +1,30 @@
|
||||
package validUtil
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsURL(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"https://www.google.com", true},
|
||||
{"http://example.com", true},
|
||||
{"https://www.example.com/path?query=string", true},
|
||||
{"ftp://ftp.example.com", false},
|
||||
{"invalid url", false},
|
||||
{"www.example.com", false},
|
||||
{"example.com", false},
|
||||
{"http://", false},
|
||||
{"有效网址1", "https://www.google.com", true},
|
||||
{"有效网址2", "http://example.com", true},
|
||||
{"有效网址3", "https://www.example.com/path?query=string", true},
|
||||
{"无效网址1", "ftp://ftp.example.com", false},
|
||||
{"无效网址2", "invalid url", false},
|
||||
{"无效网址3", "www.example.com", false},
|
||||
{"无效网址4", "example.com", false},
|
||||
{"无效网址5", "http://", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := IsURL(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("Expected IsURL(%q) to be %v, but got %v", tc.input, tc.expected, result)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := IsURL(tc.input)
|
||||
assert.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user