1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-25 23:02:22 +02:00

сделал Int64FromString()

This commit is contained in:
Nikitin Aleksandr
2023-12-19 11:30:26 +03:00
parent 7e34f65ac9
commit f4796f9c1e
2 changed files with 45 additions and 0 deletions

View File

@@ -792,3 +792,13 @@ func DeleteEndSlash(Text string) string {
return Otvet
}
// Int64FromString - возвращает int64 из строки
func Int64FromString(s string) (int64, error) {
var Otvet int64
var err error
Otvet, err = strconv.ParseInt(s, 10, 64)
return Otvet, err
}

View File

@@ -511,3 +511,38 @@ func TestDeleteEndSlash(t *testing.T) {
})
}
}
func TestInt64FromString(t *testing.T) {
// Test converting a valid string to int64
input1 := "12345"
expected1 := int64(12345)
result1, err1 := Int64FromString(input1)
if err1 != nil {
t.Errorf("Expected no error, but got: %v", err1)
}
if result1 != expected1 {
t.Errorf("Expected %d, but got: %d", expected1, result1)
}
// Test converting an empty string to int64
input2 := ""
expected2 := int64(0)
result2, err2 := Int64FromString(input2)
if err2 == nil {
t.Errorf("Expected error, but got: %v", err2)
}
if result2 != expected2 {
t.Errorf("Expected %d, but got: %d", expected2, result2)
}
// Test converting an invalid string to int64
input3 := "abc"
expected3 := int64(0)
result3, err3 := Int64FromString(input3)
if err3 == nil {
t.Error("Expected an error, but got none")
}
if result3 != expected3 {
t.Errorf("Expected %d, but got: %d", expected3, result3)
}
}