mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2024-05-03 11:34:53 +08:00
e9cc195c39
example docs
132 lines
2.3 KiB
Go
132 lines
2.3 KiB
Go
package strUtil
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestStrToInt(t *testing.T) {
|
|
// Test cases
|
|
testCases := []struct {
|
|
input string
|
|
expected int
|
|
}{
|
|
{"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrToInt8(t *testing.T) {
|
|
// Test cases
|
|
testCases := []struct {
|
|
input string
|
|
expected int8
|
|
}{
|
|
{"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrToInt16(t *testing.T) {
|
|
// Test cases
|
|
testCases := []struct {
|
|
input string
|
|
expected int16
|
|
}{
|
|
{"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrToInt32(t *testing.T) {
|
|
// Test cases
|
|
testCases := []struct {
|
|
input string
|
|
expected int32
|
|
}{
|
|
{"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrToInt64(t *testing.T) {
|
|
// Test cases
|
|
testCases := []struct {
|
|
input string
|
|
expected int64
|
|
}{
|
|
{"0", 0},
|
|
{"1", 1},
|
|
{"-1", -1},
|
|
{"9223372036854775807", 9223372036854775807},
|
|
{"-9223372036854775808", -9223372036854775808},
|
|
{"2147483647", 2147483647},
|
|
{"-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)
|
|
}
|
|
}
|
|
}
|