Back to Repositories

Testing Three Sum Algorithm Implementation in AutoGPT

This test suite validates the three_sum algorithm implementation, which finds three numbers in an array that sum to a target value. The suite includes comprehensive test cases covering basic scenarios, edge cases with zeros, and operations with negative numbers.

Test Coverage Overview

The test coverage encompasses three primary test cases that verify the three_sum function’s core functionality.

  • Basic summation with consecutive numbers
  • Edge cases involving zeros and duplicate numbers
  • Boundary testing with negative numbers and array extremities

Implementation Analysis

The testing approach employs a straightforward unit testing pattern using Python’s assert statements. Each test case provides input arrays, target sums, and expected index results, validating the algorithm’s ability to find correct index combinations.

  • Type hints for enhanced code clarity
  • Custom assertion messages for debugging
  • Print statements for execution tracing

Technical Details

The test implementation utilizes:

  • Python’s typing module for type annotations
  • Custom test function with parameterized inputs
  • Pyright configuration for import handling
  • Main block for test execution control

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Clear test case organization with descriptive comments
  • Strong type safety with annotations
  • Comprehensive edge case coverage
  • Informative assertion messages for debugging

significant-gravitas/autogpt

classic/benchmark/agbenchmark/challenges/verticals/code/1_three_sum/custom_python/test.py

            
# pyright: reportMissingImports=false
from typing import List

from sample_code import three_sum


def test_three_sum(nums: List[int], target: int, expected_result: List[int]) -> None:
    result = three_sum(nums, target)
    print(result)
    assert (
        result == expected_result
    ), f"AssertionError: Expected the output to be {expected_result}"


if __name__ == "__main__":
    # test the trivial case with the first three numbers
    nums = [2, 7, 11, 15]
    target = 20
    expected_result = [0, 1, 2]
    test_three_sum(nums, target, expected_result)

    # test for ability to use zero and the same number twice
    nums = [2, 7, 0, 15, 12, 0]
    target = 2
    expected_result = [0, 2, 5]
    test_three_sum(nums, target, expected_result)

    # test for first and last index usage and negative numbers
    nums = [-6, 7, 11, 4]
    target = 9
    expected_result = [0, 2, 3]
    test_three_sum(nums, target, expected_result)