tests: update "atomicrmw with ints" to test u8 through u64
This commit is contained in:
@@ -251,31 +251,61 @@ test "atomicrmw with ints" {
|
||||
return error.SkipZigTest;
|
||||
}
|
||||
|
||||
try testAtomicRmwInt();
|
||||
comptime try testAtomicRmwInt();
|
||||
// TODO: https://github.com/ziglang/zig/issues/13989
|
||||
const bit_values = [_]usize{ 8, 16, 32, 64 };// ++ if (builtin.zig_backend != .stage2_c) [_]usize{ } else [_]usize{ 128 };
|
||||
inline for (bit_values) |bits| {
|
||||
try testAtomicRmwInt(.unsigned, bits);
|
||||
comptime try testAtomicRmwInt(.unsigned, bits);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
fn testAtomicRmwInt(comptime signedness: std.builtin.Signedness, comptime N: usize) !void {
|
||||
const int = std.meta.Int(signedness, N);
|
||||
|
||||
_ = @atomicRmw(u8, &x, .Max, 1, .SeqCst);
|
||||
try expect(x == 0xfd);
|
||||
_ = @atomicRmw(u8, &x, .Min, 1, .SeqCst);
|
||||
try expect(x == 1);
|
||||
var x: int = 1;
|
||||
var res = @atomicRmw(int, &x, .Xchg, 3, .SeqCst);
|
||||
try expect(x == 3 and res == 1);
|
||||
|
||||
res = @atomicRmw(int, &x, .Add, 3, .SeqCst);
|
||||
var y: int = 3;
|
||||
try expect(res == y);
|
||||
y = y + 3;
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .Sub, 1, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = y - 1;
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .And, 4, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = y & 4;
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .Nand, 4, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = ~(y & 4);
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .Or, 6, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = y | 6;
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .Xor, 2, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = y ^ 2;
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .Max, 1, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = @max(y, 1);
|
||||
try expect(x == y);
|
||||
|
||||
res = @atomicRmw(int, &x, .Min, 1, .SeqCst);
|
||||
try expect(res == y);
|
||||
y = @min(y, 1);
|
||||
try expect(x == y);
|
||||
}
|
||||
|
||||
test "atomics with different types" {
|
||||
|
||||
Reference in New Issue
Block a user