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.
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.
11 use core::sync::atomic::*;
12 use core::sync::atomic::Ordering::SeqCst;
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);
20 a.store(false, SeqCst);
21 assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
26 let a = AtomicBool::new(true);
27 assert_eq!(a.fetch_and(false, SeqCst), true);
28 assert_eq!(a.load(SeqCst),false);
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);
46 let x = AtomicUsize::new(0xf731);
47 assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
48 assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
53 let x = AtomicUsize::new(0xf731);
54 assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
55 assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
60 let x = AtomicUsize::new(0xf731);
61 assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
62 assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
67 let x = AtomicIsize::new(0xf731);
68 assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
69 assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
74 let x = AtomicIsize::new(0xf731);
75 assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
76 assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
81 let x = AtomicIsize::new(0xf731);
82 assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
83 assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
86 static S_FALSE: AtomicBool = AtomicBool::new(false);
87 static S_TRUE: AtomicBool = AtomicBool::new(true);
88 static S_INT: AtomicIsize = AtomicIsize::new(0);
89 static S_UINT: AtomicUsize = AtomicUsize::new(0);
93 assert!(!S_FALSE.load(SeqCst));
94 assert!(S_TRUE.load(SeqCst));
95 assert!(S_INT.load(SeqCst) == 0);
96 assert!(S_UINT.load(SeqCst) == 0);