PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 188 lines | 137 code | 36 blank | 15 comment | 0 complexity | 8e14e0c5d956b694f3b8f0fb2ce2e2f4 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-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.Reflection;
  17. using FluentAssertions;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Bson.TestHelpers.XunitExtensions;
  20. using Xunit;
  21. namespace MongoDB.Bson.Tests.IO
  22. {
  23. public class ByteArrayChunkTests
  24. {
  25. [Fact]
  26. public void Bytes_get_should_return_expected_result()
  27. {
  28. var size = 1;
  29. var bytes = new byte[size];
  30. var subject = new ByteArrayChunk(bytes);
  31. var result = subject.Bytes;
  32. result.Array.Should().BeSameAs(bytes);
  33. result.Offset.Should().Be(0);
  34. result.Count.Should().Be(size);
  35. }
  36. [Fact]
  37. public void Bytes_get_should_throw_when_subject_is_disposed()
  38. {
  39. var subject = new ByteArrayChunk(1);
  40. subject.Dispose();
  41. Action action = () => { var _ = subject.Bytes; };
  42. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayChunk");
  43. }
  44. [Theory]
  45. [ParameterAttributeData]
  46. public void constructor_with_bytes_should_initialize_subject(
  47. [Values(1, 2, 16)]
  48. int size)
  49. {
  50. var bytes = new byte[size];
  51. var subject = new ByteArrayChunk(bytes);
  52. var segment = subject.Bytes;
  53. segment.Array.Should().BeSameAs(bytes);
  54. segment.Offset.Should().Be(0);
  55. segment.Count.Should().Be(size);
  56. }
  57. [Fact]
  58. public void constructor_with_bytes_should_throw_when_bytes_is_null()
  59. {
  60. Action action = () => new ByteArrayChunk(null);
  61. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("bytes");
  62. }
  63. [Theory]
  64. [ParameterAttributeData]
  65. public void constructor_with_size_should_initialize_subject(
  66. [Values(1, 2, 16)]
  67. int size)
  68. {
  69. var subject = new ByteArrayChunk(size);
  70. var segment = subject.Bytes;
  71. segment.Array.Should().NotBeNull();
  72. segment.Offset.Should().Be(0);
  73. segment.Count.Should().Be(size);
  74. }
  75. [Fact]
  76. public void constructor_with_size_should_throw_when_size_is_less_than_zero()
  77. {
  78. Action action = () => new ByteArrayChunk(-1);
  79. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("size");
  80. }
  81. [Fact]
  82. public void Disposed_can_be_called_more_than_once()
  83. {
  84. var subject = new ByteArrayChunk(1);
  85. subject.Dispose();
  86. subject.Dispose();
  87. }
  88. [Fact]
  89. public void Dispose_forked_handle_should_not_dispose_subject()
  90. {
  91. var subject = new ByteArrayChunk(1);
  92. var forked = subject.Fork();
  93. forked.Dispose();
  94. var reflector = new Reflector(subject);
  95. reflector._disposed.Should().BeFalse();
  96. }
  97. [Fact]
  98. public void Dispose_should_dispose_subject()
  99. {
  100. var subject = new ByteArrayChunk(1);
  101. subject.Dispose();
  102. var reflector = new Reflector(subject);
  103. reflector._disposed.Should().BeTrue();
  104. }
  105. [Fact]
  106. public void Dispose_should_not_dispose_forked_handle()
  107. {
  108. var subject = new ByteArrayChunk(1);
  109. var forked = subject.Fork();
  110. subject.Dispose();
  111. var reflector = new Reflector((ByteArrayChunk)forked);
  112. reflector._disposed.Should().BeFalse();
  113. }
  114. [Fact]
  115. public void Fork_should_return_a_new_handle()
  116. {
  117. var subject = new ByteArrayChunk(1);
  118. var result = subject.Fork();
  119. result.Should().NotBeSameAs(subject);
  120. var subjectSegment = subject.Bytes;
  121. var resultSegment = result.Bytes;
  122. resultSegment.Array.Should().BeSameAs(subjectSegment.Array);
  123. resultSegment.Offset.Should().Be(subjectSegment.Offset);
  124. resultSegment.Count.Should().Be(subjectSegment.Count);
  125. }
  126. [Fact]
  127. public void Fork_should_throw_when_subject_is_disposed()
  128. {
  129. var subject = new ByteArrayChunk(1);
  130. subject.Dispose();
  131. Action action = () => subject.Fork();
  132. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayChunk");
  133. }
  134. // nested types
  135. private class Reflector
  136. {
  137. private readonly ByteArrayChunk _instance;
  138. public Reflector(ByteArrayChunk instance)
  139. {
  140. _instance = instance;
  141. }
  142. public bool _disposed
  143. {
  144. get
  145. {
  146. var field = typeof(ByteArrayChunk).GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic);
  147. return (bool)field.GetValue(_instance);
  148. }
  149. }
  150. }
  151. }
  152. }