Skip to content

Commit

Permalink
Add test for file write and seek
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed May 15, 2024
1 parent 8d8b1b8 commit 8ecdf59
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/wasi-fyi/fs_write-and-seek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fs::{metadata, OpenOptions};
use std::io::{Seek, SeekFrom, Write};

fn main() {
let mut file = OpenOptions::new()
.write(true)
.read(true)
.create(true)
.open("file")
.unwrap();

write!(file, "hell").unwrap();

// We wrote 4 bytes
let md = metadata("file").unwrap();
assert_eq!(md.len(), 4);

assert_eq!(file.seek(SeekFrom::Start(0)).unwrap(), 0);

write!(file, "eh").unwrap();

// We overwrote the first 2 bytes, should still have 4 bytes
let md = metadata("file").unwrap();
assert_eq!(md.len(), 4);

assert_eq!(file.seek(SeekFrom::Start(0)).unwrap(), 0);

write!(file, "hello").unwrap();

// Now we wrote past the end, should have 5 bytes
let md = metadata("file").unwrap();
assert_eq!(md.len(), 5);

write!(file, " world!").unwrap();

// We wrote past the end entirely, should have 5 + 7 = 12 bytes
let md = metadata("file").unwrap();
assert_eq!(md.len(), 12);
}

0 comments on commit 8ecdf59

Please sign in to comment.