PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 279 lines | 214 code | 51 blank | 14 comment | 11 complexity | d9da886ff5297622b3d319ac63fba986 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 FluentAssertions;
  17. using Xunit;
  18. namespace MongoDB.Bson.Tests.ObjectModel
  19. {
  20. public class BsonTimestampTests
  21. {
  22. [Fact]
  23. public void BsonType_get_should_return_expected_result()
  24. {
  25. var subject = new BsonTimestamp(0);
  26. var result = subject.BsonType;
  27. result.Should().Be(BsonType.Timestamp);
  28. }
  29. [Theory]
  30. [InlineData(0L, null, 1)]
  31. [InlineData(0L, 0L, 0)]
  32. [InlineData(0L, -1L, 1)]
  33. [InlineData(0L, 1L, -1)]
  34. [InlineData(-1L, 0L, -1)]
  35. [InlineData(1L, 0L, 1)]
  36. public void CompareTo_should_return_expected_result(long value1, long? value2, int expectedResult)
  37. {
  38. var subject = new BsonTimestamp(value1);
  39. var other = value2 == null ? null : new BsonTimestamp(value2.Value);
  40. var result1 = subject.CompareTo(other);
  41. var result2 = subject.CompareTo((BsonValue)other);
  42. result1.Should().Be(expectedResult);
  43. result2.Should().Be(expectedResult);
  44. }
  45. [Fact]
  46. public void CompareTo_should_return_minus_one_when_other_type_compares_higher()
  47. {
  48. var subject = new BsonTimestamp(0);
  49. var result = subject.CompareTo(BsonMaxKey.Value);
  50. result.Should().Be(-1);
  51. }
  52. [Fact]
  53. public void CompareTo_should_return_plus_one_when_other_type_compares_lower()
  54. {
  55. var subject = new BsonTimestamp(0);
  56. var result = subject.CompareTo(BsonMinKey.Value);
  57. result.Should().Be(1);
  58. }
  59. [Theory]
  60. [InlineData(0, 0, 0UL)]
  61. [InlineData(1, 2, 0x100000002UL)]
  62. [InlineData(-1, -2, 0xfffffffffffffffeUL)]
  63. [InlineData(int.MinValue, int.MinValue, 0x8000000080000000UL)]
  64. [InlineData(int.MaxValue, int.MaxValue, 0x7fffffff7fffffffUL)]
  65. [InlineData(int.MinValue, int.MaxValue, 0x800000007fffffffUL)]
  66. [InlineData(int.MaxValue, int.MinValue, 0x7fffffff80000000UL)]
  67. public void constructor_with_timestamp_increment_should_initialize_instance(int timestamp, int increment, ulong expectedValue)
  68. {
  69. var result = new BsonTimestamp(timestamp, increment);
  70. result.Value.Should().Be((long)expectedValue);
  71. }
  72. [Theory]
  73. [InlineData(0)]
  74. [InlineData(long.MinValue)]
  75. [InlineData(long.MaxValue)]
  76. public void constructor_with_value_should_initialize_instance(long value)
  77. {
  78. var result = new BsonTimestamp(value);
  79. result.Value.Should().Be(value);
  80. }
  81. [Theory]
  82. [InlineData(0L, 0L)]
  83. [InlineData(long.MinValue, long.MinValue)]
  84. [InlineData(long.MaxValue, long.MaxValue)]
  85. [InlineData(1UL, 1L)]
  86. [InlineData(ulong.MinValue, 0L)]
  87. [InlineData(ulong.MaxValue, -1L)]
  88. public void Create_should_return_expected_result(object value, long expectedValue)
  89. {
  90. var result = BsonTimestamp.Create(value);
  91. result.Value.Should().Be(expectedValue);
  92. }
  93. [Fact]
  94. public void Create_should_throw_when_value_is_null()
  95. {
  96. Action action = () => { BsonTimestamp.Create(null); };
  97. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("value");
  98. }
  99. [Fact]
  100. public void Equals_should_return_false_when_other_is_null()
  101. {
  102. var subject = new BsonTimestamp(0);
  103. BsonTimestamp other = null;
  104. var result1 = subject.Equals(other);
  105. var result2 = subject.Equals((object)other);
  106. result1.Should().BeFalse();
  107. result2.Should().BeFalse();
  108. }
  109. [Fact]
  110. public void Equals_should_return_false_when_other_is_wrong_type()
  111. {
  112. var subject = new BsonTimestamp(0);
  113. var other = new object();
  114. var result1 = subject.Equals(other);
  115. var result2 = subject.Equals((object)other);
  116. result1.Should().BeFalse();
  117. result2.Should().BeFalse();
  118. }
  119. [Theory]
  120. [InlineData(0L, 1L)]
  121. public void Equals_should_return_false_when_values_are_not_equal(long value1, long value2)
  122. {
  123. var subject = new BsonTimestamp(value1);
  124. var other = new BsonTimestamp(value2);
  125. var result1 = subject.Equals(other);
  126. var result2 = subject.Equals((object)other);
  127. var subjectHashCode = subject.GetHashCode();
  128. var otherHashCode = other.GetHashCode();
  129. result1.Should().BeFalse();
  130. result2.Should().BeFalse();
  131. otherHashCode.Should().NotBe(subjectHashCode);
  132. }
  133. [Theory]
  134. [InlineData(0L)]
  135. [InlineData(long.MinValue)]
  136. [InlineData(long.MaxValue)]
  137. public void Equals_should_return_true_when_values_are_equal(long value)
  138. {
  139. var subject = new BsonTimestamp(value);
  140. var other = new BsonTimestamp(value);
  141. other.Should().NotBeSameAs(subject);
  142. var result1 = subject.Equals(other);
  143. var result2 = subject.Equals((object)other);
  144. var subjectHashCode = subject.GetHashCode();
  145. var otherHashCode = other.GetHashCode();
  146. result1.Should().BeTrue();
  147. result2.Should().BeTrue();
  148. otherHashCode.Should().Be(subjectHashCode);
  149. }
  150. [Theory]
  151. [InlineData(0UL, 0)]
  152. [InlineData(0x100000002UL, 2)]
  153. [InlineData(0x7fffffff7fffffffUL, int.MaxValue)]
  154. [InlineData(0x7fffffff80000000UL, int.MinValue)]
  155. [InlineData(0x800000007fffffffUL, int.MaxValue)]
  156. [InlineData(0x8000000080000000UL, int.MinValue)]
  157. public void Increment_get_should_return_expected_result(ulong value, int expectedResult)
  158. {
  159. var subject = new BsonTimestamp((long)value);
  160. var result = subject.Increment;
  161. result.Should().Be(expectedResult);
  162. }
  163. [Theory]
  164. [InlineData(0L, 1L)]
  165. [InlineData(null, 1L)]
  166. [InlineData(0L, null)]
  167. public void operator_equals_should_return_false_when_values_are_not_equal(long? value1, long? value2)
  168. {
  169. var lhs = value1 == null ? null : new BsonTimestamp(value1.Value);
  170. var rhs = value2 == null ? null : new BsonTimestamp(value2.Value);
  171. var result1 = lhs == rhs;
  172. var result2 = lhs != rhs;
  173. result1.Should().BeFalse();
  174. result2.Should().BeTrue();
  175. }
  176. [Theory]
  177. [InlineData(null)]
  178. [InlineData(0L)]
  179. [InlineData(long.MinValue)]
  180. [InlineData(long.MaxValue)]
  181. public void operator_equals_should_return_true_when_values_are_equal(long? value)
  182. {
  183. var lhs = value == null ? null : new BsonTimestamp(value.Value);
  184. var rhs = value == null ? null : new BsonTimestamp(value.Value);
  185. if (value != null)
  186. {
  187. rhs.Should().NotBeSameAs(lhs);
  188. }
  189. var result1 = lhs == rhs;
  190. var result2 = lhs != rhs;
  191. result1.Should().BeTrue();
  192. result2.Should().BeFalse();
  193. }
  194. [Theory]
  195. [InlineData(0UL, 0)]
  196. [InlineData(0x100000002UL, 1)]
  197. [InlineData(0x7fffffff7fffffffUL, int.MaxValue)]
  198. [InlineData(0x800000007fffffffUL, int.MinValue)]
  199. [InlineData(0x7fffffff80000000UL, int.MaxValue)]
  200. [InlineData(0x8000000080000000UL, int.MinValue)]
  201. public void Timestamp_get_should_return_expected_result(ulong value, int expectedResult)
  202. {
  203. var subject = new BsonTimestamp((long)value);
  204. var result = subject.Timestamp;
  205. result.Should().Be(expectedResult);
  206. }
  207. [Theory]
  208. [InlineData(0L, "0")]
  209. [InlineData(1L, "1")]
  210. [InlineData(-1L, "-1")]
  211. public void ToString_should_return_expected_result(long value, string expectedResult)
  212. {
  213. var subject = new BsonTimestamp(value);
  214. var result = subject.ToString();
  215. result.Should().Be(expectedResult);
  216. }
  217. [Theory]
  218. [InlineData(0L)]
  219. [InlineData(long.MinValue)]
  220. [InlineData(long.MaxValue)]
  221. public void Value_get_should_return_expected_result(long value)
  222. {
  223. var subject = new BsonTimestamp(value);
  224. var result = subject.Value;
  225. result.Should().Be(value);
  226. }
  227. }
  228. }