2022-01-07 05:06:06 +00:00
|
|
|
const std = @import("std");
|
2021-12-01 10:27:40 +00:00
|
|
|
const parity = @import("parity.zig");
|
2022-01-07 05:06:06 +00:00
|
|
|
const testing = std.testing;
|
2021-12-01 10:27:40 +00:00
|
|
|
|
|
|
|
fn paritydi2Naive(a: i64) i32 {
|
|
|
|
var x = @bitCast(u64, a);
|
|
|
|
var has_parity: bool = false;
|
|
|
|
while (x > 0) {
|
|
|
|
has_parity = !has_parity;
|
|
|
|
x = x & (x - 1);
|
|
|
|
}
|
2023-06-15 08:14:16 +01:00
|
|
|
return @intCast(i32, @intFromBool(has_parity));
|
2021-12-01 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test__paritydi2(a: i64) !void {
|
|
|
|
var x = parity.__paritydi2(a);
|
|
|
|
var 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(i64, @as(u64, 0xffffffff_fffffffd)));
|
|
|
|
try test__paritydi2(@bitCast(i64, @as(u64, 0xffffffff_fffffffe)));
|
|
|
|
try test__paritydi2(@bitCast(i64, @as(u64, 0xffffffff_ffffffff)));
|
|
|
|
|
2022-01-07 05:06:06 +00:00
|
|
|
const RndGen = std.rand.DefaultPrng;
|
2021-12-01 10:27:40 +00:00
|
|
|
var rnd = RndGen.init(42);
|
|
|
|
var i: u32 = 0;
|
|
|
|
while (i < 10_000) : (i += 1) {
|
|
|
|
var rand_num = rnd.random().int(i64);
|
|
|
|
try test__paritydi2(rand_num);
|
|
|
|
}
|
|
|
|
}
|