예제 #1
0
        public void Can_use_int_key_tree_to_represent_general_HashTree_with_possible_hash_conflicts()
        {
            var tree = ImMapSlots.CreateWithEmpty <KeyValuePair <Type, string>[]>();

            var key     = typeof(ImMapSlotsTests);
            var keyHash = key.GetHashCode();
            var value   = "test";

            KeyValuePair <Type, string>[] Update(int _, KeyValuePair <Type, string>[] oldValue, KeyValuePair <Type, string>[] newValue)
            {
                var newItem      = newValue[0];
                var oldItemCount = oldValue.Length;

                for (var i = 0; i < oldItemCount; i++)
                {
                    if (oldValue[i].Key == newItem.Key)
                    {
                        var updatedItems = new KeyValuePair <Type, string> [oldItemCount];
                        Array.Copy(oldValue, updatedItems, updatedItems.Length);
                        updatedItems[i] = newItem;
                        return(updatedItems);
                    }
                }

                var addedItems = new KeyValuePair <Type, string> [oldItemCount + 1];

                Array.Copy(oldValue, addedItems, addedItems.Length);
                addedItems[oldItemCount] = newItem;
                return(addedItems);
            }

            tree.AddOrUpdate(keyHash, new[] { new KeyValuePair <Type, string>(key, value) }, Update);
            tree.AddOrUpdate(keyHash, new[] { new KeyValuePair <Type, string>(key, value) }, Update);

            string result = null;

            var items = tree[keyHash & ImMapSlots.KEY_MASK_TO_FIND_SLOT].GetValueOrDefault(keyHash);

            if (items != null)
            {
                var firstItem = items[0];
                if (firstItem.Key == key)
                {
                    result = firstItem.Value;
                }
                else if (items.Length > 1)
                {
                    for (var i = 1; i < items.Length; i++)
                    {
                        if (items[i].Key == key)
                        {
                            result = items[i].Value;
                            break;
                        }
                    }
                }
            }

            Assert.AreEqual("test", result);
        }
예제 #2
0
        public void Search_for_non_existent_key_should_NOT_throw_TryFind()
        {
            var tree = ImMapSlots.CreateWithEmpty <int>();

            tree.AddOrUpdate(1, 1);
            tree.AddOrUpdate(3, 2);

            Assert.IsFalse(tree[2].TryFind(2, out _));
        }
예제 #3
0
        public void Search_for_non_existent_key_should_NOT_throw()
        {
            var tree = ImMapSlots.CreateWithEmpty <int>();

            tree.AddOrUpdate(1, 1);
            tree.AddOrUpdate(3, 2);

            Assert.AreEqual(0, tree[2].GetValueOrDefault(2));
        }
예제 #4
0
        public void Test_that_all_added_values_are_accessible()
        {
            var t = ImMapSlots.CreateWithEmpty <int>();

            t.AddOrUpdate(1, 11);
            t.AddOrUpdate(2, 22);
            t.AddOrUpdate(3, 33);

            Assert.AreEqual(11, t[1].GetValueOrDefault(1));
            Assert.AreEqual(22, t[2].GetValueOrDefault(2));
            Assert.AreEqual(33, t[3].GetValueOrDefault(3));
        }
예제 #5
0
        public void ImMapSlots_Folded_values_should_be_returned_in_sorted_order()
        {
            var items = Enumerable.Range(0, 10).ToArray();
            var tree  = items.Aggregate(ImMapSlots.CreateWithEmpty <int>(), (t, i) => t.Do(x => x.AddOrUpdate(i, i)));

            var list = tree.Fold(new List <int>(), (data, l) =>
            {
                l.Add(data.Value);
                return(l);
            });

            CollectionAssert.AreEqual(items, list);
        }
예제 #6
0
        public void Update_with_not_found_key_should_return_the_same_tree()
        {
            var maps = ImMapSlots.CreateWithEmpty <string>();

            maps.AddOrUpdate(1, "a");
            maps.AddOrUpdate(2, "b");
            maps.AddOrUpdate(3, "c");
            maps.AddOrUpdate(4, "d");

            var x = maps[5];

            maps.Update(5, "e");

            Assert.AreSame(x, maps[5]);
        }
예제 #7
0
        public void Update_to_null_and_then_to_value_should_remove_null()
        {
            var maps = ImMapSlots.CreateWithEmpty <string>();

            maps.AddOrUpdate(1, "a");
            maps.AddOrUpdate(2, "b");
            maps.AddOrUpdate(3, "c");
            maps.AddOrUpdate(4, "d");

            Assert.AreEqual("d", maps[4].GetValueOrDefault(4));

            maps.Update(4, null);
            Assert.IsNull(maps[4].GetValueOrDefault(4));

            maps.Update(4, "X");
        }
예제 #8
0
        public void Search_in_empty_tree_should_NOT_throw_TryFind()
        {
            var maps = ImMapSlots.CreateWithEmpty <int>();

            Assert.IsFalse(maps[0].TryFind(0, out _));
        }
예제 #9
0
        public void Search_in_empty_tree_should_NOT_throw()
        {
            var tree = ImMapSlots.CreateWithEmpty <int>();

            Assert.AreEqual(0, tree[0].GetValueOrDefault(0));
        }