Skip to content

Commit e7cb5e3

Browse files
committed
Move various stdlib tests to library/std/tests
1 parent 585e788 commit e7cb5e3

9 files changed

+312
-0
lines changed

std/tests/builtin-clone.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test that `Clone` is correctly implemented for builtin types.
2+
// Also test that cloning an array or a tuple is done right, i.e.
3+
// each component is cloned.
4+
5+
fn test_clone<T: Clone>(arg: T) {
6+
let _ = arg.clone();
7+
}
8+
9+
fn foo() { }
10+
11+
#[derive(Debug, PartialEq, Eq)]
12+
struct S(i32);
13+
14+
impl Clone for S {
15+
fn clone(&self) -> Self {
16+
S(self.0 + 1)
17+
}
18+
}
19+
20+
#[test]
21+
fn builtin_clone() {
22+
test_clone(foo);
23+
test_clone([1; 56]);
24+
test_clone((1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
25+
26+
let a = [S(0), S(1), S(2)];
27+
let b = [S(1), S(2), S(3)];
28+
assert_eq!(b, a.clone());
29+
30+
let a = (
31+
(S(1), S(0)),
32+
(
33+
(S(0), S(0), S(1)),
34+
S(0)
35+
)
36+
);
37+
let b = (
38+
(S(2), S(1)),
39+
(
40+
(S(1), S(1), S(2)),
41+
S(1)
42+
)
43+
);
44+
assert_eq!(b, a.clone());
45+
}

std/tests/eq-multidispatch.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[derive(PartialEq, Debug)]
2+
struct Bar;
3+
#[derive(Debug)]
4+
struct Baz;
5+
#[derive(Debug)]
6+
struct Foo;
7+
#[derive(Debug)]
8+
struct Fu;
9+
10+
impl PartialEq for Baz { fn eq(&self, _: &Baz) -> bool { true } }
11+
12+
impl PartialEq<Fu> for Foo { fn eq(&self, _: &Fu) -> bool { true } }
13+
impl PartialEq<Foo> for Fu { fn eq(&self, _: &Foo) -> bool { true } }
14+
15+
impl PartialEq<Bar> for Foo { fn eq(&self, _: &Bar) -> bool { false } }
16+
impl PartialEq<Foo> for Bar { fn eq(&self, _: &Foo) -> bool { false } }
17+
18+
#[test]
19+
fn eq_multidispatch() {
20+
assert!(Bar != Foo);
21+
assert!(Foo != Bar);
22+
23+
assert_eq!(Bar, Bar);
24+
25+
assert_eq!(Baz, Baz);
26+
27+
assert_eq!(Foo, Fu);
28+
assert_eq!(Fu, Foo);
29+
}

std/tests/issue-21058.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![allow(dead_code)]
2+
3+
use std::fmt::Debug;
4+
5+
struct NT(str);
6+
struct DST { a: u32, b: str }
7+
8+
macro_rules! check {
9+
(val: $ty_of:expr, $expected:expr) => {
10+
assert_eq!(type_name_of_val($ty_of), $expected);
11+
};
12+
($ty:ty, $expected:expr) => {
13+
assert_eq!(std::any::type_name::<$ty>(), $expected);
14+
};
15+
}
16+
17+
#[test]
18+
fn issue_21058() {
19+
// type_name should support unsized types
20+
check!([u8], "[u8]");
21+
check!(str, "str");
22+
check!(dyn Send, "dyn core::marker::Send");
23+
check!(NT, "issue_21058::NT");
24+
check!(DST, "issue_21058::DST");
25+
check!(&i32, "&i32");
26+
check!(&'static i32, "&i32");
27+
check!((i32, u32), "(i32, u32)");
28+
check!(val: foo(), "issue_21058::Foo");
29+
check!(val: Foo::new, "issue_21058::Foo::new");
30+
check!(val:
31+
<Foo as Debug>::fmt,
32+
"<issue_21058::Foo as core::fmt::Debug>::fmt"
33+
);
34+
check!(val: || {}, "issue_21058::issue_21058::{{closure}}");
35+
bar::<i32>();
36+
}
37+
38+
trait Trait {
39+
type Assoc;
40+
}
41+
42+
impl Trait for i32 {
43+
type Assoc = String;
44+
}
45+
46+
fn bar<T: Trait>() {
47+
check!(T::Assoc, "alloc::string::String");
48+
check!(T, "i32");
49+
}
50+
51+
fn type_name_of_val<T>(_: T) -> &'static str {
52+
std::any::type_name::<T>()
53+
}
54+
55+
#[derive(Debug)]
56+
struct Foo;
57+
58+
impl Foo {
59+
fn new() -> Self { Foo }
60+
}
61+
62+
fn foo() -> impl Debug {
63+
Foo
64+
}

std/tests/istr.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#[test]
2+
fn test_stack_assign() {
3+
let s: String = "a".to_string();
4+
println!("{}", s.clone());
5+
let t: String = "a".to_string();
6+
assert_eq!(s, t);
7+
let u: String = "b".to_string();
8+
assert!((s != u));
9+
}
10+
11+
#[test]
12+
fn test_heap_lit() { "a big string".to_string(); }
13+
14+
#[test]
15+
fn test_heap_assign() {
16+
let s: String = "a big ol' string".to_string();
17+
let t: String = "a big ol' string".to_string();
18+
assert_eq!(s, t);
19+
let u: String = "a bad ol' string".to_string();
20+
assert!((s != u));
21+
}
22+
23+
#[test]
24+
fn test_heap_log() {
25+
let s = "a big ol' string".to_string();
26+
println!("{}", s);
27+
}
28+
29+
#[test]
30+
fn test_append() {
31+
let mut s = String::new();
32+
s.push_str("a");
33+
assert_eq!(s, "a");
34+
35+
let mut s = String::from("a");
36+
s.push_str("b");
37+
println!("{}", s.clone());
38+
assert_eq!(s, "ab");
39+
40+
let mut s = String::from("c");
41+
s.push_str("offee");
42+
assert_eq!(s, "coffee");
43+
44+
s.push_str("&tea");
45+
assert_eq!(s, "coffee&tea");
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![allow(non_camel_case_types)]
2+
#![allow(dead_code)]
3+
4+
#[derive(Clone, Debug)]
5+
enum foo {
6+
a(usize),
7+
b(String),
8+
}
9+
10+
fn check_log<T: std::fmt::Debug>(exp: String, v: T) {
11+
assert_eq!(exp, format!("{:?}", v));
12+
}
13+
14+
#[test]
15+
fn log_knows_the_names_of_variants_in_std() {
16+
let mut x = Some(foo::a(22));
17+
let exp = "Some(a(22))".to_string();
18+
let act = format!("{:?}", x);
19+
assert_eq!(act, exp);
20+
check_log(exp, x);
21+
22+
x = None;
23+
let exp = "None".to_string();
24+
let act = format!("{:?}", x);
25+
assert_eq!(act, exp);
26+
check_log(exp, x);
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::fmt::Debug;
2+
use std::cmp::{self, Ordering};
3+
4+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5+
struct Foo {
6+
n: u8,
7+
name: &'static str
8+
}
9+
10+
impl PartialOrd for Foo {
11+
fn partial_cmp(&self, other: &Foo) -> Option<Ordering> {
12+
Some(self.cmp(other))
13+
}
14+
}
15+
16+
impl Ord for Foo {
17+
fn cmp(&self, other: &Foo) -> Ordering {
18+
self.n.cmp(&other.n)
19+
}
20+
}
21+
22+
#[test]
23+
fn minmax_stability() {
24+
let a = Foo { n: 4, name: "a" };
25+
let b = Foo { n: 4, name: "b" };
26+
let c = Foo { n: 8, name: "c" };
27+
let d = Foo { n: 8, name: "d" };
28+
let e = Foo { n: 22, name: "e" };
29+
let f = Foo { n: 22, name: "f" };
30+
31+
let data = [a, b, c, d, e, f];
32+
33+
// `min` should return the left when the values are equal
34+
assert_eq!(data.iter().min(), Some(&a));
35+
assert_eq!(data.iter().min_by_key(|a| a.n), Some(&a));
36+
assert_eq!(cmp::min(a, b), a);
37+
assert_eq!(cmp::min(b, a), b);
38+
39+
// `max` should return the right when the values are equal
40+
assert_eq!(data.iter().max(), Some(&f));
41+
assert_eq!(data.iter().max_by_key(|a| a.n), Some(&f));
42+
assert_eq!(cmp::max(e, f), f);
43+
assert_eq!(cmp::max(f, e), e);
44+
45+
let mut presorted = data.to_vec();
46+
presorted.sort();
47+
assert_stable(&presorted);
48+
49+
let mut presorted = data.to_vec();
50+
presorted.sort_by(|a, b| a.cmp(b));
51+
assert_stable(&presorted);
52+
53+
// Assert that sorted and min/max are the same
54+
fn assert_stable<T: Ord + Debug>(presorted: &[T]) {
55+
for slice in presorted.windows(2) {
56+
let a = &slice[0];
57+
let b = &slice[1];
58+
59+
assert_eq!(a, cmp::min(a, b));
60+
assert_eq!(b, cmp::max(a, b));
61+
}
62+
}
63+
}

std/tests/seq-compare.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[test]
2+
fn seq_compare() {
3+
assert!(("hello".to_string() < "hellr".to_string()));
4+
assert!(("hello ".to_string() > "hello".to_string()));
5+
assert!(("hello".to_string() != "there".to_string()));
6+
assert!((vec![1, 2, 3, 4] > vec![1, 2, 3]));
7+
assert!((vec![1, 2, 3] < vec![1, 2, 3, 4]));
8+
assert!((vec![1, 2, 4, 4] > vec![1, 2, 3, 4]));
9+
assert!((vec![1, 2, 3, 4] < vec![1, 2, 4, 4]));
10+
assert!((vec![1, 2, 3] <= vec![1, 2, 3]));
11+
assert!((vec![1, 2, 3] <= vec![1, 2, 3, 3]));
12+
assert!((vec![1, 2, 3, 4] > vec![1, 2, 3]));
13+
assert_eq!(vec![1, 2, 3], vec![1, 2, 3]);
14+
assert!((vec![1, 2, 3] != vec![1, 1, 3]));
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This intends to use the unsizing coercion from array to slice, but it only
2+
// works if we resolve `<&[u8]>::from` as the reflexive `From<T> for T`. In
3+
// #113238, we found that gimli had added its own `From<EndianSlice> for &[u8]`
4+
// that affected all `std/backtrace` users.
5+
#[test]
6+
fn slice_from_array() {
7+
let _ = <&[u8]>::from(&[]);
8+
}

std/tests/volatile-fat-ptr.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![allow(stable_features)]
2+
#![feature(volatile)]
3+
4+
use std::ptr::{read_volatile, write_volatile};
5+
6+
#[test]
7+
fn volatile_fat_ptr() {
8+
let mut x: &'static str = "test";
9+
unsafe {
10+
let a = read_volatile(&x);
11+
assert_eq!(a, "test");
12+
write_volatile(&mut x, "foo");
13+
assert_eq!(x, "foo");
14+
}
15+
}

0 commit comments

Comments
 (0)