From 8a45cd95353ae9fe1286dbc4fcd36faaa66c9f82 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Sun, 19 Nov 2023 01:03:19 -0800 Subject: Layer 1 test not passing --- src/tests/bitmap.rs | 23 ++++++++++++++ src/tests/block_cache.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ src/tests/common/mod.rs | 8 +++++ src/tests/mod.rs | 6 ++++ 4 files changed, 116 insertions(+) create mode 100644 src/tests/bitmap.rs create mode 100644 src/tests/block_cache.rs create mode 100644 src/tests/common/mod.rs create mode 100644 src/tests/mod.rs (limited to 'src/tests') diff --git a/src/tests/bitmap.rs b/src/tests/bitmap.rs new file mode 100644 index 0000000..9b21b6f --- /dev/null +++ b/src/tests/bitmap.rs @@ -0,0 +1,23 @@ +use crate::tests::common; + +#[test] +fn test_allocate() { + let mut fs = common::setup(); + for _ in 0..10 { + fs.data_bitmap.allocate().unwrap(); + } + assert!(fs.data_bitmap.deallocate(5)); + assert_eq!(fs.data_bitmap.allocate().unwrap(), 5); + assert_eq!(fs.data_bitmap.allocate().unwrap(), 11); +} + +#[test] +fn test_query() { + let mut fs = common::setup(); + for _ in 0..10 { + fs.data_bitmap.allocate().unwrap(); + } + assert_eq!(fs.data_bitmap.query(0), false); + assert_eq!(fs.data_bitmap.query(5), true); + assert_eq!(fs.data_bitmap.query(11), false); +} diff --git a/src/tests/block_cache.rs b/src/tests/block_cache.rs new file mode 100644 index 0000000..f1c18cc --- /dev/null +++ b/src/tests/block_cache.rs @@ -0,0 +1,79 @@ +use crate::disk::block::{DataBlock, DoubleIndirectBlock, IndirectBlock}; +use crate::disk::inode::InodeMode; +use crate::tests::common; + +#[test] +fn test_basic_lru() { + let mut fs = common::setup(); + + let v: Vec = (1..=256) + .map(|_| { + let index = fs.data_bitmap.allocate().unwrap(); + fs.get_block::(index).unwrap(); + index + }) + .collect(); + assert!(fs.peek_block::(v[0]).is_some()); + + for i in 0..256 { + let index = fs.data_bitmap.allocate().unwrap(); + fs.get_block::(index).unwrap(); + assert!(fs.peek_block::(v[i]).is_none()); + } +} + +#[test] +fn test_inode() { + let mut fs = common::setup(); + + let inode_index = fs.create_inode(0o755, InodeMode::IFDIR, 0, 0, 0).unwrap(); + let mut inode = fs.get_inode(inode_index).unwrap().clone(); + + const DIRECT_NUMBER: u32 = 15; + const INDIRECT_NUMBER: u32 = 1024; + const DOUBLE_INDIRECT_NUMBER: u32 = 1024 * 1024; + + for i in 0..DIRECT_NUMBER { + fs.allocate_block_for(&mut inode).unwrap(); + assert!(fs.data_bitmap.query(inode.direct[i as usize] as usize)) + } + + for i in 0..INDIRECT_NUMBER { + fs.allocate_block_for(&mut inode).unwrap(); + } + + for i in 0..DOUBLE_INDIRECT_NUMBER { + fs.allocate_block_for(&mut inode).unwrap(); + } + + let single_indirect = inode.single_indirect; + let double_indirect = inode.double_indirect; + let triple_indirect = inode.triple_indirect; + + fs.update_inode(inode_index, inode); + + let indirect_block = fs + .peek_block::(single_indirect as usize) + .unwrap(); + for entry in indirect_block.block.entries { + assert_ne!(entry, 0); + assert!(fs.data_bitmap.query(entry as usize)); + } + + // let double_indirect_block = fs + // .peek_block::(double_indirect as usize) + // .unwrap(); + // for indirect_entry in double_indirect_block.block.indirect { + // let indirect_block = fs + // .peek_block::(indirect_entry as usize) + // .unwrap(); + // for entry in indirect_block.block.entries { + // assert_ne!(entry, 0); + // assert!(fs.data_bitmap.query(entry as usize)); + // } + // } + + assert_eq!(fs.data_bitmap.query(double_indirect as usize), false); + + assert_eq!(fs.data_bitmap.query(triple_indirect as usize), false); +} diff --git a/src/tests/common/mod.rs b/src/tests/common/mod.rs new file mode 100644 index 0000000..9314fd9 --- /dev/null +++ b/src/tests/common/mod.rs @@ -0,0 +1,8 @@ +use crate::block_device::memory_disk::MemoryDisk; +use crate::AyaFS; +use std::sync::Arc; + +pub(crate) fn setup() -> AyaFS { + let mem_disk = Arc::new(MemoryDisk::new(1059715)); + AyaFS::new(mem_disk, 1059715) +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..df442c1 --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,6 @@ +#[cfg(test)] +mod bitmap; +mod common; + +#[cfg(test)] +mod block_cache; -- cgit v1.2.3-70-g09d2