zig/lib/std/special/compiler_rt/popcountdi2_test.zig
Andrew Kelley 4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00

33 lines
1.0 KiB
Zig

// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2020 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
const __popcountdi2 = @import("popcountdi2.zig").__popcountdi2;
const testing = @import("std").testing;
fn naive_popcount(a_param: i64) i32 {
var a = a_param;
var r: i32 = 0;
while (a != 0) : (a = @bitCast(i64, @bitCast(u64, a) >> 1)) {
r += @intCast(i32, a & 1);
}
return r;
}
fn test__popcountdi2(a: i64) void {
const x = __popcountdi2(a);
const expected = naive_popcount(a);
testing.expect(expected == x);
}
test "popcountdi2" {
test__popcountdi2(0);
test__popcountdi2(1);
test__popcountdi2(2);
test__popcountdi2(@bitCast(i64, @as(u64, 0xFFFFFFFFFFFFFFFD)));
test__popcountdi2(@bitCast(i64, @as(u64, 0xFFFFFFFFFFFFFFFE)));
test__popcountdi2(@bitCast(i64, @as(u64, 0xFFFFFFFFFFFFFFFF)));
// TODO some fuzz testing
}