|
| 1 | +// Copyright (c) 2021 Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-2-Clause-Patent |
| 4 | + |
| 5 | +#![no_std] |
| 6 | +#![cfg_attr(test, no_main)] |
| 7 | +// The `custom_test_frameworks` feature allows the use of `#[test_case]` and `#![test_runner]`. |
| 8 | +// Any function, const, or static can be annotated with `#[test_case]` causing it to be aggregated |
| 9 | +// (like #[test]) and be passed to the test runner determined by the `#![test_runner]` crate |
| 10 | +// attribute. |
| 11 | +#![feature(custom_test_frameworks)] |
| 12 | +#![test_runner(test_runner)] |
| 13 | +// Reexport the test harness main function under a different symbol. |
| 14 | +#![reexport_test_harness_main = "test_main"] |
| 15 | + |
| 16 | +#[cfg(test)] |
| 17 | +use bootloader::{boot_info, entry_point, BootInfo}; |
| 18 | +#[cfg(test)] |
| 19 | +use core::ops::Deref; |
| 20 | +#[cfg(test)] |
| 21 | +use test_runner_client::{init_heap, serial_println, test_runner}; |
| 22 | + |
| 23 | +#[cfg(test)] |
| 24 | +entry_point!(kernel_main); |
| 25 | + |
| 26 | +#[cfg(test)] |
| 27 | +fn kernel_main(boot_info: &'static mut BootInfo) -> ! { |
| 28 | + // turn the screen gray |
| 29 | + if let Some(framebuffer) = boot_info.framebuffer.as_mut() { |
| 30 | + for byte in framebuffer.buffer_mut() { |
| 31 | + *byte = 0x90; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + let memoryregions = boot_info.memory_regions.deref(); |
| 36 | + let offset = boot_info.physical_memory_offset.into_option().unwrap(); |
| 37 | + |
| 38 | + for usable in memoryregions.iter() { |
| 39 | + if usable.kind == boot_info::MemoryRegionKind::Usable { |
| 40 | + init_heap((usable.start + offset) as usize, 0x100000); |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + serial_println!("Start to execute test cases..."); |
| 46 | + test_main(); |
| 47 | + panic!("Unexpected return from test_main()!!!"); |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use td_exception::{setup_exception_handlers, DIVIDED_BY_ZERO_EVENT_COUNT}; |
| 53 | + |
| 54 | + #[test_case] |
| 55 | + fn test_divided_by_zero() { |
| 56 | + use core::sync::atomic::Ordering::Acquire; |
| 57 | + setup_exception_handlers(); |
| 58 | + |
| 59 | + assert_eq!(DIVIDED_BY_ZERO_EVENT_COUNT.load(Acquire), 0); |
| 60 | + // TODO: make this work:) |
| 61 | + //let _ = 1 / DIVIDED_BY_ZERO_EVENT_COUNT.load(Acquire); |
| 62 | + //assert_eq!(DIVIDED_BY_ZERO_EVENT_COUNT.load(Acquire), 1); |
| 63 | + } |
| 64 | +} |
0 commit comments