public void Test001_DoubleEndedUniqueCollectionBehavesLikeLinkedList() { var lklist = new LinkedList <int>(); lklist.AddLast(456); lklist.AddLast(123); lklist.AddLast(789); lklist.AddLast(567); lklist.Remove(123); var delist = new DoubleEndedUniqueCollection <int>(); delist.Add(456); delist.Add(123); delist.Add(789); delist.Add(567); delist.Remove(123); var expectedArray = new int[] { 456, 789, 567 }; var lklistArray = lklist.ToArray(); var delistArray = delist.ToArray(); Assert.AreEqual(expectedArray, lklistArray); Assert.AreEqual(expectedArray, delistArray); }
public void Test001_DoubleEndedUniqueCollectionIsMuchFasterOnRandomItemAccess() { var random = new Random((int)DateTime.Now.Millisecond); var accessed = new bool[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS]; var access = new int[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS]; for (var a = 0; a < access.Length; a++) { int at; while (accessed[at = random.Next(accessed.Length)]) ; accessed[at] = true; access[a] = at; } ICollection<int> lklist = new LinkedList<int>(); for (var i = 0; i < accessed.Length; i++) { lklist.Add(i); } ICollection<int> delist = new DoubleEndedUniqueCollection<int>(); for (var i = 0; i < accessed.Length; i++) { delist.Add(i); } var list = lklist; var time = Helpers.Measuring.Time.Start(); for (var i = 0; i < access.Length; i++) { var value = access[i]; list.Remove(value); list.Add(value); } var elapsed = time.ElapsedMilliseconds; Console.WriteLine(); Console.WriteLine("LinkedList: {0} ms", elapsed.ToString("0,0")); list = delist; time = Helpers.Measuring.Time.Start(); for (var i = 0; i < access.Length; i++) { var value = access[i]; list.Remove(value); list.Add(value); } elapsed = time.ElapsedMilliseconds; Console.WriteLine(); Console.WriteLine("Double-ended unique collection: {0} ms", elapsed.ToString("0,0")); var lklistArray = lklist.ToArray(); var delistArray = delist.ToArray(); Assert.AreEqual(lklistArray, delistArray); }
public void Test001_DoubleEndedUniqueCollectionDoesntAcceptKeyDuplicates() { Assert.Throws <ArgumentException>(() => { var delist = new DoubleEndedUniqueCollection <int>(); delist.Add(123); Console.WriteLine("Added " + 123); delist.Add(789); Console.WriteLine("Added " + 789); delist.Add(456); Console.WriteLine("Added " + 456); var error = null as Exception; try { Console.WriteLine("Trying to add " + 789 + " again"); delist.Add(789); Console.WriteLine("Added " + 789); } catch (Exception ex) { Console.WriteLine("Couldn't add " + 789 + " again... removing it..."); delist.Remove(789); error = ex; } Assert.AreEqual(delist.Contains(789), false); Console.WriteLine("Re-adding " + 789); delist.Add(789); Console.WriteLine("Added " + 789); Assert.AreEqual(delist.Contains(789), true); if (error != null) { Console.WriteLine("Re-throwing the exception we caught..."); throw error; } }); }
public void Test001_DoubleEndedUniqueCollectionBehavesLikeLinkedList() { var lklist = new LinkedList<int>(); lklist.AddLast(456); lklist.AddLast(123); lklist.AddLast(789); lklist.AddLast(567); lklist.Remove(123); var delist = new DoubleEndedUniqueCollection<int>(); delist.Add(456); delist.Add(123); delist.Add(789); delist.Add(567); delist.Remove(123); var expectedArray = new int[] { 456, 789, 567 }; var lklistArray = lklist.ToArray(); var delistArray = delist.ToArray(); Assert.AreEqual(expectedArray, lklistArray); Assert.AreEqual(expectedArray, delistArray); }
public void Test001_DoubleEndedUniqueCollectionDoesntAcceptKeyDuplicates() { var delist = new DoubleEndedUniqueCollection<int>(); delist.Add(123); Console.WriteLine("Added " + 123); delist.Add(789); Console.WriteLine("Added " + 789); delist.Add(456); Console.WriteLine("Added " + 456); var error = null as Exception; try { Console.WriteLine("Trying to add " + 789 + " again"); delist.Add(789); Console.WriteLine("Added " + 789); } catch (Exception ex) { Console.WriteLine("Couldn't add " + 789 + " again... removing it..."); delist.Remove(789); error = ex; } Assert.AreEqual(delist.Contains(789), false); Console.WriteLine("Re-adding " + 789); delist.Add(789); Console.WriteLine("Added " + 789); Assert.AreEqual(delist.Contains(789), true); if (error != null) { Console.WriteLine("Re-throwing the exception we caught..."); throw error; } }
public void Test001_DoubleEndedUniqueCollectionIsMuchFasterOnRandomItemAccess() { var random = new Random((int)DateTime.Now.Millisecond); var accessed = new bool[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS]; var access = new int[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS]; for (var a = 0; a < access.Length; a++) { int at; while (accessed[at = random.Next(accessed.Length)]) { ; } accessed[at] = true; access[a] = at; } ICollection <int> lklist = new LinkedList <int>(); for (var i = 0; i < accessed.Length; i++) { lklist.Add(i); } ICollection <int> delist = new DoubleEndedUniqueCollection <int>(); for (var i = 0; i < accessed.Length; i++) { delist.Add(i); } var list = lklist; var time = Helpers.Measuring.Time.Start(); for (var i = 0; i < access.Length; i++) { var value = access[i]; list.Remove(value); list.Add(value); } var elapsed = time.ElapsedMilliseconds; Console.WriteLine(); Console.WriteLine("LinkedList: {0} ms", elapsed.ToString("0,0")); list = delist; time = Helpers.Measuring.Time.Start(); for (var i = 0; i < access.Length; i++) { var value = access[i]; list.Remove(value); list.Add(value); } elapsed = time.ElapsedMilliseconds; Console.WriteLine(); Console.WriteLine("Double-ended unique collection: {0} ms", elapsed.ToString("0,0")); var lklistArray = lklist.ToArray(); var delistArray = delist.ToArray(); Assert.AreEqual(lklistArray, delistArray); }