public void TestRecoverBlocks() { long idFirst, idSecond, idThird; using (TempFile file = new TempFile()) { if (true) { FragmentedFile ff = FragmentedFile.CreateNew(file.TempPath, 512, 100, 2, FragmentedFile.OptionsDefault); idFirst = ff.Create(); idSecond = ff.Create(); idThird = ff.Create(); ff.Delete(idFirst); //Dangerous, only used for testing the case when ff was never disposed, nor GC'd GC.SuppressFinalize(ff); ff = null; } GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); using (FragmentedFile f2 = new FragmentedFile(file.TempPath, 512)) { Assert.IsTrue(f2.Create() < idSecond); Assert.IsTrue(f2.Create() > idThird); } } }
void TestCrud(FragmentedFile ff, int blockCount, int blockSize) { Dictionary <long, byte[]> data = new Dictionary <long, byte[]>(); //Create: for (int i = 0; i < blockCount; i++) { data.Add(ff.Create(), null); } //Write: foreach (long id in new List <long>(data.Keys)) { using (Stream io = ff.Open(id, FileAccess.Write)) io.Write(data[id] = MakeBytes(blockSize), 0, blockSize); } //Read: foreach (KeyValuePair <long, byte[]> kv in data) { using (Stream io = ff.Open(kv.Key, FileAccess.Read)) Assert.AreEqual(kv.Value, IOStream.ReadAllBytes(io)); } //Enumerate: Dictionary <long, byte[]> copy = new Dictionary <long, byte[]>(data); foreach (KeyValuePair <long, Stream> fragment in ff.ForeachBlock(true, true, null)) { Assert.AreEqual(copy[fragment.Key], IOStream.ReadAllBytes(fragment.Value)); Assert.IsTrue(copy.Remove(fragment.Key)); } //Update: foreach (long id in new List <long>(data.Keys)) { Assert.AreEqual(data[id], IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); using (Stream io = ff.Open(id, FileAccess.Write)) io.Write(data[id] = MakeBytes(blockSize * 2), 0, blockSize * 2); Assert.AreEqual(data[id], IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); using (Stream io = ff.Open(id, FileAccess.Write)) io.Write(data[id] = MakeBytes(blockSize / 2), 0, blockSize / 2); Assert.AreEqual(data[id], IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); } //Delete: foreach (long id in new List <long>(data.Keys)) { ff.Delete(id); } //Empty? foreach (KeyValuePair <long, Stream> fragment in ff.ForeachBlock(true, true, null)) { Assert.Fail(); } }