1
0
Fork 0
libfringe/tests/stack.rs

104 lines
3.5 KiB
Rust
Raw Normal View History

// This file is part of libfringe, a low-level green threading library.
// Copyright (c) whitequark <whitequark@whitequark.org>
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
2017-09-10 17:41:04 +08:00
#![feature(alloc, heap_api, allocator_api)]
extern crate alloc;
extern crate fringe;
2018-05-26 03:21:50 +08:00
use alloc::heap::Global;
2017-09-10 17:41:04 +08:00
use alloc::allocator::{Alloc, Layout};
use alloc::boxed::Box;
use std::slice;
use fringe::{STACK_ALIGNMENT, Stack, SliceStack, OwnedStack, OsStack};
2017-09-10 17:41:04 +08:00
unsafe fn heap_allocate(size: usize, align: usize) -> *mut u8 {
2018-05-26 03:21:50 +08:00
let ptr = Global.alloc(Layout::from_size_align_unchecked(size, align)).expect("couldn't allocate");
ptr.as_ptr() as *mut u8
2017-09-10 17:41:04 +08:00
}
#[test]
fn slice_aligned() {
unsafe {
2017-09-10 17:41:04 +08:00
let ptr = heap_allocate(16384, STACK_ALIGNMENT);
let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, 16384));
let stack = SliceStack::new(&mut slice[4096..8192]);
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
}
}
#[test]
fn slice_unaligned() {
unsafe {
2017-09-10 17:41:04 +08:00
let ptr = heap_allocate(16384, STACK_ALIGNMENT);
let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, 16384));
let stack = SliceStack::new(&mut slice[4097..8193]);
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
}
}
#[test]
fn slice_too_small() {
unsafe {
2017-09-10 17:41:04 +08:00
let ptr = heap_allocate(STACK_ALIGNMENT, STACK_ALIGNMENT);
let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, STACK_ALIGNMENT));
let stack = SliceStack::new(&mut slice[0..1]);
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
}
}
#[test]
2017-02-25 21:24:36 +08:00
#[should_panic(expected = "SliceStack too small")]
fn slice_too_small_unaligned() {
unsafe {
2017-09-10 17:41:04 +08:00
let ptr = heap_allocate(STACK_ALIGNMENT, STACK_ALIGNMENT);
let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, STACK_ALIGNMENT));
SliceStack::new(&mut slice[1..2]);
}
}
#[test]
2016-08-19 17:18:46 +08:00
fn slice_stack() {
let mut memory = [0; 1024];
let stack = SliceStack::new(&mut memory);
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
// Size may be a bit smaller due to alignment
assert!(stack.base() as usize - stack.limit() as usize > 1024 - STACK_ALIGNMENT * 2);
2016-08-19 17:18:46 +08:00
}
2016-08-19 21:58:45 +08:00
#[test]
fn owned_stack() {
let stack = OwnedStack::new(1024);
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.base() as usize - stack.limit() as usize, 1024);
2016-08-19 21:58:45 +08:00
}
2016-08-19 17:18:46 +08:00
#[test]
fn default_os_stack() {
2016-07-17 09:24:20 +08:00
let stack = OsStack::new(0).unwrap();
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
2016-07-17 09:24:20 +08:00
// Make sure the topmost page of the stack, at least, is accessible.
unsafe { *(stack.base().offset(-1)) = 0; }
2016-07-17 09:24:20 +08:00
}
#[test]
2016-08-19 17:18:46 +08:00
fn one_page_os_stack() {
let stack = OsStack::new(4096).unwrap();
assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0);
assert_eq!(stack.limit() as usize & (STACK_ALIGNMENT - 1), 0);
// Make sure the topmost page of the stack, at least, is accessible.
unsafe { *(stack.base().offset(-1)) = 0; }
}