2014-06-28 20:57:36 +00:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-08-17 21:16:41 +00:00
|
|
|
use core::sync::atomic::*;
|
|
|
|
use core::sync::atomic::Ordering::SeqCst;
|
2014-06-28 20:57:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bool_() {
|
|
|
|
let a = AtomicBool::new(false);
|
|
|
|
assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
|
|
|
|
assert_eq!(a.compare_and_swap(false, true, SeqCst), true);
|
|
|
|
|
|
|
|
a.store(false, SeqCst);
|
|
|
|
assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bool_and() {
|
|
|
|
let a = AtomicBool::new(true);
|
2017-04-07 15:28:55 +00:00
|
|
|
assert_eq!(a.fetch_and(false, SeqCst), true);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(a.load(SeqCst),false);
|
|
|
|
}
|
|
|
|
|
2017-04-07 15:28:55 +00:00
|
|
|
#[test]
|
|
|
|
fn bool_nand() {
|
|
|
|
let a = AtomicBool::new(false);
|
|
|
|
assert_eq!(a.fetch_nand(false, SeqCst), false);
|
|
|
|
assert_eq!(a.load(SeqCst), true);
|
|
|
|
assert_eq!(a.fetch_nand(false, SeqCst), true);
|
|
|
|
assert_eq!(a.load(SeqCst), true);
|
|
|
|
assert_eq!(a.fetch_nand(true, SeqCst), true);
|
|
|
|
assert_eq!(a.load(SeqCst), false);
|
|
|
|
assert_eq!(a.fetch_nand(true, SeqCst), false);
|
|
|
|
assert_eq!(a.load(SeqCst), true);
|
|
|
|
}
|
|
|
|
|
2014-06-28 20:57:36 +00:00
|
|
|
#[test]
|
|
|
|
fn uint_and() {
|
2015-01-10 21:42:48 +00:00
|
|
|
let x = AtomicUsize::new(0xf731);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
|
|
|
|
assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uint_or() {
|
2015-01-10 21:42:48 +00:00
|
|
|
let x = AtomicUsize::new(0xf731);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
|
|
|
|
assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uint_xor() {
|
2015-01-10 21:42:48 +00:00
|
|
|
let x = AtomicUsize::new(0xf731);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
|
|
|
|
assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn int_and() {
|
2015-01-10 21:42:48 +00:00
|
|
|
let x = AtomicIsize::new(0xf731);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
|
|
|
|
assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn int_or() {
|
2015-01-10 21:42:48 +00:00
|
|
|
let x = AtomicIsize::new(0xf731);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
|
|
|
|
assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn int_xor() {
|
2015-01-10 21:42:48 +00:00
|
|
|
let x = AtomicIsize::new(0xf731);
|
2014-06-28 20:57:36 +00:00
|
|
|
assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
|
|
|
|
assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
|
|
|
|
}
|
|
|
|
|
2015-05-27 08:18:36 +00:00
|
|
|
static S_FALSE: AtomicBool = AtomicBool::new(false);
|
|
|
|
static S_TRUE: AtomicBool = AtomicBool::new(true);
|
|
|
|
static S_INT: AtomicIsize = AtomicIsize::new(0);
|
|
|
|
static S_UINT: AtomicUsize = AtomicUsize::new(0);
|
2014-06-28 20:57:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn static_init() {
|
2015-05-27 08:18:36 +00:00
|
|
|
assert!(!S_FALSE.load(SeqCst));
|
|
|
|
assert!(S_TRUE.load(SeqCst));
|
2014-10-11 04:59:10 +00:00
|
|
|
assert!(S_INT.load(SeqCst) == 0);
|
|
|
|
assert!(S_UINT.load(SeqCst) == 0);
|
2014-06-28 20:57:36 +00:00
|
|
|
}
|