Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2ed7a7b

Browse filesBrowse files
authored
Enhance FastSearcherTests with additional test cases and improved assertions using FluentAssertions. (#501)
1 parent 046f26e commit 2ed7a7b
Copy full SHA for 2ed7a7b

File tree

Expand file treeCollapse file tree

1 file changed

+58
-31
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+58
-31
lines changed
+58-31Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Algorithms.Search;
2+
using FluentAssertions;
23
using NUnit.Framework;
4+
using System;
35
using Utilities.Exceptions;
46

57
namespace Algorithms.Tests.Search;
@@ -9,73 +11,98 @@ public static class FastSearcherTests
911
[Test]
1012
public static void FindIndex_ItemPresent_IndexCorrect()
1113
{
14+
// Arrange
1215
var searcher = new FastSearcher();
1316
var arr = Helper.GetSortedArray(1000);
1417
var present = Helper.GetItemIn(arr);
18+
19+
// Act
1520
var index = searcher.FindIndex(arr, present);
16-
Assert.That(arr[index], Is.EqualTo(present));
21+
22+
// Assert
23+
arr[index].Should().Be(present);
1724
}
1825

1926
[TestCase(new[] { 1, 2 }, 1)]
2027
[TestCase(new[] { 1, 2 }, 2)]
2128
[TestCase(new[] { 1, 2, 3, 3, 3 }, 2)]
2229
public static void FindIndex_ItemPresentInSpecificCase_IndexCorrect(int[] arr, int present)
2330
{
31+
// Arrange
2432
var searcher = new FastSearcher();
33+
34+
// Act
2535
var index = searcher.FindIndex(arr, present);
26-
Assert.That(arr[index], Is.EqualTo(present));
36+
37+
// Assert
38+
arr[index].Should().Be(present);
2739
}
2840

2941
[Test]
30-
public static void FindIndex_ItemMissing_ItemNotFoundExceptionThrown()
42+
public static void FindIndex_ItemPresentInArrayOfDuplicates_IndexCorrect()
3143
{
44+
// Arrange
3245
var searcher = new FastSearcher();
33-
var arr = Helper.GetSortedArray(1000);
34-
var missing = Helper.GetItemNotIn(arr);
35-
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
46+
var arr = CreateArrayOfDuplicates(1000, 0); // Helper for large duplicate arrays
47+
var present = 0;
48+
49+
// Act
50+
var index = searcher.FindIndex(arr, present);
51+
52+
// Assert
53+
arr[index].Should().Be(0);
3654
}
3755

38-
[TestCase(new int[0], 2)]
39-
public static void FindIndex_ItemMissingInSpecificCase_ItemNotFoundExceptionThrown(int[] arr, int missing)
56+
[TestCase(new int[0], 2)] // Empty array
57+
[TestCase(new[] { 1, 2, 3 }, 4)] // Item missing in array
58+
public static void FindIndex_ItemMissing_ItemNotFoundExceptionThrown(int[] arr, int missing)
4059
{
60+
// Arrange
4161
var searcher = new FastSearcher();
42-
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
62+
63+
// Act
64+
Action act = () => searcher.FindIndex(arr, missing);
65+
66+
// Assert
67+
act.Should().Throw<ItemNotFoundException>();
4368
}
4469

4570
[Test]
46-
public static void FindIndex_ItemSmallerThanAllMissing_ItemNotFoundExceptionThrown()
71+
public static void FindIndex_ItemMissingInArrayOfDuplicates_ItemNotFoundExceptionThrown()
4772
{
73+
// Arrange
4874
var searcher = new FastSearcher();
49-
var arr = Helper.GetSortedArray(1000);
50-
var missing = Helper.GetItemSmallerThanAllIn(arr);
51-
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
75+
var arr = CreateArrayOfDuplicates(1000, 0); // Helper for large duplicate arrays
76+
var missing = 1;
77+
78+
// Act
79+
Action act = () => searcher.FindIndex(arr, missing);
80+
81+
// Assert
82+
act.Should().Throw<ItemNotFoundException>();
5283
}
5384

5485
[Test]
55-
public static void FindIndex_ItemBiggerThanAllMissing_ItemNotFoundExceptionThrown()
86+
public static void FindIndex_ItemOutOfRange_ItemNotFoundExceptionThrown()
5687
{
88+
// Arrange
5789
var searcher = new FastSearcher();
5890
var arr = Helper.GetSortedArray(1000);
59-
var missing = Helper.GetItemBiggerThanAllIn(arr);
60-
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
61-
}
91+
var smaller = Helper.GetItemSmallerThanAllIn(arr);
92+
var bigger = Helper.GetItemBiggerThanAllIn(arr);
6293

63-
[Test]
64-
public static void FindIndex_ArrayOfDuplicatesItemPresent_IndexCorrect()
65-
{
66-
var searcher = new FastSearcher();
67-
var arr = new int[1000];
68-
var present = 0;
69-
var index = searcher.FindIndex(arr, present);
70-
Assert.That(arr[index], Is.EqualTo(0));
94+
// Act & Assert
95+
Action act1 = () => searcher.FindIndex(arr, smaller);
96+
Action act2 = () => searcher.FindIndex(arr, bigger);
97+
98+
act1.Should().Throw<ItemNotFoundException>();
99+
act2.Should().Throw<ItemNotFoundException>();
71100
}
72101

73-
[Test]
74-
public static void FindIndex_ArrayOfDuplicatesItemMissing_ItemNotFoundExceptionThrown()
102+
private static int[] CreateArrayOfDuplicates(int length, int value)
75103
{
76-
var searcher = new FastSearcher();
77-
var arr = new int[1000];
78-
var missing = 1;
79-
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
104+
var arr = new int[length];
105+
Array.Fill(arr, value);
106+
return arr;
80107
}
81108
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.