1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

strconv: make atoi return ?int

This commit is contained in:
Alexander Medvednikov
2020-12-21 08:35:24 +01:00
parent 2147d8785b
commit fc965b7d92
3 changed files with 66 additions and 48 deletions

View File

@ -1,11 +1,31 @@
import strconv
fn test_atoi() {
assert strconv.atoi('16') == 16
assert strconv.atoi('+16') == 16
assert strconv.atoi('-16') == -16
assert strconv.atoi('str') == 0
assert strconv.atoi('') == 0
if x := strconv.atoi('16') {
assert x == 16
} else {
assert false
}
if x := strconv.atoi('+16') {
assert x == 16
} else {
assert false
}
if x := strconv.atoi('-16') {
assert x == -16
} else {
assert false
}
if x := strconv.atoi('str') {
assert false
} else {
assert true
}
if x := strconv.atoi('') {
assert false
} else {
assert true
}
}
fn test_parse_int() {
@ -31,31 +51,24 @@ fn test_common_parse_uint2() {
mut result, mut error := strconv.common_parse_uint2('1', 10, 8)
assert result == 1
assert error == 0
result, error = strconv.common_parse_uint2('123', 10, 8)
assert result == 123
assert error == 0
result, error = strconv.common_parse_uint2('123', 10, 65)
assert result == 0
assert error == -2
result, error = strconv.common_parse_uint2('123', 10, -1)
assert result == 0
assert error == -2
result, error = strconv.common_parse_uint2('', 10, 8)
assert result == 0
assert error == 1
result, error = strconv.common_parse_uint2('1a', 10, 8)
assert result == 1
assert error == 2
result, error = strconv.common_parse_uint2('12a', 10, 8)
assert result == 12
assert error == 3
result, error = strconv.common_parse_uint2('123a', 10, 8)
assert result == 123
assert error == 4