Skip to content

Commit e691468

Browse files
Add tests
1 parent e76c1fd commit e691468

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

src/tests.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use core::convert::TryInto;
22
use generic_array::typenum::consts;
3+
use littlefs2_core::PathBuf;
34

45
use crate::{
5-
fs::{Allocation, Attribute, File, Filesystem},
6+
driver::Storage,
7+
fs::{Allocation, Attribute, File, Filesystem, MountFlags},
68
io::{Error, OpenSeekFrom, Read, Result, SeekFrom},
79
path, BACKEND_VERSION, DISK_VERSION,
810
};
@@ -35,6 +37,20 @@ ram_storage!(
3537
path_max_plus_one_ty = consts::U256,
3638
);
3739

40+
ram_storage!(
41+
name = LargerRamStorage,
42+
backend = LargerRam,
43+
erase_value = 0xff,
44+
read_size = 20 * 5,
45+
write_size = 20 * 7,
46+
cache_size_ty = consts::U700,
47+
block_size = 20 * 35,
48+
block_count = 64,
49+
lookahead_size_ty = consts::U16,
50+
filename_max_plus_one_ty = consts::U256,
51+
path_max_plus_one_ty = consts::U256,
52+
);
53+
3854
#[test]
3955
fn version() {
4056
assert_eq!((BACKEND_VERSION.major(), BACKEND_VERSION.minor()), (2, 9));
@@ -545,3 +561,54 @@ fn test_mount_or_else_clobber_alloc() {
545561
// t.compile_fail("tests/ui/*-fail.rs");
546562
// t.pass("tests/ui/*-pass.rs");
547563
// }
564+
565+
#[test]
566+
fn shrinking() {
567+
let backend = &mut Ram::default();
568+
let storage = &mut RamStorage::new(backend);
569+
let alloc = &mut Allocation::new();
570+
571+
Filesystem::format(storage).unwrap();
572+
let _fs = Filesystem::mount(alloc, storage).unwrap();
573+
574+
let larger_backend = &mut LargerRam::default();
575+
larger_backend.buf[..backend.buf.len()].copy_from_slice(&backend.buf);
576+
let larger_storage = &mut LargerRamStorage::new(larger_backend);
577+
let larger_alloc = &mut Allocation::new();
578+
assert!(matches!(
579+
Filesystem::mount(larger_alloc, larger_storage),
580+
Err(Error::INVALID)
581+
));
582+
583+
let larger_alloc = &mut Allocation::with_config(crate::fs::Config {
584+
mount_flags: MountFlags::DISABLE_BLOCK_COUNT_CHECK,
585+
});
586+
587+
let fs = Filesystem::mount(larger_alloc, larger_storage).unwrap();
588+
fs.grow(LargerRamStorage::BLOCK_COUNT).unwrap();
589+
fs.shrink(RamStorage::BLOCK_COUNT).unwrap();
590+
}
591+
592+
#[test]
593+
fn shrinking_full() {
594+
let larger_backend = &mut LargerRam::default();
595+
let larger_storage = &mut LargerRamStorage::new(larger_backend);
596+
let larger_alloc = &mut Allocation::new();
597+
Filesystem::format(larger_storage).unwrap();
598+
let fs = Filesystem::mount(larger_alloc, larger_storage).unwrap();
599+
600+
for i in 0.. {
601+
let path = format!("file-{i}");
602+
let contents = &[0; 1024];
603+
match fs.write(&PathBuf::try_from(&*path).unwrap(), contents) {
604+
Ok(_) => continue,
605+
Err(Error::NO_SPACE) => break,
606+
Err(err) => panic!("{err:?}"),
607+
}
608+
}
609+
610+
assert!(matches!(
611+
fs.shrink(RamStorage::BLOCK_COUNT),
612+
Err(Error::DIR_NOT_EMPTY)
613+
))
614+
}

0 commit comments

Comments
 (0)