Feature/cover unit test (#81)

This commit is contained in:
libin
2024-07-31 19:51:01 +08:00
committed by GitHub
parent 0990bbce4d
commit a59af60d9a
60 changed files with 2571 additions and 2606 deletions

View File

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