stoi.ha (1182B)
1 // License: MPL-2.0 2 // (c) 2021 Alexey Yerin <yyp@disroot.org> 3 // (c) 2021 Andri Yngvason <andri@yngvason.is> 4 // (c) 2022 Bor Grošelj Simić <bor.groseljsimic@telemach.net> 5 // (c) 2021 Ember Sawady <ecs@d2evs.net> 6 use types; 7 8 @test fn stoi() void = { 9 assert(stoi64("") as invalid == 0: invalid); 10 assert(stoi64("abc") as invalid == 0: invalid); 11 assert(stoi64("1a") as invalid == 1: invalid); 12 13 assert(stoi64("9223372036854775808") is overflow); 14 assert(stoi64("-9223372036854775809") is overflow); 15 16 assert(stoi64("0") as i64 == 0); 17 assert(stoi64("1") as i64 == 1); 18 assert(stoi64("+1") as i64 == 1); 19 assert(stoi64("-1") as i64 == -1); 20 assert(stoi64("9223372036854775807") as i64 == types::I64_MAX); 21 assert(stoi64("-9223372036854775808") as i64 == types::I64_MIN); 22 23 assert(stoi32("2147483648") is overflow); 24 assert(stoi32("-2147483649") is overflow); 25 26 assert(stoi32("2147483647") as i32 == 2147483647); 27 assert(stoi32("-2147483648") as i32 == -2147483648); 28 }; 29 30 @test fn stoib() void = { 31 assert(stoi64b("-7f", 16) as i64 == -0x7f); 32 assert(stoi64b("7F", 16) as i64 == 0x7f); 33 assert(stoi64b("37", 8) as i64 == 0o37); 34 assert(stoi64b("-110101", 2) as i64 == -0b110101); 35 };