go-easy-utils/strUtil/str_to_int_x_test.go

132 lines
2.3 KiB
Go
Raw Normal View History

2023-03-23 08:27:24 +08:00
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)
}
2023-03-23 08:27:24 +08:00
}
}
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)
}
2023-03-23 08:27:24 +08:00
}
}
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)
}
2023-03-23 08:27:24 +08:00
}
}
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)
}
2023-03-23 08:27:24 +08:00
}
}
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)
}
2023-03-23 08:27:24 +08:00
}
}