PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickSortDemo/SorterTest.cs

https://github.com/ikhavkin/QuickSortDemo
C# | 122 lines | 78 code | 23 blank | 21 comment | 0 complexity | 76e203bec8ce26ed2376f42409a7e2bb MD5 | raw file
  1. using System.Collections.Generic;
  2. using FluentAssertions;
  3. using NUnit.Framework;
  4. using System.Linq;
  5. namespace QuickSortDemo
  6. {
  7. [TestFixture(typeof (SystemSorter<int>))]
  8. public class SorterTest<TSorter>
  9. where TSorter : ISorter<int>, new()
  10. {
  11. TSorter sorter;
  12. [SetUp]
  13. public void SetUp()
  14. {
  15. sorter = new TSorter();
  16. }
  17. [Test]
  18. public void When_sorting_empty_list_it_should_not_throw()
  19. {
  20. // Arrange.
  21. var emptyCollection = new List<int>();
  22. // Act.
  23. sorter.Sort(emptyCollection);
  24. // Assert.
  25. emptyCollection.Should().BeEmpty();
  26. }
  27. [Test]
  28. public void When_sorting_1_element_list_it_should_stay_intact()
  29. {
  30. // Arrange.
  31. var oneElementList = new List<int> { 123 };
  32. // Act.
  33. sorter.Sort(oneElementList);
  34. // Assert.
  35. oneElementList.Should().Equal(123);
  36. }
  37. [Test]
  38. public void When_sorting_2_element_sorted_list_it_should_stay_intact()
  39. {
  40. // Arrange.
  41. var oneElementList = new List<int> { 1, 2 };
  42. // Act.
  43. sorter.Sort(oneElementList);
  44. // Assert.
  45. oneElementList.Should().Equal(1, 2);
  46. }
  47. [Test]
  48. public void When_sorting_list_with_2_same_values_it_should_stay_intact()
  49. {
  50. // Arrange.
  51. var oneElementList = new List<int> { 1, 1 };
  52. // Act.
  53. sorter.Sort(oneElementList);
  54. // Assert.
  55. oneElementList.Should().Equal(1, 1);
  56. }
  57. [Test]
  58. public void When_sorting_2_element_reversely_sorted_list_it_should_swap_elements()
  59. {
  60. // Arrange.
  61. var oneElementList = new List<int> { 2, 1 };
  62. // Act.
  63. sorter.Sort(oneElementList);
  64. // Assert.
  65. oneElementList.Should().Equal(1, 2);
  66. }
  67. [TestCase(new[] {1, 2, 3})]
  68. [TestCase(new[] {3, 2, 1})]
  69. [TestCase(new[] {2, 1, 3})]
  70. [TestCase(new[] {1, 1, 1})]
  71. [TestCase(new[] {int.MaxValue, int.MaxValue, int.MaxValue})]
  72. [TestCase(new[] {int.MinValue, int.MinValue, int.MinValue})]
  73. [TestCase(new[] {int.MaxValue, int.MinValue, int.MinValue})]
  74. public void Data_driven_test(int[] values)
  75. {
  76. // Arrange.
  77. var sorted = values.OrderBy(_ => _).ToList();
  78. // Act.
  79. sorter.Sort(values);
  80. // Assert.
  81. values.Should().Equal(sorted);
  82. }
  83. [Test]
  84. public void Combinatorics_test(
  85. [Values(int.MinValue, int.MaxValue, -10, 10)] int value1,
  86. [Values(int.MinValue, int.MaxValue, -10, 10)] int value2,
  87. [Values(int.MinValue, int.MaxValue, -10, 10)] int value3,
  88. [Values(int.MinValue, int.MaxValue, -10, 10)] int value4)
  89. {
  90. // Arrange.
  91. var values = new[] { value1, value2, value3, value4 };
  92. var sorted = values.OrderBy(_ => _).ToList();
  93. // Act.
  94. sorter.Sort(values);
  95. // Assert.
  96. values.Should().Equal(sorted);
  97. }
  98. }
  99. }