mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
- Previously, some of the compress tests used `@src()` in combination with `dirname` and `openDirAbsolute` to read test files at runtime, which both excludes platforms that `openDirAbsolute` is not implemented for (WASI) and platforms that `SourceLocation.file` is not absolute (this was true for me locally on Windows). Instead of converting the tests to use `fs.cwd().openDir`, they have been converted to use `@embedFile` to avoid any potential problems with the runtime cwd. - In order to use `@embedFile`, some of the `[]u8` parameters needed to be changed to `[]const u8`; none of them needed to be non-const anyway - The tests now use `expectEqual` and `expectEqualSlices` where appropriate for better diagnostics
34 lines
986 B
Zig
34 lines
986 B
Zig
const math = @import("std").math;
|
|
|
|
// Reverse bit-by-bit a N-bit code.
|
|
pub fn bitReverse(comptime T: type, value: T, N: usize) T {
|
|
const r = @bitReverse(value);
|
|
return r >> @intCast(math.Log2Int(T), @typeInfo(T).Int.bits - N);
|
|
}
|
|
|
|
test "bitReverse" {
|
|
const std = @import("std");
|
|
|
|
const ReverseBitsTest = struct {
|
|
in: u16,
|
|
bit_count: u5,
|
|
out: u16,
|
|
};
|
|
|
|
var reverse_bits_tests = [_]ReverseBitsTest{
|
|
.{ .in = 1, .bit_count = 1, .out = 1 },
|
|
.{ .in = 1, .bit_count = 2, .out = 2 },
|
|
.{ .in = 1, .bit_count = 3, .out = 4 },
|
|
.{ .in = 1, .bit_count = 4, .out = 8 },
|
|
.{ .in = 1, .bit_count = 5, .out = 16 },
|
|
.{ .in = 17, .bit_count = 5, .out = 17 },
|
|
.{ .in = 257, .bit_count = 9, .out = 257 },
|
|
.{ .in = 29, .bit_count = 5, .out = 23 },
|
|
};
|
|
|
|
for (reverse_bits_tests) |h| {
|
|
var v = bitReverse(u16, h.in, h.bit_count);
|
|
try std.testing.expectEqual(h.out, v);
|
|
}
|
|
}
|