Skip to content

Commit c3662ca

Browse files
authored
Merge pull request #6 from jfrimmel/realworld-allocations
Add a real-world allocation test case
2 parents f5ddf97 + 6f116e0 commit c3662ca

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ static ALLOCATOR: emballoc::Allocator<4096> = emballoc::Allocator::new();
3232
extern crate alloc;
3333
```
3434

35-
Now the crate can use the `std` collections such as `Vec<T>`, `HashMap<K, V>`, etc. together with important types like `Box<T>` and `Rc<T>`.
35+
Now the crate can use the `std` collections such as `Vec<T>`, `BTreeMap<K, V>`, etc. together with important types like `Box<T>` and `Rc<T>`.
3636
Note, that things in the `std`-prelude (e.g. `Vec<T>`, `Box<T>`, ...) have to be imported explicitly.

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
//! The usage is simple: just copy and paste the following code snipped into
1313
//! your binary crate and potentially adjust the number of bytes of the heap
1414
//! (here 4K):
15-
//! ```no_run
15+
//! ```
1616
//! #[global_allocator]
1717
//! static ALLOCATOR: emballoc::Allocator<4096> = emballoc::Allocator::new();
1818
//!
1919
//! extern crate alloc;
2020
//! ```
2121
//! Afterwards you don't need to interact with the crate or the variable
2222
//! `ALLOCATOR` anymore. Now you can just `use alloc::vec::Vec` or even
23-
//! `use alloc::collections::HashMap`, i.e. every fancy collection which is
23+
//! `use alloc::collections::BTreeMap`, i.e. every fancy collection which is
2424
//! normally provided by the `std`.
2525
//!
2626
//! The minimal buffer size is `8`, which would allow exactly one allocation of

tests/allocation.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![no_std]
2+
3+
const HEAP_SIZE: usize = 4 * 1024 * 1024;
4+
5+
#[global_allocator]
6+
static ALLOCATOR: emballoc::Allocator<HEAP_SIZE> = emballoc::Allocator::new();
7+
8+
extern crate alloc;
9+
10+
#[test]
11+
fn vec() {
12+
let mut v = alloc::vec![1, 2, 3];
13+
v.push(4);
14+
15+
assert_eq!((1..=4).collect::<alloc::vec::Vec<_>>(), v);
16+
}
17+
18+
#[test]
19+
fn map_and_formatting() {
20+
let mut map = alloc::collections::BTreeMap::new();
21+
map.insert(10, "Hello");
22+
map.insert(11, "world");
23+
map.insert(20, "Hallo");
24+
map.insert(21, "Welt");
25+
map.insert(-1, "english");
26+
map.insert(-2, "german");
27+
28+
let english = alloc::format!("[{}]: {}, {}!", map[&-1], map[&10], map[&11]);
29+
let german = alloc::format!("[{}]: {}, {}!", map[&-2], map[&20], map[&21]);
30+
assert_eq!(english, "[english]: Hello, world!");
31+
assert_eq!(german, "[german]: Hallo, Welt!");
32+
}

0 commit comments

Comments
 (0)