public void SegmentGetNextItem()
        {
            var segment = Segment <long?, int> .Create(4);

            var traits = new SegmentTraits();

            long?[] items = new long?[] { 34L, 2L, 94L, 236L, 41L, 34928L, 222L, 345L, 123L, 1092L, 347L };

            for (int i = 0, end = items.Length; i != end; ++i)
            {
                long?oldItem;
                segment.InsertItem(ref items[i], out oldItem, traits);
            }

            int          iterator = -1;
            int          count    = 0;
            long?        storedItem;
            List <long?> itemsList = new List <long?>(items);

            while ((iterator = segment.GetNextItem(iterator, out storedItem, traits)) >= 0)
            {
                ++count;
                Assert.IsTrue(itemsList.Contains(storedItem), "Received unexpected item from GetNextItem.");
                itemsList.Remove(storedItem);
            }

            Assert.AreEqual(items.Length, count, "Unexpected number of items returned.");
        }
        public void SegmentGetOldest()
        {
            var segment = Segment <long?, int> .Create(4);

            var traits = new SegmentTraits();

            long?[] items = new long?[] { 34L, 2L, 94L, 236L, 41L, 34928L, 222L, 345L, 123L, 1092L, 347L };

            for (int i = 0, end = items.Length; i != end; ++i)
            {
                long?oldItem;
                Assert.IsFalse(segment.GetOldestItem(ref items[i], out oldItem, traits), "Unique keys, should not expect an older item.");
                Assert.AreEqual(items[i], oldItem, "Should receive newly inserted item.");
            }

            for (int i = 0, end = items.Length; i != end; ++i)
            {
                long?oldItem;
                Assert.IsTrue(segment.InsertItem(ref items[i], out oldItem, traits), "Duplicate keys, expect older items");
                Assert.AreEqual(items[i], oldItem, "Should receive correct older item.");
            }

            Assert.AreEqual(
                segment._Count,
                items.Length,
                "Number of items in segment doesn't match number of unique inserts"
                );
        }
        public void SegmentFindItem()
        {
            var segment = Segment <long?, int> .Create(4);

            var traits = new SegmentTraits();

            long?[] items = new long?[] { 34L, 2L, 94L, 236L, 41L, 34928L, 222L, 345L, 123L, 1092L, 347L };

            for (int i = 0, end = items.Length; i != end; ++i)
            {
                long?oldItem;
                Assert.IsFalse(segment.InsertItem(ref items[i], out oldItem, traits), "Unique keys, should not expect a replace.");
            }

            int[] searchKeys = new int[] { 2, 34928, 1092, 222, 94, 34, 347, 123, 41, 345 };

            for (int i = 0, end = searchKeys.Length; i != end; ++i)
            {
                long?foundItem;
                Assert.IsTrue(segment.FindItem(ref searchKeys[i], out foundItem, traits), "Expected to find an item.");
                Assert.AreEqual(foundItem, (long?)searchKeys[i], "Found item not expected value.");
            }

            int[] searchKeys2 = new int[] { 12, 0, 13, 55, 1000, 10, 20, 124, 42, 345345 };

            for (int i = 0, end = searchKeys2.Length; i != end; ++i)
            {
                long?foundItem;
                Assert.IsFalse(segment.FindItem(ref searchKeys2[i], out foundItem, traits), "Expected not to find an item.");
            }
        }
        public void SegmentClear()
        {
            var segment = Segment <long?, int> .Create(4);

            var traits = new SegmentTraits();

            long?[] items = new long?[] { 34L, 2L, 94L, 236L, 41L, 34928L, 222L, 345L, 123L, 1092L, 347L };

            for (int i = 0, end = items.Length; i != end; ++i)
            {
                long?oldItem;
                segment.InsertItem(ref items[i], out oldItem, traits);
            }

            segment.Clear(traits);

            Assert.AreEqual(segment._Count, 0, "Expected segment to be empty after Clear().");

            int[] searchKeys = new int[] { 2, 34928, 1092, 222, 94, 34, 347, 123, 41, 345 };

            for (int i = 0, end = searchKeys.Length; i != end; ++i)
            {
                long?foundItem;
                Assert.IsFalse(segment.FindItem(ref searchKeys[i], out foundItem, traits), "Expected not to find an item after Clear().");
            }
        }
        public void SegmentInserts()
        {
            var segment = Segment <long?, int> .Create(4);

            var traits = new SegmentTraits();

            long?[] items = new long?[] { 34L, 2L, 94L, 236L, 41L, 34928L, 222L, 345L, 123L, 1092L, 347L };

            for (int i = 0, end = items.Length; i != end; ++i)
            {
                long?oldItem;
                bool res = segment.InsertItem(ref items[i], out oldItem, traits);
                Assert.IsFalse(res, "Unique inserts in empty table should return false.");
            }

            Assert.AreEqual(
                items.Length,
                segment._Count,
                "Number of items in segment doesn't match number of inserts"
                );

            var  it = -1;
            long?item;

            while ((it = segment.GetNextItem(it, out item, traits)) >= 0)
            {
                Assert.IsTrue(
                    items.Contains(item),
                    "Item not present in inserted items collection."
                    );
            }
        }