add test cases for arbitrary pointer sentinels

This commit is contained in:
Andrew Kelley 2019-11-24 16:52:32 -05:00
parent 09ec720dab
commit 4018034708
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9

View File

@ -213,3 +213,51 @@ test "null terminated pointer" {
S.doTheTest();
comptime S.doTheTest();
}
test "allow any sentinel" {
const S = struct {
fn doTheTest() void {
var array = [_:std.math.minInt(i32)]i32{1, 2, 3, 4};
var ptr: [*:std.math.minInt(i32)]i32 = &array;
expect(ptr[4] == std.math.minInt(i32));
}
};
S.doTheTest();
comptime S.doTheTest();
}
test "pointer sentinel with enums" {
const S = struct {
const Number = enum{one, two, sentinel};
fn doTheTest() void {
var ptr: [*:.sentinel]Number = &[_:.sentinel]Number{.one, .two, .two, .one};
expect(ptr[4] == .sentinel); // TODO this should be comptime expect, see #3731
}
};
S.doTheTest();
comptime S.doTheTest();
}
test "pointer sentinel with optional element" {
const S = struct {
fn doTheTest() void {
var ptr: [*:null]?i32 = &[_:null]?i32{1, 2, 3, 4};
expect(ptr[4] == null); // TODO this should be comptime expect, see #3731
}
};
S.doTheTest();
comptime S.doTheTest();
}
test "pointer sentinel with +inf" {
const S = struct {
fn doTheTest() void {
const inf = std.math.inf_f32;
var ptr: [*:inf]f32 = &[_:inf]f32{1.1, 2.2, 3.3, 4.4};
expect(ptr[4] == inf); // TODO this should be comptime expect, see #3731
}
};
S.doTheTest();
comptime S.doTheTest();
}