-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a real-world allocation test case
This commit adds a simple but important test case: it uses the allocator on the machine running the tests. Since this is a hosted environment, allocations are rather frequent and the heap space is relatively large (here 4M). This way all the allocations in the `std` and in the test runner are also directed to `emballoc`, which is great for testing. This commit also contains a minor adjustment: the README and crate-level documentation previously mentioned the availability of `HashMap`: this is not true (most likely since `alloc` still has no random generator re- quired for the random state of the HashMap) and thus replaced with the `BTreeMap`.
- Loading branch information
Showing
3 changed files
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#![no_std] | ||
|
||
const HEAP_SIZE: usize = 4 * 1024 * 1024; | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: emballoc::Allocator<HEAP_SIZE> = emballoc::Allocator::new(); | ||
|
||
extern crate alloc; | ||
|
||
#[test] | ||
fn vec() { | ||
let mut v = alloc::vec![1, 2, 3]; | ||
v.push(4); | ||
|
||
assert_eq!((1..=4).collect::<alloc::vec::Vec<_>>(), v); | ||
} | ||
|
||
#[test] | ||
fn map_and_formatting() { | ||
let mut map = alloc::collections::BTreeMap::new(); | ||
map.insert(10, "Hello"); | ||
map.insert(11, "world"); | ||
map.insert(20, "Hallo"); | ||
map.insert(21, "Welt"); | ||
map.insert(-1, "english"); | ||
map.insert(-2, "german"); | ||
|
||
let english = alloc::format!("[{}]: {}, {}!", map[&-1], map[&10], map[&11]); | ||
let german = alloc::format!("[{}]: {}, {}!", map[&-2], map[&20], map[&21]); | ||
assert_eq!(english, "[english]: Hello, world!"); | ||
assert_eq!(german, "[german]: Hallo, Welt!"); | ||
} |