Commit 4018034708

Andrew Kelley <andrew@ziglang.org>
2019-11-24 22:52:32
add test cases for arbitrary pointer sentinels
1 parent 09ec720
Changed files (1)
test
stage1
behavior
test/stage1/behavior/pointers.zig
@@ -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();
+}