public void RemoveRangeByRank() { for (var _ = 0; _ < 100; _++) { var list = new SortSet <int, int>(); for (var i = 0; i < 10; i++) { list.Add(i, i); } list.RemoveRangeByRank(3, 8); Assert.AreEqual(0, list.GetElementByRank(0)); Assert.AreEqual(1, list.GetElementByRank(1)); Assert.AreEqual(2, list.GetElementByRank(2)); Assert.AreEqual(9, list.GetElementByRank(3)); for (var i = 3; i < 9; i++) { list.Add(i, i); } list.Add(33, 3); list.RemoveRangeByRank(3, 3); Assert.AreEqual(0, list.GetElementByRank(0)); Assert.AreEqual(1, list.GetElementByRank(1)); Assert.AreEqual(2, list.GetElementByRank(2)); var r = list.GetElementByRank(3); Assert.AreEqual(true, r == 3 || r == 33); } }
public void OverrideElement() { var master = new SortSet <int, int>(); master.Add(10, 100); master.Add(10, 200); Assert.AreEqual(200, master.GetScore(10)); }
public void RemoveNotExistsElementTest() { var master = new SortSet <int, int>(0.25, 32); master.Add(10, 10); master.Add(90, 10); master.Add(20, 10); master.Add(80, 10); if (master.Remove(8888)) { Assert.Fail(); } }
/// <summary> /// 创建一个计时器 /// </summary> /// <param name="task">计时器执行的任务</param> /// <returns>计时器</returns> public ITimer Make(Action task = null) { var withGroupStack = timerQueue.Count > 0; var queue = withGroupStack ? timerQueue.Peek() : new TimerQueue(timeManager.Default); var timer = new Timer(queue, task); queue.Add(timer); if (!withGroupStack) { executeList.Add(queue, int.MaxValue); } return(timer); }
public void RemoveTest() { int num = 50000; var list = new SortSet <int, int>(); var lst = new List <int>(); var rand = new System.Random(); for (var i = 0; i < num; i++) { var val = rand.Next(); lst.Add(val); list.Add(val, val); } foreach (int n in lst) { list.Remove(n); } foreach (var val in list) { Assert.Fail(); if (val < 0) { } } if (list.Count != 0) { Assert.Fail(); } }
public void GetScoreTest() { var master = new SortSet <int, int>(); master.Add(10, 100); Assert.AreEqual(100, master.GetScore(10)); }
public void AddElementTest() { //var list2 = new SortSet<object, int>(); int num = 50000; var list = new SortSet <int, int>(); var rand = new System.Random(); for (var i = 0; i < num; i++) { var val = rand.Next(); list.Add(val, val); //list.Add(i,i); } var max = 0; foreach (var val in list) { if (max <= val) { max = val; } else { Assert.Fail(); } } }
public void TestRandValue() { var sortSets = new SortSet <int, int>(); var random = new Random(); for (var i = 1000; i >= 1; i--) { sortSets.Add(i, random.Next(0, 1000)); } for (var i = 1; i <= 1000; i++) { if (sortSets.Remove(i)) { continue; } Assert.Fail("can not remove i : " + i); } Assert.AreEqual(0, sortSets.Count); foreach (var sortSet in sortSets) { Assert.Fail(); } }
public void FirstLastTest() { var master = new SortSet <int, int>(0.25, 32); for (var i = 0; i < 65536; i++) { master.Add(i, i); } Assert.AreEqual(0, master.First()); Assert.AreEqual(65535, master.Last()); for (var i = 0; i < 65536; i++) { Assert.AreEqual(i, master.First()); Assert.AreEqual(i, master.Shift()); } Assert.AreEqual(0, master.Count); master = new SortSet <int, int>(0.25, 32); for (var i = 0; i < 65536; i++) { master.Add(i, i); } for (var i = 0; i < 65536; i++) { Assert.AreEqual(65535 - i, master.Last()); Assert.AreEqual(65535 - i, master.Pop()); } }
public void TestCustomComparer() { var foo = new SortSet <object, int>(new PriorityComparer()); for (var i = 0; i < 10; i++) { foo.Add(i, i); } for (var i = 9; i >= 0; i--) { Assert.AreEqual(i, foo.Shift()); } foo = new SortSet <object, int>(); for (var i = 0; i < 10; i++) { foo.Add(i, i); } for (var i = 0; i < 10; i++) { Assert.AreEqual(i, foo.Shift()); } }
public void GetRankTest() { for (var _ = 0; _ < 100; _++) { var num = 100; var list = new SortSet <int, int>(); var lst = new List <int>(); var rand = new System.Random(); for (var i = 0; i < num; i++) { if (rand.NextDouble() < 0.1) { lst.Add(i); } list.Add(i, i); } foreach (var n in lst) { if (list.GetRank(n) != n) { Assert.Fail(); } } Assert.AreEqual(-1, list.GetRank(-1)); } }
/// <summary> /// 记录一个日志 /// </summary> /// <param name="entry">日志条目</param> internal void Log(ILogEntry entry) { while (logEntrys.Count >= maxLogEntrys) { logEntrys.Shift(); } logEntrys.Add(entry, entry.Id); }
public void GetRankOverflowTest() { var master = new SortSet <int, int>(); master.Add(10, 100); Assert.AreEqual(-1, master.GetRank(100)); }
public void TestMaxLevelLimit() { sortset = new SortSet <int, int>(0.5, 3); for (var i = 0; i < 2048; i++) { sortset.Add(i, i); } }
public void SameScoreTest() { //根据有序集规则,后插入的相同分数将会被优先遍历到 var master = new SortSet <int, int>(0.25, 32); master.Add(10, 10); master.Add(90, 10); master.Add(20, 10); master.Add(80, 10); var a = master.ToArray(); Assert.AreEqual(80, a[0]); Assert.AreEqual(20, a[1]); Assert.AreEqual(90, a[2]); Assert.AreEqual(10, a[3]); }
public void ContainsTest() { var master = new SortSet <int, int>(); master.Add(10, 100); Assert.AreEqual(true, master.Contains(10)); Assert.AreEqual(false, master.Contains(11)); }
/// <summary> /// 当查找类型无法找到时会尝试去调用开发者提供的查找类型函数 /// </summary> /// <param name="finder">查找类型的回调</param> /// <param name="priority">查询优先级(值越小越优先)</param> /// <returns>当前容器实例</returns> public IContainer OnFindType(Func <string, Type> finder, int priority = int.MaxValue) { Guard.NotNull(finder, "finder"); lock (syncRoot) { findType.Add(finder, priority); } return(this); }
public void MaxLevelLimitTest() { var master = new SortSet <int, int>(0.5, 3); for (var i = 0; i < 65536; i++) { master.Add(i, i); } }
public void GetRevRank() { var list = new SortSet <int, int>(); for (int i = 0; i < 10; i++) { list.Add(i, i); } Assert.AreEqual(6, list.GetRevRank(3)); }
public void GetElementByRankOverflowTest() { var master = new SortSet <int, int>(); master.Add(10, 10); ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => { master.GetElementByRank(1000); }); }
public void GetElementByRevRank() { var list = new SortSet <int, int>(); for (var i = 0; i < 10; i++) { list.Add(i, i); } Assert.AreEqual(6, list.GetElementByRevRank(3)); Assert.AreEqual(9, list.GetElementByRevRank(0)); Assert.AreEqual(0, list.GetElementByRevRank(9)); }
/// <summary> /// 初始化主线程组 /// </summary> /// <param name="abs"></param> private void InitMainThreadGroup(IABSystem abs) { var driver = App.Make <IMonoDriver>(); driver.StartCoroutine(CheckMethod(abs.CheckUnusedBundle)); var set = new SortSet <IUpdate, int>(); set.Add((IUpdate)abs, 1); driver.Attach(set); abs.Init(null); }
public void ClearTest() { var master = new SortSet <int, int>(0.25, 32); for (var i = 0; i < 65536; i++) { master.Add(i, i); } master.Clear(); for (var i = 0; i < 65536; i++) { master.Add(i, i); } for (var i = 0; i < 65536; i++) { Assert.AreEqual(i, master.GetRank(i)); } Assert.AreEqual(65536, master.Count); }
public void TestRank() { var n = 100; while (n-- > 0) { var sortSets = new SortSet <int, int>(); sortSets.Add(1000, 85); sortSets.Add(999, 75); sortSets.Add(998, 185); sortSets.Add(997, 85); sortSets.Add(996, 185); sortSets.Add(995, 85); Assert.AreEqual(1, sortSets.GetRank(995)); Assert.AreEqual(995, sortSets.GetElementByRank(1)); Assert.AreEqual(997, sortSets.GetElementByRank(2)); Assert.AreEqual(1000, sortSets.GetElementByRank(3)); Assert.AreEqual(996, sortSets.GetElementByRank(4)); Assert.AreEqual(998, sortSets.GetElementByRank(5)); var i = 100; var faild = 0; while (i-- > 0) { Assert.AreEqual(3, sortSets.GetRangeCount(80, 90)); } Console.WriteLine(faild); } }
/// <summary> /// 转换到指定目标并且添加 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="sortset">有序集</param> /// <param name="obj">对象</param> /// <param name="function">获取优先级的函数名</param> /// <returns>是否成功</returns> private bool ConvertAndAdd <T>(SortSet <T, int> sortset, object obj, string function) where T : class { T t = obj as T; bool flag = t != null; if (flag) { sortset.Add(t, 0); } return(flag); }
public void ToArray() { var master = new SortSet <int, int>(0.25, 32); master.Add(10, 10); master.Add(90, 90); master.Add(20, 20); master.Add(80, 80); master.Add(0, 0); master.Add(40, 40); master.Add(50, 50); master.Add(60, 60); master.Add(30, 30); master.Add(70, 70); var i = 0; foreach (var e in master.ToArray()) { Assert.AreEqual(i++ *10, e); } }
public void TestAddObject() { var foo = new SortSet <object, int>(); var collection = new List <object>(); for (var i = 0; i < 10; i++) { var obj = new object(); collection.Add(obj); foo.Add(obj, i); } CollectionAssert.AreEqual(collection, foo.ToArray()); }
/// <summary> /// 头部推出测试 /// </summary> public void ShiftTest() { var master = new SortSet <int, int>(0.25, 32); for (var i = 0; i < 100; i++) { master.Add(i, i); } for (var i = 0; i < 65536; i++) { Assert.AreEqual(i, master.Shift()); } Assert.AreEqual(0, master.Count); }
public void RemoveRangeByScore() { var list = new SortSet <int, int>(); for (var i = 0; i < 10; i++) { list.Add(i, i); } list.RemoveRangeByScore(3, 8); Assert.AreEqual(0, list.GetElementByRank(0)); Assert.AreEqual(1, list.GetElementByRank(1)); Assert.AreEqual(2, list.GetElementByRank(2)); Assert.AreEqual(9, list.GetElementByRank(3)); for (var i = 3; i < 9; i++) { list.Add(i, i); } list.Add(33, 3); list.RemoveRangeByScore(3, 3); Assert.AreEqual(0, list.GetElementByRank(0)); Assert.AreEqual(1, list.GetElementByRank(1)); Assert.AreEqual(2, list.GetElementByRank(2)); Assert.AreEqual(4, list.GetElementByRank(3)); }
public void BoundTestScoreRangeCount() { var list = new SortSet <int, int>(); list.Add(6, 6); Assert.AreEqual(1, list.GetRangeCount(0, 100)); Assert.AreEqual(0, list.GetRangeCount(7, 100)); Assert.AreEqual(0, list.GetRangeCount(0, 5)); Assert.AreEqual(1, list.GetRangeCount(6, 100)); ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => { Assert.AreEqual(0, list.GetRangeCount(800, 100)); }); }