using System; using System.IO; using System.Linq; using Developpez.Dotnet.Collections; using Developpez.Dotnet.IO; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class VirtualizingCollectionBaseTests { class TestCollection : VirtualizingCollectionBase { private readonly string _dicoFileName; public TestCollection(string dicoFileName) : base(50) { _dicoFileName = dicoFileName; } #region Overrides of VirtualizingCollectionBase protected override string[] FetchItems(int start, int count) { using (var reader = File.OpenText(_dicoFileName)) { var res = reader.AsLineEnumerable().Skip(start).Take(count).ToArray(); if (PageFetched != null) PageFetched(start, count); return res; } } protected override int FetchCount() { using (var reader = File.OpenText(_dicoFileName)) { int count = reader.AsLineEnumerable().Count(); if (CountFetched != null) CountFetched(); return count; } } #endregion public event Action PageFetched; public event Action CountFetched; } [Test] public void Test_CountIsFetchedOnlyOnce() { var collection = new TestCollection(@"Collections\dico.txt"); bool countFetched = false; collection.CountFetched += () => countFetched = true; int count = collection.Count; Assert.IsTrue(countFetched); countFetched = false; count = collection.Count; Assert.IsFalse(countFetched); } [Test] public void Test_PageAreLoadedAndUnloadedAsNecessary() { var collection = new TestCollection(@"Collections\dico.txt"); int fetchStart = -1; collection.PageFetched += (start, count) => fetchStart = start; // Should load first page string s = collection[0]; Assert.AreEqual(0, fetchStart); // Still on the same page, shouldn't fetch fetchStart = -1; s = collection[49]; Assert.AreEqual(-1, fetchStart); // Should load second page s = collection[50]; Assert.AreEqual(50, fetchStart); // First page should still be there, no fetch necessary fetchStart = -1; s = collection[25]; Assert.AreEqual(-1, fetchStart); // Should load third page s = collection[123]; Assert.AreEqual(100, fetchStart); // Second page should still be there, no fetch necessary fetchStart = -1; s = collection[70]; Assert.AreEqual(-1, fetchStart); // First page has been unloaded and should be fetched again s = collection[42]; Assert.AreEqual(0, fetchStart); // Second page should still be there, no fetch necessary fetchStart = -1; s = collection[70]; Assert.AreEqual(-1, fetchStart); // Third page has been unloaded and should be fetched again s = collection[140]; Assert.AreEqual(100, fetchStart); } [Test] public void Test_InvalidateForcesReload() { var collection = new TestCollection(@"Collections\dico.txt"); int fetchStart = -1; bool countFetched = false; collection.PageFetched += (start, count) => fetchStart = start; collection.CountFetched += () => countFetched = true; // Should load first page string s = collection[0]; Assert.AreEqual(0, fetchStart); Assert.IsTrue(countFetched); // Data still there, shouldn't fetch fetchStart = -1; countFetched = false; s = collection[49]; Assert.AreEqual(-1, fetchStart); Assert.IsFalse(countFetched); collection.Invalidate(); // Data should be reloaded s = collection[0]; Assert.AreEqual(0, fetchStart); Assert.IsTrue(countFetched); } } }