Example #1
0
        public void TestMapArgumentExceptions()
        {
            var map = new Customized32HashedMap <Order, Bill>(5, new MurmurHash32(), x => x.Id.ToString().ToBytes(), (x1, x2) => x1.Id == x2.Id);

            for (var i = 0; i < 5; i++)
            {
                var order = new Order()
                {
                    Id = i, Name = Guid.NewGuid().ToString()
                };
                map.Add(order, new Bill()
                {
                    Id = i, Count = i
                });
            }

            var notExistingOrder = new Order()
            {
                Id = 10, Name = Guid.NewGuid().ToString()
            };

            Assert.Throws(typeof(KeyNotFoundException), () => { var v = map[notExistingOrder]; });
            Assert.False(map.Remove(notExistingOrder));

            var existingOrder = new Order()
            {
                Id = 1, Name = "  "
            };

            Assert.Throws(typeof(ArgumentException), () => map.Add(existingOrder, new Bill()));
        }
Example #2
0
        public void TestHashMapContains()
        {
            var orders = new Customized32HashedMap <string, Order>();
            var key    = Guid.NewGuid().ToString();
            var order  = new Order();

            orders.Add(key, order);
            Assert.True(orders.ContainsKey(key));
        }
Example #3
0
        public void TestMapNullExceptions()
        {
            var  map = new Customized32HashedMap <Order, Bill>();
            Bill b;

            Assert.Throws(typeof(ArgumentNullException), () => map.Add(null, null));
            Assert.Throws(typeof(ArgumentNullException), () => map[null] = null);
            Assert.Throws(typeof(ArgumentNullException), () => { var v = map[null]; });
            Assert.Throws(typeof(ArgumentNullException), () => map.Remove(null));
            Assert.Throws(typeof(ArgumentNullException), () => map.TryGetValue(null, out b));
            Assert.Throws(typeof(ArgumentNullException), () => map.ContainsKey(null));
            Assert.Throws(typeof(ArgumentNullException), () => map.CopyTo(null, 0));

            var simpleMap = new Customized32HashedMap <int, string>();

            simpleMap.Add(0, null);
        }
Example #4
0
        public void TestHashMapOperations()
        {
            var orders = new Order[1000];

            for (var i = 0; i < orders.Length; i++)
            {
                orders[i] = new Order()
                {
                    Id = i, Name = Guid.NewGuid().ToString()
                };
            }

            var bills = new Bill[1000];

            for (var i = 0; i < bills.Length; i++)
            {
                bills[i] = new Bill()
                {
                    Id = i, Count = i
                };
            }

            IDictionary <Order, Bill> orderMap = new Customized32HashedMap <Order, Bill>(1000, x => x.Name.ToBytes());

            Assert.Equal(0, orderMap.Keys.Count);
            Assert.Equal(0, orderMap.Values.Count);
            for (var i = 0; i < orders.Length; i++)
            {
                orderMap.Add(orders[i], bills[i]);
            }
            Assert.Equal(orders.Length, orderMap.Count);
            var keys = orderMap.Keys;

            Assert.Equal(orders.Length, keys.Count);
            foreach (var key in keys)
            {
                Assert.True(orders.Contains(key, new OrderEqualityComparer()));
            }
            foreach (var o in orders)
            {
                Assert.True(keys.Contains(o, new OrderEqualityComparer()));
            }

            var values = orderMap.Values;

            Assert.Equal(bills.Length, values.Count);
            foreach (var v in values)
            {
                Assert.True(bills.Contains(v, new BillEqualityComparer()));
            }

            foreach (var bill in bills)
            {
                Assert.True(values.Contains(bill, new BillEqualityComparer()));
            }

            var order = new Order()
            {
                Id = 564, Name = "000000000000"
            };
            Bill b;

            Assert.False(orderMap.TryGetValue(order, out b));
            Assert.Equal(null, b);

            for (var i = 0; i < 150; i++)
            {
                Bill bill;
                Assert.Contains(new KeyValuePair <Order, Bill>(orders[200 + i], bills[200 + i]), orderMap);
                Assert.True(orderMap.Contains(new KeyValuePair <Order, Bill>(orders[100 + i], bills[100 + i])));
                Assert.True(orderMap.TryGetValue(orders[500], out bill));
                Assert.Equal(orderMap[orders[500]], bill, new BillEqualityComparer());
            }

            foreach (var item in orderMap)
            {
                Assert.Contains(item.Key, orders, new OrderEqualityComparer());
                Assert.Contains(item.Value, bills, new BillEqualityComparer());
            }

            var kvps1 = new KeyValuePair <Order, Bill> [1010];

            orderMap.CopyTo(kvps1, 5);
            for (var i = 0; i < 1000; i++)
            {
                Assert.Contains(kvps1[i + 5].Key, orders, new OrderEqualityComparer());
                Assert.Contains(kvps1[i + 5].Value, bills, new BillEqualityComparer());
            }

            orderMap.Clear();
            Assert.Equal(0, orderMap.Count);
            Assert.Empty(orderMap);
            foreach (var item in orderMap)
            {
                Assert.Equal(0, 1);
            }
        }