diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-11-29 22:36:52 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-11-29 22:36:52 -0700 |
| commit | 0a9b4d092f58595888f9e4be8ef683b2ed8a0da1 (patch) | |
| tree | eceadc8b9eb451fa4952cffe92638f0e0c5d707e /test/behavior/struct_llvm.zig | |
| parent | 20e19e75fd3030e1b567044ef02cc3958ed9f73a (diff) | |
| download | zig-0a9b4d092f58595888f9e4be8ef683b2ed8a0da1.tar.gz zig-0a9b4d092f58595888f9e4be8ef683b2ed8a0da1.zip | |
I found some more passing behavior tests
Diffstat (limited to 'test/behavior/struct_llvm.zig')
| -rw-r--r-- | test/behavior/struct_llvm.zig | 115 |
1 files changed, 51 insertions, 64 deletions
diff --git a/test/behavior/struct_llvm.zig b/test/behavior/struct_llvm.zig index a809905cef..35c71fe7a2 100644 --- a/test/behavior/struct_llvm.zig +++ b/test/behavior/struct_llvm.zig @@ -6,44 +6,6 @@ const expectEqual = std.testing.expectEqual; const expectEqualSlices = std.testing.expectEqualSlices; const maxInt = std.math.maxInt; -const StructWithNoFields = struct { - fn add(a: i32, b: i32) i32 { - return a + b; - } -}; - -const StructFoo = struct { - a: i32, - b: bool, - c: f32, -}; -test "structs" { - var foo: StructFoo = undefined; - @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo)); - foo.a += 1; - foo.b = foo.a == 1; - try testFoo(foo); - testMutation(&foo); - try expect(foo.c == 100); -} -fn testFoo(foo: StructFoo) !void { - try expect(foo.b); -} -fn testMutation(foo: *StructFoo) void { - foo.c = 100; -} - -test "struct byval assign" { - var foo1: StructFoo = undefined; - var foo2: StructFoo = undefined; - - foo1.a = 1234; - foo2.a = 0; - try expect(foo2.a == 0); - foo2 = foo1; - try expect(foo2.a == 1234); -} - const Node = struct { val: Val, next: *Node, @@ -81,32 +43,6 @@ const VoidStructFieldsFoo = struct { c: void, }; -test "member functions" { - const r = MemberFnRand{ .seed = 1234 }; - try expect(r.getSeed() == 1234); -} -const MemberFnRand = struct { - seed: u32, - pub fn getSeed(r: *const MemberFnRand) u32 { - return r.seed; - } -}; - -test "return struct byval from function" { - const bar = makeBar2(1234, 5678); - try expect(bar.y == 5678); -} -const Bar = struct { - x: i32, - y: i32, -}; -fn makeBar2(x: i32, y: i32) Bar { - return Bar{ - .x = x, - .y = y, - }; -} - test "return empty struct from fn" { _ = testReturnEmptyStructFromFn(); } @@ -114,3 +50,54 @@ const EmptyStruct2 = struct {}; fn testReturnEmptyStructFromFn() EmptyStruct2 { return EmptyStruct2{}; } + +test "pass slice of empty struct to fn" { + try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1); +} +fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize { + return slice.len; +} + +test "for loop over pointers to struct, getting field from struct pointer" { + const S = struct { + const Foo = struct { + name: []const u8, + }; + + var ok = true; + + fn eql(a: []const u8) bool { + _ = a; + return true; + } + + const ArrayList = struct { + fn toSlice(self: *ArrayList) []*Foo { + _ = self; + return @as([*]*Foo, undefined)[0..0]; + } + }; + + fn doTheTest() !void { + var objects: ArrayList = undefined; + + for (objects.toSlice()) |obj| { + if (eql(obj.name)) { + ok = false; + } + } + + try expect(ok); + } + }; + try S.doTheTest(); +} + +test "self-referencing struct via array member" { + const T = struct { + children: [1]*@This(), + }; + var x: T = undefined; + x = T{ .children = .{&x} }; + try expect(x.children[0] == &x); +} |
