|
| 1 | +using System.Collections.Generic; |
| 2 | +using NHibernate.Util; |
| 3 | +using NUnit.Framework; |
| 4 | + |
| 5 | +namespace NHibernate.Test.UtilityTest |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class CollectionHelperFixture |
| 9 | + { |
| 10 | + #region Bag |
| 11 | + |
| 12 | + [Test] |
| 13 | + public void BagComparedToSelfShouldBeEqual() |
| 14 | + { |
| 15 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 16 | + Assert.That(CollectionHelper.BagEquals(c1, c1), Is.True); |
| 17 | + } |
| 18 | + |
| 19 | + [Test] |
| 20 | + public void BagComparedToNullShouldBeInequal() |
| 21 | + { |
| 22 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 23 | + Assert.That(CollectionHelper.BagEquals(c1, null), Is.False); |
| 24 | + Assert.That(CollectionHelper.BagEquals(null, c1), Is.False); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public void BagNullComparedToNullShouldBeEqual() |
| 29 | + { |
| 30 | + Assert.That(CollectionHelper.BagEquals<string>(null, null), Is.True); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public void BagsWithSameContentShouldBeEqual() |
| 35 | + { |
| 36 | + var c1 = new List<string> { "1", "2", "3", "4", "2" }; |
| 37 | + var c2 = new List<string> { "1", "2", "3", "4", "2" }; |
| 38 | + Assert.That(CollectionHelper.BagEquals(c1, c2), Is.True); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void BagsWithSameCountButDistinctValuesShouldBeInequal() |
| 43 | + { |
| 44 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 45 | + var c2 = new List<string> { "1", "2", "3", "3" }; |
| 46 | + Assert.That(CollectionHelper.BagEquals(c1, c2), Is.False); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public void BagsWithSameElementsButDistinctOrderShouldBeEqual() |
| 51 | + { |
| 52 | + var c1 = new List<string> { "1", "2", "3", "4", "2" }; |
| 53 | + var c2 = new List<string> { "2", "1", "2", "4", "3" }; |
| 54 | + Assert.That(CollectionHelper.BagEquals(c1, c2), Is.True); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public void BagsWithoutSameCountShouldBeInequal() |
| 59 | + { |
| 60 | + var c1 = new List<string> { "1", "2", "2", "1" }; |
| 61 | + var c2 = new List<string> { "1", "2" }; |
| 62 | + Assert.That(CollectionHelper.BagEquals(c1, c2), Is.False); |
| 63 | + Assert.That(CollectionHelper.BagEquals(c2, c1), Is.False); |
| 64 | + } |
| 65 | + |
| 66 | + #endregion |
| 67 | + |
| 68 | + #region Dictionary/Map |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void MapComparedToSelfShouldBeEqual() |
| 72 | + { |
| 73 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 74 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d1), Is.True); |
| 75 | + } |
| 76 | + |
| 77 | + [Test] |
| 78 | + public void MapComparedToNullShouldBeInequal() |
| 79 | + { |
| 80 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 81 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, null), Is.False); |
| 82 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(null, d1), Is.False); |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + public void MapNullComparedToNullShouldBeEqual() |
| 87 | + { |
| 88 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(null, null), Is.True); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public void MapsWithSameContentShouldBeEqual() |
| 93 | + { |
| 94 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 95 | + var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 96 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.True); |
| 97 | + } |
| 98 | + |
| 99 | + // Fails till NH-3981 is fixed. |
| 100 | + [Test] |
| 101 | + public void MapsWithSameCountButDistinctKeysShouldBeInequal() |
| 102 | + { |
| 103 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 104 | + var d2 = new Dictionary<string, string> { { "1", "2" }, { "4", "3" } }; |
| 105 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.False); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public void MapsWithSameCountButDistinctValuesShouldBeInequal() |
| 110 | + { |
| 111 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 112 | + var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "3" } }; |
| 113 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.False); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void MapsWithoutSameCountShouldBeInequal() |
| 118 | + { |
| 119 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 120 | + var d2 = new Dictionary<string, string> { { "1", "2" } }; |
| 121 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.False); |
| 122 | + Assert.That(CollectionHelper.DictionaryEquals<string, string>(d2, d1), Is.False); |
| 123 | + } |
| 124 | + |
| 125 | + #endregion |
| 126 | + |
| 127 | + #region Set |
| 128 | + |
| 129 | + [Test] |
| 130 | + public void SetComparedToSelfShouldBeEqual() |
| 131 | + { |
| 132 | + var c1 = new HashSet<string> { "1", "2", "3", "4" }; |
| 133 | + Assert.That(CollectionHelper.SetEquals(c1, c1), Is.True); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void SetComparedToNullShouldBeInequal() |
| 138 | + { |
| 139 | + var c1 = new HashSet<string> { "1", "2", "3", "4" }; |
| 140 | + Assert.That(CollectionHelper.SetEquals(c1, null), Is.False); |
| 141 | + Assert.That(CollectionHelper.SetEquals(null, c1), Is.False); |
| 142 | + } |
| 143 | + |
| 144 | + [Test] |
| 145 | + public void SetNullComparedToNullShouldBeEqual() |
| 146 | + { |
| 147 | + Assert.That(CollectionHelper.SetEquals<string>(null, null), Is.True); |
| 148 | + } |
| 149 | + |
| 150 | + [Test] |
| 151 | + public void SetsWithSameContentShouldBeEqual() |
| 152 | + { |
| 153 | + var c1 = new HashSet<string> { "1", "2", "3", "4" }; |
| 154 | + var c2 = new HashSet<string> { "1", "2", "3", "4" }; |
| 155 | + Assert.That(CollectionHelper.SetEquals(c1, c2), Is.True); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public void SetsWithSameCountButDistinctValuesShouldBeInequal() |
| 160 | + { |
| 161 | + var c1 = new HashSet<string> { "1", "2", "3", "4" }; |
| 162 | + var c2 = new HashSet<string> { "1", "2", "3", "5" }; |
| 163 | + Assert.That(CollectionHelper.SetEquals(c1, c2), Is.False); |
| 164 | + } |
| 165 | + |
| 166 | + [Test] |
| 167 | + public void SetsWithSameElementsButDistinctOrderShouldBeEqual() |
| 168 | + { |
| 169 | + var c1 = new HashSet<string> { "1", "2", "3", "4" }; |
| 170 | + var c2 = new HashSet<string> { "1", "2", "4", "3" }; |
| 171 | + Assert.That(CollectionHelper.SetEquals(c1, c2), Is.True); |
| 172 | + } |
| 173 | + |
| 174 | + [Test] |
| 175 | + public void SetsWithoutSameCountShouldBeInequal() |
| 176 | + { |
| 177 | + var c1 = new HashSet<string> { "1", "2", "3", "4" }; |
| 178 | + var c2 = new HashSet<string> { "1", "2" }; |
| 179 | + Assert.That(CollectionHelper.SetEquals(c1, c2), Is.False); |
| 180 | + Assert.That(CollectionHelper.SetEquals(c2, c1), Is.False); |
| 181 | + } |
| 182 | + |
| 183 | + #endregion |
| 184 | + |
| 185 | + #region List/Sequence |
| 186 | + |
| 187 | + [Test] |
| 188 | + public void SequenceComparedToSelfShouldBeEqual() |
| 189 | + { |
| 190 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 191 | + Assert.That(CollectionHelper.SequenceEquals(c1, c1), Is.True); |
| 192 | + } |
| 193 | + |
| 194 | + [Test] |
| 195 | + public void SequenceComparedToNullShouldBeInequal() |
| 196 | + { |
| 197 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 198 | + Assert.That(CollectionHelper.SequenceEquals(c1, null), Is.False); |
| 199 | + Assert.That(CollectionHelper.SequenceEquals(null, c1), Is.False); |
| 200 | + } |
| 201 | + |
| 202 | + [Test] |
| 203 | + public void SequenceNullComparedToNullShouldBeEqual() |
| 204 | + { |
| 205 | + Assert.That(CollectionHelper.SequenceEquals<string>(null, null), Is.True); |
| 206 | + } |
| 207 | + |
| 208 | + [Test] |
| 209 | + public void SequencesWithSameContentShouldBeEqual() |
| 210 | + { |
| 211 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 212 | + var c2 = new List<string> { "1", "2", "3", "4" }; |
| 213 | + Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.True); |
| 214 | + } |
| 215 | + |
| 216 | + [Test] |
| 217 | + public void SequencesWithSameCountButDistinctValuesShouldBeInequal() |
| 218 | + { |
| 219 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 220 | + var c2 = new List<string> { "1", "2", "3", "3" }; |
| 221 | + Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.False); |
| 222 | + } |
| 223 | + |
| 224 | + [Test] |
| 225 | + public void SequencesWithSameElementsButDistinctOrderShouldBeInequal() |
| 226 | + { |
| 227 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 228 | + var c2 = new List<string> { "1", "2", "4", "3" }; |
| 229 | + Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.False); |
| 230 | + } |
| 231 | + |
| 232 | + [Test] |
| 233 | + public void SequencesWithoutSameCountShouldBeInequal() |
| 234 | + { |
| 235 | + var c1 = new List<string> { "1", "2", "3", "4" }; |
| 236 | + var c2 = new List<string> { "1", "2" }; |
| 237 | + Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.False); |
| 238 | + Assert.That(CollectionHelper.SequenceEquals(c2, c1), Is.False); |
| 239 | + } |
| 240 | + |
| 241 | + #endregion |
| 242 | + } |
| 243 | +} |
0 commit comments