zig/lib/std/special/compiler_rt/absvti2_test.zig
Jan Philipp Hafer 405ff911da compiler_rt: add __absvsi2, __absvdi2, __absvti2
- abs can only overflow, if a == MIN
- comparing the sign change from wrapping addition is branchless
- tests: MIN, MIN+1,..MIN+4, -42, -7, -1, 0, 1, 7..

See #1290
2021-12-26 13:21:18 -08:00

31 lines
1.5 KiB
Zig

const absv = @import("absv.zig");
const testing = @import("std").testing;
fn test__absvti2(a: i128, expected: i128) !void {
var result = absv.__absvti2(a);
try testing.expectEqual(expected, result);
}
test "absvti2" {
// -2^127 <= i128 <= 2^127-1
// 2^127 = 170141183460469231731687303715884105728
// 2^127+1 = 170141183460469231731687303715884105727
// TODO write panic handler for testing panics
//try test__absvti2(-170141183460469231731687303715884105728, -5); // tested with return -5; and panic
try test__absvti2(-170141183460469231731687303715884105727, 170141183460469231731687303715884105727);
try test__absvti2(-170141183460469231731687303715884105726, 170141183460469231731687303715884105726);
try test__absvti2(-170141183460469231731687303715884105725, 170141183460469231731687303715884105725);
try test__absvti2(-170141183460469231731687303715884105724, 170141183460469231731687303715884105724);
try test__absvti2(-42, 42);
try test__absvti2(-7, 7);
try test__absvti2(-1, 1);
try test__absvti2(0, 0);
try test__absvti2(1, 1);
try test__absvti2(7, 7);
try test__absvti2(42, 42);
try test__absvti2(170141183460469231731687303715884105724, 170141183460469231731687303715884105724);
try test__absvti2(170141183460469231731687303715884105725, 170141183460469231731687303715884105725);
try test__absvti2(170141183460469231731687303715884105726, 170141183460469231731687303715884105726);
try test__absvti2(170141183460469231731687303715884105727, 170141183460469231731687303715884105727);
}