summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/bitmap.rs23
-rw-r--r--src/tests/block_cache.rs79
-rw-r--r--src/tests/common/mod.rs8
-rw-r--r--src/tests/mod.rs6
4 files changed, 116 insertions, 0 deletions
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<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() {
+ 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::<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));
+ }
+
+ // 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);
+}
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;