|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using NHibernate.Engine; |
2 | 4 | using NHibernate.Util;
|
3 | 5 | using NUnit.Framework;
|
4 | 6 |
|
@@ -180,6 +182,69 @@ public void SetsWithoutSameCountShouldBeInequal()
|
180 | 182 | Assert.That(CollectionHelper.SetEquals(c2, c1), Is.False);
|
181 | 183 | }
|
182 | 184 |
|
| 185 | + [Test] |
| 186 | + public void MapsWithSameContentShouldHaveSameHashCode() |
| 187 | + { |
| 188 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 189 | + var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 190 | + Assert.That( |
| 191 | + CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance), |
| 192 | + Is.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance))); |
| 193 | + } |
| 194 | + |
| 195 | + // Failure of following tests is not an error from GetHashCode semantic viewpoint, but it causes it |
| 196 | + // to be potentially inefficients for usages in dictionnaries or hashset. |
| 197 | + |
| 198 | + [Test] |
| 199 | + public void MapsWithSameCountButDistinctKeysShouldNotHaveSameHashCode() |
| 200 | + { |
| 201 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 202 | + var d2 = new Dictionary<string, string> { { "1", "2" }, { "4", "3" } }; |
| 203 | + Assert.That( |
| 204 | + CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance), |
| 205 | + Is.Not.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance))); |
| 206 | + } |
| 207 | + |
| 208 | + [Test] |
| 209 | + public void MapsWithSameCountButDistinctValuesShouldNotHaveSameHashCode() |
| 210 | + { |
| 211 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 212 | + var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "3" } }; |
| 213 | + Assert.That( |
| 214 | + CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance), |
| 215 | + Is.Not.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance))); |
| 216 | + } |
| 217 | + |
| 218 | + [Test] |
| 219 | + public void MapsWithoutSameCountShouldNotHaveSameHashCode() |
| 220 | + { |
| 221 | + var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } }; |
| 222 | + var d2 = new Dictionary<string, string> { { "1", "2" } }; |
| 223 | + Assert.That( |
| 224 | + CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance), |
| 225 | + Is.Not.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance))); |
| 226 | + } |
| 227 | + |
| 228 | + internal class KvpStringComparer : IEqualityComparer<KeyValuePair<string, string>> |
| 229 | + { |
| 230 | + public static readonly KvpStringComparer Instance = new KvpStringComparer(); |
| 231 | + |
| 232 | + public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y) |
| 233 | + { |
| 234 | + return StringComparer.Ordinal.Equals(x.Key, y.Key) && |
| 235 | + StringComparer.Ordinal.Equals(x.Value, y.Value); |
| 236 | + } |
| 237 | + |
| 238 | + public int GetHashCode(KeyValuePair<string, string> obj) |
| 239 | + { |
| 240 | + unchecked |
| 241 | + { |
| 242 | + return 397 * StringComparer.Ordinal.GetHashCode(obj.Key) ^ |
| 243 | + (obj.Value != null ? StringComparer.Ordinal.GetHashCode(obj.Value) : 0); |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + |
183 | 248 | #endregion
|
184 | 249 |
|
185 | 250 | #region List/Sequence
|
|
0 commit comments