Skip to content

Commit 449df1e

Browse files
Add read/write span tests
1 parent 51f8db0 commit 449df1e

File tree

1 file changed

+91
-7
lines changed

1 file changed

+91
-7
lines changed

OpenMcdf.Tests/StreamTests.cs

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,66 @@ public void OpenStream(string fileName)
4343
[DataRow(Version.V4, 4097)]
4444
public void ReadViaCopyTo(Version version, int length)
4545
{
46+
// Test files are filled with bytes equal to their position modulo 256
47+
using MemoryStream expectedStream = new(length);
48+
for (int i = 0; i < length; i++)
49+
expectedStream.WriteByte((byte)i);
50+
4651
string fileName = $"TestStream_v{(int)version}_{length}.cfs";
4752
using var rootStorage = RootStorage.OpenRead(fileName);
48-
rootStorage.Validate();
4953

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

53-
// Test files are filled with bytes equal to their position modulo 256
54-
using MemoryStream expectedStream = new(length);
55-
for (int i = 0; i < length; i++)
56-
expectedStream.WriteByte((byte)i);
57-
5857
using MemoryStream actualStream = new();
5958
stream.CopyTo(actualStream);
6059

6160
StreamAssert.AreEqual(expectedStream, actualStream);
6261
}
6362

63+
#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
64+
[TestMethod]
65+
[DataRow(Version.V3, 0)]
66+
[DataRow(Version.V3, 63)]
67+
[DataRow(Version.V3, 64)]
68+
[DataRow(Version.V3, 65)]
69+
[DataRow(Version.V3, 511)]
70+
[DataRow(Version.V3, 512)]
71+
[DataRow(Version.V3, 513)]
72+
[DataRow(Version.V3, 4095)]
73+
[DataRow(Version.V3, 4096)]
74+
[DataRow(Version.V3, 4097)]
75+
[DataRow(Version.V3, 65536)]
76+
[DataRow(Version.V4, 0)]
77+
[DataRow(Version.V4, 63)]
78+
[DataRow(Version.V4, 64)]
79+
[DataRow(Version.V4, 65)]
80+
[DataRow(Version.V4, 511)]
81+
[DataRow(Version.V4, 512)]
82+
[DataRow(Version.V4, 513)]
83+
[DataRow(Version.V4, 4095)]
84+
[DataRow(Version.V4, 4096)]
85+
[DataRow(Version.V4, 4097)]
86+
public void ReadSpan(Version version, int length)
87+
{
88+
// Test files are filled with bytes equal to their position modulo 256
89+
byte[] expectedBuffer = new byte[length];
90+
for (int i = 0; i < length; i++)
91+
expectedBuffer[i] = ((byte)i);
92+
93+
string fileName = $"TestStream_v{(int)version}_{length}.cfs";
94+
using var rootStorage = RootStorage.OpenRead(fileName);
95+
96+
using Stream stream = rootStorage.OpenStream("TestStream");
97+
Assert.AreEqual(length, stream.Length);
98+
99+
byte[] actualBuffer = new byte[length];
100+
stream.Read(actualBuffer);
101+
102+
CollectionAssert.AreEqual(expectedBuffer, actualBuffer);
103+
}
104+
#endif
105+
64106
[TestMethod]
65107
[DataRow(Version.V3, 0)]
66108
[DataRow(Version.V3, 63)]
@@ -150,7 +192,41 @@ public void Seek(Version version, int length)
150192
[DataRow(Version.V4, 1024 * 4096)] // Multiple FAT sectors (1024 * 4096)
151193
[DataRow(Version.V4, 7087616 * 4)] // First DIFAT chain
152194
[DataRow(Version.V4, 2 * 7087616 * 4)] // Long DIFAT chain
153-
public void Write(Version version, int length)
195+
public void Write(Version version, int length) => WriteCore(version, length, false);
196+
197+
#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
198+
[TestMethod]
199+
[DataRow(Version.V3, 0)]
200+
[DataRow(Version.V3, 63)]
201+
[DataRow(Version.V3, 64)] // Mini-stream sector size
202+
[DataRow(Version.V3, 65)]
203+
[DataRow(Version.V3, 511)]
204+
[DataRow(Version.V3, 512)] // Multiple stream sectors
205+
[DataRow(Version.V3, 513)]
206+
[DataRow(Version.V3, 4095)]
207+
[DataRow(Version.V3, 4096)]
208+
[DataRow(Version.V3, 4097)]
209+
[DataRow(Version.V3, 128 * 512)] // Multiple FAT sectors
210+
[DataRow(Version.V3, 1024 * 4096)] // Multiple FAT sectors
211+
[DataRow(Version.V3, 7087616)] // First DIFAT chain
212+
[DataRow(Version.V3, 2 * 7087616)] // Long DIFAT chain
213+
[DataRow(Version.V4, 0)]
214+
[DataRow(Version.V4, 63)]
215+
[DataRow(Version.V4, 64)] // Mini-stream sector size
216+
[DataRow(Version.V4, 65)]
217+
[DataRow(Version.V4, 511)]
218+
[DataRow(Version.V4, 512)]
219+
[DataRow(Version.V4, 513)]
220+
[DataRow(Version.V4, 4095)]
221+
[DataRow(Version.V4, 4096)] // Multiple stream sectors
222+
[DataRow(Version.V4, 4097)]
223+
[DataRow(Version.V4, 1024 * 4096)] // Multiple FAT sectors (1024 * 4096)
224+
[DataRow(Version.V4, 7087616 * 4)] // First DIFAT chain
225+
[DataRow(Version.V4, 2 * 7087616 * 4)] // Long DIFAT chain
226+
public void WriteSpan(Version version, int length) => WriteCore(version, length, true);
227+
#endif
228+
229+
static void WriteCore(Version version, int length, bool preferSpan)
154230
{
155231
using MemoryStream memoryStream = new();
156232
using var rootStorage = RootStorage.Create(memoryStream, version);
@@ -162,7 +238,15 @@ public void Write(Version version, int length)
162238
for (int i = 0; i < length; i++)
163239
expectedBuffer[i] = (byte)i;
164240

241+
#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
242+
if (preferSpan)
243+
stream.Write(expectedBuffer);
244+
else
245+
stream.Write(expectedBuffer, 0, expectedBuffer.Length);
246+
#else
165247
stream.Write(expectedBuffer, 0, expectedBuffer.Length);
248+
#endif
249+
166250
Assert.AreEqual(length, stream.Length);
167251
Assert.AreEqual(length, stream.Position);
168252

0 commit comments

Comments
 (0)