public void TestSingleThreadContains() { var list = new ConcurrentSortedList <Order>(new OrderComparer()); var idList = ConstructIdList(10000); Parallel.ForEach(idList, x => list.Add(new Order { Id = x, Name = x.ToString() })); foreach (var i in idList) { Assert.True(list.Contains(new Order { Id = i, Name = i.ToString() })); } var random = new Random((int)DateTime.Now.Ticks & 0x0000ffff); for (var i = 0; i < 20; i++) { var rand = random.Next(); if (!idList.Contains(rand)) { Assert.False(list.Contains(new Order { Id = rand, Name = rand.ToString() })); } } }
public void TestMultithreadRemoveAndContains() { var list = new ConcurrentSortedList <int>(); var idList = Enumerable.Range(0, 10000).ToList(); Parallel.ForEach(idList, x => list.Add(x)); Parallel.ForEach(idList, x => { if (x < 5000) { Assert.True(list.Remove(x)); } else { Assert.True(list.Contains(x)); } }); Assert.Equal(5000, list.Count); for (var i = 0; i < 5000; i++) { Assert.False(list.Contains(idList[i])); } AssertIntList(list, 5000); }
public void TestSingleThreadRemove() { var list = new ConcurrentSortedList <Order>(new OrderComparer()); var idList = ConstructIdList(10000); Parallel.ForEach(idList, x => list.Add(new Order { Id = x, Name = x.ToString() })); for (var i = 0; i < 1000; i++) { Assert.True(list.Remove(new Order { Id = idList[i], Name = idList[i].ToString() })); } Assert.Equal(9000, list.Count); for (var i = 0; i < 1000; i++) { Assert.False(list.Contains(new Order { Id = idList[i], Name = idList[i].ToString() })); } for (var i = 0; i < 9000; i++) { Assert.True(list.Contains(new Order { Id = idList[i + 1000], Name = idList[i + 1000].ToString() })); } AssertOrderList(list, 9000); }
public void TestMultithreadAddAndReomove() { var list = new ConcurrentSortedList <int>(); var idList = Enumerable.Range(0, 10000).ToList(); var newIdList = Enumerable.Range(10000, 2000).ToList(); Parallel.ForEach(idList, x => list.Add(x)); var tasks = new Task[3000]; for (var i = 0; i < 2000; i++) { var id = newIdList[i]; tasks[i] = new Task(() => list.Add(id)); } for (var i = 0; i < 1000; i++) { var id = idList[i]; tasks[i + 2000] = new Task(() => list.Remove(id)); } Parallel.ForEach(tasks, x => x.Start()); Task.WaitAll(tasks); Assert.Equal(11000, list.Count); for (var i = 0; i < 2000; i++) { Assert.True(list.Contains(newIdList[i])); } for (var i = 0; i < 1000; i++) { Assert.False(list.Contains(idList[i])); } for (var i = 0; i < 9000; i++) { Assert.True(list.Contains(idList[i + 1000])); } AssertIntList(list, 11000); }
public void TestMultithreadAddAndContains() { var list = new ConcurrentSortedList <int>(); var idList = Enumerable.Range(0, 5000).ToList(); Parallel.ForEach(idList, x => list.Add(x)); var newIdList = Enumerable.Range(5000, 5000).ToList(); var tasks = new Task[10000]; for (var i = 0; i < 5000; i++) { var id = newIdList[i]; tasks[i] = new Task(() => list.Add(id)); } for (var i = 0; i < 5000; i++) { var id = i; tasks[i + 5000] = new Task(() => Assert.True(list.Contains(id))); } Parallel.ForEach(tasks, x => x.Start()); Task.WaitAll(tasks); Assert.Equal(10000, list.Count); AssertIntList(list, 10000); }
public void AddAndContainsTest() { var csl = new ConcurrentSortedList(); Assert.Equal(0, csl.Count); Assert.False(csl.Contains(12)); csl.Add(12); Assert.Equal(1, csl.Count); Assert.True(csl.Contains(12)); Assert.False(csl.Contains(15)); csl.Add(15); Assert.Equal(2, csl.Count); Assert.True(csl.Contains(12)); Assert.True(csl.Contains(15)); Assert.False(csl.Contains(5)); csl.Add(5); Assert.Equal(3, csl.Count); Assert.True(csl.Contains(12)); Assert.True(csl.Contains(15)); Assert.True(csl.Contains(5)); Assert.False(csl.Contains(13)); Assert.Equal((UInt64)5, csl[0]); Assert.Equal((UInt64)15, csl[2]); Assert.Equal((UInt64)12, csl[1]); csl.Add(12); // repeat Assert.Equal(3, csl.Count); Assert.True(csl.Contains(12)); Assert.True(csl.Contains(15)); Assert.True(csl.Contains(5)); Assert.False(csl.Contains(13)); Assert.Equal((UInt64)5, csl[0]); Assert.Equal((UInt64)12, csl[1]); Assert.Equal((UInt64)15, csl[2]); csl.Add(13); Assert.Equal(4, csl.Count); Assert.True(csl.Contains(12)); Assert.True(csl.Contains(15)); Assert.True(csl.Contains(5)); Assert.True(csl.Contains(13)); Assert.False(csl.Contains(100)); // random Assert.Equal((UInt64)5, csl[0]); Assert.Equal((UInt64)12, csl[1]); Assert.Equal((UInt64)13, csl[2]); Assert.Equal((UInt64)15, csl[3]); }