Back to Repositories

Testing Peak-Valley Stock Trading Algorithm Implementation in javascript-algorithms

This test suite validates a peak-valley algorithm for determining optimal stock trading profits. It examines various price sequences to verify the algorithm correctly calculates maximum potential gains from buying and selling stocks at different time points.

Test Coverage Overview

The test suite provides comprehensive coverage of the peak-valley stock trading algorithm, examining both simple and complex price sequences.

  • Tests basic two-price scenarios
  • Validates single price edge cases
  • Examines ascending and descending sequences
  • Verifies complex price patterns with multiple peaks and valleys
  • Tests visit function call counts for performance tracking

Implementation Analysis

The testing approach uses Jest’s expect assertions and mock functions to verify both algorithm outputs and internal behavior.

  • Implements jest.fn() for visit tracking
  • Uses toEqual matcher for precise result validation
  • Employs toHaveBeenCalledTimes for execution count verification
  • Structured test cases progress from simple to complex scenarios

Technical Details

  • Testing Framework: Jest
  • Mock Functions: jest.fn()
  • Assertion Methods: expect, toEqual, toHaveBeenCalledTimes
  • Test Structure: describe/it blocks
  • Input Types: Array of numbers, optional visit callback

Best Practices Demonstrated

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

  • Progressive complexity in test cases
  • Edge case coverage
  • Performance monitoring through visit counting
  • Clear test case organization
  • Consistent assertion patterns

trekhleb/javascript-algorithms

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

            
import peakvalleyBestTimeToBuySellStocks from '../peakvalleyBestTimeToBuySellStocks';

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

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

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

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

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

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

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

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

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

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

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