zig/test/behavior/popcount.zig
Andrew Kelley 5619ce2406 Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
 * doc/langref.html.in
 * lib/std/enums.zig
 * lib/std/fmt.zig
 * lib/std/hash/auto_hash.zig
 * lib/std/math.zig
 * lib/std/mem.zig
 * lib/std/meta.zig
 * test/behavior/alignof.zig
 * test/behavior/bitcast.zig
 * test/behavior/bugs/1421.zig
 * test/behavior/cast.zig
 * test/behavior/ptrcast.zig
 * test/behavior/type_info.zig
 * test/behavior/vector.zig

Master branch added `try` to a bunch of testing function calls, and some
lines also had changed how to refer to the native architecture and other
`@import("builtin")` stuff.
2021-05-08 14:45:21 -07:00

44 lines
965 B
Zig

const expect = @import("std").testing.expect;
test "@popCount" {
comptime try testPopCount();
try testPopCount();
}
fn testPopCount() !void {
{
var x: u32 = 0xffffffff;
try expect(@popCount(u32, x) == 32);
}
{
var x: u5 = 0x1f;
try expect(@popCount(u5, x) == 5);
}
{
var x: u32 = 0xaa;
try expect(@popCount(u32, x) == 4);
}
{
var x: u32 = 0xaaaaaaaa;
try expect(@popCount(u32, x) == 16);
}
{
var x: u32 = 0xaaaaaaaa;
try expect(@popCount(u32, x) == 16);
}
{
var x: i16 = -1;
try expect(@popCount(i16, x) == 16);
}
{
var x: i8 = -120;
try expect(@popCount(i8, x) == 2);
}
comptime {
try expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
}
comptime {
try expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
}
}