PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 229 lines | 172 code | 43 blank | 14 comment | 6 complexity | 8c104040c48fc706f00d522789d4b18d 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 BsonInt64Tests
  26. {
  27. [Theory]
  28. [InlineData(0L, 1.0, -1)]
  29. [InlineData(0L, 0.0, 0)]
  30. [InlineData(1L, 0.0, 1)]
  31. public void CompareTo_BsonDecimal128_should_return_expected_result(long int64Value, double otherDoubleValue, int expectedResult)
  32. {
  33. var subject = new BsonInt64(int64Value);
  34. var other = new BsonDecimal128((Decimal128)(decimal)otherDoubleValue);
  35. var result = subject.CompareTo(other);
  36. result.Should().Be(expectedResult);
  37. }
  38. [Theory]
  39. [InlineData(0L, 1.0, -1)]
  40. [InlineData(0L, 0.0, 0)]
  41. [InlineData(1L, 0.0, 1)]
  42. public void CompareTo_BsonDouble_should_return_expected_result(long int64Value, double otherDoubleValue, int expectedResult)
  43. {
  44. var subject = new BsonInt64(int64Value);
  45. var other = new BsonDouble(otherDoubleValue);
  46. var result = subject.CompareTo(other);
  47. result.Should().Be(expectedResult);
  48. }
  49. [Theory]
  50. [InlineData(0L, 1, -1)]
  51. [InlineData(0L, 0, 0)]
  52. [InlineData(1L, 0, 1)]
  53. public void CompareTo_BsonInt32_should_return_expected_result(long int64Value, int otherInt32Value, int expectedResult)
  54. {
  55. var subject = new BsonInt64(int64Value);
  56. var other = new BsonInt32(otherInt32Value);
  57. var result = subject.CompareTo(other);
  58. result.Should().Be(expectedResult);
  59. }
  60. [Theory]
  61. [InlineData(0L, 1L, -1)]
  62. [InlineData(0L, 0L, 0)]
  63. [InlineData(1L, 0L, 1)]
  64. public void CompareTo_BsonInt64_should_return_expected_result(long int64Value, long otherInt64Value, int expectedResult)
  65. {
  66. var subject = new BsonInt64(int64Value);
  67. var other = new BsonInt64(otherInt64Value);
  68. var result1 = subject.CompareTo((BsonInt64)other);
  69. var result2 = subject.CompareTo((BsonValue)other);
  70. result1.Should().Be(expectedResult);
  71. result2.Should().Be(expectedResult);
  72. }
  73. [Fact]
  74. public void CompareTo_null_should_return_expected_result()
  75. {
  76. var subject = new BsonInt64(0);
  77. var result1 = subject.CompareTo((BsonInt64)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_long_should_return_new_instance(
  85. [Values(-101L, 101L)]
  86. long value)
  87. {
  88. var result1 = (BsonInt64)value;
  89. var result2 = (BsonInt64)value;
  90. result2.Should().NotBeSameAs(result1);
  91. }
  92. [Theory]
  93. [ParameterAttributeData]
  94. public void implicit_conversion_from_long_should_return_precreated_instance(
  95. [Range(-100L, 100L, 1L)]
  96. long value)
  97. {
  98. var result1 = (BsonInt64)value;
  99. var result2 = (BsonInt64)value;
  100. result2.Should().BeSameAs(result1);
  101. }
  102. [Theory]
  103. [InlineData(1L, 1.0, true)]
  104. [InlineData(1L, 2.0, false)]
  105. [InlineData(2L, 1.0, false)]
  106. public void operator_equals_with_BsonDecimal128_should_return_expected_result(long lhsInt64Value, double rhsDoubleValue, bool expectedResult)
  107. {
  108. var lhs = new BsonInt64(lhsInt64Value);
  109. var rhs = new BsonDecimal128((Decimal128)(decimal)rhsDoubleValue);
  110. var result = lhs == rhs;
  111. result.Should().Be(expectedResult);
  112. }
  113. [Theory]
  114. [InlineData(1L, 1.0, true)]
  115. [InlineData(1L, 2.0, false)]
  116. [InlineData(2L, 1.0, false)]
  117. public void operator_equals_with_BsonDouble_should_return_expected_result(long lhsInt64Value, double rhsDoubleValue, bool expectedResult)
  118. {
  119. var lhs = new BsonInt64(lhsInt64Value);
  120. var rhs = new BsonDouble(rhsDoubleValue);
  121. var result = lhs == rhs;
  122. result.Should().Be(expectedResult);
  123. }
  124. [Theory]
  125. [InlineData(1L, 1, true)]
  126. [InlineData(1L, 2, false)]
  127. [InlineData(2L, 1, false)]
  128. public void operator_equals_with_BsonInt32_should_return_expected_result(long lhsInt64Value, int rhsInt32Value, bool expectedResult)
  129. {
  130. var lhs = new BsonInt64(lhsInt64Value);
  131. var rhs = new BsonInt32(rhsInt32Value);
  132. var result = lhs == rhs;
  133. result.Should().Be(expectedResult);
  134. }
  135. [Theory]
  136. [InlineData(1L, 1L, true)]
  137. [InlineData(1L, 2L, false)]
  138. [InlineData(2L, 1L, false)]
  139. public void operator_equals_with_BsonInt64_should_return_expected_result(long lhsInt64Value, long rhsInt64Value, bool expectedResult)
  140. {
  141. var lhs = new BsonInt64(lhsInt64Value);
  142. var rhs = new BsonInt64(rhsInt64Value);
  143. var result = lhs == rhs;
  144. result.Should().Be(expectedResult);
  145. }
  146. [Theory]
  147. [InlineData(false, false)]
  148. [InlineData(false, true)]
  149. [InlineData(true, false)]
  150. [InlineData(true, true)]
  151. public void operator_equals_with_nulls_should_return_expected_result(bool isLhsNull, bool isRhsNull)
  152. {
  153. var lhs = isLhsNull ? null : new BsonInt64(1);
  154. var rhs = isRhsNull ? null : new BsonInt64(1);
  155. var result = lhs == rhs;
  156. result.Should().Be(isLhsNull == isRhsNull);
  157. }
  158. [Theory]
  159. [ParameterAttributeData]
  160. public void precreated_instances_should_have_the_expected_value(
  161. [Range(-100L, 100L, 1L)]
  162. long value)
  163. {
  164. var result = (BsonInt64)value;
  165. result.Value.Should().Be(value);
  166. }
  167. [Theory]
  168. [InlineData(-1L)]
  169. [InlineData(1L)]
  170. public void ToDecimal_should_return_expected_result(long int64Value)
  171. {
  172. var subject = new BsonInt64(int64Value);
  173. var result = subject.ToDecimal();
  174. result.Should().Be((decimal)int64Value);
  175. }
  176. [Theory]
  177. [InlineData(-1L)]
  178. [InlineData(1L)]
  179. public void ToDecimal128_should_return_expected_result(long int64Value)
  180. {
  181. var subject = new BsonInt64(int64Value);
  182. var result = subject.ToDecimal128();
  183. result.Should().Be((Decimal128)int64Value);
  184. }
  185. }
  186. }