ExtensionsTest_Take_Count.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Renci.SshNet.Common;
  7. namespace Renci.SshNet.Tests.Classes.Common
  8. {
  9. [TestClass]
  10. [SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
  11. public class ExtensionsTest_Take_Count
  12. {
  13. private Random _random;
  14. [TestInitialize]
  15. public void Init()
  16. {
  17. _random = new Random();
  18. }
  19. [TestMethod]
  20. public void ShouldThrowArgumentNullExceptionWhenValueIsNull()
  21. {
  22. const byte[] value = null;
  23. const int count = 0;
  24. try
  25. {
  26. Extensions.Take(value, count);
  27. Assert.Fail();
  28. }
  29. catch (ArgumentNullException ex)
  30. {
  31. Assert.IsNull(ex.InnerException);
  32. Assert.AreEqual("value", ex.ParamName);
  33. }
  34. }
  35. [TestMethod]
  36. public void ShouldReturnEmptyByteArrayWhenCountIsZero()
  37. {
  38. var value = CreateBuffer(16);
  39. const int count = 0;
  40. var actual = Extensions.Take(value, count);
  41. Assert.IsNotNull(actual);
  42. Assert.AreEqual(0, actual.Length);
  43. }
  44. [TestMethod]
  45. public void ShouldReturnValueWhenCountIsEqualToLengthOfValue()
  46. {
  47. var value = CreateBuffer(16);
  48. var count = value.Length;
  49. var actual = Extensions.Take(value, count);
  50. Assert.IsNotNull(actual);
  51. Assert.AreSame(value, actual);
  52. }
  53. [TestMethod]
  54. public void ShouldReturnLeadingBytesWhenCountIsLessThanLengthOfValue()
  55. {
  56. var value = CreateBuffer(16);
  57. const int count = 5;
  58. var actual = Extensions.Take(value, count);
  59. Assert.IsNotNull(actual);
  60. Assert.AreEqual(count, actual.Length);
  61. Assert.AreEqual(value[0], actual[0]);
  62. Assert.AreEqual(value[1], actual[1]);
  63. Assert.AreEqual(value[2], actual[2]);
  64. Assert.AreEqual(value[3], actual[3]);
  65. Assert.AreEqual(value[4], actual[4]);
  66. }
  67. [TestMethod]
  68. public void ShouldThrowArgumentExceptionWhenCountIsGreaterThanLengthOfValue()
  69. {
  70. var value = CreateBuffer(16);
  71. var count = value.Length + 1;
  72. try
  73. {
  74. Extensions.Take(value, count);
  75. Assert.Fail();
  76. }
  77. catch (ArgumentException)
  78. {
  79. }
  80. }
  81. [TestMethod]
  82. public void Performance_LargeArray_All()
  83. {
  84. var value = CreateBuffer(50000);
  85. var count = value.Length;
  86. const int runs = 10000;
  87. Performance(value, count, runs);
  88. }
  89. [TestMethod]
  90. public void Performance_LargeArray_LargeCount()
  91. {
  92. var value = CreateBuffer(50000);
  93. const int count = 40000;
  94. const int runs = 1000000;
  95. Performance(value, count, runs);
  96. }
  97. [TestMethod]
  98. public void Performance_LargeArray_SmallCount()
  99. {
  100. var value = CreateBuffer(50000);
  101. const int count = 50;
  102. const int runs = 1000000;
  103. Performance(value, count, runs);
  104. }
  105. [TestMethod]
  106. public void Performance_LargeArray_ZeroCount()
  107. {
  108. var value = CreateBuffer(50000);
  109. const int count = 0;
  110. const int runs = 1000000;
  111. Performance(value, count, runs);
  112. }
  113. private void Performance(byte[] value, int count, int runs)
  114. {
  115. var stopWatch = new Stopwatch();
  116. GC.Collect();
  117. GC.WaitForPendingFinalizers();
  118. GC.Collect();
  119. stopWatch.Start();
  120. for (var i = 0; i < runs; i++)
  121. {
  122. var result = Extensions.Take(value, count);
  123. var resultLength = result.Length;
  124. }
  125. GC.Collect();
  126. GC.WaitForPendingFinalizers();
  127. GC.Collect();
  128. stopWatch.Stop();
  129. Console.WriteLine(stopWatch.ElapsedMilliseconds);
  130. stopWatch.Reset();
  131. stopWatch.Start();
  132. for (var i = 0; i < runs; i++)
  133. {
  134. var result = Enumerable.Take(value, count);
  135. var resultLength = result.ToArray().Length;
  136. }
  137. GC.Collect();
  138. GC.WaitForPendingFinalizers();
  139. GC.Collect();
  140. stopWatch.Stop();
  141. Console.WriteLine(stopWatch.ElapsedMilliseconds);
  142. }
  143. private byte[] CreateBuffer(int length)
  144. {
  145. var buffer = new byte[length];
  146. _random.NextBytes(buffer);
  147. return buffer;
  148. }
  149. }
  150. }