public void TestDedupFirst()
        {
            AllEqual[]      set = new[] { new AllEqual(), new AllEqual(), new AllEqual() };
            List <AllEqual> arr = new List <AllEqual>(
                OrderedEnumeration <AllEqual> .WithDuplicateHandling(
                    set, Comparer <AllEqual> .Default, DuplicateHandling.FirstValueWins));

            Assert.AreEqual(1, arr.Count);
            Assert.IsTrue(ReferenceEquals(set[0], arr[0]));

            arr = new List <AllEqual>(
                OrderedEnumeration <AllEqual> .WithDuplicateHandling(
                    set, Comparer <AllEqual> .Default, DuplicateHandling.LastValueWins));
            Assert.AreEqual(1, arr.Count);
            Assert.IsTrue(ReferenceEquals(set[2], arr[0]));

            arr = new List <AllEqual>(
                OrderedEnumeration <AllEqual> .WithDuplicateHandling(
                    set, Comparer <AllEqual> .Default, DuplicateHandling.None));
            Assert.AreEqual(3, arr.Count);
            Assert.IsTrue(ReferenceEquals(set[0], arr[0]));
            Assert.IsTrue(ReferenceEquals(set[1], arr[1]));
            Assert.IsTrue(ReferenceEquals(set[2], arr[2]));

            try
            {
                new List <AllEqual>(
                    OrderedEnumeration <AllEqual> .WithDuplicateHandling(
                        set, Comparer <AllEqual> .Default, DuplicateHandling.RaisesException));
                Assert.Fail();
            }
            catch (ArgumentException) { }
        }
        public void TestOrderedEnumPaginatedCleanup()
        {
            byte[] input = new byte[512];
            new Random().NextBytes(input);
            var ordered = new OrderedEnumeration <byte>(input);

            ordered.Serializer        = PrimitiveSerializer.Byte;
            ordered.InMemoryLimit     = 10;
            ordered.DuplicateHandling = DuplicateHandling.FirstValueWins;

            using (var e = ordered.GetEnumerator())
                Assert.IsTrue(e.MoveNext());
        }
        public void TestEnumTwiceFails()
        {
            var ordered = new OrderedEnumeration <byte>(new byte[0]);

            using (var e = ordered.GetEnumerator())
                Assert.IsFalse(e.MoveNext());

            try
            {
                ((System.Collections.IEnumerable)ordered).GetEnumerator();
                Assert.Fail();
            }
            catch (InvalidOperationException) { }
        }
        public void TestOrderedEnumProperties()
        {
            var ordered = new OrderedEnumeration <byte>(Comparer <byte> .Default, FailBeforeYield <byte>(true));

            Assert.IsTrue(ReferenceEquals(Comparer <byte> .Default, ordered.Comparer));
            ordered.Comparer = new ReverseOrder <byte>(ordered.Comparer);
            Assert.IsTrue(ordered.Comparer is ReverseOrder <byte>);

            Assert.IsNull(ordered.Serializer);
            ordered.Serializer = PrimitiveSerializer.Byte;
            Assert.IsTrue(ReferenceEquals(ordered.Serializer, PrimitiveSerializer.Byte));

            Assert.AreEqual(0x10000, ordered.InMemoryLimit);
            Assert.AreEqual(10, ordered.InMemoryLimit = 10);

            Assert.AreEqual(DuplicateHandling.None, ordered.DuplicateHandling);
            Assert.AreEqual(DuplicateHandling.FirstValueWins,
                            ordered.DuplicateHandling = DuplicateHandling.FirstValueWins);
        }
        public void TestOrderedEnumDedup()
        {
            byte[] input = new byte[512];
            new Random().NextBytes(input);
            var ordered = new OrderedEnumeration <byte>(input);

            ordered.InMemoryLimit     = 10;
            ordered.DuplicateHandling = DuplicateHandling.FirstValueWins;

            int last = -1, count = 0;

            byte[] test = new List <byte>(ordered).ToArray();
            foreach (byte b in test)
            {
                count++;
                Assert.IsTrue(last < b);
                last = b;
            }
            Assert.IsTrue(count <= 256);
        }
        public void TestMergeEnumerations()
        {
            char[] x = "aeiou".ToCharArray();
            char[] y = "bcdfg".ToCharArray();
            char[] z = "ez".ToCharArray();

            var order = OrderedEnumeration <char> .Merge(x, y, z);

            Assert.AreEqual("abcdeefgiouz", new string(new List <char>(order).ToArray()));

            order = OrderedEnumeration <char> .Merge(Comparer <char> .Default, DuplicateHandling.LastValueWins, x, y, z);

            Assert.AreEqual("abcdefgiouz", new string(new List <char>(order).ToArray()));

            order = OrderedEnumeration <char> .Merge(Comparer <char> .Default, x, y);

            order = OrderedEnumeration <char> .WithDuplicateHandling(order, Comparer <char> .Default,
                                                                     DuplicateHandling.FirstValueWins);

            Assert.AreEqual("abcdefgiou", new string(new List <char>(order).ToArray()));
        }
        public void TestEnumInvalid()
        {
            var order = new OrderedEnumeration <byte>(new byte[1]);

            System.Collections.IEnumerator e = ((System.Collections.IEnumerable)order).GetEnumerator();
            Assert.IsTrue(e.MoveNext());
            Assert.IsFalse(e.MoveNext());
            try
            {
                object val = e.Current;
                GC.KeepAlive(val);
                Assert.Fail();
            }
            catch (InvalidOperationException) { }

            try
            {
                e.Reset();
                Assert.Fail();
            }
            catch (NotSupportedException) { }
        }
 public void TestUnorderedAssertion()
 {
     new List <int>(OrderedEnumeration <int> .WithDuplicateHandling(
                        new [] { 2, 1 }, Comparer <int> .Default, DuplicateHandling.RaisesException));
 }