PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

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