summaryrefslogtreecommitdiff
path: root/ayafs-core/src/tests
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-30 12:01:11 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-30 12:01:11 -0800
commitfd125947c9db0b33761414e65e919f73d9bf1815 (patch)
treec4c66d95ba85601427928aa7f23659590055d464 /ayafs-core/src/tests
parent1eac97eea4ec0bcef0be061a2cba93a584355283 (diff)
downloadmyfs-fd125947c9db0b33761414e65e919f73d9bf1815.tar.gz
myfs-fd125947c9db0b33761414e65e919f73d9bf1815.zip
Refactor workspace
Diffstat (limited to 'ayafs-core/src/tests')
-rw-r--r--ayafs-core/src/tests/bitmap.rs38
-rw-r--r--ayafs-core/src/tests/block_cache.rs245
-rw-r--r--ayafs-core/src/tests/common/mod.rs9
-rw-r--r--ayafs-core/src/tests/mod.rs6
4 files changed, 298 insertions, 0 deletions
diff --git a/ayafs-core/src/tests/bitmap.rs b/ayafs-core/src/tests/bitmap.rs
new file mode 100644
index 0000000..1a43f09
--- /dev/null
+++ b/ayafs-core/src/tests/bitmap.rs
@@ -0,0 +1,38 @@
+use crate::tests::common;
+use indexmap::IndexMap;
+
+#[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);
+}
+
+#[test]
+fn test_index_map() {
+ let mut map: IndexMap<i32, i32> = IndexMap::new();
+ map.insert(1, 2);
+ map.insert(2, 3);
+ map.insert(3, 4);
+ map.insert(4, 5);
+ map.remove(&3);
+
+ for (entry_index, (key, value)) in map.iter().enumerate() {
+ println!("index {}, key {}, value {}", entry_index, key, value);
+ }
+}
diff --git a/ayafs-core/src/tests/block_cache.rs b/ayafs-core/src/tests/block_cache.rs
new file mode 100644
index 0000000..52117b9
--- /dev/null
+++ b/ayafs-core/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);
+}
diff --git a/ayafs-core/src/tests/common/mod.rs b/ayafs-core/src/tests/common/mod.rs
new file mode 100644
index 0000000..3abfcb4
--- /dev/null
+++ b/ayafs-core/src/tests/common/mod.rs
@@ -0,0 +1,9 @@
+use crate::block_device::memory_disk::MemoryDisk;
+use crate::AyaFS;
+use std::sync::Arc;
+
+#[allow(unused)]
+pub(crate) fn setup() -> AyaFS {
+ let mem_disk = Arc::new(MemoryDisk::new(1059715));
+ AyaFS::new(mem_disk, 1059715)
+}
diff --git a/ayafs-core/src/tests/mod.rs b/ayafs-core/src/tests/mod.rs
new file mode 100644
index 0000000..df442c1
--- /dev/null
+++ b/ayafs-core/src/tests/mod.rs
@@ -0,0 +1,6 @@
+#[cfg(test)]
+mod bitmap;
+mod common;
+
+#[cfg(test)]
+mod block_cache;