zig/lib/compiler_rt/paritydi2_test.zig

37 lines
1016 B
Zig
Raw Normal View History

2022-01-07 05:06:06 +00:00
const std = @import("std");
const parity = @import("parity.zig");
2022-01-07 05:06:06 +00:00
const testing = std.testing;
fn paritydi2Naive(a: i64) i32 {
var x: u64 = @bitCast(a);
var has_parity: bool = false;
while (x > 0) {
has_parity = !has_parity;
x = x & (x - 1);
}
return @intCast(@intFromBool(has_parity));
}
fn test__paritydi2(a: i64) !void {
2023-11-10 05:27:17 +00:00
const x = parity.__paritydi2(a);
const expected: i64 = paritydi2Naive(a);
try testing.expectEqual(expected, x);
}
test "paritydi2" {
try test__paritydi2(0);
try test__paritydi2(1);
try test__paritydi2(2);
try test__paritydi2(@bitCast(@as(u64, 0xffffffff_fffffffd)));
try test__paritydi2(@bitCast(@as(u64, 0xffffffff_fffffffe)));
try test__paritydi2(@bitCast(@as(u64, 0xffffffff_ffffffff)));
const RndGen = std.Random.DefaultPrng;
var rnd = RndGen.init(42);
var i: u32 = 0;
while (i < 10_000) : (i += 1) {
2023-11-10 05:27:17 +00:00
const rand_num = rnd.random().int(i64);
try test__paritydi2(rand_num);
}
}