zig/test/behavior/void.zig

55 lines
1.2 KiB
Zig
Raw Normal View History

const expect = @import("std").testing.expect;
2022-02-22 21:15:09 +08:00
const builtin = @import("builtin");
const Foo = struct {
a: void,
b: i32,
c: void,
};
2017-05-24 09:38:31 +08:00
test "compare void with void compile time known" {
comptime {
const foo = Foo{
.a = {},
.b = 1,
.c = {},
};
try expect(foo.a == {});
}
}
test "iterate over a void slice" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
2022-02-22 21:15:09 +08:00
var j: usize = 0;
for (times(10)) |_, i| {
try expect(i == j);
j += 1;
}
}
fn times(n: usize) []const void {
2019-11-07 12:25:57 +08:00
return @as([*]void, undefined)[0..n];
}
test "void optional" {
2022-02-22 21:15:09 +08:00
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
2022-02-22 21:15:09 +08:00
var x: ?void = {};
try expect(x != null);
}
test "void array as a local variable initializer" {
var x = [_]void{{}} ** 1004;
_ = x[0];
}
const void_constant = {};
test "reference to void constants" {
var a = void_constant;
_ = a;
}