]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/atomic.rs
fix spacing issue in trpl/documentation doc
[rust.git] / src / libcoretest / atomic.rs
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
11 use core::atomic::*;
12 use core::atomic::Ordering::SeqCst;
13
14 #[test]
15 fn 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]
25 fn bool_and() {
26     let a = AtomicBool::new(true);
27     assert_eq!(a.fetch_and(false, SeqCst),true);
28     assert_eq!(a.load(SeqCst),false);
29 }
30
31 #[test]
32 fn uint_and() {
33     let x = AtomicUsize::new(0xf731);
34     assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
35     assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
36 }
37
38 #[test]
39 fn uint_or() {
40     let x = AtomicUsize::new(0xf731);
41     assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
42     assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
43 }
44
45 #[test]
46 fn uint_xor() {
47     let x = AtomicUsize::new(0xf731);
48     assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
49     assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
50 }
51
52 #[test]
53 fn int_and() {
54     let x = AtomicIsize::new(0xf731);
55     assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
56     assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
57 }
58
59 #[test]
60 fn int_or() {
61     let x = AtomicIsize::new(0xf731);
62     assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
63     assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
64 }
65
66 #[test]
67 fn int_xor() {
68     let x = AtomicIsize::new(0xf731);
69     assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
70     assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
71 }
72
73 static S_FALSE: AtomicBool = AtomicBool::new(false);
74 static S_TRUE: AtomicBool = AtomicBool::new(true);
75 static S_INT: AtomicIsize  = AtomicIsize::new(0);
76 static S_UINT: AtomicUsize = AtomicUsize::new(0);
77
78 #[test]
79 fn static_init() {
80     assert!(!S_FALSE.load(SeqCst));
81     assert!(S_TRUE.load(SeqCst));
82     assert!(S_INT.load(SeqCst) == 0);
83     assert!(S_UINT.load(SeqCst) == 0);
84 }