1
1

ConcurrentHashSetTests.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using Xunit;
  5. namespace Masuit.Tools.Systems.Tests
  6. {
  7. public class ConcurrentHashSetTests
  8. {
  9. [Fact]
  10. public void Add_ShouldAddItem()
  11. {
  12. var set = new ConcurrentHashSet<int>();
  13. set.Add(1);
  14. Assert.Contains(1, set);
  15. }
  16. [Fact]
  17. public void TryAdd_ShouldReturnTrueForNewItem()
  18. {
  19. var set = new ConcurrentHashSet<int>();
  20. var result = set.TryAdd(1);
  21. Assert.True(result);
  22. Assert.Contains(1, set);
  23. }
  24. [Fact]
  25. public void TryAdd_ShouldReturnFalseForExistingItem()
  26. {
  27. var set = new ConcurrentHashSet<int>();
  28. set.Add(1);
  29. var result = set.TryAdd(1);
  30. Assert.False(result);
  31. }
  32. [Fact]
  33. public void UnionWith_ShouldUnionSets()
  34. {
  35. var set = new ConcurrentHashSet<int> { 1, 2 };
  36. set.UnionWith(new[] { 2, 3 });
  37. Assert.Contains(1, set);
  38. Assert.Contains(2, set);
  39. Assert.Contains(3, set);
  40. }
  41. [Fact]
  42. public void IntersectWith_ShouldIntersectSets()
  43. {
  44. var set = new ConcurrentHashSet<int> { 1, 2 };
  45. set.IntersectWith(new[] { 2, 3 });
  46. Assert.DoesNotContain(1, set);
  47. Assert.Contains(2, set);
  48. Assert.DoesNotContain(3, set);
  49. }
  50. [Fact]
  51. public void ExceptWith_ShouldExceptSets()
  52. {
  53. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  54. set.ExceptWith(new[] { 2, 3 });
  55. Assert.Contains(1, set);
  56. Assert.DoesNotContain(2, set);
  57. Assert.DoesNotContain(3, set);
  58. }
  59. [Fact]
  60. public void SymmetricExceptWith_ShouldSymmetricExceptSets()
  61. {
  62. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  63. set.SymmetricExceptWith(new[] { 2, 3, 4 });
  64. Assert.Contains(1, set);
  65. Assert.DoesNotContain(2, set);
  66. Assert.DoesNotContain(3, set);
  67. Assert.Contains(4, set);
  68. }
  69. [Fact]
  70. public void IsSubsetOf_ShouldReturnTrueForSubset()
  71. {
  72. var set = new ConcurrentHashSet<int> { 1, 2 };
  73. var result = set.IsSubsetOf(new[] { 1, 2, 3 });
  74. Assert.True(result);
  75. }
  76. [Fact]
  77. public void IsSupersetOf_ShouldReturnTrueForSuperset()
  78. {
  79. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  80. var result = set.IsSupersetOf(new[] { 1, 2 });
  81. Assert.True(result);
  82. }
  83. [Fact]
  84. public void IsProperSupersetOf_ShouldReturnTrueForProperSuperset()
  85. {
  86. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  87. var result = set.IsProperSupersetOf(new[] { 1, 2 });
  88. Assert.True(result);
  89. }
  90. [Fact]
  91. public void IsProperSubsetOf_ShouldReturnTrueForProperSubset()
  92. {
  93. var set = new ConcurrentHashSet<int> { 1, 2 };
  94. var result = set.IsProperSubsetOf(new[] { 1, 2, 3 });
  95. Assert.True(result);
  96. }
  97. [Fact]
  98. public void Overlaps_ShouldReturnTrueForOverlappingSets()
  99. {
  100. var set = new ConcurrentHashSet<int> { 1, 2 };
  101. var result = set.Overlaps(new[] { 2, 3 });
  102. Assert.True(result);
  103. }
  104. [Fact]
  105. public void SetEquals_ShouldReturnTrueForEqualSets()
  106. {
  107. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  108. var result = set.SetEquals(new[] { 1, 2, 3 });
  109. Assert.True(result);
  110. }
  111. [Fact]
  112. public void Clear_ShouldClearSet()
  113. {
  114. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  115. set.Clear();
  116. Assert.Empty(set);
  117. }
  118. [Fact]
  119. public void Contains_ShouldReturnTrueForContainedItem()
  120. {
  121. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  122. var result = set.Contains(2);
  123. Assert.True(result);
  124. }
  125. [Fact]
  126. public void CopyTo_ShouldCopyItemsToArray()
  127. {
  128. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  129. var array = new int[3];
  130. set.CopyTo(array, 0);
  131. Assert.Contains(1, array);
  132. Assert.Contains(2, array);
  133. Assert.Contains(3, array);
  134. }
  135. [Fact]
  136. public void Remove_ShouldRemoveItem()
  137. {
  138. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  139. var result = set.Remove(2);
  140. Assert.True(result);
  141. Assert.DoesNotContain(2, set);
  142. }
  143. [Fact]
  144. public void Count_ShouldReturnCorrectCount()
  145. {
  146. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  147. Assert.Equal(3, set.Count);
  148. }
  149. [Fact]
  150. public void IsReadOnly_ShouldReturnFalse()
  151. {
  152. var set = new ConcurrentHashSet<int>();
  153. Assert.False(set.IsReadOnly);
  154. }
  155. [Fact]
  156. public void GetEnumerator_ShouldEnumerateItems()
  157. {
  158. var set = new ConcurrentHashSet<int> { 1, 2, 3 };
  159. var enumerator = set.GetEnumerator();
  160. var items = new List<int>();
  161. while (enumerator.MoveNext())
  162. {
  163. items.Add(enumerator.Current);
  164. }
  165. Assert.Contains(1, items);
  166. Assert.Contains(2, items);
  167. Assert.Contains(3, items);
  168. }
  169. [Fact]
  170. public void Dispose_ShouldDisposeLock()
  171. {
  172. var set = new ConcurrentHashSet<int>();
  173. set.Dispose();
  174. Assert.Throws<ObjectDisposedException>(() => set.Add(1));
  175. }
  176. }
  177. }