Skip to content

Commit

Permalink
auto merge of #15245 : sfackler/rust/coretest, r=alexcrichton
Browse files Browse the repository at this point in the history
Libcore's test infrastructure is complicated by the fact that many lang
items are defined in the crate. The current approach (realcore/realstd
imports) is hacky and hard to work with (tests inside of core::cmp
haven't been run for months!).

Moving tests to a separate crate does mean that they can only test the
public API of libcore, but I don't feel that that is too much of an
issue. The only tests that I had to get rid of were some checking the
various numeric formatters, but those are also exercised through normal
format! calls in other tests.
  • Loading branch information
bors committed Jun 29, 2014
2 parents a490871 + 1ed646e commit bb5695b
Show file tree
Hide file tree
Showing 60 changed files with 3,348 additions and 3,339 deletions.
9 changes: 7 additions & 2 deletions mk/tests.mk
Expand Up @@ -14,7 +14,12 @@
######################################################################

# The names of crates that must be tested
TEST_TARGET_CRATES = $(TARGET_CRATES)

# libcore tests are in a separate crate
DEPS_coretest :=
$(eval $(call RUST_CRATE,coretest))

TEST_TARGET_CRATES = $(filter-out core,$(TARGET_CRATES)) coretest
TEST_DOC_CRATES = $(DOC_CRATES)
TEST_HOST_CRATES = $(HOST_CRATES)
TEST_CRATES = $(TEST_TARGET_CRATES) $(TEST_HOST_CRATES)
Expand Down Expand Up @@ -172,7 +177,7 @@ check-notidy: cleantmptestlogs cleantestlibs all check-stage2
$(Q)$(CFG_PYTHON) $(S)src/etc/check-summary.py tmp/*.log

check-lite: cleantestlibs cleantmptestlogs \
$(foreach crate,$(TARGET_CRATES),check-stage2-$(crate)) \
$(foreach crate,$(TEST_TARGET_CRATES),check-stage2-$(crate)) \
check-stage2-rpass \
check-stage2-rfail check-stage2-cfail check-stage2-rmake
$(Q)$(CFG_PYTHON) $(S)src/etc/check-summary.py tmp/*.log
Expand Down
48 changes: 48 additions & 0 deletions src/liballoc/owned.rs
Expand Up @@ -146,3 +146,51 @@ impl fmt::Show for Box<Any> {
f.pad("Box<Any>")
}
}

#[cfg(test)]
mod test {
#[test]
fn test_owned_clone() {
let a = box 5i;
let b: Box<int> = a.clone();
assert!(a == b);
}

#[test]
fn any_move() {
let a = box 8u as Box<Any>;
let b = box Test as Box<Any>;

match a.move::<uint>() {
Ok(a) => { assert!(a == box 8u); }
Err(..) => fail!()
}
match b.move::<Test>() {
Ok(a) => { assert!(a == box Test); }
Err(..) => fail!()
}

let a = box 8u as Box<Any>;
let b = box Test as Box<Any>;

assert!(a.move::<Box<Test>>().is_err());
assert!(b.move::<Box<uint>>().is_err());
}

#[test]
fn test_show() {
let a = box 8u as Box<Any>;
let b = box Test as Box<Any>;
let a_str = a.to_str();
let b_str = b.to_str();
assert_eq!(a_str.as_slice(), "Box<Any>");
assert_eq!(b_str.as_slice(), "Box<Any>");

let a = &8u as &Any;
let b = &Test as &Any;
let s = format!("{}", a);
assert_eq!(s.as_slice(), "&Any");
let s = format!("{}", b);
assert_eq!(s.as_slice(), "&Any");
}
}
176 changes: 0 additions & 176 deletions src/libcore/any.rs
Expand Up @@ -115,179 +115,3 @@ impl<'a> AnyMutRefExt<'a> for &'a mut Any {
}
}
}

#[cfg(test)]
mod tests {
use prelude::*;
use super::*;
use realstd::owned::{Box, AnyOwnExt};
use realstd::str::Str;

#[deriving(PartialEq, Show)]
struct Test;

static TEST: &'static str = "Test";

#[test]
fn any_referenced() {
let (a, b, c) = (&5u as &Any, &TEST as &Any, &Test as &Any);

assert!(a.is::<uint>());
assert!(!b.is::<uint>());
assert!(!c.is::<uint>());

assert!(!a.is::<&'static str>());
assert!(b.is::<&'static str>());
assert!(!c.is::<&'static str>());

assert!(!a.is::<Test>());
assert!(!b.is::<Test>());
assert!(c.is::<Test>());
}

#[test]
fn any_owning() {
let (a, b, c) = (box 5u as Box<Any>, box TEST as Box<Any>, box Test as Box<Any>);

assert!(a.is::<uint>());
assert!(!b.is::<uint>());
assert!(!c.is::<uint>());

assert!(!a.is::<&'static str>());
assert!(b.is::<&'static str>());
assert!(!c.is::<&'static str>());

assert!(!a.is::<Test>());
assert!(!b.is::<Test>());
assert!(c.is::<Test>());
}

#[test]
fn any_as_ref() {
let a = &5u as &Any;

match a.as_ref::<uint>() {
Some(&5) => {}
x => fail!("Unexpected value {}", x)
}

match a.as_ref::<Test>() {
None => {}
x => fail!("Unexpected value {}", x)
}
}

#[test]
fn any_as_mut() {
let mut a = 5u;
let mut b = box 7u;

let a_r = &mut a as &mut Any;
let tmp: &mut uint = &mut *b;
let b_r = tmp as &mut Any;

match a_r.as_mut::<uint>() {
Some(x) => {
assert_eq!(*x, 5u);
*x = 612;
}
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<uint>() {
Some(x) => {
assert_eq!(*x, 7u);
*x = 413;
}
x => fail!("Unexpected value {}", x)
}

match a_r.as_mut::<Test>() {
None => (),
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<Test>() {
None => (),
x => fail!("Unexpected value {}", x)
}

match a_r.as_mut::<uint>() {
Some(&612) => {}
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<uint>() {
Some(&413) => {}
x => fail!("Unexpected value {}", x)
}
}

#[test]
fn any_move() {
use realstd::any::Any;
use realstd::result::{Ok, Err};
let a = box 8u as Box<Any>;
let b = box Test as Box<Any>;

match a.move::<uint>() {
Ok(a) => { assert!(a == box 8u); }
Err(..) => fail!()
}
match b.move::<Test>() {
Ok(a) => { assert!(a == box Test); }
Err(..) => fail!()
}

let a = box 8u as Box<Any>;
let b = box Test as Box<Any>;

assert!(a.move::<Box<Test>>().is_err());
assert!(b.move::<Box<uint>>().is_err());
}

#[test]
fn test_show() {
use realstd::to_str::ToStr;
let a = box 8u as Box<::realstd::any::Any>;
let b = box Test as Box<::realstd::any::Any>;
let a_str = a.to_str();
let b_str = b.to_str();
assert_eq!(a_str.as_slice(), "Box<Any>");
assert_eq!(b_str.as_slice(), "Box<Any>");

let a = &8u as &Any;
let b = &Test as &Any;
let s = format!("{}", a);
assert_eq!(s.as_slice(), "&Any");
let s = format!("{}", b);
assert_eq!(s.as_slice(), "&Any");
}

#[test]
fn any_fixed_vec() {
let test = [0u, ..8];
let test = &test as &Any;
assert!(test.is::<[uint, ..8]>());
assert!(!test.is::<[uint, ..10]>());
}
}

#[cfg(test)]
mod bench {
extern crate test;

use any::{Any, AnyRefExt};
use option::Some;
use self::test::Bencher;

#[bench]
fn bench_as_ref(b: &mut Bencher) {
b.iter(|| {
let mut x = 0i;
let mut y = &mut x as &mut Any;
test::black_box(&mut y);
test::black_box(y.as_ref::<int>() == Some(&0));
});
}
}
94 changes: 0 additions & 94 deletions src/libcore/atomics.rs
Expand Up @@ -693,97 +693,3 @@ pub fn fence(order: Ordering) {
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[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);
assert_eq!(a.fetch_and(false, SeqCst),true);
assert_eq!(a.load(SeqCst),false);
}

#[test]
fn uint_and() {
let x = AtomicUint::new(0xf731);
assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
}

#[test]
fn uint_or() {
let x = AtomicUint::new(0xf731);
assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
}

#[test]
fn uint_xor() {
let x = AtomicUint::new(0xf731);
assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
}

#[test]
fn int_and() {
let x = AtomicInt::new(0xf731);
assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731);
assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
}

#[test]
fn int_or() {
let x = AtomicInt::new(0xf731);
assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731);
assert_eq!(x.load(SeqCst), 0xf731 | 0x137f);
}

#[test]
fn int_xor() {
let x = AtomicInt::new(0xf731);
assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731);
assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
}

static mut S_BOOL : AtomicBool = INIT_ATOMIC_BOOL;
static mut S_INT : AtomicInt = INIT_ATOMIC_INT;
static mut S_UINT : AtomicUint = INIT_ATOMIC_UINT;

#[test]
fn static_init() {
unsafe {
assert!(!S_BOOL.load(SeqCst));
assert!(S_INT.load(SeqCst) == 0);
assert!(S_UINT.load(SeqCst) == 0);
}
}

#[test]
fn different_sizes() {
unsafe {
let mut slot = 0u16;
assert_eq!(super::atomic_swap(&mut slot, 1, SeqCst), 0);

let mut slot = 0u8;
assert_eq!(super::atomic_compare_and_swap(&mut slot, 1, 2, SeqCst), 0);

let slot = 0u32;
assert_eq!(super::atomic_load(&slot, SeqCst), 0);

let mut slot = 0u64;
super::atomic_store(&mut slot, 2, SeqCst);
}
}
}

0 comments on commit bb5695b

Please sign in to comment.