Back to Repositories

Testing Power of Two Detection Implementation in javascript-algorithms

This test suite validates the isPowerOfTwo function implementation, which determines whether a given number is a power of 2. The tests systematically verify the function’s ability to correctly identify powers of two from a range of input values.

Test Coverage Overview

The test suite provides comprehensive coverage for the isPowerOfTwo function, examining both positive and negative cases.

Key areas tested include:
  • Common powers of two (1, 2, 4, 8, 16, 32, 128)
  • Non-powers of two (3, 5, 6, 7, 9, 23, 127)
  • Sequential number testing to ensure robust validation

Implementation Analysis

The testing approach utilizes Jest’s describe/it block structure for organized test cases. The implementation employs expect().toBe() assertions to verify boolean return values, systematically testing numbers from 1 to 128 to ensure accurate power-of-two detection.

Testing patterns include:
  • Sequential test cases for continuous validation
  • Boolean assertion checks
  • Boundary value testing

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • ES6 module import syntax
  • Describe/It block test organization
  • Expect assertions for boolean validation
  • Individual test cases for specific number checks

Best Practices Demonstrated

The test suite exemplifies several testing best practices including comprehensive case coverage, clear test organization, and effective use of Jest’s testing patterns.

Notable practices include:
  • Systematic test case organization
  • Clear test descriptions
  • Comprehensive edge case coverage
  • Efficient test case grouping

trekhleb/javascript-algorithms

src/algorithms/math/bits/__test__/isPowerOfTwo.test.js

            
import isPowerOfTwo from '../isPowerOfTwo';

describe('isPowerOfTwo', () => {
  it('should detect if the number is power of two', () => {
    expect(isPowerOfTwo(1)).toBe(true);
    expect(isPowerOfTwo(2)).toBe(true);
    expect(isPowerOfTwo(3)).toBe(false);
    expect(isPowerOfTwo(4)).toBe(true);
    expect(isPowerOfTwo(5)).toBe(false);
    expect(isPowerOfTwo(6)).toBe(false);
    expect(isPowerOfTwo(7)).toBe(false);
    expect(isPowerOfTwo(8)).toBe(true);
    expect(isPowerOfTwo(9)).toBe(false);
    expect(isPowerOfTwo(16)).toBe(true);
    expect(isPowerOfTwo(23)).toBe(false);
    expect(isPowerOfTwo(32)).toBe(true);
    expect(isPowerOfTwo(127)).toBe(false);
    expect(isPowerOfTwo(128)).toBe(true);
  });
});