From f94da60496d0698a6978cfb5aa5c87aa22a59402 Mon Sep 17 00:00:00 2001 From: GersonJr Date: Fri, 6 Dec 2024 00:34:07 -0300 Subject: [PATCH] Enhanced FastSearcherTests with additional test cases and improved assertions using FluentAssertions. Introduced new test cases to cover additional scenarios: FindIndex_ItemPresentInArrayOfDuplicates_IndexCorrect: Verifies correct behavior when searching for an item in an array with all duplicate values. FindIndex_ItemMissingInArrayOfDuplicates_ItemNotFoundExceptionThrown: Ensures an exception is thrown when searching for an absent item in an array of duplicates. FindIndex_ItemOutOfRange_ItemNotFoundExceptionThrown: Confirms exceptions are raised for items smaller or larger than the array's range. Replaced NUnit's traditional Assert syntax with FluentAssertions for: Improved readability. Consistent assertion style. Expressive exception validation using act.Should().Throw(). Maintained edge case validation for empty arrays, out-of-range values, and general functionality. These updates refine test clarity, improve maintainability, and ensure comprehensive coverage of the FastSearcher class. --- Algorithms.Tests/Search/FastSearcherTests.cs | 89 +++++++++++++------- 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/Algorithms.Tests/Search/FastSearcherTests.cs b/Algorithms.Tests/Search/FastSearcherTests.cs index 2bc95bf3..224c244d 100644 --- a/Algorithms.Tests/Search/FastSearcherTests.cs +++ b/Algorithms.Tests/Search/FastSearcherTests.cs @@ -1,5 +1,7 @@ using Algorithms.Search; +using FluentAssertions; using NUnit.Framework; +using System; using Utilities.Exceptions; namespace Algorithms.Tests.Search; @@ -9,11 +11,16 @@ 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)] @@ -21,61 +28,81 @@ public static void FindIndex_ItemPresent_IndexCorrect() [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(() => 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(() => searcher.FindIndex(arr, missing)); + + // Act + Action act = () => searcher.FindIndex(arr, missing); + + // Assert + act.Should().Throw(); } [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(() => 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(); } [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(() => 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(); + act2.Should().Throw(); } - [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(() => searcher.FindIndex(arr, missing)); + var arr = new int[length]; + Array.Fill(arr, value); + return arr; } }