zig/test/behavior/pointers.zig
Andrew Kelley f81b2531cb stage2: pass some pointer tests
* New AIR instructions: ptr_add, ptr_sub, ptr_elem_val, ptr_ptr_elem_val
   - See the doc comments for details.
 * Sema: implement runtime pointer arithmetic.
 * Sema: implement elem_val for many-pointers.
 * Sema: support coercion from `*[N:s]T` to `[*]T`.
 * Type: isIndexable handles many-pointers.
2021-08-07 15:46:53 -07:00

47 lines
923 B
Zig

const std = @import("std");
const testing = std.testing;
const expect = testing.expect;
const expectError = testing.expectError;
test "dereference pointer" {
comptime try testDerefPtr();
try testDerefPtr();
}
fn testDerefPtr() !void {
var x: i32 = 1234;
var y = &x;
y.* += 1;
try expect(x == 1235);
}
test "pointer arithmetic" {
var ptr: [*]const u8 = "abcd";
try expect(ptr[0] == 'a');
ptr += 1;
try expect(ptr[0] == 'b');
ptr += 1;
try expect(ptr[0] == 'c');
ptr += 1;
try expect(ptr[0] == 'd');
ptr += 1;
try expect(ptr[0] == 0);
ptr -= 1;
try expect(ptr[0] == 'd');
ptr -= 1;
try expect(ptr[0] == 'c');
ptr -= 1;
try expect(ptr[0] == 'b');
ptr -= 1;
try expect(ptr[0] == 'a');
}
test "double pointer parsing" {
comptime try expect(PtrOf(PtrOf(i32)) == **i32);
}
fn PtrOf(comptime T: type) type {
return *T;
}