PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/Microsoft/Internal/Collections/WeakReferenceCollectionTests.cs

https://bitbucket.org/danipen/mono
C# | 104 lines | 76 code | 25 blank | 3 comment | 0 complexity | 0e2c2992e6d7682ee7b95e938187eb3c MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.UnitTesting;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. namespace Microsoft.Internal.Collections
  10. {
  11. [TestClass]
  12. public class WeakReferenceCollectionTests
  13. {
  14. [TestMethod]
  15. public void Add_ObjectShouldGetCollected()
  16. {
  17. var obj = new object();
  18. var wrc = new WeakReferenceCollection<object>();
  19. wrc.Add(obj);
  20. var wr = new WeakReference(obj);
  21. obj = null;
  22. Assert.IsNotNull(wr.Target, "Object should NOT have been collected yet!");
  23. GC.Collect();
  24. GC.WaitForPendingFinalizers();
  25. Assert.IsNull(wr.Target, "Object should have been collected!");
  26. GC.KeepAlive(wrc);
  27. }
  28. [TestMethod]
  29. public void Remove_ObjectShouldGetRemoved()
  30. {
  31. var obj = new object();
  32. var wrc = new WeakReferenceCollection<object>();
  33. wrc.Add(obj);
  34. Assert.AreEqual(1, wrc.AliveItemsToList().Count, "Should have 1 item!");
  35. wrc.Remove(obj);
  36. Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 item!");
  37. }
  38. [TestMethod]
  39. public void AliveItemsToList_ShouldReturnAllItems()
  40. {
  41. var list = new object[] {new object(), new object(), new object()};
  42. var wrc = new WeakReferenceCollection<object>();
  43. foreach (object obj in list)
  44. {
  45. wrc.Add(obj);
  46. }
  47. Assert.AreEqual(list.Length, wrc.AliveItemsToList().Count, "Should have same number of items!");
  48. }
  49. [TestMethod]
  50. public void AliveItemsToList_ShouldReturnAllAliveItems()
  51. {
  52. var list = new object[] { new object(), new object(), new object() };
  53. var wrc = new WeakReferenceCollection<object>();
  54. var obj1 = new object();
  55. wrc.Add(obj1);
  56. foreach (object obj in list)
  57. {
  58. wrc.Add(obj);
  59. }
  60. var obj2 = new object();
  61. wrc.Add(obj2);
  62. Assert.AreEqual(list.Length + 2, wrc.AliveItemsToList().Count, "Should have same number of items!");
  63. obj1 = obj2 = null;
  64. GC.Collect();
  65. GC.WaitForPendingFinalizers();
  66. var aliveItems = wrc.AliveItemsToList();
  67. Assert.AreEqual(list.Length, aliveItems.Count, "Should have 2 less items!");
  68. Assert.AreEqual(list[0], aliveItems[0]);
  69. Assert.AreEqual(list[1], aliveItems[1]);
  70. Assert.AreEqual(list[2], aliveItems[2]);
  71. }
  72. [TestMethod]
  73. public void AliveItemsToList_ShouldReturnEmpty()
  74. {
  75. var wrc = new WeakReferenceCollection<object>();
  76. Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 items!");
  77. }
  78. }
  79. }