Exemplo n.º 1
0
        public static void TestIndex()
        {
            var d = new ImmutableSortedDictionary <int, int>();
            var n = 1000;

            Debug.Assert(!d.ContainsKey(0));
            for (int k = 0; k < n; k += 1)
            {
                d = d.Add(k, k * 2);
            }
            Debug.Assert(d.GetIndexStartWithKey(-1) == 0);
            Debug.Assert(d.GetIndexStartWithKey(0) == 0);
            Debug.Assert(d.GetIndexStartWithKey(100) == 100);
            Debug.Assert(d.GetIndexStartWithKey(1000) == 1000);
            Debug.Assert(d.GetIndexStartWithKey(1001) == 1000);
            Debug.Assert(d.GetIndexEndWithKey(-1) == -1);
            Debug.Assert(d.GetIndexEndWithKey(0) == 0);
            Debug.Assert(d.GetIndexEndWithKey(100) == 100);
            Debug.Assert(d.GetIndexEndWithKey(1000) == 999);
            Debug.Assert(d.GetIndexEndWithKey(1001) == 999);
            d = d.Remove(500);
            Debug.Assert(d.GetIndexStartWithKey(500) == 500);
            Debug.Assert(d.GetIndexEndWithKey(500) == 499);
            {
                int k = 100;
                foreach (var p in d.Range(50, 250, 50, 50))
                {
                    Debug.Assert(p.Key == k);
                    k += 1;
                }
            }
        }
Exemplo n.º 2
0
        public static void TestRandomAdd()
        {
            var d = new ImmutableSortedDictionary <int, int>();
            var n = 1009; //Prime

            Debug.Assert(!d.ContainsKey(0));
            for (int k = 0; k < n; k += 1)
            {
                var v = (k * 7 + 11) % n;
                d = d.Add(v, v * 2);
            }
            for (int k = 0; k < n; k += 1)
            {
                Debug.Assert(d.ContainsKey(k));
                Debug.Assert(d.TryGetValue(k) == k * 2);
            }
            for (int k = 0; k < n; k += 1)
            {
                var p = d.TryGetPairByIndex(k);
                Debug.Assert(p.Value.Key == k);
                Debug.Assert(p.Value.Value == k * 2);
            }
            Debug.Assert(d.TryGetMin().Value == 0);
            Debug.Assert(d.TryGetMax().Value == (n - 1) * 2);
            Debug.Assert(!d.ContainsKey(-1));
            Debug.Assert(d.Count == n);
            {
                var k = 0;
                foreach (var p in d.Range(Optional <int> .Empty, Optional <int> .Empty))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k += 1;
                }
            }
            {
                var k = n - 1;
                foreach (var p in d.RangeReversed(Optional <int> .Empty, Optional <int> .Empty))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k -= 1;
                }
            }
            {
                var k = 0;
                foreach (var p in d.RangeByIndex(Optional <int> .Empty, Optional <int> .Empty))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k += 1;
                }
            }
            {
                var k = n - 1;
                foreach (var p in d.RangeByIndexReversed(Optional <int> .Empty, Optional <int> .Empty))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k -= 1;
                }
            }
            {
                var k = 100;
                foreach (var p in d.Range(100, 200))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k += 1;
                }
            }
            {
                var k = 200;
                foreach (var p in d.RangeReversed(100, 200))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k -= 1;
                }
            }
            {
                var k = 100;
                foreach (var p in d.RangeByIndex(100, 200))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k += 1;
                }
            }
            {
                var k = 200;
                foreach (var p in d.RangeByIndexReversed(100, 200))
                {
                    Debug.Assert(p.Key == k);
                    Debug.Assert(p.Value == k * 2);
                    k -= 1;
                }
            }
            Debug.Assert(d.RangeCount(100, 200) == 101);
            Debug.Assert(d.RangeCount(0, n - 1) == n);
            Debug.Assert(d.RangeCount(0, Optional <int> .Empty) == n);
            Debug.Assert(d.RangeCount(Optional <int> .Empty, n - 1) == n);
            Debug.Assert(d.RangeCount(Optional <int> .Empty, Optional <int> .Empty) == n);
            var l = d.ToList();

            Debug.Assert(l.Count == n);
            for (int k = 0; k < n; k += 1)
            {
                var p = l[k];
                Debug.Assert(p.Key == k);
                Debug.Assert(p.Value == k * 2);
            }
            for (int k = 0; k < n; k += 1)
            {
                d = d.SetItem(k, k * 3);
            }
            for (int k = 0; k < n; k += 1)
            {
                Debug.Assert(d.ContainsKey(k));
                Debug.Assert(d.TryGetValue(k) == k * 3);
            }
            for (int k = 0; k < n; k += 1)
            {
                var v = (k * 11 + 7) % n;
                d = d.Remove(v);
            }
            Debug.Assert(d.Count == 0);
            var l2 = d.ToList();

            Debug.Assert(l2.Count == 0);
        }