Back to Repositories

Testing Square Root Algorithm Implementation in javascript-algorithms

This test suite validates the square root calculation algorithm implementation, covering various numeric inputs and precision levels. It ensures accurate computation of square roots with configurable tolerance values and proper error handling for invalid inputs.

Test Coverage Overview

The test suite provides comprehensive coverage of the square root calculation functionality across different scenarios.

  • Validates handling of negative numbers with error throwing
  • Tests basic square root calculations with default tolerance
  • Verifies calculations with multiple custom tolerance levels (1, 3, and 10 decimal places)
  • Examines decimal number inputs with high precision

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. Multiple test cases verify the algorithm’s accuracy and precision handling.

  • Uses Jest’s expect assertions for result validation
  • Implements function error testing with toThrow matcher
  • Employs parameterized testing for different tolerance levels
  • Validates both integer and floating-point calculations

Technical Details

Testing infrastructure leverages Jest framework capabilities.

  • Jest test runner and assertion library
  • Module import system for testing isolation
  • Custom tolerance parameter testing
  • Floating-point precision validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for mathematical algorithms.

  • Comprehensive edge case coverage
  • Structured test organization with clear descriptions
  • Granular test cases for different precision levels
  • Error condition validation
  • Progressive complexity in test scenarios

trekhleb/javascript-algorithms

src/algorithms/math/square-root/__test__/squareRoot.test.js

            
import squareRoot from '../squareRoot';

describe('squareRoot', () => {
  it('should throw for negative numbers', () => {
    function failingSquareRoot() {
      squareRoot(-5);
    }
    expect(failingSquareRoot).toThrow();
  });

  it('should correctly calculate square root with default tolerance', () => {
    expect(squareRoot(0)).toBe(0);
    expect(squareRoot(1)).toBe(1);
    expect(squareRoot(2)).toBe(1);
    expect(squareRoot(3)).toBe(2);
    expect(squareRoot(4)).toBe(2);
    expect(squareRoot(15)).toBe(4);
    expect(squareRoot(16)).toBe(4);
    expect(squareRoot(256)).toBe(16);
    expect(squareRoot(473)).toBe(22);
    expect(squareRoot(14723)).toBe(121);
  });

  it('should correctly calculate square root for integers with custom tolerance', () => {
    let tolerance = 1;

    expect(squareRoot(0, tolerance)).toBe(0);
    expect(squareRoot(1, tolerance)).toBe(1);
    expect(squareRoot(2, tolerance)).toBe(1.4);
    expect(squareRoot(3, tolerance)).toBe(1.8);
    expect(squareRoot(4, tolerance)).toBe(2);
    expect(squareRoot(15, tolerance)).toBe(3.9);
    expect(squareRoot(16, tolerance)).toBe(4);
    expect(squareRoot(256, tolerance)).toBe(16);
    expect(squareRoot(473, tolerance)).toBe(21.7);
    expect(squareRoot(14723, tolerance)).toBe(121.3);

    tolerance = 3;

    expect(squareRoot(0, tolerance)).toBe(0);
    expect(squareRoot(1, tolerance)).toBe(1);
    expect(squareRoot(2, tolerance)).toBe(1.414);
    expect(squareRoot(3, tolerance)).toBe(1.732);
    expect(squareRoot(4, tolerance)).toBe(2);
    expect(squareRoot(15, tolerance)).toBe(3.873);
    expect(squareRoot(16, tolerance)).toBe(4);
    expect(squareRoot(256, tolerance)).toBe(16);
    expect(squareRoot(473, tolerance)).toBe(21.749);
    expect(squareRoot(14723, tolerance)).toBe(121.338);

    tolerance = 10;

    expect(squareRoot(0, tolerance)).toBe(0);
    expect(squareRoot(1, tolerance)).toBe(1);
    expect(squareRoot(2, tolerance)).toBe(1.4142135624);
    expect(squareRoot(3, tolerance)).toBe(1.7320508076);
    expect(squareRoot(4, tolerance)).toBe(2);
    expect(squareRoot(15, tolerance)).toBe(3.8729833462);
    expect(squareRoot(16, tolerance)).toBe(4);
    expect(squareRoot(256, tolerance)).toBe(16);
    expect(squareRoot(473, tolerance)).toBe(21.7485631709);
    expect(squareRoot(14723, tolerance)).toBe(121.3383698588);
  });

  it('should correctly calculate square root for integers with custom tolerance', () => {
    expect(squareRoot(4.5, 10)).toBe(2.1213203436);
    expect(squareRoot(217.534, 10)).toBe(14.7490338667);
  });
});