Files
go-easy-utils/strUtil/string_to_int_x_test.go
jeffery cea73b8e18 version 1.0.1 (#8)
* anyutil test

* fix:delete debug

* unit test utils

* unit test

* strUtil unit test

* fix;4, 6, 9, 11 Month determination error

* fix:The last digit of ID number is x, and the weighted sum comparison condition is wrong

* test

* test

* docs

* fix:docs

* idcard test

* test unit

* no message

* docs update

* 单元测试覆盖

---------

Co-authored-by: libin <libinjob@163.com>
Co-authored-by: 李斌 <libin1-hj@huafang.com>
2023-03-27 16:52:08 +08:00

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