From 0ded5deea41f1dd0a64639e899abb18f3b481705 Mon Sep 17 00:00:00 2001 From: SpaceCheetah Date: Tue, 4 Jun 2024 14:16:45 -0700 Subject: [PATCH] Add tests --- LiteDB.Tests/Issues/Issue_2458_Tests.cs | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 LiteDB.Tests/Issues/Issue_2458_Tests.cs diff --git a/LiteDB.Tests/Issues/Issue_2458_Tests.cs b/LiteDB.Tests/Issues/Issue_2458_Tests.cs new file mode 100644 index 000000000..a05d7df46 --- /dev/null +++ b/LiteDB.Tests/Issues/Issue_2458_Tests.cs @@ -0,0 +1,54 @@ +using System; +using System.IO; +using Xunit; + +namespace LiteDB.Tests.Issues; + +public class Issue_2458_Tests +{ + [Fact] + public void NegativeSeekFails() + { + using var db = new LiteDatabase(":memory:"); + var fs = db.FileStorage; + AddTestFile("test", 1, fs); + using Stream stream = fs.OpenRead("test"); + Assert.Throws(() => stream.Position = -1); + } + + //https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.position?view=net-8.0 says seeking to a position + //beyond the end of a stream is supported, so implementations should support it (error on read). + [Fact] + public void SeekPastFileSucceds() + { + using var db = new LiteDatabase(":memory:"); + var fs = db.FileStorage; + AddTestFile("test", 1, fs); + using Stream stream = fs.OpenRead("test"); + stream.Position = Int32.MaxValue; + } + + [Fact] + public void SeekShortChunks() + { + using var db = new LiteDatabase(":memory:"); + var fs = db.FileStorage; + using(Stream writeStream = fs.OpenWrite("test", "test")) + { + writeStream.WriteByte(0); + writeStream.Flush(); //Create single-byte chunk just containing a 0 + writeStream.WriteByte(1); + writeStream.Flush(); + writeStream.WriteByte(2); + } + using Stream readStream = fs.OpenRead("test"); + readStream.Position = 2; + Assert.Equal(2, readStream.ReadByte()); + } + + private void AddTestFile(string id, long length, ILiteStorage fs) + { + using Stream writeStream = fs.OpenWrite(id, id); + writeStream.Write(new byte[length]); + } +} \ No newline at end of file