PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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