ProtobufEqualityComparersTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2017 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using NUnit.Framework;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using static Google.Protobuf.Collections.ProtobufEqualityComparers;
  36. namespace Google.Protobuf.Collections
  37. {
  38. public class ProtobufEqualityComparersTest
  39. {
  40. private static readonly double[] doubles =
  41. {
  42. 0,
  43. 1,
  44. 1.5,
  45. -1.5,
  46. double.PositiveInfinity,
  47. double.NegativeInfinity,
  48. // Three different types of NaN...
  49. SampleNaNs.Regular,
  50. SampleNaNs.SignallingFlipped,
  51. SampleNaNs.PayloadFlipped
  52. };
  53. [Test]
  54. public void GetEqualityComparer_Default()
  55. {
  56. // It's more pain than it's worth to try to parameterize these tests.
  57. Assert.AreSame(EqualityComparer<object>.Default, GetEqualityComparer<object>());
  58. Assert.AreSame(EqualityComparer<string>.Default, GetEqualityComparer<string>());
  59. Assert.AreSame(EqualityComparer<int>.Default, GetEqualityComparer<int>());
  60. Assert.AreSame(EqualityComparer<int?>.Default, GetEqualityComparer<int?>());
  61. }
  62. [Test]
  63. public void GetEqualityComparer_NotDefault()
  64. {
  65. // It's more pain than it's worth to try to parameterize these tests.
  66. Assert.AreSame(BitwiseDoubleEqualityComparer, GetEqualityComparer<double>());
  67. Assert.AreSame(BitwiseSingleEqualityComparer, GetEqualityComparer<float>());
  68. Assert.AreSame(BitwiseNullableDoubleEqualityComparer, GetEqualityComparer<double?>());
  69. Assert.AreSame(BitwiseNullableSingleEqualityComparer, GetEqualityComparer<float?>());
  70. }
  71. [Test]
  72. public void DoubleComparisons()
  73. {
  74. ValidateEqualityComparer(BitwiseDoubleEqualityComparer, doubles);
  75. }
  76. [Test]
  77. public void NullableDoubleComparisons()
  78. {
  79. ValidateEqualityComparer(BitwiseNullableDoubleEqualityComparer, doubles.Select(d => (double?) d).Concat(new double?[] { null }));
  80. }
  81. [Test]
  82. public void SingleComparisons()
  83. {
  84. ValidateEqualityComparer(BitwiseSingleEqualityComparer, doubles.Select(d => (float) d));
  85. }
  86. [Test]
  87. public void NullableSingleComparisons()
  88. {
  89. ValidateEqualityComparer(BitwiseNullableSingleEqualityComparer, doubles.Select(d => (float?) d).Concat(new float?[] { null }));
  90. }
  91. private static void ValidateEqualityComparer<T>(EqualityComparer<T> comparer, IEnumerable<T> values)
  92. {
  93. var array = values.ToArray();
  94. // Each value should be equal to itself, but not to any other value.
  95. for (int i = 0; i < array.Length; i++)
  96. {
  97. for (int j = 0; j < array.Length; j++)
  98. {
  99. if (i == j)
  100. {
  101. Assert.IsTrue(comparer.Equals(array[i], array[j]),
  102. "{0} should be equal to itself", array[i], array[j]);
  103. }
  104. else
  105. {
  106. Assert.IsFalse(comparer.Equals(array[i], array[j]),
  107. "{0} and {1} should not be equal", array[i], array[j]);
  108. Assert.AreNotEqual(comparer.GetHashCode(array[i]), comparer.GetHashCode(array[j]),
  109. "Hash codes for {0} and {1} should not be equal", array[i], array[j]);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }