ExtensionsTest_Take_Count.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.AreEqual(value.Length, actual.Length);
  52. Assert.AreEqual(value, actual);
  53. }
  54. [TestMethod]
  55. public void ShouldReturnLeadingBytesWhenCountIsLessThanLengthOfValue()
  56. {
  57. var value = CreateBuffer(16);
  58. const int count = 5;
  59. var actual = Extensions.Take(value, count);
  60. Assert.IsNotNull(actual);
  61. Assert.AreEqual(count, actual.Length);
  62. Assert.AreEqual(value[0], actual[0]);
  63. Assert.AreEqual(value[1], actual[1]);
  64. Assert.AreEqual(value[2], actual[2]);
  65. Assert.AreEqual(value[3], actual[3]);
  66. Assert.AreEqual(value[4], actual[4]);
  67. }
  68. [TestMethod]
  69. public void ShouldThrowArgumentExceptionWhenCountIsGreaterThanLengthOfValue()
  70. {
  71. var value = CreateBuffer(16);
  72. var count = value.Length + 1;
  73. try
  74. {
  75. Extensions.Take(value, count);
  76. Assert.Fail();
  77. }
  78. catch (ArgumentException)
  79. {
  80. }
  81. }
  82. [TestMethod]
  83. public void Performance_LargeArray_All()
  84. {
  85. var value = CreateBuffer(50000);
  86. var count = value.Length;
  87. const int runs = 10000;
  88. Performance(value, count, runs);
  89. }
  90. [TestMethod]
  91. public void Performance_LargeArray_LargeCount()
  92. {
  93. var value = CreateBuffer(50000);
  94. const int count = 40000;
  95. const int runs = 1000000;
  96. Performance(value, count, runs);
  97. }
  98. [TestMethod]
  99. public void Performance_LargeArray_SmallCount()
  100. {
  101. var value = CreateBuffer(50000);
  102. const int count = 50;
  103. const int runs = 1000000;
  104. Performance(value, count, runs);
  105. }
  106. [TestMethod]
  107. public void Performance_LargeArray_ZeroCount()
  108. {
  109. var value = CreateBuffer(50000);
  110. const int count = 0;
  111. const int runs = 1000000;
  112. Performance(value, count, runs);
  113. }
  114. private void Performance(byte[] value, int count, int runs)
  115. {
  116. var stopWatch = new Stopwatch();
  117. GC.Collect();
  118. GC.WaitForPendingFinalizers();
  119. GC.WaitForFullGCComplete();
  120. stopWatch.Start();
  121. for (var i = 0; i < runs; i++)
  122. {
  123. var result = Extensions.Take(value, count);
  124. var resultLength = result.Length;
  125. }
  126. GC.Collect();
  127. GC.WaitForPendingFinalizers();
  128. GC.WaitForFullGCComplete();
  129. stopWatch.Stop();
  130. Console.WriteLine(stopWatch.ElapsedMilliseconds);
  131. stopWatch.Restart();
  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.WaitForFullGCComplete();
  139. stopWatch.Stop();
  140. Console.WriteLine(stopWatch.ElapsedMilliseconds);
  141. }
  142. private byte[] CreateBuffer(int length)
  143. {
  144. var buffer = new byte[length];
  145. _random.NextBytes(buffer);
  146. return buffer;
  147. }
  148. }
  149. }