Skip to content

Commit

Permalink
Enhance FastSearcherTests with additional test cases and improved ass…
Browse files Browse the repository at this point in the history
…ertions using FluentAssertions. (#501)
  • Loading branch information
gmottajr authored Dec 6, 2024
1 parent 046f26e commit 2ed7a7b
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions Algorithms.Tests/Search/FastSearcherTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Algorithms.Search;
using FluentAssertions;
using NUnit.Framework;
using System;
using Utilities.Exceptions;

namespace Algorithms.Tests.Search;
Expand All @@ -9,73 +11,98 @@ public static class FastSearcherTests
[Test]
public static void FindIndex_ItemPresent_IndexCorrect()
{
// Arrange
var searcher = new FastSearcher();
var arr = Helper.GetSortedArray(1000);
var present = Helper.GetItemIn(arr);

// Act
var index = searcher.FindIndex(arr, present);
Assert.That(arr[index], Is.EqualTo(present));

// Assert
arr[index].Should().Be(present);
}

[TestCase(new[] { 1, 2 }, 1)]
[TestCase(new[] { 1, 2 }, 2)]
[TestCase(new[] { 1, 2, 3, 3, 3 }, 2)]
public static void FindIndex_ItemPresentInSpecificCase_IndexCorrect(int[] arr, int present)
{
// Arrange
var searcher = new FastSearcher();

// Act
var index = searcher.FindIndex(arr, present);
Assert.That(arr[index], Is.EqualTo(present));

// Assert
arr[index].Should().Be(present);
}

[Test]
public static void FindIndex_ItemMissing_ItemNotFoundExceptionThrown()
public static void FindIndex_ItemPresentInArrayOfDuplicates_IndexCorrect()
{
// Arrange
var searcher = new FastSearcher();
var arr = Helper.GetSortedArray(1000);
var missing = Helper.GetItemNotIn(arr);
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
var arr = CreateArrayOfDuplicates(1000, 0); // Helper for large duplicate arrays
var present = 0;

// Act
var index = searcher.FindIndex(arr, present);

// Assert
arr[index].Should().Be(0);
}

[TestCase(new int[0], 2)]
public static void FindIndex_ItemMissingInSpecificCase_ItemNotFoundExceptionThrown(int[] arr, int missing)
[TestCase(new int[0], 2)] // Empty array
[TestCase(new[] { 1, 2, 3 }, 4)] // Item missing in array
public static void FindIndex_ItemMissing_ItemNotFoundExceptionThrown(int[] arr, int missing)
{
// Arrange
var searcher = new FastSearcher();
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));

// Act
Action act = () => searcher.FindIndex(arr, missing);

// Assert
act.Should().Throw<ItemNotFoundException>();
}

[Test]
public static void FindIndex_ItemSmallerThanAllMissing_ItemNotFoundExceptionThrown()
public static void FindIndex_ItemMissingInArrayOfDuplicates_ItemNotFoundExceptionThrown()
{
// Arrange
var searcher = new FastSearcher();
var arr = Helper.GetSortedArray(1000);
var missing = Helper.GetItemSmallerThanAllIn(arr);
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
var arr = CreateArrayOfDuplicates(1000, 0); // Helper for large duplicate arrays
var missing = 1;

// Act
Action act = () => searcher.FindIndex(arr, missing);

// Assert
act.Should().Throw<ItemNotFoundException>();
}

[Test]
public static void FindIndex_ItemBiggerThanAllMissing_ItemNotFoundExceptionThrown()
public static void FindIndex_ItemOutOfRange_ItemNotFoundExceptionThrown()
{
// Arrange
var searcher = new FastSearcher();
var arr = Helper.GetSortedArray(1000);
var missing = Helper.GetItemBiggerThanAllIn(arr);
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
}
var smaller = Helper.GetItemSmallerThanAllIn(arr);
var bigger = Helper.GetItemBiggerThanAllIn(arr);

[Test]
public static void FindIndex_ArrayOfDuplicatesItemPresent_IndexCorrect()
{
var searcher = new FastSearcher();
var arr = new int[1000];
var present = 0;
var index = searcher.FindIndex(arr, present);
Assert.That(arr[index], Is.EqualTo(0));
// Act & Assert
Action act1 = () => searcher.FindIndex(arr, smaller);
Action act2 = () => searcher.FindIndex(arr, bigger);

act1.Should().Throw<ItemNotFoundException>();
act2.Should().Throw<ItemNotFoundException>();
}

[Test]
public static void FindIndex_ArrayOfDuplicatesItemMissing_ItemNotFoundExceptionThrown()
private static int[] CreateArrayOfDuplicates(int length, int value)
{
var searcher = new FastSearcher();
var arr = new int[1000];
var missing = 1;
_ = Assert.Throws<ItemNotFoundException>(() => searcher.FindIndex(arr, missing));
var arr = new int[length];
Array.Fill(arr, value);
return arr;
}
}

0 comments on commit 2ed7a7b

Please sign in to comment.