Skip to content

Commit

Permalink
Add read/write span tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 18, 2024
1 parent 909a888 commit 34b2552
Showing 1 changed file with 91 additions and 7 deletions.
98 changes: 91 additions & 7 deletions OpenMcdf.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,66 @@ public void OpenStream(string fileName)
[DataRow(Version.V4, 4097)]
public void ReadViaCopyTo(Version version, int length)
{
// Test files are filled with bytes equal to their position modulo 256
using MemoryStream expectedStream = new(length);
for (int i = 0; i < length; i++)
expectedStream.WriteByte((byte)i);

string fileName = $"TestStream_v{(int)version}_{length}.cfs";
using var rootStorage = RootStorage.OpenRead(fileName);
rootStorage.Validate();

using Stream stream = rootStorage.OpenStream("TestStream");
Assert.AreEqual(length, stream.Length);

// Test files are filled with bytes equal to their position modulo 256
using MemoryStream expectedStream = new(length);
for (int i = 0; i < length; i++)
expectedStream.WriteByte((byte)i);

using MemoryStream actualStream = new();
stream.CopyTo(actualStream);

StreamAssert.AreEqual(expectedStream, actualStream);
}

#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 63)]
[DataRow(Version.V3, 64)]
[DataRow(Version.V3, 65)]
[DataRow(Version.V3, 511)]
[DataRow(Version.V3, 512)]
[DataRow(Version.V3, 513)]
[DataRow(Version.V3, 4095)]
[DataRow(Version.V3, 4096)]
[DataRow(Version.V3, 4097)]
[DataRow(Version.V3, 65536)]
[DataRow(Version.V4, 0)]
[DataRow(Version.V4, 63)]
[DataRow(Version.V4, 64)]
[DataRow(Version.V4, 65)]
[DataRow(Version.V4, 511)]
[DataRow(Version.V4, 512)]
[DataRow(Version.V4, 513)]
[DataRow(Version.V4, 4095)]
[DataRow(Version.V4, 4096)]
[DataRow(Version.V4, 4097)]
public void ReadSpan(Version version, int length)
{
// Test files are filled with bytes equal to their position modulo 256
byte[] expectedBuffer = new byte[length];
for (int i = 0; i < length; i++)
expectedBuffer[i] = ((byte)i);

string fileName = $"TestStream_v{(int)version}_{length}.cfs";
using var rootStorage = RootStorage.OpenRead(fileName);

using Stream stream = rootStorage.OpenStream("TestStream");
Assert.AreEqual(length, stream.Length);

byte[] actualBuffer = new byte[length];
stream.Read(actualBuffer);

CollectionAssert.AreEqual(expectedBuffer, actualBuffer);
}
#endif

[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 63)]
Expand Down Expand Up @@ -150,7 +192,41 @@ public void Seek(Version version, int length)
[DataRow(Version.V4, 1024 * 4096)] // Multiple FAT sectors (1024 * 4096)
[DataRow(Version.V4, 7087616 * 4)] // First DIFAT chain
[DataRow(Version.V4, 2 * 7087616 * 4)] // Long DIFAT chain
public void Write(Version version, int length)
public void Write(Version version, int length) => WriteCore(version, length, false);

#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 63)]
[DataRow(Version.V3, 64)] // Mini-stream sector size
[DataRow(Version.V3, 65)]
[DataRow(Version.V3, 511)]
[DataRow(Version.V3, 512)] // Multiple stream sectors
[DataRow(Version.V3, 513)]
[DataRow(Version.V3, 4095)]
[DataRow(Version.V3, 4096)]
[DataRow(Version.V3, 4097)]
[DataRow(Version.V3, 128 * 512)] // Multiple FAT sectors
[DataRow(Version.V3, 1024 * 4096)] // Multiple FAT sectors
[DataRow(Version.V3, 7087616)] // First DIFAT chain
[DataRow(Version.V3, 2 * 7087616)] // Long DIFAT chain
[DataRow(Version.V4, 0)]
[DataRow(Version.V4, 63)]
[DataRow(Version.V4, 64)] // Mini-stream sector size
[DataRow(Version.V4, 65)]
[DataRow(Version.V4, 511)]
[DataRow(Version.V4, 512)]
[DataRow(Version.V4, 513)]
[DataRow(Version.V4, 4095)]
[DataRow(Version.V4, 4096)] // Multiple stream sectors
[DataRow(Version.V4, 4097)]
[DataRow(Version.V4, 1024 * 4096)] // Multiple FAT sectors (1024 * 4096)
[DataRow(Version.V4, 7087616 * 4)] // First DIFAT chain
[DataRow(Version.V4, 2 * 7087616 * 4)] // Long DIFAT chain
public void WriteSpan(Version version, int length) => WriteCore(version, length, true);
#endif

static void WriteCore(Version version, int length, bool preferSpan)
{
using MemoryStream memoryStream = new();
using var rootStorage = RootStorage.Create(memoryStream, version);
Expand All @@ -162,7 +238,15 @@ public void Write(Version version, int length)
for (int i = 0; i < length; i++)
expectedBuffer[i] = (byte)i;

#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
if (preferSpan)
stream.Write(expectedBuffer);
else
stream.Write(expectedBuffer, 0, expectedBuffer.Length);
#else
stream.Write(expectedBuffer, 0, expectedBuffer.Length);
#endif

Assert.AreEqual(length, stream.Length);
Assert.AreEqual(length, stream.Position);

Expand Down

0 comments on commit 34b2552

Please sign in to comment.