public void prevents_duplicates_if_asked_to_do_so() { var car1 = new Car() { Year = 2012 }; var car2 = new Car() { Year = 2012 }; var sc = new SortedCollection <Car, int>((c) => c.Year, false); sc.Add(car1); Assert.Throws <ArgumentException>(() => sc.Add(car1)); }
public void Test_DupId_B_GetError() { var tree = new SortedCollection <string, TestData>(); tree.AddSorted(new TestData(0)); tree.AddSorted(new TestData(1)); tree.AddSorted(new TestData(0)); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(1, list.Count); Assert.AreEqual("1", list[0]); }
public void allows_duplicates_if_asked_to_do_so() { var car1 = new Car() { Year = 2012 }; var car2 = new Car() { Year = 2012 }; var sc = new SortedCollection <Car, int>((c) => c.Year, true); sc.Add(car1); sc.Add(car2); }
public void Test_MissingDependency_GetExpectedOrder() { var tree = new SortedCollection <string, TestData>(); var entity = new TestData(0); entity.RequiredDependencies.Add("1"); tree.AddSorted(entity); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(1, list.Count); // Still added to list and checked later }
public void Add_Random_Ascending() { var random = new Random(DateTime.UtcNow.Millisecond); var input = Enumerable.Range(1, 10_000) .Select(n => random.Next(1, 1_000)) .ToArray(); var collection = new SortedCollection <double>(); foreach (var n in input) { collection.Add(n); } collection.Should().Equal(input.Select(n => (double)n).OrderBy(n => n)); collection.Should().BeInAscendingOrder(); }
public void Contains_RandomExist_Find() { var random = new Random(DateTime.UtcNow.Millisecond); var input = Enumerable.Range(1, 10_000) .Select(n => random.Next(1, 1_000)) .ToArray(); var collection = new SortedCollection <int>(); collection.AddRange(input); foreach (var n in input) { collection.Contains(n).Should().BeTrue(); } collection.Should().BeInAscendingOrder(); }
public void Test_NoPreferences_GetExpectedOrder() { var tree = new SortedCollection <string, TestData>(); tree.AddSorted(new TestData(0)); tree.AddSorted(new TestData(1)); tree.AddSorted(new TestData(2)); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(3, list.Count); Assert.AreEqual("1", list[0]); Assert.AreEqual("2", list[1]); Assert.AreEqual("0", list[2]); }
public void Test_MissingDependency_GetExpectedOrder() { var tree = new SortedCollection <string, TestData>(); var entity = new TestData(0); entity.Dependencies.Add("1"); tree.AddSorted(entity); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(0, list.Count); Assert.Pass(ListToString(list)); }
internal void LoadModsFromDirectories(string[] subDirectories, SortedCollection <string, QMod> modSorter, List <QMod> earlyErrors) { foreach (string subDir in subDirectories.Where(subDir => (new DirectoryInfo(subDir).Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)) // exclude hidden directories { string[] dllFiles = Directory.GetFiles(subDir, "*.dll", SearchOption.TopDirectoryOnly); if (dllFiles.Length < 1) { continue; } string jsonFile = Path.Combine(subDir, "mod.json"); string folderName = new DirectoryInfo(subDir).Name; if (!File.Exists(jsonFile)) { Logger.Error($"Unable to set up mod in folder \"{folderName}\""); earlyErrors.Add(new QModPlaceholder(folderName, ModStatus.MissingCoreInfo)); continue; } QMod mod = CreateFromJsonManifestFile(subDir); if (mod == null) { Logger.Error($"Unable to set up mod in folder \"{folderName}\""); earlyErrors.Add(new QModPlaceholder(folderName, ModStatus.MissingCoreInfo)); continue; } this.Validator.CheckRequiredMods(mod); Logger.Debug($"Sorting mod {mod.Id}"); bool added = modSorter.AddSorted(mod); if (!added) { Logger.Debug($"DuplicateId on mod {mod.Id}"); mod.Status = ModStatus.DuplicateIdDetected; earlyErrors.Add(mod); } } }
public void SortedCollectionContainsReturnsTrue() { SortedCollection <int> collection = new SortedCollection <int>() { 1, 2, 3 }; Assert.IsTrue(collection.Contains(2)); // TestTools Code UnitTest test = Factory.CreateTest(); TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >(); test.Arrange(_collection, Expr(() => new SortedCollection <int>() { 1, 2, 3 })); test.Assert.IsTrue(Expr(_collection, c => c.Contains(2))); test.Execute(); }
public void SortedCollectionGetAllReversedReturnsReversedEnumerationOfCollection2() { SortedCollection <int> collection = new SortedCollection <int>() { 1, 2, 3, 4 }; Assert.IsTrue(collection.GetAllReversed(x => x % 2 == 0).SequenceEqual(new int[] { 4, 2 })); // TestTools Code UnitTest test = Factory.CreateTest(); TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >(); test.Arrange(_collection, Expr(() => new SortedCollection <int>() { 1, 2, 3, 4 })); test.Assert.IsTrue(Expr(_collection, c => c.GetAllReversed(x => x % 2 == 0).SequenceEqual(new[] { 4, 2 }))); test.Execute(); }
/// <summary> /// Searches through all folders in the provided directory and returns an ordered list of mods to load.<para/> /// Mods that cannot be loaded will have an unsuccessful <see cref="QMod.Status"/> value. /// </summary> /// <param name="qmodsDirectory">The QMods directory</param> /// <returns>A new, sorted <see cref="List{QMod}"/> ready to be initialized or skipped.</returns> public List <QMod> BuildModLoadingList(string qmodsDirectory) { if (!Directory.Exists(qmodsDirectory)) { Logger.Info("QMods directory was not found! Creating..."); Directory.CreateDirectory(qmodsDirectory); return(new List <QMod>(0)); } string[] subDirectories = Directory.GetDirectories(qmodsDirectory); var modSorter = new SortedCollection <string, QMod>(); var earlyErrors = new List <QMod>(subDirectories.Length); LoadModsFromDirectories(subDirectories, modSorter, earlyErrors); List <QMod> modsToLoad = modSorter.GetSortedList(); return(CreateModStatusList(earlyErrors, modsToLoad)); }
public void Test_CircularLoadOrder_DependencyChain3inLoop_NonDependentEntitiesIncluded() { // A -> B ~ B -> C ~ C -> A var tree = new SortedCollection <string, TestData>(); var iA = new TestData(0); iA.LoadAfter.Add("1"); var iB = new TestData(1); iB.LoadAfter.Add("2"); var iC = new TestData(2); iC.LoadAfter.Add("0"); tree.AddSorted(new TestData(3)); tree.AddSorted(iA); tree.AddSorted(new TestData(4)); tree.AddSorted(iB); tree.AddSorted(new TestData(5)); tree.AddSorted(iC); tree.AddSorted(new TestData(6)); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(4, list.Count); Assert.IsTrue(list.Contains("3")); Assert.IsTrue(list.Contains("4")); Assert.IsTrue(list.Contains("5")); Assert.IsTrue(list.Contains("6")); Assert.IsFalse(list.Contains("0")); Assert.IsFalse(list.Contains("1")); Assert.IsFalse(list.Contains("2")); Assert.Pass(ListToString(list)); }
public void Test_AfterOnlySortPrefrence_AB_GetExpectedOrder() { var tree = new SortedCollection <string, TestData>(); var i0 = new TestData(0); var i1 = new TestData(1); i1.LoadAfterPreferences.Add("0"); // Load 1 after 0 tree.AddSorted(i0); tree.AddSorted(i1); tree.AddSorted(new TestData(2)); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(3, list.Count); Assert.IsTrue(list.IndexOf("0") < list.IndexOf("1")); }
public void SortedCollectionReturnsCorrectElement() { SortedCollection <int> collection = new SortedCollection <int>() { 1, 2, 3 }; Assert.AreEqual(collection[1], 2); // TestTools Code UnitTest test = Factory.CreateTest(); TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >(); test.Arrange(_collection, Expr(() => new SortedCollection <int>() { 1, 2, 3 })); test.Assert.AreEqual(Expr(_collection, c => c[1]), Const(2)); test.Execute(); }
public void SortedCollectionAddAddsElementInOrder() { SortedCollection <int> collection = new SortedCollection <int>(); collection.Add(3); collection.Add(5); collection.Add(2); Assert.IsTrue(collection.SequenceEqual(new int[] { 2, 3, 5 })); // TestTools Code UnitTest test = Factory.CreateTest(); TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >(); test.Arrange(_collection, Expr(() => new SortedCollection <int>())); test.Act(Expr(_collection, c => c.Add(3))); test.Act(Expr(_collection, c => c.Add(5))); test.Act(Expr(_collection, c => c.Add(2))); test.Assert.IsTrue(Expr(_collection, c => c.SequenceEqual(new[] { 2, 3, 5 }))); test.Execute(); }
public void Render(SortedCollection <Person, PersonSortCriteria> data) { Console.Clear(); var collectionArray = data.Data; if ((collectionArray != null)) { foreach (var element in collectionArray) { Console.WriteLine($"PersonID:{element.PersonID} , " + $"FirstName:{element.FirstName} , " + $"LastName:{element.LastName} , " + $"DateOfBirth:{element.DateOfBirth} "); } } else { Console.WriteLine("Curent collectionArray is Null"); } }
private void OnItemsChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: { AddSortedItem(Comparer, SortedItems, e.NewItems[0]); break; } case NotifyCollectionChangedAction.Remove: { RemSortedItem(Comparer, SortedItems, e.OldItems[0]); break; } case NotifyCollectionChangedAction.Replace: { SortComparer comparer = Comparer; SortedCollection sorted = SortedItems; RemSortedItem(comparer, sorted, e.OldItems[0]); AddSortedItem(comparer, sorted, e.NewItems[0]); break; } case NotifyCollectionChangedAction.Move: { // nothing to do break; } case NotifyCollectionChangedAction.Reset: { SortItems(); break; } } }
public void SortedCollectionClearRemovesAllElements() { SortedCollection <int> collection = new SortedCollection <int>() { 1, 2, 3 }; collection.Clear(); Assert.IsFalse(collection.Any()); // TestTools Code UnitTest test = Factory.CreateTest(); TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >(); test.Arrange(_collection, Expr(() => new SortedCollection <int>() { 1, 2, 3 })); test.Act(Expr(_collection, c => c.Clear())); test.Assert.IsFalse(Expr(_collection, c => c.Any())); test.Execute(); }
public void SortedCollectionRemoveRemovesElement() { SortedCollection <int> collection = new SortedCollection <int>() { 1, 2, 3 }; collection.Remove(1); Assert.IsTrue(collection.SequenceEqual(new int[] { 2, 3 })); // TestTools Code UnitTest test = Factory.CreateTest(); TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >(); test.Arrange(_collection, Expr(() => new SortedCollection <int>() { 1, 2, 3 })); test.Act(Expr(_collection, c => c.Remove(1))); test.Assert.IsTrue(Expr(_collection, c => c.SequenceEqual(new[] { 2, 3 }))); test.Execute(); }
public BlacklistedAddresses(IList <AddressRange> addressRanges) { var sortedStarts = new SortedCollection <AddressRange>(addressRanges, AddressRange.AscendingStart); ranges = new List <AddressRange>(sortedStarts.Count); var merged = sortedStarts[0]; for (int i = 1; i < sortedStarts.Count; i++) { var next = sortedStarts[i]; if (next.Start <= merged.End) { merged.End = Math.Max(next.End, merged.End); } else { ranges.Add(merged); merged = next; } } ranges.Add(merged); }
public void Test_AfterOnlySortPrefrence_BA_GetExpectedOrder() { var tree = new SortedCollection <string, TestData>(); var iA = new TestData(0); var iB = new TestData(1); iB.LoadAfter.Add("0"); tree.AddSorted(iB); tree.AddSorted(iA); tree.AddSorted(new TestData(2)); List <string> list = tree.GetSortedIndexList(); Console.WriteLine(ListToString(list)); Assert.AreEqual(3, list.Count); Assert.IsTrue(list.IndexOf("1") < list.IndexOf("0")); Assert.Pass(ListToString(list)); }
public void Contains_DescendingRandomAbsent_NotFound() { var random = new Random(DateTime.UtcNow.Millisecond); var input = Enumerable.Range(1, 10_000) .Select(n => random.Next(1, 1_000)) .ToArray(); var collection = new SortedCollection <int>(inAscendingOrder: false); collection.AddRange(input); var existing = new HashSet <int>(input); var absent = Enumerable.Range(1, 1_000) .Where(n => !existing.Contains(n)) .ToArray(); absent.Length.Should().BeGreaterThan(0); foreach (var n in absent) { collection.Contains(n).Should().BeFalse(); } collection.Should().BeInDescendingOrder(); }
public PackageFolder(string name, PackageFolder parent) : base(name, parent, parent.PackageViewModel) { Children = new SortedCollection<PackagePart>(); }
/// <summary> /// The constructor creates an emitter. /// </summary> public Emitter() { m_particleFactory = m_creator; m_particles = new List<Particle>(); m_behaviours = new SortedCollection<Behaviour>(this); m_initializers = new SortedCollection<Initializer>(this); m_activities = new SortedCollection<Activity>(this); m_counter = new ZeroCounter(); }
public void TestCleanup() { numbers = null; sortedCollection = null; }
public DiscCollection(ICollection <Disc> discs) { sortedDiscs = new(discs, Disc.DescendingPositionCounts); }
public void Collect(bool force) { try { int checkCount = 0; int deleteCount = 0; // Synchronize over the master data table source so no other threads // can interfere when we collect this information. lock (tableSource) { if (tableSource.IsClosed) return; // If root is locked, or has transaction changes pending, then we // can't delete any rows marked as deleted because they could be // referenced by transactions or result sets. if (force || (!tableSource.IsRootLocked && !tableSource.HasChangesPending)) { // lastSuccess = DateTime.Now; // lastTry = null; // Are we due a full sweep? if (fullSweep) { var rawRowCount = tableSource.RawRowCount; for (int i = 0; i < rawRowCount; ++i) { // Synchronized in dataSource. if (tableSource.HardCheckAndReclaimRow(i)) ++deleteCount; ++checkCount; } fullSweep = false; } else { // Are there any rows marked as deleted? var size = deletedRows.Count; if (size > 0) { // Go remove all rows marked as deleted. foreach (int rowIndex in deletedRows) { // Synchronized in dataSource. tableSource.HardRemoveRow(rowIndex); ++deleteCount; ++checkCount; } } deletedRows = new SortedCollection<SqlObject, long>(); } if (checkCount > 0) { // TODO: Emit the information to the system } } // if not roots locked and not transactions pending } // lock } catch (IOException) { // TODO: Log the error to the system } }
public CollectionSortBehavior() { SortedItems = new SortedCollection(); }
public void TearDown() { numbers = null; sortedCollection = null; }
public PackageFolder(string name, PackageViewModel viewModel) : base(name, null, viewModel) { Children = new SortedCollection<PackagePart>(); }
public void CleanRedundantLoadBefore_MultipleRedundancy_RedundanciesCleared() { var rng = new Random(); const string innerDependency = "mostUsedDependency"; const string outterDep1 = "redundant1"; const string outterDep2 = "redundant2"; const string outterDep3 = "redundant3"; var entries = new List <TestLoadBefore> { new TestLoadBefore(innerDependency), new TestLoadBefore(outterDep1, innerDependency), new TestLoadBefore(outterDep2, innerDependency, outterDep1), new TestLoadBefore(outterDep3, outterDep2, innerDependency), new TestLoadBefore("commonEntry1", innerDependency), new TestLoadBefore("commonEntry1a", outterDep2, innerDependency), new TestLoadBefore("commonEntry2", innerDependency, outterDep1), new TestLoadBefore("commonEntry2a", innerDependency, outterDep1, outterDep2), new TestLoadBefore("commonEntry3", innerDependency, outterDep2, outterDep3, outterDep1), new TestLoadBefore("commonEntry3a", innerDependency, outterDep1), new TestLoadBefore("commonEntry4"), new TestLoadBefore("commonEntry5", innerDependency, outterDep2), new TestLoadBefore("commonEntry5a", innerDependency, outterDep1, outterDep2, outterDep3), new TestLoadBefore("commonEntry6", innerDependency, outterDep1), new TestLoadBefore("commonEntry6a", innerDependency, outterDep2, outterDep1), new TestLoadBefore("commonEntry7", innerDependency), }; int originalCount = entries.Count; var tree = new SortedCollection <string, TestLoadBefore>(); // Add entries in random order while (entries.Count > 0) { int rngSelected = rng.Next(0, entries.Count); Assert.IsTrue(tree.AddSorted(entries[rngSelected])); entries.RemoveAt(rngSelected); } Assert.AreEqual(originalCount, tree.Count); tree.CleanRedundantLoadBefore(); Assert.AreEqual(originalCount, tree.Count); Assert.IsTrue(tree[outterDep1].LoadBefore.Contains(innerDependency)); Assert.IsTrue(tree[outterDep2].LoadBefore.Contains(outterDep1)); Assert.IsTrue(tree[outterDep3].LoadBefore.Contains(outterDep2)); Assert.IsFalse(tree["commonEntry1a"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry2"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry2a"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry2a"].LoadBefore.Contains(outterDep1)); Assert.IsFalse(tree["commonEntry3"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry3"].LoadBefore.Contains(outterDep1)); Assert.IsFalse(tree["commonEntry3"].LoadBefore.Contains(outterDep2)); Assert.IsFalse(tree["commonEntry3a"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry5"].LoadBefore.Contains(outterDep1)); Assert.IsFalse(tree["commonEntry5"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry5a"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry5a"].LoadBefore.Contains(outterDep1)); Assert.IsFalse(tree["commonEntry5a"].LoadBefore.Contains(outterDep2)); Assert.IsFalse(tree["commonEntry6a"].LoadBefore.Contains(innerDependency)); Assert.IsFalse(tree["commonEntry6a"].LoadBefore.Contains(outterDep1)); }
internal static int pagePoolSize = 0; // infine page pool static public void Main(System.String[] args) { int i; Storage db = StorageFactory.Instance.CreateStorage(); db.Open("testidx2.dbs", pagePoolSize); Root root = (Root)db.Root; if (root == null) { root = new Root(); #if USE_GENERICS root.strIndex = db.CreateSortedCollection <string, Record>(new StrRecordComparator(), true); root.intIndex = db.CreateSortedCollection <long, Record>(new IntRecordComparator(), true); #else root.strIndex = db.CreateSortedCollection(new StrRecordComparator(), true); root.intIndex = db.CreateSortedCollection(new IntRecordComparator(), true); #endif db.Root = root; } #if USE_GENERICS SortedCollection <long, Record> intIndex = root.intIndex; SortedCollection <string, Record> strIndex = root.strIndex; #else SortedCollection intIndex = root.intIndex; SortedCollection strIndex = root.strIndex; #endif DateTime start = DateTime.Now; long key = 1999; for (i = 0; i < nRecords; i++) { Record rec = new Record(); key = (3141592621L * key + 2718281829L) % 1000000007L; rec.intKey = key; rec.strKey = System.Convert.ToString(key); intIndex.Add(rec); strIndex.Add(rec); } db.Commit(); System.Console.WriteLine("Elapsed time for inserting " + nRecords + " records: " + (DateTime.Now - start)); start = System.DateTime.Now; key = 1999; for (i = 0; i < nRecords; i++) { key = (3141592621L * key + 2718281829L) % 1000000007L; #if USE_GENERICS Record rec1 = intIndex[key]; Record rec2 = strIndex[Convert.ToString(key)]; #else Record rec1 = (Record)intIndex[key]; Record rec2 = (Record)strIndex[Convert.ToString(key)]; #endif Debug.Assert(rec1 != null && rec1 == rec2); } System.Console.WriteLine("Elapsed time for performing " + nRecords * 2 + " index searches: " + (DateTime.Now - start)); start = System.DateTime.Now; key = Int64.MinValue; i = 0; foreach (Record rec in intIndex) { Debug.Assert(rec.intKey >= key); key = rec.intKey; i += 1; } Debug.Assert(i == nRecords); i = 0; String strKey = ""; foreach (Record rec in strIndex) { Debug.Assert(rec.strKey.CompareTo(strKey) >= 0); strKey = rec.strKey; i += 1; } Debug.Assert(i == nRecords); System.Console.WriteLine("Elapsed time for iteration through " + (nRecords * 2) + " records: " + (DateTime.Now - start)); Hashtable map = db.GetMemoryDump(); Console.WriteLine("Memory usage"); start = DateTime.Now; foreach (MemoryUsage usage in db.GetMemoryDump().Values) { Console.WriteLine(" " + usage.type.Name + ": instances=" + usage.nInstances + ", total size=" + usage.totalSize + ", allocated size=" + usage.allocatedSize); } Console.WriteLine("Elapsed time for memory dump: " + (DateTime.Now - start)); start = System.DateTime.Now; key = 1999; for (i = 0; i < nRecords; i++) { key = (3141592621L * key + 2718281829L) % 1000000007L; #if USE_GENERICS Record rec = intIndex[key]; #else Record rec = (Record)intIndex[key]; #endif intIndex.Remove(rec); strIndex.Remove(rec); rec.Deallocate(); } System.Console.WriteLine("Elapsed time for deleting " + nRecords + " records: " + (DateTime.Now - start)); db.Close(); }