PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/ObjectModel/BsonDoubleTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 231 lines | 174 code | 43 blank | 14 comment | 6 complexity | e13c4e9b17bb18349e3c06d4f2faf69d MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using Xunit;
  23. namespace MongoDB.Bson.Tests.ObjectModel
  24. {
  25. public class BsonDoubleTests
  26. {
  27. [Theory]
  28. [InlineData(0.0, 1.0, -1)]
  29. [InlineData(0.0, 0.0, 0)]
  30. [InlineData(1.0, 0.0, 1)]
  31. public void CompareTo_BsonDecimal128_should_return_expected_result(double doubleValue, double otherDoubleValue, int expectedResult)
  32. {
  33. var subject = new BsonDouble(doubleValue);
  34. var other = new BsonDecimal128((Decimal128)(decimal)otherDoubleValue);
  35. var result = subject.CompareTo(other);
  36. result.Should().Be(expectedResult);
  37. }
  38. [Theory]
  39. [InlineData(0.0, 1.0, -1)]
  40. [InlineData(0.0, 0.0, 0)]
  41. [InlineData(1.0, 0.0, 1)]
  42. public void CompareTo_BsonDouble_should_return_expected_result(double doubleValue, double otherDoubleValue, int expectedResult)
  43. {
  44. var subject = new BsonDouble(doubleValue);
  45. var other = new BsonDouble(otherDoubleValue);
  46. var result1 = subject.CompareTo((BsonDouble)other);
  47. var result2 = subject.CompareTo((BsonValue)other);
  48. result1.Should().Be(expectedResult);
  49. result2.Should().Be(expectedResult);
  50. }
  51. [Theory]
  52. [InlineData(0.0, 1, -1)]
  53. [InlineData(0.0, 0, 0)]
  54. [InlineData(1.0, 0, 1)]
  55. public void CompareTo_BsonInt32_should_return_expected_result(double doubleValue, int otherInt32Value, int expectedResult)
  56. {
  57. var subject = new BsonDouble(doubleValue);
  58. var other = new BsonInt32(otherInt32Value);
  59. var result = subject.CompareTo(other);
  60. result.Should().Be(expectedResult);
  61. }
  62. [Theory]
  63. [InlineData(0.0, 1L, -1)]
  64. [InlineData(0.0, 0L, 0)]
  65. [InlineData(1.0, 0L, 1)]
  66. public void CompareTo_BsonInt64_should_return_expected_result(double doubleValue, long otherInt64Value, int expectedResult)
  67. {
  68. var subject = new BsonDouble(doubleValue);
  69. var other = new BsonInt64(otherInt64Value);
  70. var result = subject.CompareTo(other);
  71. result.Should().Be(expectedResult);
  72. }
  73. [Fact]
  74. public void CompareTo_null_should_return_expected_result()
  75. {
  76. var subject = new BsonDouble(0.0);
  77. var result1 = subject.CompareTo((BsonDouble)null);
  78. var result2 = subject.CompareTo((BsonValue)null);
  79. result1.Should().Be(1);
  80. result2.Should().Be(1);
  81. }
  82. [Theory]
  83. [ParameterAttributeData]
  84. public void implicit_conversion_from_double_should_return_new_instance(
  85. [Values(-101.0, 101.0)]
  86. double value)
  87. {
  88. var result1 = (BsonDouble)value;
  89. var result2 = (BsonDouble)value;
  90. result2.Should().NotBeSameAs(result1);
  91. }
  92. [Theory]
  93. [ParameterAttributeData]
  94. public void implicit_conversion_from_double_should_return_precreated_instance(
  95. [Range(-100.0, 100.0, 1.0)]
  96. double value)
  97. {
  98. var result1 = (BsonDouble)value;
  99. var result2 = (BsonDouble)value;
  100. result2.Should().BeSameAs(result1);
  101. }
  102. [Theory]
  103. [InlineData(1.0, 1.0, true)]
  104. [InlineData(1.0, 2.0, false)]
  105. [InlineData(2.0, 1.0, false)]
  106. [InlineData(double.NaN, double.NaN, false)]
  107. public void operator_equals_with_BsonDecimal128_should_return_expected_result(double lhsDoubleValue, double rhsDoubleValue, bool expectedResult)
  108. {
  109. var lhs = new BsonDouble(lhsDoubleValue);
  110. var rhs = new BsonDecimal128(double.IsNaN(rhsDoubleValue) ? Decimal128.QNaN : (Decimal128)(decimal)rhsDoubleValue);
  111. var result = lhs == rhs;
  112. result.Should().Be(expectedResult);
  113. }
  114. [Theory]
  115. [InlineData(1.0, 1.0, true)]
  116. [InlineData(1.0, 2.0, false)]
  117. [InlineData(2.0, 1.0, false)]
  118. [InlineData(double.NaN, double.NaN, false)]
  119. public void operator_equals_with_BsonDouble_should_return_expected_result(double lhsDoubleValue, double rhsDoubleValue, bool expectedResult)
  120. {
  121. var lhs = new BsonDouble(lhsDoubleValue);
  122. var rhs = new BsonDouble(rhsDoubleValue);
  123. var result = lhs == rhs;
  124. result.Should().Be(expectedResult);
  125. }
  126. [Theory]
  127. [InlineData(1.0, 1, true)]
  128. [InlineData(1.0, 2, false)]
  129. [InlineData(2.0, 1, false)]
  130. public void operator_equals_with_BsonInt32_should_return_expected_result(double lshDoubleValue, int rhsInt32Value, bool expectedResult)
  131. {
  132. var lhs = new BsonDouble(lshDoubleValue);
  133. var rhs = new BsonInt32(rhsInt32Value);
  134. var result = lhs == rhs;
  135. result.Should().Be(expectedResult);
  136. }
  137. [Theory]
  138. [InlineData(1.0, 1L, true)]
  139. [InlineData(1.0, 2L, false)]
  140. [InlineData(2.0, 1L, false)]
  141. public void operator_equals_with_BsonInt64_should_return_expected_result(double lhsDoubleValue, long rhsInt64Value, bool expectedResult)
  142. {
  143. var lhs = new BsonDouble(lhsDoubleValue);
  144. var rhs = new BsonInt64(rhsInt64Value);
  145. var result = lhs == rhs;
  146. result.Should().Be(expectedResult);
  147. }
  148. [Theory]
  149. [InlineData(false, false)]
  150. [InlineData(false, true)]
  151. [InlineData(true, false)]
  152. [InlineData(true, true)]
  153. public void operator_equals_with_nulls_should_return_expected_result(bool isLhsNull, bool isRhsNull)
  154. {
  155. var lhs = isLhsNull ? null : new BsonDouble(1.0);
  156. var rhs = isRhsNull ? null : new BsonDouble(1.0);
  157. var result = lhs == rhs;
  158. result.Should().Be(isLhsNull == isRhsNull);
  159. }
  160. [Theory]
  161. [ParameterAttributeData]
  162. public void precreated_instances_should_have_the_expected_value(
  163. [Range(-100.0, 100.0, 1.0)]
  164. double value)
  165. {
  166. var result = (BsonDouble)value;
  167. result.Value.Should().Be(value);
  168. }
  169. [Theory]
  170. [InlineData(-1.0)]
  171. [InlineData(1.0)]
  172. public void ToDecimal_should_return_expected_result(double doubleValue)
  173. {
  174. var subject = new BsonDouble(doubleValue);
  175. var result = subject.ToDecimal();
  176. result.Should().Be((decimal)doubleValue);
  177. }
  178. [Theory]
  179. [InlineData(-1.0)]
  180. [InlineData(1.0)]
  181. public void ToDecimal128_should_return_expected_result(double doubleValue)
  182. {
  183. var subject = new BsonDouble(doubleValue);
  184. var result = subject.ToDecimal128();
  185. result.Should().Be((Decimal128)(decimal)doubleValue);
  186. }
  187. }
  188. }