diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-11-30 00:19:37 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-11-30 00:19:37 -0700 |
| commit | 692c254336da71cbe21aaf9fbc21240fd1269b95 (patch) | |
| tree | fe9c5fdfd23281c593345ae40c90ccecab48ce9b /test/behavior/struct_llvm.zig | |
| parent | 902df103c6151c257c90de9ba5f29f7f4b9dbea2 (diff) | |
| download | zig-692c254336da71cbe21aaf9fbc21240fd1269b95.tar.gz zig-692c254336da71cbe21aaf9fbc21240fd1269b95.zip | |
Revert "I found some more passing behavior tests"
This reverts commit 0a9b4d092f58595888f9e4be8ef683b2ed8a0da1.
Hm, these are all passing for me locally. I'll have to do some
troubleshooting to figure out which one(s) are failing on the CI.
Diffstat (limited to 'test/behavior/struct_llvm.zig')
| -rw-r--r-- | test/behavior/struct_llvm.zig | 115 |
1 files changed, 64 insertions, 51 deletions
diff --git a/test/behavior/struct_llvm.zig b/test/behavior/struct_llvm.zig index 35c71fe7a2..a809905cef 100644 --- a/test/behavior/struct_llvm.zig +++ b/test/behavior/struct_llvm.zig @@ -6,6 +6,44 @@ 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, @@ -43,61 +81,36 @@ const VoidStructFieldsFoo = struct { c: void, }; -test "return empty struct from fn" { - _ = testReturnEmptyStructFromFn(); -} -const EmptyStruct2 = struct {}; -fn testReturnEmptyStructFromFn() EmptyStruct2 { - return EmptyStruct2{}; +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 "pass slice of empty struct to fn" { - try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1); +test "return struct byval from function" { + const bar = makeBar2(1234, 5678); + try expect(bar.y == 5678); } -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); - } +const Bar = struct { + x: i32, + y: i32, +}; +fn makeBar2(x: i32, y: i32) Bar { + return Bar{ + .x = x, + .y = y, }; - 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); +test "return empty struct from fn" { + _ = testReturnEmptyStructFromFn(); +} +const EmptyStruct2 = struct {}; +fn testReturnEmptyStructFromFn() EmptyStruct2 { + return EmptyStruct2{}; } |
