public void StreamingTest() { string fileName = GetBinFileName(); byte[] data = TestUtils.GenerateData<byte>(10000, 0); if (AllowCreate) { using (var b = new BinIndexedFile<byte>(fileName)) { b.InitializeNewFile(); b.WriteData(0, new ArraySegment<byte>(data)); } } byte[] bytes = File.ReadAllBytes(fileName); using (var b = (BinIndexedFile<byte>) BinaryFile.Open(fileName, false, LegacyResolver)) { var ms = new MemoryStream(bytes); var cs = new ConfigurableStream(ms); var data2 = new byte[data.Length/2]; cs.AllowSeek = cs.AllowWrite = false; var b2 = (BinIndexedFile<byte>) BinaryFile.Open(cs, LegacyResolver); Assert.IsTrue(b2.IsOpen); Assert.AreEqual(b.ItemSize, b2.ItemSize); b2.ReadData(0, new ArraySegment<byte>(data2)); TestUtils.CollectionAssertEqual(TestUtils.GenerateData<byte>(data.Length/2, 0), data2); b2.ReadData(0, new ArraySegment<byte>(data2)); TestUtils.CollectionAssertEqual(TestUtils.GenerateData<byte>(data.Length/2, data.Length/2), data2); } }
public void StreamingTest() { string fileName = GetBinFileName(); byte[] data = TestUtils.GenerateData <byte>(10000, 0); if (AllowCreate) { using (var b = new BinIndexedFile <byte>(fileName)) { b.InitializeNewFile(); b.WriteData(0, new ArraySegment <byte>(data)); } } byte[] bytes = File.ReadAllBytes(fileName); using (var b = (BinIndexedFile <byte>)BinaryFile.Open(fileName, false, LegacyResolver)) { var ms = new MemoryStream(bytes); var cs = new ConfigurableStream(ms); var data2 = new byte[data.Length / 2]; cs.AllowSeek = cs.AllowWrite = false; var b2 = (BinIndexedFile <byte>)BinaryFile.Open(cs, LegacyResolver); Assert.IsTrue(b2.IsOpen); Assert.AreEqual(b.ItemSize, b2.ItemSize); b2.ReadData(0, new ArraySegment <byte>(data2)); TestUtils.CollectionAssertEqual(TestUtils.GenerateData <byte>(data.Length / 2, 0), data2); b2.ReadData(0, new ArraySegment <byte>(data2)); TestUtils.CollectionAssertEqual(TestUtils.GenerateData <byte>(data.Length / 2, data.Length / 2), data2); } }
public void Test() { const int blockSize = 100; var data = new TradesBlock[1000]; for (int i = 0; i < data.Length; i++) { data[i] = new TradesBlock(i, blockSize); } string fileName = GetBinFileName(); if (AllowCreate) { using (var f = new BinIndexedFile <TradesBlock>(fileName)) { ((IBinBlockSerializer)f.Serializer).ItemCount = blockSize; f.InitializeNewFile(); f.WriteData(0, new ArraySegment <TradesBlock>(data)); VerifyData(f, data); } } using (var bf = (BinIndexedFile <TradesBlock>)BinaryFile.Open(fileName, false, LegacyResolver)) { VerifyData(bf, data); } }
private void PageBorderOperations <T>(bool enableMma, int pageSize) where T : IEquatable <T> { DeleteTempFiles(); string fileName = GetBinFileName(); using ( BinIndexedFile <T> f = AllowCreate ? new BinIndexedFile <T>(fileName) : (BinIndexedFile <T>) BinaryFile.Open(fileName, false, LegacyResolver)) { if (AllowCreate) { f.InitializeNewFile(); f.EnableMemMappedAccessOnRead = enableMma; f.EnableMemMappedAccessOnWrite = enableMma; } int itemsPerPage = pageSize / Marshal.SizeOf(typeof(T)); int headerSizeAsItemCount = f.HeaderSize / f.ItemSize; int items1StPg = (int)TestUtils.RoundUpToMultiple(headerSizeAsItemCount, itemsPerPage) - headerSizeAsItemCount; if (items1StPg == 0) { items1StPg = itemsPerPage; } T[] dataMinusOne = TestUtils.GenerateData <T>(items1StPg - 1, 0); T[] dataZero = TestUtils.GenerateData <T>(items1StPg, 0); T[] dataPlusOne = TestUtils.GenerateData <T>(items1StPg + 1, 0); if (AllowCreate) { f.WriteData(0, new ArraySegment <T>(dataMinusOne)); Assert.AreEqual(f.HeaderSize + (items1StPg - 1) * f.ItemSize, new FileInfo(fileName).Length); ReadAndAssert(dataMinusOne, f, 0, dataMinusOne.Length); f.WriteData(0, new ArraySegment <T>(dataZero)); Assert.AreEqual(f.HeaderSize + items1StPg * f.ItemSize, new FileInfo(fileName).Length); ReadAndAssert(dataZero, f, 0, dataZero.Length); f.WriteData(0, new ArraySegment <T>(dataPlusOne)); } Assert.AreEqual(f.HeaderSize + (items1StPg + 1) * f.ItemSize, new FileInfo(fileName).Length); ReadAndAssert(dataPlusOne, f, 0, dataPlusOne.Length); ReadAndAssert(TestUtils.GenerateData <T>(1, items1StPg - 1), f, items1StPg - 1, 1); ReadAndAssert(TestUtils.GenerateData <T>(1, items1StPg), f, items1StPg, 1); ReadAndAssert(TestUtils.GenerateData <T>(2, items1StPg - 1), f, items1StPg - 1, 2); } }
public void MappingTest() { string fileName = GetBinFileName(); _DatetimeByte_SeqPk1[] data = TestUtils.GenerateData <_DatetimeByte_SeqPk1>(1, 10); if (AllowCreate) { using (var f = new BinIndexedFile <_DatetimeByte_SeqPk1>(fileName)) { f.InitializeNewFile(); f.WriteData(0, new ArraySegment <_DatetimeByte_SeqPk1>(data)); } } Type oldT = typeof(_DatetimeByte_SeqPk1); string oldAn = oldT.Assembly.GetName().Name; using (BinaryFile f = BinaryFile.Open( fileName, false, tn => TypeUtils.ParseAndResolve( tn, ts2 => TypeSpec.DefaultFullTypeResolver( ts2, (ts, an) => an != null && an.Name == oldAn && ts.Name == oldT.FullName ? typeof(_LongByte_SeqPk1) : null, TypeResolver, LegacySupport.TypeResolver, TypeUtils.ResolverFromAnyAssemblyVersion)))) { var p = (BinIndexedFile <_LongByte_SeqPk1>)f; var data2 = new _LongByte_SeqPk1[1]; p.ReadData(0, new ArraySegment <_LongByte_SeqPk1>(data2)); Assert.AreEqual(data[0].a.Ticks, data2[0].a); Assert.AreEqual(data[0].b, data2[0].b); } }
private void EmptyFile <T>(int expectedItemSize) { const string testName = "EmptyFile"; try { Stopwatch sw = TestStart(); int hdrSize; Version fileVersion, baseVersion, serializerVersion; string fileName = GetBinFileName(); if (AllowCreate) { using (var f = new BinIndexedFile <T>(fileName)) { f.InitializeNewFile(); fileVersion = f.Version; Assert.IsNotNull(fileVersion); baseVersion = f.BaseVersion; Assert.IsNotNull(baseVersion); serializerVersion = f.Serializer.Version; Assert.IsNotNull(serializerVersion); Assert.AreEqual(true, f.CanWrite); Assert.AreEqual(0, f.Count); hdrSize = f.HeaderSize; Assert.AreEqual(expectedItemSize, f.ItemSize); Assert.IsTrue(hdrSize % f.ItemSize == 0); Assert.IsTrue(f.IsEmpty); } } else { fileVersion = baseVersion = serializerVersion = default(Version); hdrSize = 0; } using (BinaryFile file = BinaryFile.Open(fileName, false, LegacyResolver)) { Assert.IsInstanceOf <BinIndexedFile <T> >(file); Assert.AreEqual(typeof(T), file.ItemType); var f = (BinIndexedFile <T>)file; if (AllowCreate) { Assert.AreEqual(fileVersion, f.Version); Assert.AreEqual(baseVersion, f.BaseVersion); Assert.AreEqual(serializerVersion, f.Serializer.Version); Assert.AreEqual(hdrSize, f.HeaderSize); } Assert.AreEqual(false, f.CanWrite); Assert.AreEqual(0, f.Count); Assert.IsTrue(f.IsEmpty); Assert.AreEqual(expectedItemSize, f.ItemSize); } TestStop <T>(testName, sw); } catch { Console.WriteLine("Error in " + testName); throw; } }
private void FileIncrementalAddition <T>() where T : IEquatable <T> { const string testName = "FileIncrementalAddition"; try { Stopwatch sw = TestStart(); T[] data0 = TestUtils.GenerateData <T>(1, 10); T[] data1 = TestUtils.GenerateData <T>(2, 20); T[] data2 = TestUtils.GenerateData <T>(3, 30); string fileName = GetBinFileName(); if (AllowCreate) { using (var f = new BinIndexedFile <T>(fileName)) { f.InitializeNewFile(); f.WriteData(0, new ArraySegment <T>(data0)); Assert.AreEqual(true, f.CanWrite); Assert.AreEqual(1, f.Count); Assert.IsFalse(f.IsEmpty); ReadAndAssert(data0, f, 0, f.Count); } } using (BinaryFile file = BinaryFile.Open(fileName, AllowCreate, LegacyResolver)) { Assert.IsInstanceOf <BinIndexedFile <T> >(file); var f = (BinIndexedFile <T>)file; Assert.AreEqual(AllowCreate, f.CanWrite); if (AllowCreate) { Assert.AreEqual(1, f.Count); ReadAndAssert(data0, f, 0, f.Count); // Replace with buff2 starting at 0 WriteData(f, 0, data1); Assert.AreEqual(2, f.Count); ReadAndAssert(data1, f, 0, f.Count); // Append buff1 WriteData(f, f.Count, data0); Assert.AreEqual(3, f.Count); ReadAndAssert(data0, f, 2, 1); // Write buff3 instead of buff1 WriteData(f, data1.Length, data2); } Assert.AreEqual(data1.Length + data2.Length, f.Count); ReadAndAssert(TestUtils.Concatenate(data1, data2), f, 0, f.Count); } TestStop <T>(testName, sw); } catch { Console.WriteLine("Error in " + testName); throw; } }
public void BasicFunctionality() { string fileName = GetBinFileName(); if (AllowCreate) { BinIndexedFile <byte> temp; using (var f = new BinIndexedFile <byte>(fileName)) { temp = f; #pragma warning disable 168 Assert.Throws <InvalidOperationException>(() => { var v = f.Count; }); Assert.Throws <InvalidOperationException>(() => { var v = f.Version; }); Assert.Throws <InvalidOperationException>(() => { var v = f.HeaderSize; }); Assert.Throws <InvalidOperationException>(() => { var v = f.IsEmpty; }); Assert.Throws <InvalidOperationException>(() => { var v = f.ItemSize; }); Assert.Throws <InvalidOperationException>(() => { var v = f.EnableMemMappedAccessOnRead; }); Assert.Throws <InvalidOperationException>(() => { var v = f.EnableMemMappedAccessOnWrite; }); Assert.Throws <InvalidOperationException>(() => { var v = f.Serializer.Version; }); Assert.Throws <InvalidOperationException>(() => { var v = f.CanWrite; }); #pragma warning restore 168 Assert.IsFalse(f.IsInitialized); Assert.IsFalse(f.IsDisposed); Assert.IsFalse(f.IsOpen); Assert.AreEqual(fileName, f.FileName); Assert.AreEqual("", f.Tag); Assert.AreEqual(typeof(byte), f.ItemType); Assert.IsNotNull(f.Serializer); f.Tag = TagString; Assert.AreEqual(TagString, f.Tag); Version curBaseVer = f.BaseVersion; f.BaseVersion = new Version(1, 0); f.BaseVersion = new Version(1, 1); f.BaseVersion = new Version(1, 2); Assert.Throws <ArgumentNullException>(() => { f.BaseVersion = null; }); Assert.Throws <IncompatibleVersionException>( () => { f.BaseVersion = new Version(0, 0); }); f.BaseVersion = curBaseVer; f.InitializeNewFile(); Assert.IsTrue(f.CanWrite); Assert.IsNotNull(f.Serializer.Version); Assert.IsTrue(f.IsInitialized); Assert.IsFalse(f.IsDisposed); Assert.IsTrue(f.IsOpen); Assert.AreEqual(fileName, f.FileName); Assert.IsFalse(f.EnableMemMappedAccessOnRead); f.EnableMemMappedAccessOnRead = false; Assert.IsFalse(f.EnableMemMappedAccessOnRead); f.EnableMemMappedAccessOnRead = true; Assert.IsTrue(f.EnableMemMappedAccessOnRead); Assert.IsFalse(f.EnableMemMappedAccessOnWrite); f.EnableMemMappedAccessOnWrite = false; Assert.IsFalse(f.EnableMemMappedAccessOnWrite); f.EnableMemMappedAccessOnWrite = true; Assert.IsTrue(f.EnableMemMappedAccessOnWrite); Assert.Throws <InvalidOperationException>(f.InitializeNewFile); Assert.Throws <InvalidOperationException>(() => { f.BaseVersion = new Version(1, 1); }); AfterInitValidation(f, true, fileName); } temp.Close(); // allowed after disposing ((IDisposable)temp).Dispose(); // disposing multiple times is ok #pragma warning disable 168 Assert.Throws <ObjectDisposedException>(() => { var v = temp.Tag; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.EnableMemMappedAccessOnRead; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.EnableMemMappedAccessOnWrite; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.Count; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.IsEmpty; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.ItemSize; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.NonGenericSerializer; }); Assert.Throws <ObjectDisposedException>(() => { var v = temp.Serializer; }); #pragma warning restore 168 Assert.IsTrue(temp.IsInitialized); Assert.IsTrue(temp.IsDisposed); Assert.IsFalse(temp.IsOpen); Assert.AreEqual(fileName, temp.FileName); Assert.AreEqual(typeof(byte), temp.ItemType); using ( var f = (BinIndexedFile <byte>)BinaryFile.Open(fileName, AllowCreate, LegacyResolver)) { AfterInitValidation(f, true, fileName); f.Close(); #pragma warning disable 168 Assert.Throws <ObjectDisposedException>(() => { var v = f.Tag; }); #pragma warning restore 168 Assert.IsTrue(f.IsInitialized); Assert.IsTrue(f.IsDisposed); Assert.IsFalse(f.IsOpen); Assert.AreEqual(fileName, f.FileName); } } using (var f = (BinIndexedFile <byte>)BinaryFile.Open(fileName, false, LegacyResolver)) { AfterInitValidation(f, false, fileName); ((IDisposable)f).Dispose(); #pragma warning disable 168 Assert.Throws <ObjectDisposedException>(() => { var v = f.Tag; }); #pragma warning restore 168 } using (var f = new BinIndexedFile <byte>(fileName)) { Assert.Throws <IOException>(f.InitializeNewFile); if (RunMode == Mode.OneTime) { File.Delete(fileName); f.InitializeNewFile(); } } }
public void BasicFunctionality() { string fileName = GetBinFileName(); if (AllowCreate) { BinIndexedFile<byte> temp; using (var f = new BinIndexedFile<byte>(fileName)) { temp = f; #pragma warning disable 168 Assert.Throws<InvalidOperationException>(() => { var v = f.Count; }); Assert.Throws<InvalidOperationException>(() => { var v = f.Version; }); Assert.Throws<InvalidOperationException>(() => { var v = f.HeaderSize; }); Assert.Throws<InvalidOperationException>(() => { var v = f.IsEmpty; }); Assert.Throws<InvalidOperationException>(() => { var v = f.ItemSize; }); Assert.Throws<InvalidOperationException>(() => { var v = f.EnableMemMappedAccessOnRead; }); Assert.Throws<InvalidOperationException>(() => { var v = f.EnableMemMappedAccessOnWrite; }); Assert.Throws<InvalidOperationException>(() => { var v = f.Serializer.Version; }); Assert.Throws<InvalidOperationException>(() => { var v = f.CanWrite; }); #pragma warning restore 168 Assert.IsFalse(f.IsInitialized); Assert.IsFalse(f.IsDisposed); Assert.IsFalse(f.IsOpen); Assert.AreEqual(fileName, f.FileName); Assert.AreEqual("", f.Tag); Assert.AreEqual(typeof (byte), f.ItemType); Assert.IsNotNull(f.Serializer); f.Tag = TagString; Assert.AreEqual(TagString, f.Tag); Version curBaseVer = f.BaseVersion; f.BaseVersion = new Version(1, 0); f.BaseVersion = new Version(1, 1); f.BaseVersion = new Version(1, 2); Assert.Throws<ArgumentNullException>(() => { f.BaseVersion = null; }); Assert.Throws<IncompatibleVersionException>( () => { f.BaseVersion = new Version(0, 0); }); f.BaseVersion = curBaseVer; f.InitializeNewFile(); Assert.IsTrue(f.CanWrite); Assert.IsNotNull(f.Serializer.Version); Assert.IsTrue(f.IsInitialized); Assert.IsFalse(f.IsDisposed); Assert.IsTrue(f.IsOpen); Assert.AreEqual(fileName, f.FileName); Assert.IsFalse(f.EnableMemMappedAccessOnRead); f.EnableMemMappedAccessOnRead = false; Assert.IsFalse(f.EnableMemMappedAccessOnRead); f.EnableMemMappedAccessOnRead = true; Assert.IsTrue(f.EnableMemMappedAccessOnRead); Assert.IsFalse(f.EnableMemMappedAccessOnWrite); f.EnableMemMappedAccessOnWrite = false; Assert.IsFalse(f.EnableMemMappedAccessOnWrite); f.EnableMemMappedAccessOnWrite = true; Assert.IsTrue(f.EnableMemMappedAccessOnWrite); Assert.Throws<InvalidOperationException>(f.InitializeNewFile); Assert.Throws<InvalidOperationException>(() => { f.BaseVersion = new Version(1, 1); }); AfterInitValidation(f, true, fileName); } temp.Close(); // allowed after disposing ((IDisposable) temp).Dispose(); // disposing multiple times is ok #pragma warning disable 168 Assert.Throws<ObjectDisposedException>(() => { var v = temp.Tag; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.EnableMemMappedAccessOnRead; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.EnableMemMappedAccessOnWrite; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.Count; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.IsEmpty; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.ItemSize; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.NonGenericSerializer; }); Assert.Throws<ObjectDisposedException>(() => { var v = temp.Serializer; }); #pragma warning restore 168 Assert.IsTrue(temp.IsInitialized); Assert.IsTrue(temp.IsDisposed); Assert.IsFalse(temp.IsOpen); Assert.AreEqual(fileName, temp.FileName); Assert.AreEqual(typeof (byte), temp.ItemType); using ( var f = (BinIndexedFile<byte>) BinaryFile.Open(fileName, AllowCreate, LegacyResolver)) { AfterInitValidation(f, true, fileName); f.Close(); #pragma warning disable 168 Assert.Throws<ObjectDisposedException>(() => { var v = f.Tag; }); #pragma warning restore 168 Assert.IsTrue(f.IsInitialized); Assert.IsTrue(f.IsDisposed); Assert.IsFalse(f.IsOpen); Assert.AreEqual(fileName, f.FileName); } } using (var f = (BinIndexedFile<byte>) BinaryFile.Open(fileName, false, LegacyResolver)) { AfterInitValidation(f, false, fileName); ((IDisposable) f).Dispose(); #pragma warning disable 168 Assert.Throws<ObjectDisposedException>(() => { var v = f.Tag; }); #pragma warning restore 168 } using (var f = new BinIndexedFile<byte>(fileName)) { Assert.Throws<IOException>(f.InitializeNewFile); if (RunMode == Mode.OneTime) { File.Delete(fileName); f.InitializeNewFile(); } } }
public void MappingTest() { string fileName = GetBinFileName(); _DatetimeByte_SeqPk1[] data = TestUtils.GenerateData<_DatetimeByte_SeqPk1>(1, 10); if (AllowCreate) { using (var f = new BinIndexedFile<_DatetimeByte_SeqPk1>(fileName)) { f.InitializeNewFile(); f.WriteData(0, new ArraySegment<_DatetimeByte_SeqPk1>(data)); } } Type oldT = typeof (_DatetimeByte_SeqPk1); string oldAn = oldT.Assembly.GetName().Name; using (BinaryFile f = BinaryFile.Open( fileName, false, tn => TypeUtils.ParseAndResolve( tn, ts2 => TypeSpec.DefaultFullTypeResolver( ts2, (ts, an) => an != null && an.Name == oldAn && ts.Name == oldT.FullName ? typeof (_LongByte_SeqPk1) : null, TypeResolver, LegacySupport.TypeResolver, TypeUtils.ResolverFromAnyAssemblyVersion)))) { var p = (BinIndexedFile<_LongByte_SeqPk1>) f; var data2 = new _LongByte_SeqPk1[1]; p.ReadData(0, new ArraySegment<_LongByte_SeqPk1>(data2)); Assert.AreEqual(data[0].a.Ticks, data2[0].a); Assert.AreEqual(data[0].b, data2[0].b); } }