| ... | ... | @@ -4,7 +4,9 @@ |
| 4 | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | 5 | // and substantial portions of the software. |
| 6 | 6 | |
| 7 | | const builtin = @import("std").builtin; |
| 7 | const std = @import("std"); |
| 8 | const builtin = std.builtin; |
| 9 | const testing = std.testing; |
| 8 | 10 | |
| 9 | 11 | /// Thread-safe, lock-free integer |
| 10 | 12 | pub fn Int(comptime T: type) type { |
| ... | ... | @@ -61,3 +63,15 @@ pub fn Int(comptime T: type) type { |
| 61 | 63 | } |
| 62 | 64 | }; |
| 63 | 65 | } |
| 66 | |
| 67 | test "std.atomic.Int" { |
| 68 | var a = Int(u8).init(0); |
| 69 | testing.expectEqual(@as(u8, 0), a.incr()); |
| 70 | testing.expectEqual(@as(u8, 1), a.load(.SeqCst)); |
| 71 | a.store(42, .SeqCst); |
| 72 | testing.expectEqual(@as(u8, 42), a.decr()); |
| 73 | testing.expectEqual(@as(u8, 41), a.xchg(100)); |
| 74 | testing.expectEqual(@as(u8, 100), a.fetchAdd(5)); |
| 75 | testing.expectEqual(@as(u8, 105), a.get()); |
| 76 | a.set(200); |
| 77 | } |