Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions csharp/Platform.IO.Tests/FileHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,33 @@ public void WriteReadTest()
Assert.Equal(readValue, originalValue);
File.Delete(temporaryFile);
}

[Fact]
public void ReadFirstOrDefaultShouldReturnDefaultForMisalignedFile()
{
var temporaryFile = Path.GetTempFileName();
// Write 5 bytes to create a file that's not aligned to ulong (8 bytes)
File.WriteAllBytes(temporaryFile, new byte[] { 1, 2, 3, 4, 5 });

// This should return default(ulong) = 0, not throw an exception
var result = FileHelpers.ReadFirstOrDefault<ulong>(temporaryFile);
Assert.Equal(0UL, result);

File.Delete(temporaryFile);
}

[Fact]
public void ReadLastOrDefaultShouldReturnDefaultForMisalignedFile()
{
var temporaryFile = Path.GetTempFileName();
// Write 5 bytes to create a file that's not aligned to ulong (8 bytes)
File.WriteAllBytes(temporaryFile, new byte[] { 1, 2, 3, 4, 5 });

// This should return default(ulong) = 0, not throw an exception
var result = FileHelpers.ReadLastOrDefault<ulong>(temporaryFile);
Assert.Equal(0UL, result);

File.Delete(temporaryFile);
}
}
}
2 changes: 1 addition & 1 deletion csharp/Platform.IO/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static FileStream GetValidFileStreamOrDefault(string path, int elementSi
var fileSize = GetSize(path);
if (fileSize % elementSize != 0)
{
throw new InvalidOperationException($"File is not aligned to elements with size {elementSize}.");
return null;
}
return fileSize > 0 ? File.OpenRead(path) : null;
}
Expand Down
Loading