summaryrefslogtreecommitdiff
path: root/ayafs/src/tests/block_cache.rs
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-30 02:15:06 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-30 02:15:06 -0800
commit1eac97eea4ec0bcef0be061a2cba93a584355283 (patch)
treed98fb1f6e3811286a0733c9df21e467590635ad2 /ayafs/src/tests/block_cache.rs
parentb3db8a5a710aa0890c80241ffb3fd9792bf1cbe7 (diff)
downloadmyfs-1eac97eea4ec0bcef0be061a2cba93a584355283.tar.gz
myfs-1eac97eea4ec0bcef0be061a2cba93a584355283.zip
Add real disk i/o, add mkfs.aya (not yet implemented)
Diffstat (limited to 'ayafs/src/tests/block_cache.rs')
-rw-r--r--ayafs/src/tests/block_cache.rs245
1 files changed, 245 insertions, 0 deletions
diff --git a/ayafs/src/tests/block_cache.rs b/ayafs/src/tests/block_cache.rs
new file mode 100644
index 0000000..52117b9
--- /dev/null
+++ b/ayafs/src/tests/block_cache.rs
@@ -0,0 +1,245 @@
+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<usize> = (1..=256)
+ .map(|_| {
+ let index = fs.data_bitmap.allocate().unwrap();
+ fs.get_block::<DataBlock>(index).unwrap();
+ index
+ })
+ .collect();
+ assert!(fs.peek_block::<DataBlock>(v[0]).is_some());
+
+ for i in 0..256 {
+ let index = fs.data_bitmap.allocate().unwrap();
+ fs.get_block::<DataBlock>(index).unwrap();
+ assert!(fs.peek_block::<DataBlock>(v[i]).is_none());
+ }
+}
+
+#[test]
+fn test_inode_allocation() {
+ let mut fs = common::setup();
+
+ let (inode_index, inode) = fs.create_directory(0o755, 0, 0, 0, None).unwrap();
+ let mut inode = inode.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();
+ }
+
+ 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 double_indirect = inode.double_indirect;
+ println!(
+ "double indirect is {} after allocation",
+ inode.double_indirect
+ );
+
+ fs.update_inode(inode_index, inode);
+
+ assert!(fs.data_bitmap.query(double_indirect as usize));
+}
+
+#[test]
+fn test_inode_deallocation() {
+ let mut fs = common::setup();
+
+ let (inode_index, inode) = fs.create_directory(0o755, 0, 0, 0, None).unwrap();
+ let mut inode = inode.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()
+ );
+ }
+
+ 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, inode_1) = fs.create_directory(0o755, 0, 0, 0, None).unwrap();
+ let mut inode_1 = inode_1.clone();
+
+ let (inode_index_2, inode_2) = fs.create_file(0o755, 0, 0, 0).unwrap();
+ let mut inode_2 = inode_2.clone();
+ // 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!(
+ "Allocated {:?} in inode {}",
+ fs.allocate_block_for(&mut inode_1).unwrap(),
+ inode_index_1
+ );
+ println!(
+ "Allocated {:?} in inode {}",
+ fs.allocate_block_for(&mut inode_2).unwrap(),
+ inode_index_2
+ );
+ }
+
+ let (inode_index_3, inode_3) = fs.create_directory(0o755, 0, 0, 0, None).unwrap();
+ let mut inode_3 = inode_3.clone();
+ // let mut inode_3 = fs.get_inode(inode_index_3).unwrap().clone();
+
+ for _i in 0..INDIRECT_NUMBER {
+ println!(
+ "Deallocated {} in inode {}",
+ fs.deallocate_block_for(&mut inode_1).unwrap(),
+ inode_index_1
+ );
+ println!(
+ "Allocated {:?} in inode {}",
+ fs.allocate_block_for(&mut inode_3).unwrap(),
+ inode_index_3
+ );
+ }
+
+ for _i in 0..DIRECT_NUMBER {
+ println!(
+ "Deallocated {} 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);
+}