Back to Repositories

Testing Iterative Euclidean Algorithm Implementation in javascript-algorithms

This test suite validates the iterative implementation of the Euclidean Algorithm for finding the Greatest Common Divisor (GCD) of two numbers. The tests comprehensively verify the algorithm’s functionality across various input combinations, including edge cases and negative numbers.

Test Coverage Overview

The test suite provides extensive coverage of the euclideanAlgorithmIterative function, examining multiple scenarios:
  • Zero handling (0,0 and combinations with non-zero numbers)
  • Equal number pairs
  • Common factor scenarios
  • Coprime numbers
  • Large number combinations
  • Negative number handling
  • Order-independent input validation

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. Multiple expect statements are used to validate different input combinations, following a progressive complexity pattern from simple to more complex cases. The implementation verifies both mathematical correctness and edge case handling.

The tests demonstrate the commutative property of GCD by testing number pairs in both orders (a,b) and (b,a).

Technical Details

Testing Framework: Jest
  • Test Structure: Single describe block with one comprehensive test case
  • Assertion Method: toBe() for exact equality checking
  • Import Strategy: Direct module import
  • Execution: Synchronous test execution

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Comprehensive edge case coverage
  • Systematic test case organization
  • Mathematical property validation
  • Boundary value testing
  • Negative input handling
  • Consistent assertion pattern

trekhleb/javascript-algorithms

src/algorithms/math/euclidean-algorithm/__test__/euclideanAlgorithmIterative.test.js

            
import euclideanAlgorithmIterative from '../euclideanAlgorithmIterative';

describe('euclideanAlgorithmIterative', () => {
  it('should calculate GCD iteratively', () => {
    expect(euclideanAlgorithmIterative(0, 0)).toBe(0);
    expect(euclideanAlgorithmIterative(2, 0)).toBe(2);
    expect(euclideanAlgorithmIterative(0, 2)).toBe(2);
    expect(euclideanAlgorithmIterative(1, 2)).toBe(1);
    expect(euclideanAlgorithmIterative(2, 1)).toBe(1);
    expect(euclideanAlgorithmIterative(6, 6)).toBe(6);
    expect(euclideanAlgorithmIterative(2, 4)).toBe(2);
    expect(euclideanAlgorithmIterative(4, 2)).toBe(2);
    expect(euclideanAlgorithmIterative(12, 4)).toBe(4);
    expect(euclideanAlgorithmIterative(4, 12)).toBe(4);
    expect(euclideanAlgorithmIterative(5, 13)).toBe(1);
    expect(euclideanAlgorithmIterative(27, 13)).toBe(1);
    expect(euclideanAlgorithmIterative(24, 60)).toBe(12);
    expect(euclideanAlgorithmIterative(60, 24)).toBe(12);
    expect(euclideanAlgorithmIterative(252, 105)).toBe(21);
    expect(euclideanAlgorithmIterative(105, 252)).toBe(21);
    expect(euclideanAlgorithmIterative(1071, 462)).toBe(21);
    expect(euclideanAlgorithmIterative(462, 1071)).toBe(21);
    expect(euclideanAlgorithmIterative(462, -1071)).toBe(21);
    expect(euclideanAlgorithmIterative(-462, -1071)).toBe(21);
  });
});