From 15a04fb9dad8d2c37501ac3b31b5423b1cfbcdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Tue, 9 May 2023 20:52:00 +0200 Subject: [PATCH] Fixed tests failing because of a too small memory --- src/simulator/mem_cmp.rs | 36 ++++++++++++++++++++++-------------- src/utility/cfg.rs | 12 ++++++++---- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index 07f48b6..099d96f 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -14,7 +14,7 @@ use std::{fs, io::{BufRead, BufReader, Lines, Error}}; use crate::Machine; /// File section -pub struct SectionFormat{ +pub struct SectionFormat { /// Memory address of the section addr: String, /// The size of data in bytes @@ -26,7 +26,7 @@ pub struct SectionFormat{ /// # Memory section /// /// Representation of a section of memory from BurritOS or NachOS -pub struct Section{ +pub struct Section { /// Memory address of the section addr: usize, /// The size of data in bytes @@ -36,15 +36,23 @@ pub struct Section{ } impl Section { - /// Creates a memory section from a SectionFormat - fn from(section: &SectionFormat) -> Section { + fn from(section: &mut SectionFormat) -> Section { let addr = usize::from_str_radix(§ion.addr, 16).unwrap_or_default(); let len = usize::from_str_radix(§ion.len, 16).unwrap_or_default(); - let content: Vec = section.content.as_bytes().chunks(2).map(|x| { - u8::from_str_radix(std::str::from_utf8(x).unwrap_or_default(), 16).unwrap_or_default() - }).collect(); - Section{addr, len, content} + let content: Vec; + unsafe { + content = section.content.as_bytes_mut() + .chunks_mut(4).map( + |x| { + x.reverse(); + u8::from_str_radix( + std::str::from_utf8(x).unwrap_or_default(), 16 + ).unwrap_or_default() + } + ).collect(); + } + Section { addr, len, content } } /// Pretty prints a memory section @@ -68,7 +76,7 @@ pub struct MemChecker { } -impl MemChecker{ +impl MemChecker { ///Translate lines of a file in e Vector of String ///We need this method to parse the memory we received @@ -126,12 +134,12 @@ impl MemChecker{ } else { //lecture ligne CONTENT - let section_f = SectionFormat{ + let mut section_f = SectionFormat { addr: tmp_addr_str.clone(), len: tmp_len_str.clone(), content: current_line.clone().replace(' ', ""), }; - sections.push(Section::from(§ion_f)); + sections.push(Section::from(&mut section_f)); } } @@ -169,7 +177,7 @@ impl MemChecker{ machine.pc = m_c.pc as u64; for section in m_c.sections.iter() { - for (i,b) in section.content.iter().enumerate() { + for (i, b) in section.content.iter().enumerate() { machine.main_memory[section.addr + i] = *b; } } @@ -235,12 +243,12 @@ mod tests { #[test] fn test_create_section_content(){ - let section_format = SectionFormat{ + let mut section_format = SectionFormat{ addr: "0".to_string(), len: "0".to_string(), content: "00FF0AA0A5".to_string(), }; - let section = Section::from(§ion_format); + let section = Section::from(&mut section_format); let expected_vec: Vec = vec![0u8, 255u8, 10u8, 160u8, 165u8]; assert_eq!(section.content, expected_vec); } diff --git a/src/utility/cfg.rs b/src/utility/cfg.rs index b2c671d..87acfc4 100644 --- a/src/utility/cfg.rs +++ b/src/utility/cfg.rs @@ -93,25 +93,29 @@ pub fn read_settings() -> Result { } /// Returns a mock configuration for Machine unit testing +/// /// FIXME: Does not cover the whole configuration yet pub fn get_debug_configuration() -> Settings { let mut settings_map = Settings::new(); - settings_map.insert(MachineSettingKey::PageSize, 128); - settings_map.insert(MachineSettingKey::NumPhysPages, 400); + settings_map.insert(MachineSettingKey::PageSize, 2048); + settings_map.insert(MachineSettingKey::NumPhysPages, 8192); settings_map.insert(MachineSettingKey::UserStackSize, 4096); settings_map } +/// Removes comments and empty lines fn filter_garbage(reader: BufReader) -> Vec { reader.lines() .map(|l| l.unwrap()) - .filter(|l| !l.is_empty() && !l.starts_with("#")) + .filter(|l| !l.is_empty() && !l.starts_with('#')) .collect() } + +/// Inserts user settings into setting map fn update_settings_map(mut settings_map: Settings, key: &str, setting: &str) -> Settings { let key = MachineSettingKey::from(key); - let setting = u64::from_str_radix(setting, 10).unwrap_or(0); + let setting = setting.parse::().unwrap_or(0); settings_map.insert(key, setting); settings_map } \ No newline at end of file