From 126c4c575193baa408d221d9eb982c5dfd798cf0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 18 Oct 2022 00:36:46 +0800 Subject: [PATCH] builtin,math: fix math.min_i64.str() (fix #16086) (#16089) --- vlib/builtin/int.v | 3 +++ vlib/math/const.v | 22 +++++++++++++--------- vlib/math/math_test.v | 11 +++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 89bc821ff7..9f308b31f4 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -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) diff --git a/vlib/math/const.v b/vlib/math/const.v index 61226f3c05..e2c1f94c1d 100644 --- a/vlib/math/const.v +++ b/vlib/math/const.v @@ -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) ) diff --git a/vlib/math/math_test.v b/vlib/math/math_test.v index 0bbf21122b..41e41bb6d2 100644 --- a/vlib/math/math_test.v +++ b/vlib/math/math_test.v @@ -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' +}