Skip to content

Commit

Permalink
Added real FS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Nov 1, 2024
1 parent 84a13f4 commit 8e803df
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/NexusMods.Paths.Tests/FileSystem/FileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,70 @@ and we propagate this when opening the MemoryMappedFile.
fs.DeleteFile(tempFile);
}
}

[Fact]
public void Test_ReadBytesRandom()
{
var fs = new Paths.FileSystem();
var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName());
var contents = new byte[] { 1, 2, 3, 4, 5 };
using (var stream = fs.CreateFile(tempFile))
{
stream.Write(contents);
}

var bytes = new byte[contents.Length];
fs.ReadBytesRandom(tempFile, bytes, 0);
bytes.Should().BeEquivalentTo(contents);
}

[Fact]
public void Test_ReadBytesRandomWithOffset()
{
var fs = new Paths.FileSystem();
var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName());
var contents = new byte[] { 1, 2, 3, 4, 5 };
using (var stream = fs.CreateFile(tempFile))
{
stream.Write(contents);
}
var offset = new Random().Next(1, contents.Length - 1);

var bytes = new byte[contents.Length - offset];
fs.ReadBytesRandom(tempFile, bytes, offset);
bytes.Should().BeEquivalentTo(contents.AsSpan(offset).ToArray());
}

[Fact]
public async Task Test_ReadBytesRandomAsync()
{
var fs = new Paths.FileSystem();
var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName());
var contents = new byte[] { 1, 2, 3, 4, 5 };
await using (var stream = fs.CreateFile(tempFile))
{
await stream.WriteAsync(contents);
}

var bytes = new byte[contents.Length];
await fs.ReadBytesRandomAsync(tempFile, bytes, 0);
bytes.Should().BeEquivalentTo(contents);
}

[Fact]
public async Task Test_ReadBytesRandomAsyncWithOffset()
{
var fs = new Paths.FileSystem();
var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName());
var contents = new byte[] { 1, 2, 3, 4, 5 };
await using (var stream = fs.CreateFile(tempFile))
{
await stream.WriteAsync(contents);
}
var offset = new Random().Next(1, contents.Length - 1);

var bytes = new byte[contents.Length - offset];
await fs.ReadBytesRandomAsync(tempFile, bytes, offset);
bytes.Should().BeEquivalentTo(contents.AsSpan(offset).ToArray());
}
}

0 comments on commit 8e803df

Please sign in to comment.