Iām writing code that will be modifying endianess a lot, based on the host endianness, and I would like to be able to get test coverage for these non-native architectures. Is this possible?
i see this -fqemu
option ā¦
-fqemu, -fno-qemu Integration with system-installed QEMU to execute
foreign-architecture programs on Linux hosts
(default: no)
You can generate a test executable for a different architecture using:
zig test --test-no-exec -femit-bin=zig-out/bin/${name} ${name}.zig
Then you can use whatever option you have available for running it and getting test coverage.
Zig also provides integration for running non native architecture tests with qemu, wine (windows) and darling (macos).
I am using kcov for test coverage, kcov sets breakpoints for getting test coverage, this might work or might not work under an interpreter like qemu.
And this is a game changer. I wanted to test if a toy project of mine worked on Windows, but I only have a Linux box. No fear: zig build -fwine -Dtarget=x86_64-windows-gnu test
was all I needed
Iām having trouble with the QEMU install. I would like to run tests for a big endian architecture, so I guess I will try powerpc64?
I have the following command to run my tests:
zig build -fqemu -Dtarget=powerpc64-linux test --summary all
I am running a debian system, so I attempted QEMU install with:
sudo apt install qemu-system-ppc qemu-utils
But I am still getting this error when running tests:
error: unable to spawn interpreter qemu-ppc64: FileNotFound
I got it working!
this is absolutely nuts that I was able to get this to work in only a few minutes!
I had to install the following packages (debian)
sudo apt install qemu-system-ppc qemu-utils binfmt-support qemu-user-static
and this is my build command:
zig build -fqemu -Dtarget=powerpc64-linux test --summary all
And I got the following output (I put a small warning when my test ran in big endian:
const native_endian = @import("builtin").target.cpu.arch.endian();
test "mem layout packed struct" {
const Pack = packed struct(u48) {
num1: u16 = 0x1234,
num2: u16 = 0x5678,
num3: u9 = 0b1_00000000,
num4: bool = true,
pad: u6 = 0,
};
const memory: [6]u8 = @bitCast(Pack{});
switch (native_endian) {
.big => {
try std.testing.expectEqual(
[6]u8{ 0x03, 0x00, 0x56, 0x78, 0x12, 0x34 },
memory,
);
std.log.warn("ran big endian test!", .{});
},
.little => {
try std.testing.expectEqual(
[6]u8{ 0x34, 0x12, 0x78, 0x56, 0x00, 0x03 },
memory,
);
std.log.warn("ran little endian test!", .{});
},
}
}
test
āā run test stderr
[default] (warn): ran big endian test!
Build Summary: 5/5 steps succeeded; 7/7 tests passed
test success
āā run test 4 passed 28ms MaxRSS:8M
ā āā zig test Debug powerpc64-linux cached 8ms MaxRSS:36M
āā run test 3 passed 23ms MaxRSS:8M
āā zig test Debug powerpc64-linux cached 8ms MaxRSS:36M
again, this is nuts that zig can do this!!!