Skip to content

Commit da895ba

Browse files
committed
Fix tests and add Travis config
1 parent d0bd841 commit da895ba

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

.travis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: rust
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- rust: stable
7+
- rust: beta
8+
- rust: nightly
9+
- rust: nightly
10+
script:
11+
- cargo test --features allocator-api
12+
- rust: nightly
13+
install: rustup target add wasm32-unknown-unknown
14+
script:
15+
- cargo build --target wasm32-unknown-unknown
16+
- cargo build --target wasm32-unknown-unknown --release
17+
18+
script:
19+
- cargo test
20+
- cargo test --features debug
21+
- cargo test --release
22+
- cargo test --release --features debug
23+
- RUSTFLAGS='--cfg test_lots' cargo test --release
24+
- RUSTFLAGS='--cfg test_lots' cargo test --release --features debug
25+
26+
notifications:
27+
email:
28+
on_success: never

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ libc = { version = "0.2", default-features = false }
1414
rand = "0.3"
1515

1616
[profile.release]
17-
opt-level = 'z'
17+
debug-assertions = true
1818

1919
[features]
2020
debug = []

tests/global.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg(feature = "allocator-api")]
12
#![feature(global_allocator)]
23

34
extern crate dlmalloc;

tests/smoke.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
#![feature(allocator_api)]
2-
31
extern crate dlmalloc;
42
extern crate rand;
53

64
use std::cmp;
7-
use std::heap::{Layout, Alloc, System};
8-
95
use dlmalloc::Dlmalloc;
106
use rand::Rng;
117

128
#[test]
139
fn smoke() {
1410
let mut a = Dlmalloc::new();
1511
unsafe {
16-
let layout = Layout::new::<u8>();
17-
let ptr = a.alloc(layout.clone()).unwrap_or_else(|e| System.oom(e));
12+
let ptr = a.malloc(1, 1);
13+
assert!(!ptr.is_null());
1814
*ptr = 9;
1915
assert_eq!(*ptr, 9);
20-
a.dealloc(ptr, layout.clone());
16+
a.free(ptr, 1, 1);
2117

22-
let ptr = a.alloc(layout.clone()).unwrap_or_else(|e| System.oom(e));
18+
let ptr = a.malloc(1, 1);
19+
assert!(!ptr.is_null());
2320
*ptr = 10;
2421
assert_eq!(*ptr, 10);
25-
a.dealloc(ptr, layout.clone());
22+
a.free(ptr, 1, 1);
2623
}
2724
}
2825

@@ -31,41 +28,39 @@ fn stress() {
3128
let mut a = Dlmalloc::new();
3229
let mut rng = rand::thread_rng();
3330
let mut ptrs = Vec::new();
31+
let max = if cfg!(test_lots) { 1_000_000 } else { 1_000 };
3432
unsafe {
35-
for _ in 0..1_000_000 {
33+
for _ in 0..max {
3634
let free =
3735
ptrs.len() > 0 &&
3836
((ptrs.len() < 10_000 && rng.gen_weighted_bool(3)) || rng.gen());
3937
if free {
4038
let idx = rng.gen_range(0, ptrs.len());
41-
let (ptr, layout): (_, Layout) = ptrs.swap_remove(idx);
42-
a.dealloc(ptr, layout);
39+
let (ptr, size, align) = ptrs.swap_remove(idx);
40+
a.free(ptr, size, align);
4341
continue
4442
}
4543

4644
if ptrs.len() > 0 && rng.gen_weighted_bool(100) {
4745
let idx = rng.gen_range(0, ptrs.len());
48-
let (ptr, old): (_, Layout) = ptrs.swap_remove(idx);
49-
let new = if rng.gen() {
50-
Layout::from_size_align(rng.gen_range(old.size(), old.size() * 2),
51-
old.align()).unwrap()
52-
} else if old.size() > 10 {
53-
Layout::from_size_align(rng.gen_range(old.size() / 2, old.size()),
54-
old.align()).unwrap()
46+
let (ptr, size, align) = ptrs.swap_remove(idx);
47+
let new_size = if rng.gen() {
48+
rng.gen_range(size, size * 2)
49+
} else if size > 10 {
50+
rng.gen_range(size / 2, size)
5551
} else {
5652
continue
5753
};
5854
let mut tmp = Vec::new();
59-
for i in 0..cmp::min(old.size(), new.size()) {
55+
for i in 0..cmp::min(size, new_size) {
6056
tmp.push(*ptr.offset(i as isize));
6157
}
62-
let ptr = Alloc::realloc(&mut a, ptr, old, new.clone()).unwrap_or_else(|e| {
63-
System.oom(e)
64-
});
58+
let ptr = a.realloc(ptr, size, align, new_size);
59+
assert!(!ptr.is_null());
6560
for (i, byte) in tmp.iter().enumerate() {
6661
assert_eq!(*byte, *ptr.offset(i as isize));
6762
}
68-
ptrs.push((ptr, new));
63+
ptrs.push((ptr, new_size, align));
6964
}
7065

7166
let size = if rng.gen() {
@@ -80,20 +75,18 @@ fn stress() {
8075
};
8176

8277
let zero = rng.gen_weighted_bool(50);
83-
let layout = Layout::from_size_align(size, align).unwrap();
84-
8578
let ptr = if zero {
86-
a.alloc_zeroed(layout.clone()).unwrap_or_else(|e| System.oom(e))
79+
a.calloc(size, align)
8780
} else {
88-
a.alloc(layout.clone()).unwrap_or_else(|e| System.oom(e))
81+
a.malloc(size, align)
8982
};
90-
for i in 0..layout.size() {
83+
for i in 0..size {
9184
if zero {
9285
assert_eq!(*ptr.offset(i as isize), 0);
9386
}
9487
*ptr.offset(i as isize) = 0xce;
9588
}
96-
ptrs.push((ptr, layout));
89+
ptrs.push((ptr, size, align));
9790
}
9891
}
9992
}

0 commit comments

Comments
 (0)