ExtensionsTest_Take_Count.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. [TestCategory("LongRunning")]
  83. [TestCategory("Performance")]
  84. public void Performance_LargeArray_All()
  85. {
  86. var value = CreateBuffer(50000);
  87. var count = value.Length;
  88. const int runs = 10000;
  89. Performance(value, count, runs);
  90. }
  91. [TestMethod]
  92. [TestCategory("LongRunning")]
  93. [TestCategory("Performance")]
  94. public void Performance_LargeArray_LargeCount()
  95. {
  96. var value = CreateBuffer(50000);
  97. const int count = 40000;
  98. const int runs = 1000000;
  99. Performance(value, count, runs);
  100. }
  101. [TestMethod]
  102. [TestCategory("LongRunning")]
  103. [TestCategory("Performance")]
  104. public void Performance_LargeArray_SmallCount()
  105. {
  106. var value = CreateBuffer(50000);
  107. const int count = 50;
  108. const int runs = 1000000;
  109. Performance(value, count, runs);
  110. }
  111. [TestMethod]
  112. [TestCategory("Performance")]
  113. public void Performance_LargeArray_ZeroCount()
  114. {
  115. var value = CreateBuffer(50000);
  116. const int count = 0;
  117. const int runs = 1000000;
  118. Performance(value, count, runs);
  119. }
  120. private static void Performance(byte[] value, int count, int runs)
  121. {
  122. var stopWatch = new Stopwatch();
  123. GC.Collect();
  124. GC.WaitForPendingFinalizers();
  125. GC.Collect();
  126. stopWatch.Start();
  127. for (var i = 0; i < runs; i++)
  128. {
  129. var result = Extensions.Take(value, count);
  130. var resultLength = result.Length;
  131. }
  132. GC.Collect();
  133. GC.WaitForPendingFinalizers();
  134. GC.Collect();
  135. stopWatch.Stop();
  136. Console.WriteLine(stopWatch.ElapsedMilliseconds);
  137. stopWatch.Reset();
  138. stopWatch.Start();
  139. for (var i = 0; i < runs; i++)
  140. {
  141. var result = Enumerable.Take(value, count);
  142. var resultLength = result.ToArray().Length;
  143. }
  144. GC.Collect();
  145. GC.WaitForPendingFinalizers();
  146. GC.Collect();
  147. stopWatch.Stop();
  148. Console.WriteLine(stopWatch.ElapsedMilliseconds);
  149. }
  150. private byte[] CreateBuffer(int length)
  151. {
  152. var buffer = new byte[length];
  153. _random.NextBytes(buffer);
  154. return buffer;
  155. }
  156. }
  157. }