diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-02-25 21:43:20 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-25 21:43:20 -0800 |
| commit | 91fb211faa3f37d08da55b0c8df92a6475624316 (patch) | |
| tree | ad824c8fcdd386e378669c21a3e65923b52d0133 /test/behavior/optional.zig | |
| parent | d656c2a7abe90d00ef6dbc3731b82bd26180038a (diff) | |
| parent | 4fcc750ba58f51606c49310bdd7c81c156d48cfc (diff) | |
| download | zig-91fb211faa3f37d08da55b0c8df92a6475624316.tar.gz zig-91fb211faa3f37d08da55b0c8df92a6475624316.zip | |
Merge pull request #18906 from jacobly0/x86_64-tests
x86_64: pass more tests
Diffstat (limited to 'test/behavior/optional.zig')
| -rw-r--r-- | test/behavior/optional.zig | 109 |
1 files changed, 77 insertions, 32 deletions
diff --git a/test/behavior/optional.zig b/test/behavior/optional.zig index 2030645d3d..edf02d3e6e 100644 --- a/test/behavior/optional.zig +++ b/test/behavior/optional.zig @@ -110,44 +110,89 @@ test "nested optional field in struct" { try expect(s.x.?.y == 127); } -test "equality compare optional with non-optional" { +test "equality compare optionals and non-optionals" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - try test_cmp_optional_non_optional(); - try comptime test_cmp_optional_non_optional(); + const S = struct { + fn doTheTest() !void { + var five: isize = 5; + var ten: isize = 10; + var opt_null: ?isize = null; + var opt_ten: ?isize = 10; + _ = .{ &five, &ten, &opt_null, &opt_ten }; + try expect(opt_null != five); + try expect(opt_null != ten); + try expect(opt_ten != five); + try expect(opt_ten == ten); + + var opt_int: ?isize = null; + try expect(opt_int != five); + try expect(opt_int != ten); + try expect(opt_int == opt_null); + try expect(opt_int != opt_ten); + + opt_int = 10; + try expect(opt_int != five); + try expect(opt_int == ten); + try expect(opt_int != opt_null); + try expect(opt_int == opt_ten); + + opt_int = five; + try expect(opt_int == five); + try expect(opt_int != ten); + try expect(opt_int != opt_null); + try expect(opt_int != opt_ten); + + // test evaluation is always lexical + // ensure that the optional isn't always computed before the non-optional + var mutable_state: i32 = 0; + _ = blk1: { + mutable_state += 1; + break :blk1 @as(?f64, 10.0); + } != blk2: { + try expect(mutable_state == 1); + break :blk2 @as(f64, 5.0); + }; + _ = blk1: { + mutable_state += 1; + break :blk1 @as(f64, 10.0); + } != blk2: { + try expect(mutable_state == 2); + break :blk2 @as(?f64, 5.0); + }; + } + }; + + try S.doTheTest(); + try comptime S.doTheTest(); } -fn test_cmp_optional_non_optional() !void { - var ten: i32 = 10; - var opt_ten: ?i32 = 10; - var five: i32 = 5; - var int_n: ?i32 = null; - - _ = .{ &ten, &opt_ten, &five, &int_n }; - - try expect(int_n != ten); - try expect(opt_ten == ten); - try expect(opt_ten != five); - - // test evaluation is always lexical - // ensure that the optional isn't always computed before the non-optional - var mutable_state: i32 = 0; - _ = blk1: { - mutable_state += 1; - break :blk1 @as(?f64, 10.0); - } != blk2: { - try expect(mutable_state == 1); - break :blk2 @as(f64, 5.0); - }; - _ = blk1: { - mutable_state += 1; - break :blk1 @as(f64, 10.0); - } != blk2: { - try expect(mutable_state == 2); - break :blk2 @as(?f64, 5.0); - }; +test "compare optionals with modified payloads" { + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; + + var lhs: ?bool = false; + const lhs_payload = &lhs.?; + var rhs: ?bool = true; + const rhs_payload = &rhs.?; + try expect(lhs != rhs and !(lhs == rhs)); + + lhs = null; + lhs_payload.* = false; + rhs = false; + try expect(lhs != rhs and !(lhs == rhs)); + + lhs = true; + rhs = null; + rhs_payload.* = true; + try expect(lhs != rhs and !(lhs == rhs)); + + lhs = null; + lhs_payload.* = false; + rhs = null; + rhs_payload.* = true; + try expect(lhs == rhs and !(lhs != rhs)); } test "unwrap function call with optional pointer return value" { |
