zig/lib/compiler_rt/paritysi2_test.zig
Andrew Kelley ec95e00e28 flatten lib/std/special and improve "pkg inside another" logic
stage2: change logic for detecting whether the main package is inside
the std package. Previously it relied on realpath() which is not portable.
This uses resolve() which is how imports already work.

 * stage2: fix cleanup bug when creating Module
 * flatten lib/std/special/* to lib/*
   - this was motivated by making main_pkg_is_inside_std false for
     compiler_rt & friends.
 * rename "mini libc" to "universal libc"
2022-05-06 22:41:00 -07:00

37 lines
999 B
Zig

const std = @import("std");
const parity = @import("parity.zig");
const testing = std.testing;
fn paritysi2Naive(a: i32) i32 {
var x = @bitCast(u32, a);
var has_parity: bool = false;
while (x > 0) {
has_parity = !has_parity;
x = x & (x - 1);
}
return @intCast(i32, @boolToInt(has_parity));
}
fn test__paritysi2(a: i32) !void {
var x = parity.__paritysi2(a);
var expected: i32 = paritysi2Naive(a);
try testing.expectEqual(expected, x);
}
test "paritysi2" {
try test__paritysi2(0);
try test__paritysi2(1);
try test__paritysi2(2);
try test__paritysi2(@bitCast(i32, @as(u32, 0xfffffffd)));
try test__paritysi2(@bitCast(i32, @as(u32, 0xfffffffe)));
try test__paritysi2(@bitCast(i32, @as(u32, 0xffffffff)));
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(i32);
try test__paritysi2(rand_num);
}
}