ExtensionsTest_Take_Count.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. namespace Renci.SshNet.Tests.Classes.Common
  5. {
  6. [TestClass]
  7. public class ExtensionsTest_Take_Count
  8. {
  9. private Random _random;
  10. [TestInitialize]
  11. public void Init()
  12. {
  13. _random = new Random();
  14. }
  15. [TestMethod]
  16. public void ShouldThrowArgumentNullExceptionWhenValueIsNull()
  17. {
  18. const byte[] value = null;
  19. const int count = 0;
  20. try
  21. {
  22. Extensions.Take(value, count);
  23. Assert.Fail();
  24. }
  25. catch (ArgumentNullException ex)
  26. {
  27. Assert.IsNull(ex.InnerException);
  28. Assert.AreEqual("value", ex.ParamName);
  29. }
  30. }
  31. [TestMethod]
  32. public void ShouldReturnEmptyByteArrayWhenCountIsZero()
  33. {
  34. var value = CreateBuffer(16);
  35. const int count = 0;
  36. var actual = Extensions.Take(value, count);
  37. Assert.IsNotNull(actual);
  38. Assert.AreEqual(0, actual.Length);
  39. }
  40. [TestMethod]
  41. public void ShouldReturnValueWhenCountIsEqualToLengthOfValue()
  42. {
  43. var value = CreateBuffer(16);
  44. var count = value.Length;
  45. var actual = Extensions.Take(value, count);
  46. Assert.IsNotNull(actual);
  47. Assert.AreSame(value, actual);
  48. }
  49. [TestMethod]
  50. public void ShouldReturnLeadingBytesWhenCountIsLessThanLengthOfValue()
  51. {
  52. var value = CreateBuffer(16);
  53. const int count = 5;
  54. var actual = Extensions.Take(value, count);
  55. Assert.IsNotNull(actual);
  56. Assert.AreEqual(count, actual.Length);
  57. Assert.AreEqual(value[0], actual[0]);
  58. Assert.AreEqual(value[1], actual[1]);
  59. Assert.AreEqual(value[2], actual[2]);
  60. Assert.AreEqual(value[3], actual[3]);
  61. Assert.AreEqual(value[4], actual[4]);
  62. }
  63. [TestMethod]
  64. public void ShouldThrowArgumentExceptionWhenCountIsGreaterThanLengthOfValue()
  65. {
  66. var value = CreateBuffer(16);
  67. var count = value.Length + 1;
  68. try
  69. {
  70. Extensions.Take(value, count);
  71. Assert.Fail();
  72. }
  73. catch (ArgumentException)
  74. {
  75. }
  76. }
  77. private byte[] CreateBuffer(int length)
  78. {
  79. var buffer = new byte[length];
  80. _random.NextBytes(buffer);
  81. return buffer;
  82. }
  83. }
  84. }