Tests
This commit is contained in:
@@ -125,38 +125,25 @@ impl fmt::Write for Writer {
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||
// 将 Writer 设置为静态以便调用
|
||||
lazy_static! { // 内置的 static 会用到标准库里的一些东西,所以这里用 lazy_static
|
||||
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer { // 用互斥锁来防止冲突
|
||||
column_position: 0,
|
||||
color_code: ColorCode::new(Color::Yellow, Color::Black),
|
||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) }
|
||||
});
|
||||
}
|
||||
|
||||
pub fn print_something() {
|
||||
use core::fmt::Write;
|
||||
let mut writer = Writer {
|
||||
column_position: 0,
|
||||
color_code: ColorCode::new(Color::Yellow, Color::Black),
|
||||
// VGA Buffer 从 0xb8000 开始
|
||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
||||
};
|
||||
writer.write_byte(b'H');
|
||||
writer.write_string("ello ");
|
||||
writer.write_string("Wörld!");
|
||||
write!(writer, "The numbers are {} and {}", 42, 1.0/3.0).unwrap();
|
||||
}
|
||||
|
||||
// 将宏导出到 crate 的根,这样我们就可以用
|
||||
// use crate::println
|
||||
// 来使用它
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
macro_rules! print { // print!()
|
||||
($($arg:tt)*) => ($crate::vga_buffer::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
macro_rules! println { // println!()
|
||||
() => ($crate::print!("\n"));
|
||||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||
}
|
||||
@@ -168,3 +155,25 @@ pub fn _print(args: fmt::Arguments) {
|
||||
use core::fmt::Write;
|
||||
WRITER.lock().write_fmt(args).unwrap();
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_simple() { // 测试单行 println!
|
||||
println!("test_println_simple output");
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_many() { // 测试多行 println!
|
||||
for _ in 0..100 {
|
||||
println!("test_println_many output");
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_output() { // 测试字符是否真的打印到了屏幕上
|
||||
let s = "Some test string that fits on a single line";
|
||||
println!("{}", s);
|
||||
for (i, c) in s.chars().enumerate() {
|
||||
let screen_char = WRITER.lock().buffer.chars[BUFFER_HEIGHT - 2][i].read();
|
||||
assert_eq!(char::from(screen_char.ascii_character), c);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user