]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/atomic.rs
Add comment about the problem, and use provided path if available
[rust.git] / src / libcore / tests / 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::sync::atomic::*;
12 use core::sync::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 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
44 #[test]
45 fn uint_and() {
46     let x = AtomicUsize::new(0xf731);
47     assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
48     assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
49 }
50
51 #[test]
52 fn uint_or() {
53     let x = AtomicUsize::new(0xf731);
54     assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
55     assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
56 }
57
58 #[test]
59 fn uint_xor() {
60     let x = AtomicUsize::new(0xf731);
61     assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
62     assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
63 }
64
65 #[test]
66 fn int_and() {
67     let x = AtomicIsize::new(0xf731);
68     assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
69     assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
70 }
71
72 #[test]
73 fn int_or() {
74     let x = AtomicIsize::new(0xf731);
75     assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
76     assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
77 }
78
79 #[test]
80 fn int_xor() {
81     let x = AtomicIsize::new(0xf731);
82     assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
83     assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
84 }
85
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);
90
91 #[test]
92 fn static_init() {
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);
97 }