diff options
author | Chuyan Zhang <me@zcy.moe> | 2023-11-22 00:04:28 -0800 |
---|---|---|
committer | Chuyan Zhang <me@zcy.moe> | 2023-11-22 00:04:28 -0800 |
commit | 3f2151c043177501b0c7beb580d9737e3d824ad0 (patch) | |
tree | 78117ca8558cd622d937db36a203708837bc691c /src/tests | |
parent | 8a45cd95353ae9fe1286dbc4fcd36faaa66c9f82 (diff) | |
download | myfs-3f2151c043177501b0c7beb580d9737e3d824ad0.tar.gz myfs-3f2151c043177501b0c7beb580d9737e3d824ad0.zip |
bugfix for layer 1
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/block_cache.rs | 154 |
1 files changed, 126 insertions, 28 deletions
diff --git a/src/tests/block_cache.rs b/src/tests/block_cache.rs index f1c18cc..bbea3a0 100644 --- a/src/tests/block_cache.rs +++ b/src/tests/block_cache.rs @@ -23,7 +23,7 @@ fn test_basic_lru() { } #[test] -fn test_inode() { +fn test_inode_allocation() { let mut fs = common::setup(); let inode_index = fs.create_inode(0o755, InodeMode::IFDIR, 0, 0, 0).unwrap(); @@ -31,49 +31,147 @@ fn test_inode() { const DIRECT_NUMBER: u32 = 15; const INDIRECT_NUMBER: u32 = 1024; - const DOUBLE_INDIRECT_NUMBER: u32 = 1024 * 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 { + for _i in 0..INDIRECT_NUMBER { fs.allocate_block_for(&mut inode).unwrap(); } - for i in 0..DOUBLE_INDIRECT_NUMBER { + println!("single indirect is {}", inode.single_indirect); + println!("double indirect is {}", inode.double_indirect); + println!("triple indirect is {}", inode.triple_indirect); + + let indirect_block = fs + .peek_block::<IndirectBlock>(inode.single_indirect 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(inode.double_indirect as usize), false); + assert_eq!(fs.data_bitmap.query(inode.triple_indirect as usize), false); + + for _i in 0..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; + println!("double indirect is {} after allocation", inode.double_indirect); fs.update_inode(inode_index, inode); - let indirect_block = fs - .peek_block::<IndirectBlock>(single_indirect as usize) - .unwrap(); - for entry in indirect_block.block.entries { - assert_ne!(entry, 0); - assert!(fs.data_bitmap.query(entry as usize)); + assert!(fs.data_bitmap.query(double_indirect as usize)); +} + +#[test] +fn test_inode_deallocation() { + 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 { + println!("Allocated {}", fs.allocate_block_for(&mut inode).unwrap()); + assert!(fs.data_bitmap.query(inode.direct[i as usize] as usize)) + } + + for _i in 0..2 * INDIRECT_NUMBER { + println!("Allocated {}", fs.allocate_block_for(&mut inode).unwrap()); + } + + println!("single indirect is {}", inode.single_indirect); + println!("double indirect is {}", inode.double_indirect); + println!("triple indirect is {}", inode.triple_indirect); + + assert!(fs.data_bitmap.query(inode.double_indirect as usize)); + + for i in 0..INDIRECT_NUMBER + 5 { + println!("Deallocated {}", fs.deallocate_block_for(&mut inode).unwrap()); } - // let double_indirect_block = fs - // .peek_block::<DoubleIndirectBlock>(double_indirect as usize) - // .unwrap(); - // for indirect_entry in double_indirect_block.block.indirect { - // let indirect_block = fs - // .peek_block::<IndirectBlock>(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); + println!("single indirect is {}", inode.single_indirect); + println!("double indirect is {}", inode.double_indirect); + println!("triple indirect is {}", inode.triple_indirect); + + assert_eq!(fs.data_bitmap.query(inode.double_indirect as usize), false); + + fs.update_inode(inode_index, inode); } + +#[test] +fn test_multiple_inode_allocation() { + let mut fs = common::setup(); + + let inode_index_1 = fs.create_inode(0o755, InodeMode::IFDIR, 0, 0, 0).unwrap(); + let inode_index_2 = fs.create_inode(0o755, InodeMode::IFREG, 0, 0, 0).unwrap(); + + let mut inode_1 = fs.get_inode(inode_index_1).unwrap().clone(); + let mut inode_2 = fs.get_inode(inode_index_2).unwrap().clone(); + + const DIRECT_NUMBER: u32 = 15; + const INDIRECT_NUMBER: u32 = 1024; + + for i in 0 .. DIRECT_NUMBER + INDIRECT_NUMBER { + println!("Allcoated {} in inode {}", fs.allocate_block_for(&mut inode_1).unwrap(), inode_index_1); + println!("Allcoated {} in inode {}", fs.allocate_block_for(&mut inode_2).unwrap(), inode_index_2); + } + + let inode_index_3 = fs.create_inode(0o755, InodeMode::IFDIR, 0, 0, 0).unwrap(); + let mut inode_3 = fs.get_inode(inode_index_3).unwrap().clone(); + + for _i in 0 .. INDIRECT_NUMBER { + println!("Deallcoated {} in inode {}", fs.deallocate_block_for(&mut inode_1).unwrap(), inode_index_1); + println!("Allcoated {} in inode {}", fs.allocate_block_for(&mut inode_3).unwrap(), inode_index_3); + } + + for _i in 0 .. DIRECT_NUMBER { + println!("Deallcoated {} in inode {}", fs.deallocate_block_for(&mut inode_1).unwrap(), inode_index_1); + } + + assert!(fs.deallocate_block_for(&mut inode_1).is_none()); + + println!("single indirect is {} for {}", inode_1.single_indirect, inode_index_1); + println!("double indirect is {} for {}", inode_1.double_indirect, inode_index_1); + println!("single indirect is {} for {}", inode_2.single_indirect, inode_index_2); + println!("double indirect is {} for {}", inode_2.double_indirect, inode_index_2); + println!("single indirect is {} for {}", inode_3.single_indirect, inode_index_3); + println!("double indirect is {} for {}", inode_3.double_indirect, inode_index_3); + + assert_eq!(fs.data_bitmap.query(inode_1.single_indirect as usize), false); + assert!(fs.data_bitmap.query(inode_2.single_indirect as usize)); + assert_eq!(fs.data_bitmap.query(inode_2.double_indirect as usize), false); + assert!(fs.data_bitmap.query(inode_3.single_indirect as usize)); + assert_eq!(fs.data_bitmap.query(inode_3.double_indirect as usize), false); + + fs.allocate_block_for(&mut inode_2).unwrap(); + println!("-----------------"); + println!("double indirect is {} for {}", inode_2.double_indirect, inode_index_2); + + assert!(fs.data_bitmap.query(inode_2.double_indirect as usize)); + + for _i in 0 .. DIRECT_NUMBER { + fs.allocate_block_for(&mut inode_3).unwrap(); + } + println!("-----------------"); + println!("double indirect is {} for {}", inode_3.double_indirect, inode_index_3); + assert_eq!(fs.data_bitmap.query(inode_3.double_indirect as usize), false); + + fs.allocate_block_for(&mut inode_3).unwrap(); + println!("-----------------"); + println!("double indirect is {} for {}", inode_3.double_indirect, inode_index_3); + assert!(fs.data_bitmap.query(inode_3.double_indirect as usize)); + + fs.update_inode(inode_index_1, inode_1); + fs.update_inode(inode_index_2, inode_2); + fs.update_inode(inode_index_3, inode_3); +}
\ No newline at end of file |