about summary refs log tree commit diff
path: root/src/test/ui/stdlib-unit-tests
diff options
context:
space:
mode:
authorCaio <c410.f3r@gmail.com>2021-11-18 12:09:34 -0300
committerCaio <c410.f3r@gmail.com>2021-11-18 12:09:34 -0300
commit41d9abd76cd514aca23e3409fe6896a3a7d61d1a (patch)
treeaff7d6c42e88201c903006083095106fd082f16b /src/test/ui/stdlib-unit-tests
parent6414e0b5b308d3ae27da83c6a25098cc8aadc1a9 (diff)
downloadrust-41d9abd76cd514aca23e3409fe6896a3a7d61d1a.tar.gz
rust-41d9abd76cd514aca23e3409fe6896a3a7d61d1a.zip
Move some tests to more reasonable directories
Diffstat (limited to 'src/test/ui/stdlib-unit-tests')
-rw-r--r--src/test/ui/stdlib-unit-tests/matches2021.rs13
-rw-r--r--src/test/ui/stdlib-unit-tests/minmax-stability-issue-23687.rs64
-rw-r--r--src/test/ui/stdlib-unit-tests/not-sync.rs22
-rw-r--r--src/test/ui/stdlib-unit-tests/not-sync.stderr81
-rw-r--r--src/test/ui/stdlib-unit-tests/raw-fat-ptr.rs118
-rw-r--r--src/test/ui/stdlib-unit-tests/volatile-fat-ptr.rs15
6 files changed, 313 insertions, 0 deletions
diff --git a/src/test/ui/stdlib-unit-tests/matches2021.rs b/src/test/ui/stdlib-unit-tests/matches2021.rs
new file mode 100644
index 00000000000..9143a8cdd59
--- /dev/null
+++ b/src/test/ui/stdlib-unit-tests/matches2021.rs
@@ -0,0 +1,13 @@
+// run-pass
+// edition:2021
+
+// regression test for https://github.com/rust-lang/rust/pull/85678
+
+#![feature(assert_matches)]
+
+use std::assert_matches::assert_matches;
+
+fn main() {
+    assert!(matches!((), ()));
+    assert_matches!((), ());
+}
diff --git a/src/test/ui/stdlib-unit-tests/minmax-stability-issue-23687.rs b/src/test/ui/stdlib-unit-tests/minmax-stability-issue-23687.rs
new file mode 100644
index 00000000000..9100bfbde95
--- /dev/null
+++ b/src/test/ui/stdlib-unit-tests/minmax-stability-issue-23687.rs
@@ -0,0 +1,64 @@
+// run-pass
+
+use std::fmt::Debug;
+use std::cmp::{self, PartialOrd, Ordering};
+
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+struct Foo {
+    n: u8,
+    name: &'static str
+}
+
+impl PartialOrd for Foo {
+    fn partial_cmp(&self, other: &Foo) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Ord for Foo {
+    fn cmp(&self, other: &Foo) -> Ordering {
+        self.n.cmp(&other.n)
+    }
+}
+
+fn main() {
+    let a = Foo { n: 4, name: "a" };
+    let b = Foo { n: 4, name: "b" };
+    let c = Foo { n: 8, name: "c" };
+    let d = Foo { n: 8, name: "d" };
+    let e = Foo { n: 22, name: "e" };
+    let f = Foo { n: 22, name: "f" };
+
+    let data = [a, b, c, d, e, f];
+
+    // `min` should return the left when the values are equal
+    assert_eq!(data.iter().min(), Some(&a));
+    assert_eq!(data.iter().min_by_key(|a| a.n), Some(&a));
+    assert_eq!(cmp::min(a, b), a);
+    assert_eq!(cmp::min(b, a), b);
+
+    // `max` should return the right when the values are equal
+    assert_eq!(data.iter().max(), Some(&f));
+    assert_eq!(data.iter().max_by_key(|a| a.n), Some(&f));
+    assert_eq!(cmp::max(e, f), f);
+    assert_eq!(cmp::max(f, e), e);
+
+    let mut presorted = data.to_vec();
+    presorted.sort();
+    assert_stable(&presorted);
+
+    let mut presorted = data.to_vec();
+    presorted.sort_by(|a, b| a.cmp(b));
+    assert_stable(&presorted);
+
+    // Assert that sorted and min/max are the same
+    fn assert_stable<T: Ord + Debug>(presorted: &[T]) {
+        for slice in presorted.windows(2) {
+            let a = &slice[0];
+            let b = &slice[1];
+
+            assert_eq!(a, cmp::min(a, b));
+            assert_eq!(b, cmp::max(a, b));
+        }
+    }
+}
diff --git a/src/test/ui/stdlib-unit-tests/not-sync.rs b/src/test/ui/stdlib-unit-tests/not-sync.rs
new file mode 100644
index 00000000000..f4648994fa9
--- /dev/null
+++ b/src/test/ui/stdlib-unit-tests/not-sync.rs
@@ -0,0 +1,22 @@
+use std::cell::{Cell, RefCell};
+use std::rc::{Rc, Weak};
+use std::sync::mpsc::{Receiver, Sender};
+
+fn test<T: Sync>() {}
+
+fn main() {
+    test::<Cell<i32>>();
+    //~^ ERROR `Cell<i32>` cannot be shared between threads safely [E0277]
+    test::<RefCell<i32>>();
+    //~^ ERROR `RefCell<i32>` cannot be shared between threads safely [E0277]
+
+    test::<Rc<i32>>();
+    //~^ ERROR `Rc<i32>` cannot be shared between threads safely [E0277]
+    test::<Weak<i32>>();
+    //~^ ERROR `std::rc::Weak<i32>` cannot be shared between threads safely [E0277]
+
+    test::<Receiver<i32>>();
+    //~^ ERROR `std::sync::mpsc::Receiver<i32>` cannot be shared between threads safely [E0277]
+    test::<Sender<i32>>();
+    //~^ ERROR `Sender<i32>` cannot be shared between threads safely [E0277]
+}
diff --git a/src/test/ui/stdlib-unit-tests/not-sync.stderr b/src/test/ui/stdlib-unit-tests/not-sync.stderr
new file mode 100644
index 00000000000..1ee358ba836
--- /dev/null
+++ b/src/test/ui/stdlib-unit-tests/not-sync.stderr
@@ -0,0 +1,81 @@
+error[E0277]: `Cell<i32>` cannot be shared between threads safely
+  --> $DIR/not-sync.rs:8:12
+   |
+LL |     test::<Cell<i32>>();
+   |            ^^^^^^^^^ `Cell<i32>` cannot be shared between threads safely
+   |
+   = help: the trait `Sync` is not implemented for `Cell<i32>`
+note: required by a bound in `test`
+  --> $DIR/not-sync.rs:5:12
+   |
+LL | fn test<T: Sync>() {}
+   |            ^^^^ required by this bound in `test`
+
+error[E0277]: `RefCell<i32>` cannot be shared between threads safely
+  --> $DIR/not-sync.rs:10:12
+   |
+LL |     test::<RefCell<i32>>();
+   |            ^^^^^^^^^^^^ `RefCell<i32>` cannot be shared between threads safely
+   |
+   = help: the trait `Sync` is not implemented for `RefCell<i32>`
+note: required by a bound in `test`
+  --> $DIR/not-sync.rs:5:12
+   |
+LL | fn test<T: Sync>() {}
+   |            ^^^^ required by this bound in `test`
+
+error[E0277]: `Rc<i32>` cannot be shared between threads safely
+  --> $DIR/not-sync.rs:13:12
+   |
+LL |     test::<Rc<i32>>();
+   |            ^^^^^^^ `Rc<i32>` cannot be shared between threads safely
+   |
+   = help: the trait `Sync` is not implemented for `Rc<i32>`
+note: required by a bound in `test`
+  --> $DIR/not-sync.rs:5:12
+   |
+LL | fn test<T: Sync>() {}
+   |            ^^^^ required by this bound in `test`
+
+error[E0277]: `std::rc::Weak<i32>` cannot be shared between threads safely
+  --> $DIR/not-sync.rs:15:12
+   |
+LL |     test::<Weak<i32>>();
+   |            ^^^^^^^^^ `std::rc::Weak<i32>` cannot be shared between threads safely
+   |
+   = help: the trait `Sync` is not implemented for `std::rc::Weak<i32>`
+note: required by a bound in `test`
+  --> $DIR/not-sync.rs:5:12
+   |
+LL | fn test<T: Sync>() {}
+   |            ^^^^ required by this bound in `test`
+
+error[E0277]: `std::sync::mpsc::Receiver<i32>` cannot be shared between threads safely
+  --> $DIR/not-sync.rs:18:12
+   |
+LL |     test::<Receiver<i32>>();
+   |            ^^^^^^^^^^^^^ `std::sync::mpsc::Receiver<i32>` cannot be shared between threads safely
+   |
+   = help: the trait `Sync` is not implemented for `std::sync::mpsc::Receiver<i32>`
+note: required by a bound in `test`
+  --> $DIR/not-sync.rs:5:12
+   |
+LL | fn test<T: Sync>() {}
+   |            ^^^^ required by this bound in `test`
+
+error[E0277]: `Sender<i32>` cannot be shared between threads safely
+  --> $DIR/not-sync.rs:20:12
+   |
+LL |     test::<Sender<i32>>();
+   |            ^^^^^^^^^^^ `Sender<i32>` cannot be shared between threads safely
+   |
+   = help: the trait `Sync` is not implemented for `Sender<i32>`
+note: required by a bound in `test`
+  --> $DIR/not-sync.rs:5:12
+   |
+LL | fn test<T: Sync>() {}
+   |            ^^^^ required by this bound in `test`
+
+error: aborting due to 6 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/stdlib-unit-tests/raw-fat-ptr.rs b/src/test/ui/stdlib-unit-tests/raw-fat-ptr.rs
new file mode 100644
index 00000000000..9f50659ed60
--- /dev/null
+++ b/src/test/ui/stdlib-unit-tests/raw-fat-ptr.rs
@@ -0,0 +1,118 @@
+// run-pass
+// check raw fat pointer ops
+
+use std::mem;
+
+fn assert_inorder<T: PartialEq + PartialOrd>(a: &[T]) {
+    for i in 0..a.len() {
+        for j in 0..a.len() {
+            if i < j {
+                assert!(a[i] < a[j]);
+                assert!(a[i] <= a[j]);
+                assert!(!(a[i] == a[j]));
+                assert!(a[i] != a[j]);
+                assert!(!(a[i] >= a[j]));
+                assert!(!(a[i] > a[j]));
+            } else if i == j {
+                assert!(!(a[i] < a[j]));
+                assert!(a[i] <= a[j]);
+                assert!(a[i] == a[j]);
+                assert!(!(a[i] != a[j]));
+                assert!(a[i] >= a[j]);
+                assert!(!(a[i] > a[j]));
+            } else {
+                assert!(!(a[i] < a[j]));
+                assert!(!(a[i] <= a[j]));
+                assert!(!(a[i] == a[j]));
+                assert!(a[i] != a[j]);
+                assert!(a[i] >= a[j]);
+                assert!(a[i] > a[j]);
+            }
+        }
+    }
+}
+
+trait Foo { fn foo(&self) -> usize; }
+impl<T> Foo for T {
+    fn foo(&self) -> usize {
+        mem::size_of::<T>()
+    }
+}
+
+struct S<T:?Sized>(u32, T);
+
+fn main() {
+    let mut array = [0,1,2,3,4];
+    let mut array2 = [5,6,7,8,9];
+
+    // fat ptr comparison: addr then extra
+
+    // check ordering for arrays
+    let mut ptrs: Vec<*const [u8]> = vec![
+        &array[0..0], &array[0..1], &array, &array[1..]
+    ];
+
+    let array_addr = &array as *const [u8] as *const u8 as usize;
+    let array2_addr = &array2 as *const [u8] as *const u8 as usize;
+    if array2_addr < array_addr {
+        ptrs.insert(0, &array2);
+    } else {
+        ptrs.push(&array2);
+    }
+    assert_inorder(&ptrs);
+
+    // check ordering for mut arrays
+    let mut ptrs: Vec<*mut [u8]> = vec![
+        &mut array[0..0], &mut array[0..1], &mut array, &mut array[1..]
+    ];
+
+    let array_addr = &mut array as *mut [u8] as *mut u8 as usize;
+    let array2_addr = &mut array2 as *mut [u8] as *mut u8 as usize;
+    if array2_addr < array_addr {
+        ptrs.insert(0, &mut array2);
+    } else {
+        ptrs.push(&mut array2);
+    }
+    assert_inorder(&ptrs);
+
+    let mut u8_ = (0u8, 1u8);
+    let mut u32_ = (4u32, 5u32);
+
+    // check ordering for ptrs
+    let buf: &mut [*const dyn Foo] = &mut [
+        &u8_, &u8_.0,
+        &u32_, &u32_.0,
+    ];
+    buf.sort_by(|u,v| {
+        let u : [*const (); 2] = unsafe { mem::transmute(*u) };
+        let v : [*const (); 2] = unsafe { mem::transmute(*v) };
+        u.cmp(&v)
+    });
+    assert_inorder(buf);
+
+    // check ordering for mut ptrs
+    let buf: &mut [*mut dyn Foo] = &mut [
+        &mut u8_, &mut u8_.0,
+        &mut u32_, &mut u32_.0,
+    ];
+    buf.sort_by(|u,v| {
+        let u : [*const (); 2] = unsafe { mem::transmute(*u) };
+        let v : [*const (); 2] = unsafe { mem::transmute(*v) };
+        u.cmp(&v)
+    });
+    assert_inorder(buf);
+
+    // check ordering for structs containing arrays
+    let ss: (S<[u8; 2]>,
+             S<[u8; 3]>,
+             S<[u8; 2]>) = (
+        S(7, [8, 9]),
+        S(10, [11, 12, 13]),
+        S(4, [5, 6])
+    );
+    assert_inorder(&[
+        &ss.0 as *const S<[u8]>,
+        &ss.1 as *const S<[u8]>,
+        &ss.2 as *const S<[u8]>
+    ]);
+}
diff --git a/src/test/ui/stdlib-unit-tests/volatile-fat-ptr.rs b/src/test/ui/stdlib-unit-tests/volatile-fat-ptr.rs
new file mode 100644
index 00000000000..f73e7e1c391
--- /dev/null
+++ b/src/test/ui/stdlib-unit-tests/volatile-fat-ptr.rs
@@ -0,0 +1,15 @@
+// run-pass
+
+#![allow(stable_features)]
+#![feature(volatile)]
+use std::ptr::{read_volatile, write_volatile};
+
+fn main() {
+    let mut x: &'static str = "test";
+    unsafe {
+        let a = read_volatile(&x);
+        assert_eq!(a, "test");
+        write_volatile(&mut x, "foo");
+        assert_eq!(x, "foo");
+    }
+}