Back to Repositories

Testing Interpolation Search Algorithm Implementation in Algorithms Repository

This test suite validates the InterpolationSearch algorithm implementation using JUnit’s parameterized testing capabilities. The tests verify both successful search scenarios and edge cases where values are not found in the sorted array.

Test Coverage Overview

The test suite provides comprehensive coverage of the InterpolationSearch algorithm, focusing on boundary conditions and typical use cases.

  • Tests both found and not-found scenarios
  • Validates positive and negative input values
  • Checks boundary conditions with values outside array range
  • Verifies exact matches within array bounds

Implementation Analysis

The testing approach utilizes JUnit 5’s parameterized testing features to efficiently test multiple scenarios with minimal code duplication.

  • Uses @ParameterizedTest annotation for data-driven testing
  • Implements @MethodSource for test data generation
  • Employs Google Truth assertions for readable verifications
  • Structured test data covering multiple edge cases

Technical Details

  • JUnit Jupiter framework with parameterized testing extension
  • Google Truth assertion library for enhanced readability
  • Static test data generation through Arguments factory
  • Organized test structure with clear input/output mapping

Best Practices Demonstrated

The test implementation showcases several testing best practices including data-driven test design and clear test case organization.

  • Descriptive test naming using parameter substitution
  • Efficient test data organization
  • Clear separation of test data and test logic
  • Comprehensive edge case coverage

williamfiset/algorithms

src/test/java/com/williamfiset/algorithms/search/InterpolationSearchTest.java

            
package com.williamfiset.algorithms.search;

import static com.google.common.truth.Truth.assertThat;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;

public class InterpolationSearchTest {

  private static Arguments[] inputs() {
    return new Arguments[] {
      Arguments.of(2, 2), Arguments.of(5, 5), Arguments.of(-1, -1), Arguments.of(8, -1)
    };
  }

  @ParameterizedTest(name = "Search value: {0}, Expected index: {1}")
  @MethodSource("inputs")
  public void testCoverage(int val, int expected) {
    int[] arr = {0, 1, 2, 3, 4, 5};
    int index = InterpolationSearch.interpolationSearch(arr, val);
    assertThat(index).isEqualTo(expected);
  }
}