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 parityti2Naive(a: i128) i32 {
|
2023-07-24 11:34:16 +01:00
|
|
|
var x: u128 = @bitCast(a);
|
2021-12-01 10:27:40 +00:00
|
|
|
var has_parity: bool = false;
|
|
|
|
while (x > 0) {
|
|
|
|
has_parity = !has_parity;
|
|
|
|
x = x & (x - 1);
|
|
|
|
}
|
2023-06-22 18:46:56 +01:00
|
|
|
return @as(i32, @intCast(@intFromBool(has_parity)));
|
2021-12-01 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test__parityti2(a: i128) !void {
|
|
|
|
var x = parity.__parityti2(a);
|
|
|
|
var expected: i128 = parityti2Naive(a);
|
|
|
|
try testing.expectEqual(expected, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "parityti2" {
|
|
|
|
try test__parityti2(0);
|
|
|
|
try test__parityti2(1);
|
|
|
|
try test__parityti2(2);
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__parityti2(@as(i128, @bitCast(@as(u128, 0xffffffff_ffffffff_ffffffff_fffffffd))));
|
|
|
|
try test__parityti2(@as(i128, @bitCast(@as(u128, 0xffffffff_ffffffff_ffffffff_fffffffe))));
|
|
|
|
try test__parityti2(@as(i128, @bitCast(@as(u128, 0xffffffff_ffffffff_ffffffff_ffffffff))));
|
2021-12-01 10:27:40 +00:00
|
|
|
|
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(i128);
|
|
|
|
try test__parityti2(rand_num);
|
|
|
|
}
|
|
|
|
}
|