2
0

ExtensionsTest_Take_OffsetAndCount.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Tests.Classes.Common
  6. {
  7. [TestClass]
  8. [SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
  9. public class ExtensionsTest_Take_OffsetAndCount
  10. {
  11. private Random _random;
  12. [TestInitialize]
  13. public void Init()
  14. {
  15. _random = new Random();
  16. }
  17. [TestMethod]
  18. public void ShouldThrowArgumentNullExceptionWhenValueIsNull()
  19. {
  20. const byte[] value = null;
  21. const int offset = 0;
  22. const int count = 0;
  23. try
  24. {
  25. Extensions.Take(value, offset, count);
  26. Assert.Fail();
  27. }
  28. catch (ArgumentNullException ex)
  29. {
  30. Assert.IsNull(ex.InnerException);
  31. Assert.AreEqual("value", ex.ParamName);
  32. }
  33. }
  34. [TestMethod]
  35. public void ShouldReturnEmptyByteArrayWhenCountIsZero()
  36. {
  37. var value = CreateBuffer(16);
  38. const int offset = 25;
  39. const int count = 0;
  40. var actual = Extensions.Take(value, offset, count);
  41. Assert.IsNotNull(actual);
  42. Assert.AreEqual(0, actual.Length);
  43. }
  44. [TestMethod]
  45. public void ShouldReturnValueWhenCountIsEqualToLengthOfValueAndOffsetIsZero()
  46. {
  47. var value = CreateBuffer(16);
  48. const int offset = 0;
  49. var count = value.Length;
  50. var actual = Extensions.Take(value, offset, count);
  51. Assert.IsNotNull(actual);
  52. Assert.AreEqual(value.Length, actual.Length);
  53. Assert.AreEqual(value, actual);
  54. }
  55. [TestMethod]
  56. public void ShouldReturnLeadingBytesWhenOffsetIsZeroAndCountIsLessThanLengthOfValue()
  57. {
  58. var value = CreateBuffer(16);
  59. const int offset = 0;
  60. const int count = 5;
  61. var actual = Extensions.Take(value, offset, count);
  62. Assert.IsNotNull(actual);
  63. Assert.AreEqual(count, actual.Length);
  64. Assert.AreEqual(value[0], actual[0]);
  65. Assert.AreEqual(value[1], actual[1]);
  66. Assert.AreEqual(value[2], actual[2]);
  67. Assert.AreEqual(value[3], actual[3]);
  68. Assert.AreEqual(value[4], actual[4]);
  69. }
  70. [TestMethod]
  71. public void ShouldReturnCorrectPartOfValueWhenOffsetIsGreaterThanZeroAndOffsetPlusCountIsLessThanLengthOfValue()
  72. {
  73. var value = CreateBuffer(16);
  74. const int offset = 3;
  75. const int count = 4;
  76. var actual = Extensions.Take(value, offset, count);
  77. Assert.IsNotNull(actual);
  78. Assert.AreEqual(count, actual.Length);
  79. Assert.AreEqual(value[3], actual[0]);
  80. Assert.AreEqual(value[4], actual[1]);
  81. Assert.AreEqual(value[5], actual[2]);
  82. Assert.AreEqual(value[6], actual[3]);
  83. }
  84. [TestMethod]
  85. public void ShouldThrowArgumentExceptionWhenCountIsGreaterThanLengthOfValue()
  86. {
  87. var value = CreateBuffer(16);
  88. const int offset = 0;
  89. var count = value.Length + 1;
  90. try
  91. {
  92. Extensions.Take(value, offset, count);
  93. Assert.Fail();
  94. }
  95. catch (ArgumentException)
  96. {
  97. }
  98. }
  99. [TestMethod]
  100. public void ShouldThrowArgumentExceptionWhenOffsetPlusCountIsGreaterThanLengthOfValue()
  101. {
  102. var value = CreateBuffer(16);
  103. const int offset = 1;
  104. var count = value.Length;
  105. try
  106. {
  107. Extensions.Take(value, offset, count);
  108. Assert.Fail();
  109. }
  110. catch (ArgumentException)
  111. {
  112. }
  113. }
  114. private byte[] CreateBuffer(int length)
  115. {
  116. var buffer = new byte[length];
  117. _random.NextBytes(buffer);
  118. return buffer;
  119. }
  120. }
  121. }