about summary refs log tree commit diff
path: root/src/test/ui/allocator
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-07-27 01:33:01 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-07-27 18:56:16 +0300
commit9be35f82c1abf2ecbab489bca9eca138ea648312 (patch)
tree69888506e34af447d9748c0d542de3ba1dd76210 /src/test/ui/allocator
parentca9faa52f5ada0054b1fa27d97aedf448afb059b (diff)
downloadrust-9be35f82c1abf2ecbab489bca9eca138ea648312.tar.gz
rust-9be35f82c1abf2ecbab489bca9eca138ea648312.zip
tests: Move run-pass tests without naming conflicts to ui
Diffstat (limited to 'src/test/ui/allocator')
-rw-r--r--src/test/ui/allocator/auxiliary/custom-as-global.rs16
-rw-r--r--src/test/ui/allocator/auxiliary/custom.rs21
-rw-r--r--src/test/ui/allocator/auxiliary/helper.rs9
-rw-r--r--src/test/ui/allocator/custom-in-block.rs22
-rw-r--r--src/test/ui/allocator/custom-in-submodule.rs26
-rw-r--r--src/test/ui/allocator/custom.rs58
-rw-r--r--src/test/ui/allocator/xcrate-use.rs35
-rw-r--r--src/test/ui/allocator/xcrate-use2.rs47
8 files changed, 234 insertions, 0 deletions
diff --git a/src/test/ui/allocator/auxiliary/custom-as-global.rs b/src/test/ui/allocator/auxiliary/custom-as-global.rs
new file mode 100644
index 00000000000..a5e96e77501
--- /dev/null
+++ b/src/test/ui/allocator/auxiliary/custom-as-global.rs
@@ -0,0 +1,16 @@
+// no-prefer-dynamic
+
+#![crate_type = "rlib"]
+
+extern crate custom;
+
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+use custom::A;
+
+#[global_allocator]
+static ALLOCATOR: A = A(AtomicUsize::new(0));
+
+pub fn get() -> usize {
+    ALLOCATOR.0.load(Ordering::SeqCst)
+}
diff --git a/src/test/ui/allocator/auxiliary/custom.rs b/src/test/ui/allocator/auxiliary/custom.rs
new file mode 100644
index 00000000000..b0ec9ab0929
--- /dev/null
+++ b/src/test/ui/allocator/auxiliary/custom.rs
@@ -0,0 +1,21 @@
+// no-prefer-dynamic
+
+#![feature(allocator_api)]
+#![crate_type = "rlib"]
+
+use std::alloc::{GlobalAlloc, System, Layout};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+pub struct A(pub AtomicUsize);
+
+unsafe impl GlobalAlloc for A {
+    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+        self.0.fetch_add(1, Ordering::SeqCst);
+        System.alloc(layout)
+    }
+
+    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
+        self.0.fetch_add(1, Ordering::SeqCst);
+        System.dealloc(ptr, layout)
+    }
+}
diff --git a/src/test/ui/allocator/auxiliary/helper.rs b/src/test/ui/allocator/auxiliary/helper.rs
new file mode 100644
index 00000000000..7f6770c226a
--- /dev/null
+++ b/src/test/ui/allocator/auxiliary/helper.rs
@@ -0,0 +1,9 @@
+// no-prefer-dynamic
+
+#![crate_type = "rlib"]
+
+use std::fmt;
+
+pub fn work_with(p: &fmt::Debug) {
+    drop(p);
+}
diff --git a/src/test/ui/allocator/custom-in-block.rs b/src/test/ui/allocator/custom-in-block.rs
new file mode 100644
index 00000000000..12813a1fc8b
--- /dev/null
+++ b/src/test/ui/allocator/custom-in-block.rs
@@ -0,0 +1,22 @@
+// run-pass
+// no-prefer-dynamic
+// aux-build:custom.rs
+// aux-build:helper.rs
+
+extern crate custom;
+extern crate helper;
+
+use custom::A;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+fn main() {
+    #[global_allocator]
+    pub static GLOBAL: A = A(AtomicUsize::new(0));
+
+    let n = GLOBAL.0.load(Ordering::SeqCst);
+    let s = Box::new(0);
+    helper::work_with(&s);
+    assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
+    drop(s);
+    assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
+}
diff --git a/src/test/ui/allocator/custom-in-submodule.rs b/src/test/ui/allocator/custom-in-submodule.rs
new file mode 100644
index 00000000000..ea341b1ac14
--- /dev/null
+++ b/src/test/ui/allocator/custom-in-submodule.rs
@@ -0,0 +1,26 @@
+// run-pass
+// no-prefer-dynamic
+// aux-build:custom.rs
+// aux-build:helper.rs
+
+extern crate custom;
+extern crate helper;
+
+use custom::A;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+mod submodule {
+    use super::*;
+
+    #[global_allocator]
+    pub static GLOBAL: A = A(AtomicUsize::new(0));
+}
+
+fn main() {
+    let n = submodule::GLOBAL.0.load(Ordering::SeqCst);
+    let s = Box::new(0);
+    helper::work_with(&s);
+    assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), n + 1);
+    drop(s);
+    assert_eq!(submodule::GLOBAL.0.load(Ordering::SeqCst), n + 2);
+}
diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs
new file mode 100644
index 00000000000..71f72ae46c2
--- /dev/null
+++ b/src/test/ui/allocator/custom.rs
@@ -0,0 +1,58 @@
+// run-pass
+
+// aux-build:helper.rs
+// no-prefer-dynamic
+
+#![feature(allocator_api)]
+
+extern crate helper;
+
+use std::alloc::{self, Global, Alloc, System, Layout};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+static HITS: AtomicUsize = AtomicUsize::new(0);
+
+struct A;
+
+unsafe impl alloc::GlobalAlloc for A {
+    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+        HITS.fetch_add(1, Ordering::SeqCst);
+        System.alloc(layout)
+    }
+
+    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
+        HITS.fetch_add(1, Ordering::SeqCst);
+        System.dealloc(ptr, layout)
+    }
+}
+
+#[global_allocator]
+static GLOBAL: A = A;
+
+fn main() {
+    println!("hello!");
+
+    let n = HITS.load(Ordering::SeqCst);
+    assert!(n > 0);
+    unsafe {
+        let layout = Layout::from_size_align(4, 2).unwrap();
+
+        let ptr = Global.alloc(layout.clone()).unwrap();
+        helper::work_with(&ptr);
+        assert_eq!(HITS.load(Ordering::SeqCst), n + 1);
+        Global.dealloc(ptr, layout.clone());
+        assert_eq!(HITS.load(Ordering::SeqCst), n + 2);
+
+        let s = String::with_capacity(10);
+        helper::work_with(&s);
+        assert_eq!(HITS.load(Ordering::SeqCst), n + 3);
+        drop(s);
+        assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
+
+        let ptr = System.alloc(layout.clone()).unwrap();
+        assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
+        helper::work_with(&ptr);
+        System.dealloc(ptr, layout);
+        assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
+    }
+}
diff --git a/src/test/ui/allocator/xcrate-use.rs b/src/test/ui/allocator/xcrate-use.rs
new file mode 100644
index 00000000000..039c70e77be
--- /dev/null
+++ b/src/test/ui/allocator/xcrate-use.rs
@@ -0,0 +1,35 @@
+// run-pass
+
+// aux-build:custom.rs
+// aux-build:helper.rs
+// no-prefer-dynamic
+
+#![feature(allocator_api)]
+
+extern crate custom;
+extern crate helper;
+
+use std::alloc::{Global, Alloc, System, Layout};
+use std::sync::atomic::{Ordering, AtomicUsize};
+
+#[global_allocator]
+static GLOBAL: custom::A = custom::A(AtomicUsize::new(0));
+
+fn main() {
+    unsafe {
+        let n = GLOBAL.0.load(Ordering::SeqCst);
+        let layout = Layout::from_size_align(4, 2).unwrap();
+
+        let ptr = Global.alloc(layout.clone()).unwrap();
+        helper::work_with(&ptr);
+        assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
+        Global.dealloc(ptr, layout.clone());
+        assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
+
+        let ptr = System.alloc(layout.clone()).unwrap();
+        assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
+        helper::work_with(&ptr);
+        System.dealloc(ptr, layout);
+        assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
+    }
+}
diff --git a/src/test/ui/allocator/xcrate-use2.rs b/src/test/ui/allocator/xcrate-use2.rs
new file mode 100644
index 00000000000..d8478fb5eaa
--- /dev/null
+++ b/src/test/ui/allocator/xcrate-use2.rs
@@ -0,0 +1,47 @@
+// run-pass
+
+// aux-build:custom.rs
+// aux-build:custom-as-global.rs
+// aux-build:helper.rs
+// no-prefer-dynamic
+
+#![feature(allocator_api)]
+
+extern crate custom;
+extern crate custom_as_global;
+extern crate helper;
+
+use std::alloc::{alloc, dealloc, GlobalAlloc, System, Layout};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+static GLOBAL: custom::A = custom::A(AtomicUsize::new(0));
+
+fn main() {
+    unsafe {
+        let n = custom_as_global::get();
+        let layout = Layout::from_size_align(4, 2).unwrap();
+
+        // Global allocator routes to the `custom_as_global` global
+        let ptr = alloc(layout.clone());
+        helper::work_with(&ptr);
+        assert_eq!(custom_as_global::get(), n + 1);
+        dealloc(ptr, layout.clone());
+        assert_eq!(custom_as_global::get(), n + 2);
+
+        // Usage of the system allocator avoids all globals
+        let ptr = System.alloc(layout.clone());
+        helper::work_with(&ptr);
+        assert_eq!(custom_as_global::get(), n + 2);
+        System.dealloc(ptr, layout.clone());
+        assert_eq!(custom_as_global::get(), n + 2);
+
+        // Usage of our personal allocator doesn't affect other instances
+        let ptr = GLOBAL.alloc(layout.clone());
+        helper::work_with(&ptr);
+        assert_eq!(custom_as_global::get(), n + 2);
+        assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 1);
+        GLOBAL.dealloc(ptr, layout);
+        assert_eq!(custom_as_global::get(), n + 2);
+        assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 2);
+    }
+}