mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Merge branch 'post-06-new' into post-07-new-rebased
This commit is contained in:
@@ -3,6 +3,9 @@ use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use volatile::Volatile;
|
||||
|
||||
#[cfg(test)]
|
||||
use crate::{serial_print, serial_println};
|
||||
|
||||
lazy_static! {
|
||||
/// A global `Writer` instance that can be used for printing to the VGA text buffer.
|
||||
///
|
||||
@@ -175,85 +178,32 @@ pub fn _print(args: fmt::Arguments) {
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
fn construct_writer() -> Writer {
|
||||
use std::boxed::Box;
|
||||
|
||||
let buffer = construct_buffer();
|
||||
Writer {
|
||||
column_position: 0,
|
||||
color_code: ColorCode::new(Color::Blue, Color::Magenta),
|
||||
buffer: Box::leak(Box::new(buffer)),
|
||||
}
|
||||
}
|
||||
|
||||
fn construct_buffer() -> Buffer {
|
||||
use array_init::array_init;
|
||||
|
||||
Buffer {
|
||||
chars: array_init(|_| array_init(|_| Volatile::new(empty_char()))),
|
||||
}
|
||||
}
|
||||
|
||||
fn empty_char() -> ScreenChar {
|
||||
ScreenChar {
|
||||
ascii_character: b' ',
|
||||
color_code: ColorCode::new(Color::Green, Color::Brown),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_byte() {
|
||||
let mut writer = construct_writer();
|
||||
writer.write_byte(b'X');
|
||||
writer.write_byte(b'Y');
|
||||
|
||||
for (i, row) in writer.buffer.chars.iter().enumerate() {
|
||||
for (j, screen_char) in row.iter().enumerate() {
|
||||
let screen_char = screen_char.read();
|
||||
if i == BUFFER_HEIGHT - 1 && j == 0 {
|
||||
assert_eq!(screen_char.ascii_character, b'X');
|
||||
assert_eq!(screen_char.color_code, writer.color_code);
|
||||
} else if i == BUFFER_HEIGHT - 1 && j == 1 {
|
||||
assert_eq!(screen_char.ascii_character, b'Y');
|
||||
assert_eq!(screen_char.color_code, writer.color_code);
|
||||
} else {
|
||||
assert_eq!(screen_char, empty_char());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_formatted() {
|
||||
use core::fmt::Write;
|
||||
|
||||
let mut writer = construct_writer();
|
||||
writeln!(&mut writer, "a").unwrap();
|
||||
writeln!(&mut writer, "b{}", "c").unwrap();
|
||||
|
||||
for (i, row) in writer.buffer.chars.iter().enumerate() {
|
||||
for (j, screen_char) in row.iter().enumerate() {
|
||||
let screen_char = screen_char.read();
|
||||
if i == BUFFER_HEIGHT - 3 && j == 0 {
|
||||
assert_eq!(screen_char.ascii_character, b'a');
|
||||
assert_eq!(screen_char.color_code, writer.color_code);
|
||||
} else if i == BUFFER_HEIGHT - 2 && j == 0 {
|
||||
assert_eq!(screen_char.ascii_character, b'b');
|
||||
assert_eq!(screen_char.color_code, writer.color_code);
|
||||
} else if i == BUFFER_HEIGHT - 2 && j == 1 {
|
||||
assert_eq!(screen_char.ascii_character, b'c');
|
||||
assert_eq!(screen_char.color_code, writer.color_code);
|
||||
} else if i >= BUFFER_HEIGHT - 2 {
|
||||
assert_eq!(screen_char.ascii_character, b' ');
|
||||
assert_eq!(screen_char.color_code, writer.color_code);
|
||||
} else {
|
||||
assert_eq!(screen_char, empty_char());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test_case]
|
||||
fn test_println_simple() {
|
||||
serial_print!("test_println... ");
|
||||
println!("test_println_simple output");
|
||||
serial_println!("[ok]");
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_many() {
|
||||
serial_print!("test_println_many... ");
|
||||
for _ in 0..200 {
|
||||
println!("test_println_many output");
|
||||
}
|
||||
serial_println!("[ok]");
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_output() {
|
||||
serial_print!("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);
|
||||
}
|
||||
|
||||
serial_println!("[ok]");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user