Back to Repositories

Testing Dynamic Programming Stock Trading Algorithm in javascript-algorithms

This test suite validates the dynamic programming implementation of the Best Time to Buy and Sell Stocks algorithm. It ensures accurate profit calculation across various price scenarios and verifies the visitation pattern of array elements during computation.

Test Coverage Overview

The test suite provides comprehensive coverage of the stock trading algorithm, examining multiple price scenarios and edge cases.

  • Tests simple two-day trading scenarios
  • Validates scenarios with no profit potential
  • Examines complex multi-day trading patterns
  • Verifies correct handling of monotonic price sequences
  • Tests large datasets with up to 20 trading days

Implementation Analysis

The testing approach employs Jest’s mocking capabilities to track algorithm traversal patterns. Each test case utilizes a visit function mock to verify the correct number of array element visits, ensuring optimal algorithmic complexity.

  • Implements Jest function mocking
  • Tracks visitation counts for performance analysis
  • Validates expected profit calculations
  • Ensures consistent algorithm behavior across different input sizes

Technical Details

  • Testing Framework: Jest
  • Mocking Utilities: jest.fn()
  • Assertion Methods: expect().toEqual(), expect().toHaveBeenCalledTimes()
  • Test Structure: Single describe block with comprehensive test cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices for algorithm validation.

  • Systematic test case organization from simple to complex scenarios
  • Performance monitoring through visitation counting
  • Edge case coverage including empty arrays and monotonic sequences
  • Clear test case documentation with expected outputs

trekhleb/javascript-algorithms

src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js

            
import dpBestTimeToBuySellStocks from '../dpBestTimeToBuySellStocks';

describe('dpBestTimeToBuySellStocks', () => {
  it('should find the best time to buy and sell stocks', () => {
    let visit;

    expect(dpBestTimeToBuySellStocks([1, 5])).toEqual(4);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([1], visit)).toEqual(0);
    expect(visit).toHaveBeenCalledTimes(1);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([1, 5], visit)).toEqual(4);
    expect(visit).toHaveBeenCalledTimes(2);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([5, 1], visit)).toEqual(0);
    expect(visit).toHaveBeenCalledTimes(2);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([1, 5, 10], visit)).toEqual(9);
    expect(visit).toHaveBeenCalledTimes(3);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([10, 1, 5, 20, 15, 21], visit)).toEqual(25);
    expect(visit).toHaveBeenCalledTimes(6);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([7, 1, 5, 3, 6, 4], visit)).toEqual(7);
    expect(visit).toHaveBeenCalledTimes(6);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([1, 2, 3, 4, 5], visit)).toEqual(4);
    expect(visit).toHaveBeenCalledTimes(5);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks([7, 6, 4, 3, 1], visit)).toEqual(0);
    expect(visit).toHaveBeenCalledTimes(5);

    visit = jest.fn();
    expect(dpBestTimeToBuySellStocks(
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
      visit,
    )).toEqual(19);
    expect(visit).toHaveBeenCalledTimes(20);
  });
});