Skip to content

Commit ba0be73

Browse files
committed
Auto merge of rust-lang#124497 - rytheo:move-std-tests-to-library, r=workingjubilee
Move some stdlib tests from `tests/ui` to `library/std/tests` Related to rust-lang#99417
2 parents 1704687 + 456ecae commit ba0be73

9 files changed

+330
-0
lines changed

Diff for: std/tests/builtin-clone.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 = ((S(1), S(0)), ((S(0), S(0), S(1)), S(0)));
31+
let b = ((S(2), S(1)), ((S(1), S(1), S(2)), S(1)));
32+
assert_eq!(b, a.clone());
33+
}

Diff for: std/tests/eq-multidispatch.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 {
11+
fn eq(&self, _: &Baz) -> bool {
12+
true
13+
}
14+
}
15+
16+
impl PartialEq<Fu> for Foo {
17+
fn eq(&self, _: &Fu) -> bool {
18+
true
19+
}
20+
}
21+
22+
impl PartialEq<Foo> for Fu {
23+
fn eq(&self, _: &Foo) -> bool {
24+
true
25+
}
26+
}
27+
28+
impl PartialEq<Bar> for Foo {
29+
fn eq(&self, _: &Bar) -> bool {
30+
false
31+
}
32+
}
33+
34+
impl PartialEq<Foo> for Bar {
35+
fn eq(&self, _: &Foo) -> bool {
36+
false
37+
}
38+
}
39+
40+
#[test]
41+
fn eq_multidispatch() {
42+
assert!(Bar != Foo);
43+
assert!(Foo != Bar);
44+
45+
assert_eq!(Bar, Bar);
46+
47+
assert_eq!(Baz, Baz);
48+
49+
assert_eq!(Foo, Fu);
50+
assert_eq!(Fu, Foo);
51+
}

Diff for: std/tests/istr.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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() {
13+
"a big string".to_string();
14+
}
15+
16+
#[test]
17+
fn test_heap_assign() {
18+
let s: String = "a big ol' string".to_string();
19+
let t: String = "a big ol' string".to_string();
20+
assert_eq!(s, t);
21+
let u: String = "a bad ol' string".to_string();
22+
assert!((s != u));
23+
}
24+
25+
#[test]
26+
fn test_heap_log() {
27+
let s = "a big ol' string".to_string();
28+
println!("{}", s);
29+
}
30+
31+
#[test]
32+
fn test_append() {
33+
let mut s = String::new();
34+
s.push_str("a");
35+
assert_eq!(s, "a");
36+
37+
let mut s = String::from("a");
38+
s.push_str("b");
39+
println!("{}", s.clone());
40+
assert_eq!(s, "ab");
41+
42+
let mut s = String::from("c");
43+
s.push_str("offee");
44+
assert_eq!(s, "coffee");
45+
46+
s.push_str("&tea");
47+
assert_eq!(s, "coffee&tea");
48+
}

Diff for: std/tests/log-knows-the-names-of-variants-in-std.rs

+27
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+
}

Diff for: std/tests/minmax-stability-issue-23687.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::cmp::{self, Ordering};
2+
use std::fmt::Debug;
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+
}

Diff for: std/tests/seq-compare.rs

+15
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+
}

Diff for: std/tests/slice-from-array-issue-113238.rs

+8
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+
}

Diff for: std/tests/type-name-unsized.rs

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

Diff for: std/tests/volatile-fat-ptr.rs

+15
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)