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

builtin,math: fix math.min_i64.str() (fix #16086) (#16089)

This commit is contained in:
yuyi 2022-10-18 00:36:46 +08:00 committed by GitHub
parent 556244576d
commit 126c4c5751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 9 deletions

View File

@ -177,6 +177,9 @@ pub fn (nn i64) str() string {
mut d := i64(0)
if n == 0 {
return '0'
} else if n == i64(-9223372036854775807 - 1) {
// math.min_i64
return '-9223372036854775808'
}
max := 20
mut buf := malloc_noscan(max + 1)

View File

@ -42,19 +42,23 @@ pub const (
// Integer limit values
pub const (
max_i8 = 127
min_i8 = -128
max_i16 = 32767
min_i16 = -32768
max_i32 = 2147483647
min_i32 = -2147483648
// -9223372036854775808 is wrong because C compilers parse literal values
min_i8 = i8(-128)
max_i8 = i8(127)
min_i16 = i16(-32768)
max_i16 = i16(32767)
min_i32 = int(-2147483648)
max_i32 = int(2147483647)
// -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1
min_i64 = i64(-9223372036854775807 - 1)
max_i64 = i64(9223372036854775807)
max_u8 = 255
max_u16 = 65535
min_u8 = u8(0)
max_u8 = u8(255)
min_u16 = u16(0)
max_u16 = u16(65535)
min_u32 = u32(0)
max_u32 = u32(4294967295)
min_u64 = u64(0)
max_u64 = u64(18446744073709551615)
)

View File

@ -1048,3 +1048,14 @@ fn test_count_digits() {
assert count_digits(123456789012345) == 15
assert count_digits(-67345) == 5
}
fn test_min_max_int_str() {
assert min_i64.str() == '-9223372036854775808'
assert max_i64.str() == '9223372036854775807'
assert min_i32.str() == '-2147483648'
assert max_i32.str() == '2147483647'
assert min_i16.str() == '-32768'
assert max_i16.str() == '32767'
assert min_i8.str() == '-128'
assert max_i8.str() == '127'
}