Skip to content

Commit f46ef4d

Browse files
Check written files have correct length.
This test currently fails.
1 parent 58b3bd6 commit f46ef4d

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

examples/write_test.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ fn main() {
138138
.open_file_in_dir(&mut volume, &root_dir, FILE_TO_WRITE, Mode::ReadOnly)
139139
.unwrap();
140140
println!("\nReading from file {}\n", FILE_TO_WRITE);
141+
let mut csum = 0;
141142
println!("FILE STARTS:");
142143
while !f.eof() {
143144
let mut buffer = [0u8; 32];
@@ -147,9 +148,11 @@ fn main() {
147148
print!("\\n");
148149
}
149150
print!("{}", *b as char);
151+
csum += u32::from(*b);
150152
}
151153
}
152154
println!("EOF\n");
155+
let mut file_size = f.length() as usize;
153156
volume_mgr.close_file(&volume, f).unwrap();
154157

155158
let mut f = volume_mgr
@@ -161,15 +164,20 @@ fn main() {
161164
println!("\nAppending to file");
162165
let num_written1 = volume_mgr.write(&mut volume, &mut f, &buffer1[..]).unwrap();
163166
let num_written = volume_mgr.write(&mut volume, &mut f, &buffer[..]).unwrap();
164-
println!("Number of bytes written: {}\n", num_written + num_written1);
165-
166-
f.seek_from_start(0).unwrap();
167-
println!("\tFinding {}...", FILE_TO_WRITE);
167+
for b in &buffer1[..] {
168+
csum += u32::from(*b);
169+
}
170+
for b in &buffer[..] {
171+
csum += u32::from(*b);
172+
}
168173
println!(
169-
"\tFound {}?: {:?}",
170-
FILE_TO_WRITE,
171-
volume_mgr.find_directory_entry(&volume, &root_dir, FILE_TO_WRITE)
174+
"Number of bytes appendedß: {}\n",
175+
num_written + num_written1
172176
);
177+
file_size += num_written;
178+
file_size += num_written1;
179+
180+
f.seek_from_start(0).unwrap();
173181
println!("\nFILE STARTS:");
174182
while !f.eof() {
175183
let mut buffer = [0u8; 32];
@@ -184,6 +192,35 @@ fn main() {
184192
println!("EOF");
185193
volume_mgr.close_file(&volume, f).unwrap();
186194

195+
println!("\tFinding {}...", FILE_TO_WRITE);
196+
let dir_ent = volume_mgr
197+
.find_directory_entry(&volume, &root_dir, FILE_TO_WRITE)
198+
.unwrap();
199+
println!("\tFound {}?: {:?}", FILE_TO_WRITE, dir_ent);
200+
assert_eq!(dir_ent.size as usize, file_size);
201+
let mut f = volume_mgr
202+
.open_file_in_dir(&mut volume, &root_dir, FILE_TO_WRITE, Mode::ReadWriteAppend)
203+
.unwrap();
204+
println!("\nReading from file {}\n", FILE_TO_WRITE);
205+
println!("FILE STARTS:");
206+
let mut csum2 = 0;
207+
while !f.eof() {
208+
let mut buffer = [0u8; 32];
209+
let num_read = volume_mgr.read(&volume, &mut f, &mut buffer).unwrap();
210+
for b in &buffer[0..num_read] {
211+
if *b == 10 {
212+
print!("\\n");
213+
}
214+
print!("{}", *b as char);
215+
csum2 += u32::from(*b);
216+
}
217+
}
218+
println!("EOF\n");
219+
assert_eq!(f.length() as usize, file_size);
220+
volume_mgr.close_file(&volume, f).unwrap();
221+
222+
assert_eq!(csum, csum2);
223+
187224
println!("\nTruncating file");
188225
let mut f = volume_mgr
189226
.open_file_in_dir(

0 commit comments

Comments
 (0)