aboutsummaryrefslogtreecommitdiff
path: root/test/cases/struct.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/cases/struct.zig')
-rw-r--r--test/cases/struct.zig131
1 files changed, 75 insertions, 56 deletions
diff --git a/test/cases/struct.zig b/test/cases/struct.zig
index c3df97678b..20d46999d5 100644
--- a/test/cases/struct.zig
+++ b/test/cases/struct.zig
@@ -2,9 +2,11 @@ const assert = @import("std").debug.assert;
const builtin = @import("builtin");
const StructWithNoFields = struct {
- fn add(a: i32, b: i32) i32 { return a + b; }
+ fn add(a: i32, b: i32) i32 {
+ return a + b;
+ }
};
-const empty_global_instance = StructWithNoFields {};
+const empty_global_instance = StructWithNoFields{};
test "call struct static method" {
const result = StructWithNoFields.add(3, 4);
@@ -25,7 +27,7 @@ test "invake static method in global scope" {
}
test "void struct fields" {
- const foo = VoidStructFieldsFoo {
+ const foo = VoidStructFieldsFoo{
.a = void{},
.b = 1,
.c = void{},
@@ -34,15 +36,14 @@ test "void struct fields" {
assert(@sizeOf(VoidStructFieldsFoo) == 4);
}
const VoidStructFieldsFoo = struct {
- a : void,
- b : i32,
- c : void,
+ a: void,
+ b: i32,
+ c: void,
};
-
test "structs" {
var foo: StructFoo = undefined;
- @memset(@ptrCast(&u8, &foo), 0, @sizeOf(StructFoo));
+ @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));
foo.a += 1;
foo.b = foo.a == 1;
testFoo(foo);
@@ -50,21 +51,20 @@ test "structs" {
assert(foo.c == 100);
}
const StructFoo = struct {
- a : i32,
- b : bool,
- c : f32,
+ a: i32,
+ b: bool,
+ c: f32,
};
-fn testFoo(foo: &const StructFoo) void {
+fn testFoo(foo: *const StructFoo) void {
assert(foo.b);
}
-fn testMutation(foo: &StructFoo) void {
+fn testMutation(foo: *StructFoo) void {
foo.c = 100;
}
-
const Node = struct {
val: Val,
- next: &Node,
+ next: *Node,
};
const Val = struct {
@@ -72,10 +72,10 @@ const Val = struct {
};
test "struct point to self" {
- var root : Node = undefined;
+ var root: Node = undefined;
root.val.x = 1;
- var node : Node = undefined;
+ var node: Node = undefined;
node.next = &root;
node.val.x = 2;
@@ -85,8 +85,8 @@ test "struct point to self" {
}
test "struct byval assign" {
- var foo1 : StructFoo = undefined;
- var foo2 : StructFoo = undefined;
+ var foo1: StructFoo = undefined;
+ var foo2: StructFoo = undefined;
foo1.a = 1234;
foo2.a = 0;
@@ -96,51 +96,52 @@ test "struct byval assign" {
}
fn structInitializer() void {
- const val = Val { .x = 42 };
+ const val = Val{ .x = 42 };
assert(val.x == 42);
}
-
test "fn call of struct field" {
- assert(callStructField(Foo {.ptr = aFunc,}) == 13);
+ assert(callStructField(Foo{ .ptr = aFunc }) == 13);
}
const Foo = struct {
- ptr: fn() i32,
+ ptr: fn () i32,
};
-fn aFunc() i32 { return 13; }
+fn aFunc() i32 {
+ return 13;
+}
-fn callStructField(foo: &const Foo) i32 {
+fn callStructField(foo: *const Foo) i32 {
return foo.ptr();
}
-
test "store member function in variable" {
- const instance = MemberFnTestFoo { .x = 1234, };
+ const instance = MemberFnTestFoo{ .x = 1234 };
const memberFn = MemberFnTestFoo.member;
const result = memberFn(instance);
assert(result == 1234);
}
const MemberFnTestFoo = struct {
x: i32,
- fn member(foo: &const MemberFnTestFoo) i32 { return foo.x; }
+ fn member(foo: *const MemberFnTestFoo) i32 {
+ return foo.x;
+ }
};
-
test "call member function directly" {
- const instance = MemberFnTestFoo { .x = 1234, };
+ const instance = MemberFnTestFoo{ .x = 1234 };
const result = MemberFnTestFoo.member(instance);
assert(result == 1234);
}
test "member functions" {
- const r = MemberFnRand {.seed = 1234};
+ const r = MemberFnRand{ .seed = 1234 };
assert(r.getSeed() == 1234);
}
const MemberFnRand = struct {
seed: u32,
- pub fn getSeed(r: &const MemberFnRand) u32 {
+ pub fn getSeed(r: *const MemberFnRand) u32 {
return r.seed;
}
};
@@ -154,7 +155,7 @@ const Bar = struct {
y: i32,
};
fn makeBar(x: i32, y: i32) Bar {
- return Bar {
+ return Bar{
.x = x,
.y = y,
};
@@ -165,22 +166,21 @@ test "empty struct method call" {
assert(es.method() == 1234);
}
const EmptyStruct = struct {
- fn method(es: &const EmptyStruct) i32 {
+ fn method(es: *const EmptyStruct) i32 {
return 1234;
}
};
-
test "return empty struct from fn" {
_ = testReturnEmptyStructFromFn();
}
const EmptyStruct2 = struct {};
fn testReturnEmptyStructFromFn() EmptyStruct2 {
- return EmptyStruct2 {};
+ return EmptyStruct2{};
}
test "pass slice of empty struct to fn" {
- assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
+ assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{EmptyStruct2{}}) == 1);
}
fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
return slice.len;
@@ -192,7 +192,7 @@ const APackedStruct = packed struct {
};
test "packed struct" {
- var foo = APackedStruct {
+ var foo = APackedStruct{
.x = 1,
.y = 2,
};
@@ -201,14 +201,13 @@ test "packed struct" {
assert(four == 4);
}
-
const BitField1 = packed struct {
a: u3,
b: u3,
c: u2,
};
-const bit_field_1 = BitField1 {
+const bit_field_1 = BitField1{
.a = 1,
.b = 2,
.c = 3,
@@ -229,19 +228,18 @@ test "bit field access" {
assert(data.b == 3);
}
-fn getA(data: &const BitField1) u3 {
+fn getA(data: *const BitField1) u3 {
return data.a;
}
-fn getB(data: &const BitField1) u3 {
+fn getB(data: *const BitField1) u3 {
return data.b;
}
-fn getC(data: &const BitField1) u2 {
+fn getC(data: *const BitField1) u2 {
return data.c;
}
-const u24 = @IntType(false, 24);
const Foo24Bits = packed struct {
field: u24,
};
@@ -258,7 +256,7 @@ test "packed struct 24bits" {
assert(@sizeOf(Foo96Bits) == 12);
}
- var value = Foo96Bits {
+ var value = Foo96Bits{
.a = 0,
.b = 0,
.c = 0,
@@ -303,7 +301,7 @@ test "packed array 24bits" {
var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);
bytes[bytes.len - 1] = 0xaa;
- const ptr = &([]FooArray24Bits)(bytes[0..bytes.len - 1])[0];
+ const ptr = &@bytesToSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
assert(ptr.a == 0);
assert(ptr.b[0].field == 0);
assert(ptr.b[1].field == 0);
@@ -352,7 +350,7 @@ test "aligned array of packed struct" {
}
var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned);
- const ptr = &([]FooArrayOfAligned)(bytes[0..bytes.len])[0];
+ const ptr = &@bytesToSlice(FooArrayOfAligned, bytes[0..bytes.len])[0];
assert(ptr.a[0].a == 0xbb);
assert(ptr.a[0].b == 0xbb);
@@ -360,16 +358,20 @@ test "aligned array of packed struct" {
assert(ptr.a[1].b == 0xbb);
}
-
-
test "runtime struct initialization of bitfield" {
- const s1 = Nibbles { .x = x1, .y = x1 };
- const s2 = Nibbles { .x = u4(x2), .y = u4(x2) };
+ const s1 = Nibbles{
+ .x = x1,
+ .y = x1,
+ };
+ const s2 = Nibbles{
+ .x = @intCast(u4, x2),
+ .y = @intCast(u4, x2),
+ };
assert(s1.x == x1);
assert(s1.y == x1);
- assert(s2.x == u4(x2));
- assert(s2.y == u4(x2));
+ assert(s2.x == @intCast(u4, x2));
+ assert(s2.y == @intCast(u4, x2));
}
var x1 = u4(1);
@@ -393,8 +395,8 @@ const Bitfields = packed struct {
test "native bit field understands endianness" {
var all: u64 = 0x7765443322221111;
var bytes: [8]u8 = undefined;
- @memcpy(&bytes[0], @ptrCast(&u8, &all), 8);
- var bitfields = *@ptrCast(&Bitfields, &bytes[0]);
+ @memcpy(bytes[0..].ptr, @ptrCast([*]u8, &all), 8);
+ var bitfields = @ptrCast(*Bitfields, bytes[0..].ptr).*;
assert(bitfields.f1 == 0x1111);
assert(bitfields.f2 == 0x2222);
@@ -412,9 +414,26 @@ test "align 1 field before self referential align 8 field as slice return type"
const Expr = union(enum) {
Literal: u8,
- Question: &Expr,
+ Question: *Expr,
};
fn alloc(comptime T: type) []T {
return []T{};
}
+
+test "call method with mutable reference to struct with no fields" {
+ const S = struct {
+ fn doC(s: *const this) bool {
+ return true;
+ }
+ fn do(s: *this) bool {
+ return true;
+ }
+ };
+
+ var s = S{};
+ assert(S.doC(&s));
+ assert(s.doC());
+ assert(S.do(&s));
+ assert(s.do());
+}