From 6f116e0c73dcf011c104da1a9c4d3f2225ab13fe Mon Sep 17 00:00:00 2001 From: Julian Frimmel Date: Sat, 20 Aug 2022 18:27:43 +0200 Subject: [PATCH] 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`. --- README.md | 2 +- src/lib.rs | 4 ++-- tests/allocation.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tests/allocation.rs diff --git a/README.md b/README.md index 3a1e1b7..21eb409 100644 --- a/README.md +++ b/README.md @@ -32,5 +32,5 @@ static ALLOCATOR: emballoc::Allocator<4096> = emballoc::Allocator::new(); extern crate alloc; ``` -Now the crate can use the `std` collections such as `Vec`, `HashMap`, etc. together with important types like `Box` and `Rc`. +Now the crate can use the `std` collections such as `Vec`, `BTreeMap`, etc. together with important types like `Box` and `Rc`. Note, that things in the `std`-prelude (e.g. `Vec`, `Box`, ...) have to be imported explicitly. diff --git a/src/lib.rs b/src/lib.rs index 4653695..bd96407 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ //! The usage is simple: just copy and paste the following code snipped into //! your binary crate and potentially adjust the number of bytes of the heap //! (here 4K): -//! ```no_run +//! ``` //! #[global_allocator] //! static ALLOCATOR: emballoc::Allocator<4096> = emballoc::Allocator::new(); //! @@ -20,7 +20,7 @@ //! ``` //! Afterwards you don't need to interact with the crate or the variable //! `ALLOCATOR` anymore. Now you can just `use alloc::vec::Vec` or even -//! `use alloc::collections::HashMap`, i.e. every fancy collection which is +//! `use alloc::collections::BTreeMap`, i.e. every fancy collection which is //! normally provided by the `std`. //! //! The minimal buffer size is `8`, which would allow exactly one allocation of diff --git a/tests/allocation.rs b/tests/allocation.rs new file mode 100644 index 0000000..da491ad --- /dev/null +++ b/tests/allocation.rs @@ -0,0 +1,32 @@ +#![no_std] + +const HEAP_SIZE: usize = 4 * 1024 * 1024; + +#[global_allocator] +static ALLOCATOR: emballoc::Allocator = 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::>(), 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!"); +}