]> git.proxmox.com Git - rustc.git/blame - src/libcore/tests/atomic.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / libcore / tests / atomic.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
e9174d1e
SL
11use core::sync::atomic::*;
12use core::sync::atomic::Ordering::SeqCst;
1a4d82fc
JJ
13
14#[test]
15fn bool_() {
16 let a = AtomicBool::new(false);
17 assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
18 assert_eq!(a.compare_and_swap(false, true, SeqCst), true);
19
20 a.store(false, SeqCst);
21 assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
22}
23
24#[test]
25fn bool_and() {
26 let a = AtomicBool::new(true);
cc61c64b 27 assert_eq!(a.fetch_and(false, SeqCst), true);
1a4d82fc
JJ
28 assert_eq!(a.load(SeqCst),false);
29}
30
cc61c64b
XL
31#[test]
32fn bool_nand() {
33 let a = AtomicBool::new(false);
34 assert_eq!(a.fetch_nand(false, SeqCst), false);
35 assert_eq!(a.load(SeqCst), true);
36 assert_eq!(a.fetch_nand(false, SeqCst), true);
37 assert_eq!(a.load(SeqCst), true);
38 assert_eq!(a.fetch_nand(true, SeqCst), true);
39 assert_eq!(a.load(SeqCst), false);
40 assert_eq!(a.fetch_nand(true, SeqCst), false);
41 assert_eq!(a.load(SeqCst), true);
42}
43
1a4d82fc
JJ
44#[test]
45fn uint_and() {
85aaf69f 46 let x = AtomicUsize::new(0xf731);
1a4d82fc
JJ
47 assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
48 assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
49}
50
2c00a5a8
XL
51#[test]
52fn uint_nand() {
53 let x = AtomicUsize::new(0xf731);
54 assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731);
55 assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f));
56}
57
1a4d82fc
JJ
58#[test]
59fn uint_or() {
85aaf69f 60 let x = AtomicUsize::new(0xf731);
1a4d82fc
JJ
61 assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
62 assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
63}
64
65#[test]
66fn uint_xor() {
85aaf69f 67 let x = AtomicUsize::new(0xf731);
1a4d82fc
JJ
68 assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
69 assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
70}
71
72#[test]
73fn int_and() {
85aaf69f 74 let x = AtomicIsize::new(0xf731);
1a4d82fc
JJ
75 assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
76 assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
77}
78
2c00a5a8
XL
79#[test]
80fn int_nand() {
81 let x = AtomicIsize::new(0xf731);
82 assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731);
83 assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f));
84}
85
1a4d82fc
JJ
86#[test]
87fn int_or() {
85aaf69f 88 let x = AtomicIsize::new(0xf731);
1a4d82fc
JJ
89 assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
90 assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
91}
92
93#[test]
94fn int_xor() {
85aaf69f 95 let x = AtomicIsize::new(0xf731);
1a4d82fc
JJ
96 assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
97 assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
98}
99
62682a34
SL
100static S_FALSE: AtomicBool = AtomicBool::new(false);
101static S_TRUE: AtomicBool = AtomicBool::new(true);
102static S_INT: AtomicIsize = AtomicIsize::new(0);
103static S_UINT: AtomicUsize = AtomicUsize::new(0);
1a4d82fc
JJ
104
105#[test]
106fn static_init() {
83c7162d
XL
107 // Note that we're not really testing the mutability here but it's important
108 // on Android at the moment (#49775)
109 assert!(!S_FALSE.swap(true, SeqCst));
110 assert!(S_TRUE.swap(false, SeqCst));
111 assert!(S_INT.fetch_add(1, SeqCst) == 0);
112 assert!(S_UINT.fetch_add(1, SeqCst) == 0);
1a4d82fc 113}