aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2018-12-17 11:05:50 -0500
committerAndrew Kelley <andrew@ziglang.org>2018-12-17 11:05:50 -0500
commitf75262b79f0656ebfac7fb5bd2d6d8e8cce01258 (patch)
tree6c8ac544c9d56c1f6381da557ec3e6512a1a66ea /test
parent5a68c600235fb7cc0e1bf6a8b314e37890e3823b (diff)
downloadzig-f75262b79f0656ebfac7fb5bd2d6d8e8cce01258.tar.gz
zig-f75262b79f0656ebfac7fb5bd2d6d8e8cce01258.zip
fix comptime pointer reinterpretation array index offset
closes #1835
Diffstat (limited to 'test')
-rw-r--r--test/behavior.zig1
-rw-r--r--test/cases/ptrcast.zig17
2 files changed, 18 insertions, 0 deletions
diff --git a/test/behavior.zig b/test/behavior.zig
index 499c20ee20..e32063dec8 100644
--- a/test/behavior.zig
+++ b/test/behavior.zig
@@ -52,6 +52,7 @@ comptime {
_ = @import("cases/optional.zig");
_ = @import("cases/pointers.zig");
_ = @import("cases/popcount.zig");
+ _ = @import("cases/ptrcast.zig");
_ = @import("cases/pub_enum/index.zig");
_ = @import("cases/ref_var_in_if_after_if_2nd_switch_prong.zig");
_ = @import("cases/reflection.zig");
diff --git a/test/cases/ptrcast.zig b/test/cases/ptrcast.zig
new file mode 100644
index 0000000000..071087c5c4
--- /dev/null
+++ b/test/cases/ptrcast.zig
@@ -0,0 +1,17 @@
+const builtin = @import("builtin");
+const std = @import("std");
+const assertOrPanic = std.debug.assertOrPanic;
+
+test "reinterpret bytes as integer with nonzero offset" {
+ testReinterpretBytesAsInteger();
+ comptime testReinterpretBytesAsInteger();
+}
+
+fn testReinterpretBytesAsInteger() void {
+ const bytes = "\x12\x34\x56\x78\xab";
+ const expected = switch (builtin.endian) {
+ builtin.Endian.Little => 0xab785634,
+ builtin.Endian.Big => 0x345678ab,
+ };
+ assertOrPanic(@ptrCast(*align(1) const u32, bytes[1..5].ptr).* == expected);
+}