Skip to content

Commit d022772

Browse files
committed
Added tests
1 parent a5d806c commit d022772

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ matrix:
2424
fast_finish: true
2525

2626
script:
27-
- cargo build --release
28-
- cargo build --release --features no_secure
27+
- cargo test --all
28+
- cargo test --all --features no_secure

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ version = "0.1.0"
44
authors = ["Octavian Oncescu <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/purpleprotocol/mimalloc_rust"
7-
keywords = ["allocator", "encrypted-heap", "performance"]
8-
categories = ["allocator"]
7+
keywords = ["mimalloc", "allocator", "encrypted-heap", "performance"]
8+
categories = ["memory-management", "api-bindings"]
99
description = "Performance and security oriented drop-in allocator"
1010
license = "MIT"
1111
readme = "README.md"
1212

13+
[badges]
14+
travis-ci = { repository = "purpleprotocol/mimalloc_rust" }
15+
1316
[dependencies]
1417
libc = "0.2"
1518
libmimalloc-sys = { path = "libmimalloc-sys", version = "0.1.0" }

libmimalloc-sys/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,28 @@ extern "C" {
77
pub fn mi_malloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
88
pub fn mi_realloc_aligned(p: *const c_void, size: size_t, alignment: size_t) -> *const c_void;
99
pub fn mi_free(p: *const c_void) -> c_void;
10+
}
11+
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
16+
#[test]
17+
fn it_frees_memory_malloc() {
18+
let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
19+
unsafe { mi_free(ptr as *const c_void) };
20+
}
21+
22+
#[test]
23+
fn it_frees_memory_zalloc() {
24+
let ptr = unsafe { mi_zalloc_aligned(8, 8) } as *mut u8;
25+
unsafe { mi_free(ptr as *const c_void) };
26+
}
27+
28+
#[test]
29+
fn it_frees_memory_realloc() {
30+
let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
31+
let ptr = unsafe { mi_realloc_aligned(ptr as *const c_void, 8, 8) } as *mut u8;
32+
unsafe { mi_free(ptr as *const c_void) };
33+
}
1034
}

src/lib.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! Mimalloc is a general purpose, performance oriented allocator built by Microsoft.
55
//!
66
//! ## Usage
7-
//! ```rust
7+
//! ```rust,ignore
88
//! use mimalloc::MiMalloc;
99
//!
1010
//! #[global_allocator]
@@ -16,7 +16,7 @@
1616
//! heap allocations are encrypted, but this results in a 3% increase in overhead.
1717
//!
1818
//! In `Cargo.toml`:
19-
//! ```rust
19+
//! ```rust,ignore
2020
//! [dependencies]
2121
//! mimalloc = { version = "*", features = ["no_secure"] }
2222
//! ```
@@ -51,7 +51,7 @@ const MIN_ALIGN: usize = 16;
5151
/// Drop-in mimalloc global allocator.
5252
///
5353
/// ## Usage
54-
/// ```rust
54+
/// ```rust,ignore
5555
/// use mimalloc::MiMalloc;
5656
///
5757
/// #[global_allocator]
@@ -97,4 +97,43 @@ unsafe impl GlobalAlloc for MiMalloc {
9797

9898
mi_realloc_aligned(ptr as *const c_void, new_size, align) as *mut u8
9999
}
100+
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
106+
#[test]
107+
fn it_frees_allocated_memory() {
108+
unsafe {
109+
let layout = Layout::from_size_align(8, 8).unwrap();
110+
let alloc = MiMalloc;
111+
112+
let ptr = alloc.alloc(layout.clone());
113+
alloc.dealloc(ptr, layout);
114+
}
115+
}
116+
117+
#[test]
118+
fn it_frees_zero_allocated_memory() {
119+
unsafe {
120+
let layout = Layout::from_size_align(8, 8).unwrap();
121+
let alloc = MiMalloc;
122+
123+
let ptr = alloc.alloc_zeroed(layout.clone());
124+
alloc.dealloc(ptr, layout);
125+
}
126+
}
127+
128+
#[test]
129+
fn it_frees_reallocated_memory() {
130+
unsafe {
131+
let layout = Layout::from_size_align(8, 8).unwrap();
132+
let alloc = MiMalloc;
133+
134+
let ptr = alloc.alloc(layout.clone());
135+
let ptr = alloc.realloc(ptr, layout.clone(), 16);
136+
alloc.dealloc(ptr, layout);
137+
}
138+
}
100139
}

0 commit comments

Comments
 (0)