Skip to content

Commit a87a733

Browse files
committed
test System/Global allocator API: alloc_zeroed, realloc
1 parent 788616d commit a87a733

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

tests/run-pass/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//ignore-windows: env var emulation not implemented on Windows
1+
//ignore-windows: TODO env var emulation stubbed out on Windows
22

33
use std::env;
44

tests/run-pass/hashmap.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ fn test_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
2727
}
2828

2929
fn main() {
30-
if cfg!(not(target_os = "macos")) {
31-
let map: HashMap<i32, i32> = HashMap::default();
32-
test_map(map);
33-
} else {
34-
// TODO: Implement random number generation on OS X.
30+
if cfg!(target_os = "macos") { // TODO: Implement random number generation on OS X.
3531
// Until then, use a deterministic map.
3632
let map : HashMap<i32, i32, BuildHasherDefault<collections::hash_map::DefaultHasher>> = HashMap::default();
3733
test_map(map);
34+
} else {
35+
let map: HashMap<i32, i32> = HashMap::default();
36+
test_map(map);
3837
}
3938
}

tests/run-pass/heap_allocator.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
//ignore-windows: inspects allocation base address on Windows
2-
31
#![feature(allocator_api)]
42

53
use std::ptr::NonNull;
64
use std::alloc::{Global, Alloc, Layout, System};
5+
use std::slice;
6+
7+
fn check_alloc<T: Alloc>(mut allocator: T) { unsafe {
8+
let layout = Layout::from_size_align(20, 4).unwrap();
9+
let a = allocator.alloc(layout).unwrap();
10+
allocator.dealloc(a, layout);
11+
12+
let p1 = allocator.alloc_zeroed(layout).unwrap();
13+
14+
let p2 = allocator.realloc(p1, Layout::from_size_align(20, 4).unwrap(), 40).unwrap();
15+
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
16+
assert_eq!(&slice, &[0_u8; 20]);
17+
18+
// old size == new size
19+
let p3 = allocator.realloc(p2, Layout::from_size_align(40, 4).unwrap(), 40).unwrap();
20+
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
21+
assert_eq!(&slice, &[0_u8; 20]);
22+
23+
// old size > new size
24+
let p4 = allocator.realloc(p3, Layout::from_size_align(40, 4).unwrap(), 10).unwrap();
25+
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
26+
assert_eq!(&slice, &[0_u8; 10]);
27+
28+
allocator.dealloc(p4, Layout::from_size_align(10, 4).unwrap());
29+
} }
730

831
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
932
let size = 8;
@@ -50,6 +73,9 @@ fn box_to_global() {
5073
}
5174

5275
fn main() {
76+
check_alloc(System);
77+
check_alloc(Global);
78+
#[cfg(not(target_os = "windows"))] // TODO: Inspects allocation base address on Windows; needs intptrcast model
5379
check_overalign_requests(System);
5480
check_overalign_requests(Global);
5581
global_to_box();

0 commit comments

Comments
 (0)