PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 900 lines | 681 code | 203 blank | 16 comment | 0 complexity | a9d8811487a49fd79fda4b695739b854 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.Linq;
  17. using System.Reflection;
  18. using FluentAssertions;
  19. using MongoDB.Bson.IO;
  20. using MongoDB.Bson.TestHelpers.XunitExtensions;
  21. using Xunit;
  22. namespace MongoDB.Bson.Tests.IO
  23. {
  24. public class ByteArrayBufferTests
  25. {
  26. [Theory]
  27. [InlineData(0, 0)]
  28. [InlineData(1, 1)]
  29. [InlineData(2, 2)]
  30. public void AccessBackingBytes_should_return_expected_result_for_length(int length, int expectedCount)
  31. {
  32. var bytes = new byte[2];
  33. var subject = CreateSubject(bytes, length);
  34. var result = subject.AccessBackingBytes(0);
  35. result.Array.Should().BeSameAs(bytes);
  36. result.Offset.Should().Be(0);
  37. result.Count.Should().Be(expectedCount);
  38. }
  39. [Theory]
  40. [InlineData(0, 0, 2)]
  41. [InlineData(1, 1, 1)]
  42. [InlineData(2, 2, 0)]
  43. public void AccessBackingBytes_should_return_expected_result_for_position(int position, int expectedOffset, int expectedCount)
  44. {
  45. var bytes = new byte[2];
  46. var subject = CreateSubject(bytes);
  47. var result = subject.AccessBackingBytes(position);
  48. result.Array.Should().BeSameAs(bytes);
  49. result.Offset.Should().Be(expectedOffset);
  50. result.Count.Should().Be(expectedCount);
  51. }
  52. [Theory]
  53. [ParameterAttributeData]
  54. public void AccessBackingBytes_should_throw_when_position_is_invalid(
  55. [Values(-1, 3)]
  56. int position)
  57. {
  58. var subject = CreateSubject(2);
  59. Action action = () => subject.AccessBackingBytes(position);
  60. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  61. }
  62. [Fact]
  63. public void AccessBackingBytes_should_throw_when_subject_is_disposed()
  64. {
  65. var subject = CreateDisposedSubject();
  66. Action action = () => subject.AccessBackingBytes(0);
  67. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  68. }
  69. [Theory]
  70. [InlineData(false, 2)]
  71. [InlineData(true, 1)]
  72. public void Capacity_get_should_return_expected_result(bool isReadOnly, int expectedResult)
  73. {
  74. var subject = CreateSubject(2, 1, isReadOnly);
  75. var result = subject.Capacity;
  76. result.Should().Be(expectedResult);
  77. }
  78. [Fact]
  79. public void Capacity_get_should_throw_when_subject_is_disposed()
  80. {
  81. var subject = CreateDisposedSubject();
  82. Action action = () => { var _ = subject.Capacity; };
  83. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  84. }
  85. [Theory]
  86. [InlineData(0, new byte[] { 1, 2 })]
  87. [InlineData(1, new byte[] { 0, 2 })]
  88. [InlineData(2, new byte[] { 0, 0 })]
  89. public void Clear_should_have_expected_effect_for_count(int count, byte[] expectedBytes)
  90. {
  91. var bytes = new byte[] { 1, 2 };
  92. var subject = CreateSubject(bytes);
  93. subject.Clear(0, count);
  94. bytes.Should().Equal(expectedBytes);
  95. }
  96. [Theory]
  97. [InlineData(1, new byte[] { 1, 0, 3 })]
  98. [InlineData(2, new byte[] { 1, 2, 0 })]
  99. public void Clear_should_have_expected_effect_for_position(int position, byte[] expectedBytes)
  100. {
  101. var bytes = new byte[] { 1, 2, 3 };
  102. var subject = CreateSubject(bytes);
  103. subject.Clear(position, 1);
  104. bytes.Should().Equal(expectedBytes);
  105. }
  106. [Theory]
  107. [InlineData(0, -1)]
  108. [InlineData(0, 3)]
  109. [InlineData(1, 2)]
  110. [InlineData(2, 1)]
  111. public void Clear_should_throw_when_count_is_invalid(int position, int count)
  112. {
  113. var subject = CreateSubject(2);
  114. Action action = () => subject.Clear(position, count);
  115. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  116. }
  117. [Theory]
  118. [ParameterAttributeData]
  119. public void Clear_should_throw_when_position_is_invalid(
  120. [Values(-1, 3)]
  121. int position)
  122. {
  123. var subject = CreateSubject(2);
  124. Action action = () => subject.Clear(position, 0);
  125. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  126. }
  127. [Fact]
  128. public void Clear_should_throw_when_subject_is_disposed()
  129. {
  130. var subject = CreateDisposedSubject();
  131. Action action = () => subject.Clear(0, 0);
  132. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  133. }
  134. [Fact]
  135. public void Clear_should_throw_when_subject_is_read_only()
  136. {
  137. var subject = CreateSubject(isReadOnly: true);
  138. Action action = () => subject.Clear(0, 0);
  139. action.ShouldThrow<InvalidOperationException>();
  140. }
  141. [Theory]
  142. [ParameterAttributeData]
  143. public void constructor_with_bytes_and_length_should_initialize_subject(
  144. [Values(0, 1)]
  145. int length,
  146. [Values(false, true)]
  147. bool isReadOnly)
  148. {
  149. var bytes = new byte[1];
  150. var subject = new ByteArrayBuffer(bytes, length, isReadOnly);
  151. var reflector = new Reflector(subject);
  152. subject.IsReadOnly.Should().Be(isReadOnly);
  153. subject.Length.Should().Be(length);
  154. reflector._bytes.Should().BeSameAs(bytes);
  155. reflector._disposed.Should().BeFalse();
  156. }
  157. [Fact]
  158. public void constructor_with_bytes_and_length_should_throw_when_bytes_is_null()
  159. {
  160. Action action = () => new ByteArrayBuffer(null, 0);
  161. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("bytes");
  162. }
  163. [Theory]
  164. [InlineData(0, -1)]
  165. [InlineData(0, 1)]
  166. [InlineData(1, 2)]
  167. public void constructor_with_bytes_and_length_should_throw_when_length_is_invalid(int size, int length)
  168. {
  169. var bytes = new byte[size];
  170. Action action = () => new ByteArrayBuffer(bytes, length);
  171. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("length");
  172. }
  173. [Theory]
  174. [ParameterAttributeData]
  175. public void constructor_with_bytes_should_initialize_subject(
  176. [Values(false, true)]
  177. bool isReadOnly)
  178. {
  179. var bytes = new byte[1];
  180. var subject = new ByteArrayBuffer(bytes, isReadOnly);
  181. var reflector = new Reflector(subject);
  182. subject.IsReadOnly.Should().Be(isReadOnly);
  183. subject.Length.Should().Be(bytes.Length);
  184. reflector._bytes.Should().BeSameAs(bytes);
  185. reflector._disposed.Should().BeFalse();
  186. }
  187. [Fact]
  188. public void constructor_with_bytes_should_throw_when_bytes_is_null()
  189. {
  190. Action action = () => new ByteArrayBuffer(null);
  191. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("bytes");
  192. }
  193. [Fact]
  194. public void Dispose_can_be_called_multiple_times()
  195. {
  196. var subject = CreateSubject();
  197. subject.Dispose();
  198. subject.Dispose();
  199. }
  200. [Fact]
  201. public void Dispose_should_dispose_subject()
  202. {
  203. var subject = CreateSubject();
  204. subject.Dispose();
  205. var reflector = new Reflector(subject);
  206. reflector._disposed.Should().BeTrue();
  207. }
  208. [Theory]
  209. [InlineData(0, 1, 32)]
  210. [InlineData(0, 31, 32)]
  211. [InlineData(0, 32, 32)]
  212. [InlineData(0, 33, 64)]
  213. [InlineData(0, 63, 64)]
  214. [InlineData(0, 64, 64)]
  215. [InlineData(0, 65, 128)]
  216. public void EnsureCapacity_should_have_expected_effect(int size, int minimumCapacity, int expectedCapacity)
  217. {
  218. var bytes = Enumerable.Range(1, size).Select(n => (byte)n).ToArray();
  219. var subject = CreateSubject(bytes);
  220. subject.EnsureCapacity(minimumCapacity);
  221. var reflector = new Reflector(subject);
  222. subject.Capacity.Should().Be(expectedCapacity);
  223. reflector._bytes.Take(size).Should().Equal(bytes);
  224. reflector._bytes.Skip(size).Should().Equal(Enumerable.Repeat<byte>(0, expectedCapacity - size));
  225. }
  226. [Fact]
  227. public void EnsureCapacity_should_throw_when_minimumCapacity_is_invalid()
  228. {
  229. var subject = CreateSubject();
  230. Action action = () => subject.EnsureCapacity(-1);
  231. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("minimumCapacity");
  232. }
  233. [Fact]
  234. public void EnsureCapacity_should_throw_when_subject_is_disposed()
  235. {
  236. var subject = CreateDisposedSubject();
  237. Action action = () => subject.EnsureCapacity(0);
  238. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  239. }
  240. [Fact]
  241. public void EnsureCapacity_should_throw_when_subject_is_read_only()
  242. {
  243. var subject = CreateSubject(isReadOnly: true);
  244. Action action = () => subject.EnsureCapacity(0);
  245. action.ShouldThrow<InvalidOperationException>();
  246. }
  247. [Theory]
  248. [InlineData(1, 2)]
  249. [InlineData(2, 3)]
  250. public void GetByte_should_return_expected_result(int position, byte expectedResult)
  251. {
  252. var bytes = new byte[] { 1, 2, 3 };
  253. var subject = CreateSubject(bytes);
  254. var result = subject.GetByte(position);
  255. result.Should().Be(expectedResult);
  256. }
  257. [Theory]
  258. [ParameterAttributeData]
  259. public void GetByte_should_throw_when_position_is_invalid(
  260. [Values(-1, 3)]
  261. int position)
  262. {
  263. var subject = CreateSubject(2);
  264. Action action = () => subject.GetByte(position);
  265. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  266. }
  267. [Fact]
  268. public void GetByte_should_throw_when_subject_is_disposed()
  269. {
  270. var subject = CreateDisposedSubject();
  271. Action action = () => subject.GetByte(0);
  272. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  273. }
  274. [Theory]
  275. [InlineData(0, new byte[] { 0, 0 })]
  276. [InlineData(1, new byte[] { 1, 0 })]
  277. [InlineData(2, new byte[] { 1, 2 })]
  278. public void GetBytes_should_have_expected_effect_for_count(int count, byte[] expectedBytes)
  279. {
  280. var bytes = new byte[] { 1, 2 };
  281. var subject = CreateSubject(bytes);
  282. var destination = new byte[2];
  283. subject.GetBytes(0, destination, 0, count);
  284. destination.Should().Equal(expectedBytes);
  285. }
  286. [Theory]
  287. [InlineData(1, new byte[] { 0, 1, 2, 0 })]
  288. [InlineData(2, new byte[] { 0, 0, 1, 2 })]
  289. public void GetBytes_should_have_expected_effect_for_offset(int offset, byte[] expectedBytes)
  290. {
  291. var bytes = new byte[] { 1, 2 };
  292. var subject = CreateSubject(bytes);
  293. var destination = new byte[4];
  294. subject.GetBytes(0, destination, offset, 2);
  295. destination.Should().Equal(expectedBytes);
  296. }
  297. [Theory]
  298. [InlineData(1, new byte[] { 2, 3 })]
  299. [InlineData(2, new byte[] { 3, 4 })]
  300. public void GetBytes_should_have_expected_effect_for_position(int position, byte[] expectedBytes)
  301. {
  302. var bytes = new byte[] { 1, 2, 3, 4 };
  303. var subject = CreateSubject(bytes);
  304. var destination = new byte[2];
  305. subject.GetBytes(position, destination, 0, 2);
  306. destination.Should().Equal(expectedBytes);
  307. }
  308. [Theory]
  309. [InlineData(0, -1)]
  310. [InlineData(0, 3)]
  311. [InlineData(1, 2)]
  312. [InlineData(2, 1)]
  313. public void GetBytes_should_throw_when_count_is_invalid_for_buffer(int position, int count)
  314. {
  315. var subject = CreateSubject(2);
  316. var destination = new byte[3];
  317. Action action = () => subject.GetBytes(position, destination, 0, count);
  318. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  319. }
  320. [Theory]
  321. [InlineData(0, -1)]
  322. [InlineData(0, 3)]
  323. [InlineData(1, 2)]
  324. [InlineData(2, 1)]
  325. public void GetBytes_should_throw_when_count_is_invalid_for_destination(int offset, int count)
  326. {
  327. var subject = CreateSubject(3);
  328. var destination = new byte[2];
  329. Action action = () => subject.GetBytes(0, destination, offset, count);
  330. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  331. }
  332. [Fact]
  333. public void GetBytes_should_throw_when_destination_is_null()
  334. {
  335. var subject = CreateSubject();
  336. Action action = () => subject.GetBytes(0, null, 0, 0);
  337. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("destination");
  338. }
  339. [Theory]
  340. [ParameterAttributeData]
  341. public void GetBytes_should_throw_when_offset_is_invalid(
  342. [Values(-1, 3)]
  343. int offset)
  344. {
  345. var subject = CreateSubject(4);
  346. var destination = new byte[2];
  347. Action action = () => subject.GetBytes(0, destination, offset, 0);
  348. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");
  349. }
  350. [Theory]
  351. [ParameterAttributeData]
  352. public void GetBytes_should_throw_when_position_is_invalid(
  353. [Values(-1, 3)]
  354. int position)
  355. {
  356. var subject = CreateSubject(2);
  357. var destination = new byte[0];
  358. Action action = () => subject.GetBytes(position, destination, 0, 0);
  359. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  360. }
  361. [Fact]
  362. public void GetBytes_should_throw_when_subject_is_disposed()
  363. {
  364. var subject = CreateDisposedSubject();
  365. var destination = new byte[0];
  366. Action action = () => subject.GetBytes(0, destination, 0, 0);
  367. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  368. }
  369. [Theory]
  370. [InlineData(0, 2)]
  371. [InlineData(1, 1)]
  372. [InlineData(2, 0)]
  373. public void GetSlice_should_return_expected_result(int position, int length)
  374. {
  375. var bytes = new byte[2];
  376. var subject = CreateSubject(bytes, isReadOnly: true);
  377. var result = subject.GetSlice(position, length);
  378. result.AccessBackingBytes(0).Array.Should().BeSameAs(bytes);
  379. result.AccessBackingBytes(0).Offset.Should().Be(position);
  380. result.AccessBackingBytes(0).Count.Should().Be(length);
  381. }
  382. [Fact]
  383. public void GetSlice_should_return_slice_that_does_not_dispose_subject_when_slice_is_disposed()
  384. {
  385. var bytes = new byte[] { 1, 2, 3 };
  386. var subject = CreateSubject(bytes, isReadOnly: true);
  387. var slice = subject.GetSlice(1, 1);
  388. slice.Dispose();
  389. subject.GetByte(1).Should().Be(2);
  390. }
  391. [Fact]
  392. public void GetSlice_should_return_slice_that_is_not_disposed_when_subject_is_disposed()
  393. {
  394. var bytes = new byte[] { 1, 2, 3 };
  395. var subject = CreateSubject(bytes, isReadOnly: true);
  396. var slice = subject.GetSlice(1, 1);
  397. subject.Dispose();
  398. slice.GetByte(0).Should().Be(2);
  399. }
  400. [Theory]
  401. [InlineData(0, -1)]
  402. [InlineData(0, 3)]
  403. [InlineData(1, 2)]
  404. [InlineData(2, 1)]
  405. public void GetSlice_should_throw_when_length_is_invalid(int position, int length)
  406. {
  407. var subject = CreateSubject(2, isReadOnly: true);
  408. Action action = () => subject.GetSlice(position, length);
  409. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("length");
  410. }
  411. [Theory]
  412. [ParameterAttributeData]
  413. public void GetSlice_should_throw_when_position_is_invalid(
  414. [Values(-1, 3)]
  415. int position)
  416. {
  417. var subject = CreateSubject(2, isReadOnly: true);
  418. Action action = () => subject.GetSlice(position, 0);
  419. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  420. }
  421. [Fact]
  422. public void GetSlice_should_throw_when_subject_is_disposed()
  423. {
  424. var subject = CreateDisposedSubject();
  425. Action action = () => subject.GetSlice(0, 0);
  426. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  427. }
  428. [Fact]
  429. public void GetSlice_should_throw_when_subject_is_not_read_only()
  430. {
  431. var subject = CreateSubject(isReadOnly: false);
  432. Action action = () => subject.GetSlice(0, 0);
  433. action.ShouldThrow<InvalidOperationException>();
  434. }
  435. [Theory]
  436. [ParameterAttributeData]
  437. public void IsReadOnly_get_should_return_expected_result(
  438. [Values(false, true)]
  439. bool isReadOnly)
  440. {
  441. var subject = CreateSubject(0, isReadOnly: isReadOnly);
  442. var result = subject.IsReadOnly;
  443. result.Should().Be(isReadOnly);
  444. }
  445. [Fact]
  446. public void IsReadOnly_get_should_throw_when_subject_is_disposed()
  447. {
  448. var subject = CreateDisposedSubject();
  449. Action action = () => { var _ = subject.IsReadOnly; };
  450. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  451. }
  452. [Theory]
  453. [ParameterAttributeData]
  454. public void Length_get_should_return_expected_result(
  455. [Values(1, 2)]
  456. int length)
  457. {
  458. var subject = CreateSubject(2, length);
  459. var result = subject.Length;
  460. result.Should().Be(length);
  461. }
  462. [Fact]
  463. public void Length_get_should_throw_when_subject_is_disposed()
  464. {
  465. var subject = CreateDisposedSubject();
  466. Action action = () => { var _ = subject.Length; };
  467. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  468. }
  469. [Theory]
  470. [ParameterAttributeData]
  471. public void Length_set_should_have_expected_effect(
  472. [Values(1, 2)]
  473. int length)
  474. {
  475. var subject = CreateSubject(2, 0);
  476. subject.Length = length;
  477. subject.Length.Should().Be(length);
  478. }
  479. [Fact]
  480. public void Length_set_should_throw_when_subject_is_read_only()
  481. {
  482. var subject = CreateSubject(0, 0, isReadOnly: true);
  483. Action action = () => subject.Length = 0;
  484. action.ShouldThrow<InvalidOperationException>();
  485. }
  486. [Theory]
  487. [InlineData(0, -1)]
  488. [InlineData(0, 1)]
  489. [InlineData(1, 2)]
  490. [InlineData(2, 3)]
  491. public void Length_set_should_throw_when_value_is_invalid(int size, int value)
  492. {
  493. var subject = CreateSubject(size, 0);
  494. Action action = () => subject.Length = value;
  495. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("value");
  496. }
  497. [Fact]
  498. public void Length_set_should_throw_when_subject_is_disposed()
  499. {
  500. var subject = CreateDisposedSubject();
  501. Action action = () => subject.Length = 0;
  502. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  503. }
  504. [Theory]
  505. [ParameterAttributeData]
  506. public void MakeReadOnly_should_have_expected_effect(
  507. [Values(false, true)]
  508. bool isReadOnly)
  509. {
  510. var subject = CreateSubject(isReadOnly: isReadOnly);
  511. subject.MakeReadOnly();
  512. subject.IsReadOnly.Should().BeTrue();
  513. }
  514. [Fact]
  515. public void MakeReadOnly_should_throw_when_subject_is_disposed()
  516. {
  517. var subject = CreateDisposedSubject();
  518. Action action = () => subject.MakeReadOnly();
  519. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  520. }
  521. [Theory]
  522. [InlineData(1, new byte[] { 0, 1, 0 })]
  523. [InlineData(2, new byte[] { 0, 0, 1 })]
  524. public void SetByte_should_have_expected_effect(int position, byte[] expectedBytes)
  525. {
  526. var bytes = new byte[3];
  527. var subject = CreateSubject(bytes);
  528. subject.SetByte(position, 1);
  529. bytes.Should().Equal(expectedBytes);
  530. }
  531. [Theory]
  532. [ParameterAttributeData]
  533. public void SetByte_should_throw_when_position_is_invalid(
  534. [Values(-1, 3)]
  535. int position)
  536. {
  537. var subject = CreateSubject(2);
  538. Action action = () => subject.SetByte(position, 0);
  539. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  540. }
  541. [Fact]
  542. public void SetByte_should_throw_when_subject_is_disposed()
  543. {
  544. var subject = CreateDisposedSubject();
  545. Action action = () => subject.SetByte(0, 0);
  546. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  547. }
  548. [Fact]
  549. public void SetByte_should_throw_when_subject_is_read_only()
  550. {
  551. var subject = CreateSubject(isReadOnly: true);
  552. Action action = () => subject.SetByte(0, 0);
  553. action.ShouldThrow<InvalidOperationException>();
  554. }
  555. [Theory]
  556. [InlineData(1, new byte[] { 1, 0 })]
  557. [InlineData(2, new byte[] { 1, 2 })]
  558. public void SetBytes_should_have_expected_effect_for_count(int count, byte[] expectedBytes)
  559. {
  560. var bytes = new byte[2];
  561. var subject = CreateSubject(bytes);
  562. var source = new byte[] { 1, 2 };
  563. subject.SetBytes(0, source, 0, count);
  564. bytes.Should().Equal(expectedBytes);
  565. }
  566. [Theory]
  567. [InlineData(1, new byte[] { 2 })]
  568. [InlineData(2, new byte[] { 3 })]
  569. public void SetBytes_should_have_expected_effect_for_offset(int offset, byte[] expectedBytes)
  570. {
  571. var bytes = new byte[1];
  572. var subject = CreateSubject(bytes);
  573. var source = new byte[] { 1, 2, 3 };
  574. subject.SetBytes(0, source, offset, 1);
  575. bytes.Should().Equal(expectedBytes);
  576. }
  577. [Theory]
  578. [InlineData(1, new byte[] { 0, 1, 0 })]
  579. [InlineData(2, new byte[] { 0, 0, 1 })]
  580. public void SetBytes_should_have_expected_effect_for_position(int position, byte[] expectedBytes)
  581. {
  582. var bytes = new byte[3];
  583. var subject = CreateSubject(bytes);
  584. var source = new byte[] { 1 };
  585. subject.SetBytes(position, source, 0, 1);
  586. bytes.Should().Equal(expectedBytes);
  587. }
  588. [Theory]
  589. [InlineData(0, -1)]
  590. [InlineData(1, 2)]
  591. [InlineData(2, 1)]
  592. public void SetBytes_should_throw_when_count_is_invalid_for_buffer(int position, int count)
  593. {
  594. var subject = CreateSubject(2);
  595. var source = new byte[4];
  596. Action action = () => subject.SetBytes(position, source, 0, count);
  597. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  598. }
  599. [Theory]
  600. [InlineData(0, -1)]
  601. [InlineData(1, 2)]
  602. [InlineData(2, 1)]
  603. public void SetBytes_should_throw_when_count_is_invalid_for_source(int offset, int count)
  604. {
  605. var subject = CreateSubject(2);
  606. var source = new byte[2];
  607. Action action = () => subject.SetBytes(0, source, offset, count);
  608. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  609. }
  610. [Theory]
  611. [ParameterAttributeData]
  612. public void SetBytes_should_throw_when_offset_is_invalid(
  613. [Values(-1, 3)]
  614. int offset)
  615. {
  616. var subject = CreateSubject(0);
  617. var source = new byte[2];
  618. Action action = () => subject.SetBytes(0, source, offset, 0);
  619. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");
  620. }
  621. [Theory]
  622. [ParameterAttributeData]
  623. public void SetBytes_should_throw_when_position_is_invalid(
  624. [Values(-1, 3)]
  625. int position)
  626. {
  627. var subject = CreateSubject(2);
  628. var source = new byte[0];
  629. Action action = () => subject.SetBytes(position, source, 0, 0);
  630. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("position");
  631. }
  632. [Fact]
  633. public void SetBytes_should_throw_when_source_is_null()
  634. {
  635. var subject = CreateSubject();
  636. Action action = () => subject.SetBytes(0, null, 0, 0);
  637. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("source");
  638. }
  639. [Fact]
  640. public void SetBytes_should_throw_when_subject_is_disposed()
  641. {
  642. var subject = CreateDisposedSubject();
  643. var source = new byte[0];
  644. Action action = () => subject.SetBytes(0, source, 0, 0);
  645. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayBuffer");
  646. }
  647. [Fact]
  648. public void SetBytes_should_throw_when_subject_is_read_only()
  649. {
  650. var subject = CreateSubject(isReadOnly: true);
  651. var source = new byte[0];
  652. Action action = () => subject.SetBytes(0, source, 0, 0);
  653. action.ShouldThrow<InvalidOperationException>();
  654. }
  655. // helper methods
  656. private ByteArrayBuffer CreateDisposedSubject()
  657. {
  658. var subject = CreateSubject();
  659. subject.Dispose();
  660. return subject;
  661. }
  662. private ByteArrayBuffer CreateSubject(byte[] bytes, int? length = null, bool isReadOnly = false)
  663. {
  664. return new ByteArrayBuffer(bytes, length ?? bytes.Length, isReadOnly);
  665. }
  666. private ByteArrayBuffer CreateSubject(int size = 0, int? length = null, bool isReadOnly = false)
  667. {
  668. var bytes = new byte[size];
  669. return new ByteArrayBuffer(bytes, length ?? size, isReadOnly);
  670. }
  671. // nested types
  672. private class Reflector
  673. {
  674. private readonly ByteArrayBuffer _instance;
  675. public Reflector(ByteArrayBuffer instance)
  676. {
  677. _instance = instance;
  678. }
  679. public byte[] _bytes
  680. {
  681. get
  682. {
  683. var field = typeof(ByteArrayBuffer).GetField("_bytes", BindingFlags.NonPublic | BindingFlags.Instance);
  684. return (byte[])field.GetValue(_instance);
  685. }
  686. }
  687. public bool _disposed
  688. {
  689. get
  690. {
  691. var field = typeof(ByteArrayBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance);
  692. return (bool)field.GetValue(_instance);
  693. }
  694. }
  695. }
  696. }
  697. }