Back to Repositories

Testing Stock Trading Algorithm Performance in javascript-algorithms

This test suite validates the dynamic programming implementation of the Best Time to Buy and Sell Stocks algorithm. It verifies profit calculations across various price scenarios while monitoring algorithm visits using Jest spies.

Test Coverage Overview

The test suite provides comprehensive coverage of the stock trading algorithm across multiple price scenarios:

  • Simple cases with two prices
  • Single price edge case
  • Ascending and descending price sequences
  • Complex price patterns with multiple peaks and valleys
  • Performance testing with large input arrays

Implementation Analysis

The testing approach employs Jest’s mock functions to track algorithm visits and verify computational efficiency. It uses expect assertions to validate both profit calculations and algorithmic complexity through visit count verification.

The test structure follows AAA (Arrange-Act-Assert) pattern with Jest’s describe/it blocks.

Technical Details

Testing Framework: Jest

  • Mock Functions: jest.fn() for visit tracking
  • Assertion Methods: expect().toEqual(), expect().toHaveBeenCalledTimes()
  • Test Organization: Single describe block with multiple test cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive edge case coverage
  • Performance monitoring through visit counting
  • Progressive complexity in test cases
  • Clear test case organization
  • Efficient use of Jest’s mocking capabilities

trekhleb/javascript-algorithms

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

            
import dqBestTimeToBuySellStocks from '../dqBestTimeToBuySellStocks';

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

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

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

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

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

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

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

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

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

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

    visit = jest.fn();
    expect(dqBestTimeToBuySellStocks(
      [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(2097151);
  });
});