public void TestOrderedMap() { var container = new LinkedHashedMap <int, string>(10000); Assert.Throws(typeof(InvalidOperationException), () => container.First); Assert.Throws(typeof(InvalidOperationException), () => container.Last); Assert.Throws(typeof(InvalidOperationException), () => container.After(1)); Assert.Throws(typeof(InvalidOperationException), () => container.Before(1)); for (var i = 0; i < 10000; i++) { container.Add(i, i.ToString()); } Assert.Equal(0, container.First.Key); Assert.Equal("0", container.First.Value); Assert.Equal(9999, container.Last.Key); Assert.Equal("9999", container.Last.Value); Assert.Equal("0", container.GetIndex(0).Value); Assert.Equal("1000", container.GetIndex(1000).Value); Assert.Equal("1600", container.GetIndex(1600).Value); Assert.Equal("2749", container.GetIndex(2749).Value); Assert.Equal("4999", container.GetIndex(4999).Value); Assert.Equal("11", container.After(10).Value); Assert.Equal("100", container.After(99).Value); Assert.Equal("1001", container.After(1000).Value); Assert.Equal("5000", container.After(4999).Value); Assert.Equal("50", container.Before(51).Value); Assert.Equal("500", container.Before(501).Value); Assert.Equal("1500", container.Before(1501).Value); Assert.Equal("7900", container.Before(7901).Value); Assert.Throws(typeof(ArgumentException), () => container.After(9999)); Assert.Throws(typeof(ArgumentException), () => container.Before(0)); Assert.Throws(typeof(ArgumentException), () => container.Before(10000)); Assert.Throws(typeof(ArgumentException), () => container.After(10000)); Assert.Throws(typeof(ArgumentException), () => container.GetIndex(-100)); Assert.Throws(typeof(ArgumentException), () => container.GetIndex(20000)); for (var i = 2000; i < 5000; i++) { Assert.True(container.Remove(i)); } Assert.Equal("0", container.First.Value); Assert.Equal("9999", container.Last.Value); Assert.Equal("1000", container.After(999).Value); Assert.Equal("999", container.Before(1000).Value); Assert.Equal("9000", container.After(8999).Value); Assert.Equal("8999", container.Before(9000).Value); Assert.Equal("8500", container.Before(8501).Value); Assert.Equal("5000", container.After(1999).Value); Assert.Equal("1999", container.Before(5000).Value); for (var i = 7000; i < 8000; i++) { Assert.True(container.Remove(i)); } Assert.Equal("0", container.First.Value); Assert.Equal("9999", container.Last.Value); Assert.Equal("1000", container.After(999).Value); Assert.Equal("999", container.Before(1000).Value); Assert.Equal("9000", container.After(8999).Value); Assert.Equal("8999", container.Before(9000).Value); Assert.Equal("8500", container.Before(8501).Value); Assert.Equal("8000", container.After(6999).Value); Assert.Equal("6999", container.Before(8000).Value); }
public void TestLinkedHashedMapRemove() { var map = new LinkedHashedMap <int, Order>(); map.Fill(x => new KeyValuePair <int, Order>(x, new Order { Id = x })); Assert.Equal(1000, map.Count); for (var i = 0; i < 100; i++) { Assert.True(map.ContainsKey(i)); Assert.True(map.Remove(i)); Assert.False(map.ContainsKey(i)); } Assert.Throws(typeof(ArgumentException), () => map.After(0)); Assert.Throws(typeof(ArgumentException), () => map.Before(100)); Assert.Throws(typeof(ArgumentException), () => map.After(999)); Assert.Equal(900, map.Count); var index = 100; foreach (var item in map) { Assert.Equal(index, item.Key); index++; } Assert.Equal(1000, index); for (var i = 900; i < 1000; i++) { Assert.True(map.ContainsKey(i)); Assert.True(map.Remove(i)); Assert.False(map.ContainsKey(i)); } Assert.Equal(800, map.Count); Assert.Throws(typeof(ArgumentException), () => map.After(899)); index = 100; foreach (var item in map) { Assert.Equal(index, item.Key); index++; } Assert.Equal(900, index); for (var i = 300; i < 500; i++) { Assert.True(map.ContainsKey(i)); Assert.True(map.Remove(i)); } Assert.Equal(299, map.Before(500).Key); Assert.Equal(500, map.After(299).Key); Assert.Equal(600, map.Count); index = 100; foreach (var item in map) { index = index == 300 ? 500 : index; Assert.Equal(index, item.Key); index++; } Assert.Equal(900, index); }