diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-09-13 22:01:40 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-09-13 22:01:40 -0700 |
| commit | 264acfdf3caef663cddc39c2bf83818abb0e0073 (patch) | |
| tree | 26f3019c76b210d3c501745298991d2c8cea18f2 /test | |
| parent | 97d69e3352ab50f88580c383b5f375b0edadacfd (diff) | |
| download | zig-264acfdf3caef663cddc39c2bf83818abb0e0073.tar.gz zig-264acfdf3caef663cddc39c2bf83818abb0e0073.zip | |
stage2: fix incorrect spelling of AtomicOrder
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior.zig | 3 | ||||
| -rw-r--r-- | test/behavior/atomics.zig | 212 | ||||
| -rw-r--r-- | test/behavior/atomics_stage1.zig | 216 |
3 files changed, 218 insertions, 213 deletions
diff --git a/test/behavior.zig b/test/behavior.zig index b39c50df32..366753c3bf 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -11,6 +11,7 @@ test { _ = @import("behavior/cast.zig"); _ = @import("behavior/array.zig"); _ = @import("behavior/usingnamespace.zig"); + _ = @import("behavior/atomics.zig"); if (!builtin.zig_is_stage2) { // Tests that only pass for stage1. @@ -21,7 +22,7 @@ test { _ = @import("behavior/asm.zig"); _ = @import("behavior/async_fn.zig"); } - _ = @import("behavior/atomics.zig"); + _ = @import("behavior/atomics_stage1.zig"); _ = @import("behavior/await_struct.zig"); _ = @import("behavior/bit_shifting.zig"); _ = @import("behavior/bitcast.zig"); diff --git a/test/behavior/atomics.zig b/test/behavior/atomics.zig index 18836931aa..f6c94f7155 100644 --- a/test/behavior/atomics.zig +++ b/test/behavior/atomics.zig @@ -2,215 +2,3 @@ const std = @import("std"); const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const builtin = @import("builtin"); - -test "cmpxchg" { - try testCmpxchg(); - comptime try testCmpxchg(); -} - -fn testCmpxchg() !void { - var x: i32 = 1234; - if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| { - try expect(x1 == 1234); - } else { - @panic("cmpxchg should have failed"); - } - - while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| { - try expect(x1 == 1234); - } - try expect(x == 5678); - - try expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null); - try expect(x == 42); -} - -test "fence" { - var x: i32 = 1234; - @fence(.SeqCst); - x = 5678; -} - -test "atomicrmw and atomicload" { - var data: u8 = 200; - try testAtomicRmw(&data); - try expect(data == 42); - try testAtomicLoad(&data); -} - -fn testAtomicRmw(ptr: *u8) !void { - const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst); - try expect(prev_value == 200); - comptime { - var x: i32 = 1234; - const y: i32 = 12345; - try expect(@atomicLoad(i32, &x, .SeqCst) == 1234); - try expect(@atomicLoad(i32, &y, .SeqCst) == 12345); - } -} - -fn testAtomicLoad(ptr: *u8) !void { - const x = @atomicLoad(u8, ptr, .SeqCst); - try expect(x == 42); -} - -test "cmpxchg with ptr" { - var data1: i32 = 1234; - var data2: i32 = 5678; - var data3: i32 = 9101; - var x: *i32 = &data1; - if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| { - try expect(x1 == &data1); - } else { - @panic("cmpxchg should have failed"); - } - - while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| { - try expect(x1 == &data1); - } - try expect(x == &data3); - - try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null); - try expect(x == &data2); -} - -test "128-bit cmpxchg" { - try test_u128_cmpxchg(); - comptime try test_u128_cmpxchg(); -} - -fn test_u128_cmpxchg() !void { - if (std.Target.current.cpu.arch != .x86_64) return error.SkipZigTest; - if (comptime !std.Target.x86.featureSetHas(std.Target.current.cpu.features, .cx16)) return error.SkipZigTest; - - var x: u128 = 1234; - if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| { - try expect(x1 == 1234); - } else { - @panic("cmpxchg should have failed"); - } - - while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| { - try expect(x1 == 1234); - } - try expect(x == 5678); - - try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null); - try expect(x == 42); -} - -test "cmpxchg with ignored result" { - var x: i32 = 1234; - - _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic); - - try expectEqual(@as(i32, 5678), x); -} - -var a_global_variable = @as(u32, 1234); - -test "cmpxchg on a global variable" { - _ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic); - try expectEqual(@as(u32, 42), a_global_variable); -} - -test "atomic load and rmw with enum" { - const Value = enum(u8) { - a, - b, - c, - }; - var x = Value.a; - - try expect(@atomicLoad(Value, &x, .SeqCst) != .b); - - _ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst); - try expect(@atomicLoad(Value, &x, .SeqCst) == .c); - try expect(@atomicLoad(Value, &x, .SeqCst) != .a); - try expect(@atomicLoad(Value, &x, .SeqCst) != .b); -} - -test "atomic store" { - var x: u32 = 0; - @atomicStore(u32, &x, 1, .SeqCst); - try expect(@atomicLoad(u32, &x, .SeqCst) == 1); - @atomicStore(u32, &x, 12345678, .SeqCst); - try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678); -} - -test "atomic store comptime" { - comptime try testAtomicStore(); - try testAtomicStore(); -} - -fn testAtomicStore() !void { - var x: u32 = 0; - @atomicStore(u32, &x, 1, .SeqCst); - try expect(@atomicLoad(u32, &x, .SeqCst) == 1); - @atomicStore(u32, &x, 12345678, .SeqCst); - try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678); -} - -test "atomicrmw with floats" { - try testAtomicRmwFloat(); - comptime try testAtomicRmwFloat(); -} - -fn testAtomicRmwFloat() !void { - var x: f32 = 0; - try expect(x == 0); - _ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst); - try expect(x == 1); - _ = @atomicRmw(f32, &x, .Add, 5, .SeqCst); - try expect(x == 6); - _ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst); - try expect(x == 4); -} - -test "atomicrmw with ints" { - try testAtomicRmwInt(); - comptime try testAtomicRmwInt(); -} - -fn testAtomicRmwInt() !void { - var x: u8 = 1; - var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst); - try expect(x == 3 and res == 1); - _ = @atomicRmw(u8, &x, .Add, 3, .SeqCst); - try expect(x == 6); - _ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst); - try expect(x == 5); - _ = @atomicRmw(u8, &x, .And, 4, .SeqCst); - try expect(x == 4); - _ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst); - try expect(x == 0xfb); - _ = @atomicRmw(u8, &x, .Or, 6, .SeqCst); - try expect(x == 0xff); - _ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst); - try expect(x == 0xfd); - - _ = @atomicRmw(u8, &x, .Max, 1, .SeqCst); - try expect(x == 0xfd); - _ = @atomicRmw(u8, &x, .Min, 1, .SeqCst); - try expect(x == 1); -} - -test "atomics with different types" { - try testAtomicsWithType(bool, true, false); - inline for (.{ u1, i4, u5, i15, u24 }) |T| { - try testAtomicsWithType(T, 0, 1); - } - try testAtomicsWithType(u0, 0, 0); - try testAtomicsWithType(i0, 0, 0); -} - -fn testAtomicsWithType(comptime T: type, a: T, b: T) !void { - var x: T = b; - @atomicStore(T, &x, a, .SeqCst); - try expect(x == a); - try expect(@atomicLoad(T, &x, .SeqCst) == a); - try expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a); - try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null); - if (@sizeOf(T) != 0) - try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a); -} diff --git a/test/behavior/atomics_stage1.zig b/test/behavior/atomics_stage1.zig new file mode 100644 index 0000000000..18836931aa --- /dev/null +++ b/test/behavior/atomics_stage1.zig @@ -0,0 +1,216 @@ +const std = @import("std"); +const expect = std.testing.expect; +const expectEqual = std.testing.expectEqual; +const builtin = @import("builtin"); + +test "cmpxchg" { + try testCmpxchg(); + comptime try testCmpxchg(); +} + +fn testCmpxchg() !void { + var x: i32 = 1234; + if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| { + try expect(x1 == 1234); + } else { + @panic("cmpxchg should have failed"); + } + + while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| { + try expect(x1 == 1234); + } + try expect(x == 5678); + + try expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null); + try expect(x == 42); +} + +test "fence" { + var x: i32 = 1234; + @fence(.SeqCst); + x = 5678; +} + +test "atomicrmw and atomicload" { + var data: u8 = 200; + try testAtomicRmw(&data); + try expect(data == 42); + try testAtomicLoad(&data); +} + +fn testAtomicRmw(ptr: *u8) !void { + const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst); + try expect(prev_value == 200); + comptime { + var x: i32 = 1234; + const y: i32 = 12345; + try expect(@atomicLoad(i32, &x, .SeqCst) == 1234); + try expect(@atomicLoad(i32, &y, .SeqCst) == 12345); + } +} + +fn testAtomicLoad(ptr: *u8) !void { + const x = @atomicLoad(u8, ptr, .SeqCst); + try expect(x == 42); +} + +test "cmpxchg with ptr" { + var data1: i32 = 1234; + var data2: i32 = 5678; + var data3: i32 = 9101; + var x: *i32 = &data1; + if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| { + try expect(x1 == &data1); + } else { + @panic("cmpxchg should have failed"); + } + + while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| { + try expect(x1 == &data1); + } + try expect(x == &data3); + + try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null); + try expect(x == &data2); +} + +test "128-bit cmpxchg" { + try test_u128_cmpxchg(); + comptime try test_u128_cmpxchg(); +} + +fn test_u128_cmpxchg() !void { + if (std.Target.current.cpu.arch != .x86_64) return error.SkipZigTest; + if (comptime !std.Target.x86.featureSetHas(std.Target.current.cpu.features, .cx16)) return error.SkipZigTest; + + var x: u128 = 1234; + if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| { + try expect(x1 == 1234); + } else { + @panic("cmpxchg should have failed"); + } + + while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| { + try expect(x1 == 1234); + } + try expect(x == 5678); + + try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null); + try expect(x == 42); +} + +test "cmpxchg with ignored result" { + var x: i32 = 1234; + + _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic); + + try expectEqual(@as(i32, 5678), x); +} + +var a_global_variable = @as(u32, 1234); + +test "cmpxchg on a global variable" { + _ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic); + try expectEqual(@as(u32, 42), a_global_variable); +} + +test "atomic load and rmw with enum" { + const Value = enum(u8) { + a, + b, + c, + }; + var x = Value.a; + + try expect(@atomicLoad(Value, &x, .SeqCst) != .b); + + _ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst); + try expect(@atomicLoad(Value, &x, .SeqCst) == .c); + try expect(@atomicLoad(Value, &x, .SeqCst) != .a); + try expect(@atomicLoad(Value, &x, .SeqCst) != .b); +} + +test "atomic store" { + var x: u32 = 0; + @atomicStore(u32, &x, 1, .SeqCst); + try expect(@atomicLoad(u32, &x, .SeqCst) == 1); + @atomicStore(u32, &x, 12345678, .SeqCst); + try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678); +} + +test "atomic store comptime" { + comptime try testAtomicStore(); + try testAtomicStore(); +} + +fn testAtomicStore() !void { + var x: u32 = 0; + @atomicStore(u32, &x, 1, .SeqCst); + try expect(@atomicLoad(u32, &x, .SeqCst) == 1); + @atomicStore(u32, &x, 12345678, .SeqCst); + try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678); +} + +test "atomicrmw with floats" { + try testAtomicRmwFloat(); + comptime try testAtomicRmwFloat(); +} + +fn testAtomicRmwFloat() !void { + var x: f32 = 0; + try expect(x == 0); + _ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst); + try expect(x == 1); + _ = @atomicRmw(f32, &x, .Add, 5, .SeqCst); + try expect(x == 6); + _ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst); + try expect(x == 4); +} + +test "atomicrmw with ints" { + try testAtomicRmwInt(); + comptime try testAtomicRmwInt(); +} + +fn testAtomicRmwInt() !void { + var x: u8 = 1; + var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst); + try expect(x == 3 and res == 1); + _ = @atomicRmw(u8, &x, .Add, 3, .SeqCst); + try expect(x == 6); + _ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst); + try expect(x == 5); + _ = @atomicRmw(u8, &x, .And, 4, .SeqCst); + try expect(x == 4); + _ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst); + try expect(x == 0xfb); + _ = @atomicRmw(u8, &x, .Or, 6, .SeqCst); + try expect(x == 0xff); + _ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst); + try expect(x == 0xfd); + + _ = @atomicRmw(u8, &x, .Max, 1, .SeqCst); + try expect(x == 0xfd); + _ = @atomicRmw(u8, &x, .Min, 1, .SeqCst); + try expect(x == 1); +} + +test "atomics with different types" { + try testAtomicsWithType(bool, true, false); + inline for (.{ u1, i4, u5, i15, u24 }) |T| { + try testAtomicsWithType(T, 0, 1); + } + try testAtomicsWithType(u0, 0, 0); + try testAtomicsWithType(i0, 0, 0); +} + +fn testAtomicsWithType(comptime T: type, a: T, b: T) !void { + var x: T = b; + @atomicStore(T, &x, a, .SeqCst); + try expect(x == a); + try expect(@atomicLoad(T, &x, .SeqCst) == a); + try expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a); + try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null); + if (@sizeOf(T) != 0) + try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a); +} |
