zig/lib/compiler_rt/paritydi2_test.zig
mlugg f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00

36 lines
1 KiB
Zig

const std = @import("std");
const parity = @import("parity.zig");
const testing = std.testing;
fn paritydi2Naive(a: i64) i32 {
var x = @as(u64, @bitCast(a));
var has_parity: bool = false;
while (x > 0) {
has_parity = !has_parity;
x = x & (x - 1);
}
return @as(i32, @intCast(@intFromBool(has_parity)));
}
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(@as(i64, @bitCast(@as(u64, 0xffffffff_fffffffd))));
try test__paritydi2(@as(i64, @bitCast(@as(u64, 0xffffffff_fffffffe))));
try test__paritydi2(@as(i64, @bitCast(@as(u64, 0xffffffff_ffffffff))));
const RndGen = std.rand.DefaultPrng;
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);
}
}