コード例 #1
0
        public void Bst()
        {
            var n        = 100000;
            var a        = RandomHelper.CreateData(n);
            var expected = (int[])a.Clone();

            var actual = new List <int>();
            var q      = new BstPriorityQueue <int>();

            foreach (var x in a)
            {
                q.Push(x);
            }
            while (q.Count > 0)
            {
                actual.Add(q.Pop());
            }

            Array.Sort(expected);
            CollectionAssert.AreEqual(expected, actual);

            actual.Clear();
            q = new BstPriorityQueue <int>(ComparerHelper <int> .Create(true));
            foreach (var x in a)
            {
                q.Push(x);
            }
            while (q.Count > 0)
            {
                actual.Add(q.Pop());
            }

            Array.Reverse(expected);
            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #2
0
        public void Keyed_1()
        {
            var n = 100000;
            var a = RandomHelper.CreateData(n);

            var actual = new List <int>();
            var q      = new KeyedPriorityQueue <int, int>(x => x / 10);

            foreach (var x in a)
            {
                q.Push(x);
            }
            while (q.Count > 0)
            {
                actual.Add(q.Pop());
            }

            var expected = a.OrderBy(x => x / 10).ToArray();

            CollectionAssert.AreEqual(expected, actual);

            actual.Clear();
            q = new KeyedPriorityQueue <int, int>(x => x / 10, ComparerHelper <int> .Create(true));
            foreach (var x in a)
            {
                q.Push(x);
            }
            while (q.Count > 0)
            {
                actual.Add(q.Pop());
            }

            expected = a.OrderByDescending(x => x / 10).ToArray();
            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void CommonComparer_Test()
        {
            var testers        = Generate();
            var testersOrdered = testers.OrderBy(m => m.Id).ToArray();
            var comparer       = ComparerHelper.Create <Tester>((x, y) => Comparer <int> .Default.Compare(x.Id, y.Id));
            var sortList       = new SortedSet <Tester>(testers, comparer);

            Assert.True(testersOrdered.SequenceEqual(sortList));
        }
コード例 #4
0
        public void KeyComparer_Test()
        {
            var testers        = Generate();
            var testersOrdered = testers.OrderBy(m => m.Id).ToArray();
            var comparer       = ComparerHelper.Create <Tester, int>(m => m.Id);
            var sortList       = new SortedSet <Tester>(testers, comparer);

            Assert.True(testersOrdered.SequenceEqual(sortList));
        }
コード例 #5
0
        public void Create()
        {
            var n        = 100000;
            var expected = RandomHelper.CreateData(n);
            var actual   = (int[])expected.Clone();

            Array.Sort(expected);
            Array.Sort(actual, ComparerHelper <int> .Create());
            CollectionAssert.AreEqual(expected, actual);

            Array.Reverse(expected);
            Array.Sort(actual, ComparerHelper <int> .Create(true));
            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #6
0
        public void Create_Key2()
        {
            var n      = 100000;
            var a      = RandomHelper.CreateData(n);
            var actual = (int[])a.Clone();

            var expected = a.OrderBy(x => x.ToString().Length).ThenByDescending(x => x).ToArray();

            Array.Sort(actual, ComparerHelper <int> .Create(x => x.ToString().Length, false, x => x, true));
            CollectionAssert.AreEqual(expected, actual);

            Array.Reverse(expected);
            Array.Sort(actual, ComparerHelper <int> .Create(x => x.ToString().Length, true, x => x, false));
            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #7
0
        public void Create_Key1()
        {
            var n      = 100000;
            var a      = RandomHelper.CreateData(n);
            var actual = (int[])a.Clone();

            var expected = a.OrderBy(x => x.ToString()).ToArray();

            Array.Sort(actual, ComparerHelper <int> .Create(x => x.ToString()));
            CollectionAssert.AreEqual(expected, actual);

            Array.Reverse(expected);
            Array.Sort(actual, ComparerHelper <int> .Create(x => x.ToString(), true));
            CollectionAssert.AreEqual(expected, actual);
        }