stoi_test.ha (1047B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use types; 5 6 @test fn stoi() void = { 7 assert(stoi64("") as invalid == 0: invalid); 8 assert(stoi64("abc") as invalid == 0: invalid); 9 assert(stoi64("1a") as invalid == 1: invalid); 10 11 assert(stoi64("9223372036854775808") is overflow); 12 assert(stoi64("-9223372036854775809") is overflow); 13 14 assert(stoi64("0") as i64 == 0); 15 assert(stoi64("1") as i64 == 1); 16 assert(stoi64("+1") as i64 == 1); 17 assert(stoi64("-1") as i64 == -1); 18 assert(stoi64("9223372036854775807") as i64 == types::I64_MAX); 19 assert(stoi64("-9223372036854775808") as i64 == types::I64_MIN); 20 21 assert(stoi32("2147483648") is overflow); 22 assert(stoi32("-2147483649") is overflow); 23 24 assert(stoi32("2147483647") as i32 == 2147483647); 25 assert(stoi32("-2147483648") as i32 == -2147483648); 26 }; 27 28 @test fn stoib() void = { 29 assert(stoi64b("-7f", 16) as i64 == -0x7f); 30 assert(stoi64b("7F", 16) as i64 == 0x7f); 31 assert(stoi64b("37", 8) as i64 == 0o37); 32 assert(stoi64b("-110101", 2) as i64 == -0b110101); 33 };