示例#1
0
        public static void TestKey()
        {
            var d = new ImmutableSortedDictionary <Key, int>(new KeyComparer(ConcurrentComparer.AsObjectComparer(ConcurrentComparer.CreateDefault <Byte[]>()), ConcurrentComparer.AsObjectComparer(ConcurrentComparer.CreateDefault <Optional <int> >()), ConcurrentComparer.AsObjectComparer(ConcurrentComparer.CreateDefault <String>())));

            d = d.Add(new Key(null, Optional <int> .Empty, "A"), 0);
            d = d.Add(new Key(null, Optional <int> .Empty, "B"), 1);
            d = d.Add(new Key(null, Optional <int> .Empty, "aA"), 2);
            d = d.Add(new Key(null, Optional <int> .Empty, "aB"), 3);
            d = d.Add(new Key(null, (Optional <int>)(1), "aA"), 4);
            d = d.Add(new Key(null, (Optional <int>)(1), "aB"), 5);
            d = d.Add(new Key(null, (Optional <int>)(2), "A"), 6);
            d = d.Add(new Key(null, (Optional <int>)(2), "B"), 7);
            d = d.Add(new Key(new Byte[] { }, (Optional <int>)(1), "A"), 8);
            d = d.Add(new Key(new Byte[] { 1 }, (Optional <int>)(1), "A"), 9);
            d = d.Add(new Key(new Byte[] { 1, 2 }, (Optional <int>)(1), "A"), 10);
            d = d.Add(new Key(new Byte[] { 1, 3 }, (Optional <int>)(1), "A"), 11);
            d = d.Add(new Key(new Byte[] { 2, 1 }, (Optional <int>)(1), "A"), 12);
            foreach (var v in d.Zip(Enumerable.Range(0, d.Count), (a, b) => new { Key = a.Key, Left = a.Value, Right = b }))
            {
                Debug.Assert(v.Left == v.Right);
            }
            Debug.Assert(d.RangeCount(new Key(null, KeyCondition.Min, KeyCondition.Min), new Key(null, KeyCondition.Max, KeyCondition.Max)) == 8);
            Debug.Assert(d.RangeCount(new Key(new Byte[] { }, KeyCondition.Min, KeyCondition.Min), new Key(new Byte[] { }, KeyCondition.Max, KeyCondition.Max)) == 1);
            Debug.Assert(d.RangeCount(new Key(new Byte[] { }, KeyCondition.Min, KeyCondition.Min), new Key(new Byte[] { 2, 1 }, KeyCondition.Max, KeyCondition.Max)) == 5);
        }
示例#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);
        }