mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-09 00:40:12 +08:00
unit test
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
package intUtil
|
||||
|
||||
import "strconv"
|
||||
|
||||
// StrToInt 将string类型转换为int类型
|
||||
func StrToInt(s string) (int, error) {
|
||||
return strconv.Atoi(s)
|
||||
}
|
||||
|
||||
// StrToInt8 将string类型转换为int8类型
|
||||
func StrToInt8(s string) (int8, error) {
|
||||
n, err := strconv.Atoi(s)
|
||||
return int8(n), err
|
||||
}
|
||||
|
||||
// StrToInt16 将string类型转换为int16类型
|
||||
func StrToInt16(s string) (int16, error) {
|
||||
n, err := strconv.Atoi(s)
|
||||
return int16(n), err
|
||||
}
|
||||
|
||||
// StrToInt32 将string类型转换为int32类型
|
||||
func StrToInt32(s string) (int32, error) {
|
||||
n, err := strconv.Atoi(s)
|
||||
return int32(n), err
|
||||
}
|
||||
|
||||
// StrToInt64 将string类型转换为int64类型
|
||||
func StrToInt64(s string) (int64, error) {
|
||||
return strconv.ParseInt(s, 10, 64)
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
package intUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStrToInt(t *testing.T) {
|
||||
s := "123"
|
||||
expected := 123
|
||||
result, err := StrToInt(s)
|
||||
if err != nil {
|
||||
t.Errorf("StrToInt(%s) returned an error: %v", s, err)
|
||||
}
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt(%s) = %d; expected %d", s, result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt8(t *testing.T) {
|
||||
s := "-123"
|
||||
expected := int8(-123)
|
||||
result, err := StrToInt8(s)
|
||||
if err != nil {
|
||||
t.Errorf("StrToInt8(%s) returned an error: %v", s, err)
|
||||
}
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt8(%s) = %d; expected %d", s, result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt16(t *testing.T) {
|
||||
s := "123"
|
||||
expected := int16(123)
|
||||
result, err := StrToInt16(s)
|
||||
if err != nil {
|
||||
t.Errorf("StrToInt16(%s) returned an error: %v", s, err)
|
||||
}
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt16(%s) = %d; expected %d", s, result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt32(t *testing.T) {
|
||||
s := "-123"
|
||||
expected := int32(-123)
|
||||
result, err := StrToInt32(s)
|
||||
if err != nil {
|
||||
t.Errorf("StrToInt32(%s) returned an error: %v", s, err)
|
||||
}
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt32(%s) = %d; expected %d", s, result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt64(t *testing.T) {
|
||||
s := "123"
|
||||
expected := int64(123)
|
||||
result, err := StrToInt64(s)
|
||||
if err != nil {
|
||||
t.Errorf("StrToInt64(%s) returned an error: %v", s, err)
|
||||
}
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt64(%s) = %d; expected %d", s, result, expected)
|
||||
}
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
package strUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStrToInt(t *testing.T) {
|
||||
expected := 123
|
||||
result := StrToInt("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt8(t *testing.T) {
|
||||
expected := int8(123)
|
||||
result := StrToInt8("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt8 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt16(t *testing.T) {
|
||||
expected := int16(123)
|
||||
result := StrToInt16("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt16 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt32(t *testing.T) {
|
||||
expected := int32(123)
|
||||
result := StrToInt32("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt32 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt64(t *testing.T) {
|
||||
expected := int64(123)
|
||||
result := StrToInt64("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt64 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
@@ -1,7 +1,6 @@
|
||||
package strUtil
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
@@ -10,7 +9,6 @@ import (
|
||||
func StrToInt(str string) int {
|
||||
i, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
log.Printf("StrToInt(%s) err:%s \n", str, err.Error())
|
||||
return 0
|
||||
}
|
||||
return i
|
||||
@@ -20,7 +18,6 @@ func StrToInt(str string) int {
|
||||
func StrToInt8(str string) int8 {
|
||||
i, err := strconv.ParseInt(str, 10, 8)
|
||||
if err != nil {
|
||||
log.Printf("StrToInt8(%s) err:%s \n", str, err.Error())
|
||||
return 0
|
||||
}
|
||||
return int8(i)
|
||||
@@ -30,7 +27,6 @@ func StrToInt8(str string) int8 {
|
||||
func StrToInt16(str string) int16 {
|
||||
i, err := strconv.ParseInt(str, 10, 16)
|
||||
if err != nil {
|
||||
log.Printf("StrToInt16(%s) err:%s \n", str, err.Error())
|
||||
return 0
|
||||
}
|
||||
return int16(i)
|
||||
@@ -40,7 +36,6 @@ func StrToInt16(str string) int16 {
|
||||
func StrToInt32(str string) int32 {
|
||||
i, err := strconv.ParseInt(str, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("StrToInt32(%s) err:%s \n", str, err.Error())
|
||||
return 0
|
||||
}
|
||||
return int32(i)
|
||||
@@ -50,7 +45,51 @@ func StrToInt32(str string) int32 {
|
||||
func StrToInt64(str string) int64 {
|
||||
i, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("StrToInt64(%s) err:%s \n", str, err.Error())
|
||||
return 0
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// StrToUint string转uint
|
||||
func StrToUint(str string) uint {
|
||||
i, err := strconv.ParseUint(str, 10, 0)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return uint(i)
|
||||
}
|
||||
|
||||
// StrToUint8 string转uint8
|
||||
func StrToUint8(str string) uint8 {
|
||||
i, err := strconv.ParseUint(str, 10, 8)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return uint8(i)
|
||||
}
|
||||
|
||||
// StrToUint16 string转uint16
|
||||
func StrToUint16(str string) uint16 {
|
||||
i, err := strconv.ParseUint(str, 10, 16)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return uint16(i)
|
||||
}
|
||||
|
||||
// StrToUint32 string转uint32
|
||||
func StrToUint32(str string) uint32 {
|
||||
i, err := strconv.ParseUint(str, 10, 32)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return uint32(i)
|
||||
}
|
||||
|
||||
// StrToUint64 string转uint64
|
||||
func StrToUint64(str string) uint64 {
|
||||
i, err := strconv.ParseUint(str, 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return i
|
149
strUtil/string_to_int_x_test.go
Normal file
149
strUtil/string_to_int_x_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package strUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStrToInt(t *testing.T) {
|
||||
expected := 123
|
||||
result := StrToInt("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt8(t *testing.T) {
|
||||
expected := int8(123)
|
||||
result := StrToInt8("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt8 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt16(t *testing.T) {
|
||||
expected := int16(123)
|
||||
result := StrToInt16("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt16 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt32(t *testing.T) {
|
||||
expected := int32(123)
|
||||
result := StrToInt32("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt32 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToInt64(t *testing.T) {
|
||||
expected := int64(123)
|
||||
result := StrToInt64("123")
|
||||
if result != expected {
|
||||
t.Errorf("StrToInt64 test failed, expected %d but got %d", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected uint
|
||||
}{
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint8(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected uint8
|
||||
}{
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint16(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected uint16
|
||||
}{
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint32(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected uint32
|
||||
}{
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrToUint64(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expectedOutput uint64
|
||||
}{
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user