Back to Repositories

Testing Bitwise Power-of-Two Detection in javascript-algorithms

This test suite validates the isPowerOfTwoBitwise function, which determines if a number is a power of 2 using bitwise operations. The comprehensive test cases cover various scenarios including positive, negative, and edge cases to ensure reliable power-of-two detection.

Test Coverage Overview

The test suite provides extensive coverage of the isPowerOfTwoBitwise function implementation.

Key test areas include:
  • Positive powers of two (1, 2, 4, 8, 16, 64, 1024)
  • Non-powers of two (3, 5, 6, 7, 10, 12, 31, 1023)
  • Edge cases (0, -1)
  • Boundary values near powers of two

Implementation Analysis

The testing approach utilizes Jest’s describe and it blocks for organized test structure. The implementation employs expect().toBe() assertions to verify bitwise power-of-two calculations.

Testing patterns include:
  • Sequential number testing
  • Boundary value analysis
  • Edge case validation
  • Systematic test case organization

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • Bitwise operation validation
  • Module import system
  • Boolean assertion patterns
  • Describe/It block structure

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive case coverage
  • Clear test case organization
  • Systematic value progression
  • Edge case inclusion
  • Consistent assertion patterns
  • Readable test descriptions

trekhleb/javascript-algorithms

src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js

            
import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise';

describe('isPowerOfTwoBitwise', () => {
  it('should check if the number is made by multiplying twos', () => {
    expect(isPowerOfTwoBitwise(-1)).toBe(false);
    expect(isPowerOfTwoBitwise(0)).toBe(false);
    expect(isPowerOfTwoBitwise(1)).toBe(true);
    expect(isPowerOfTwoBitwise(2)).toBe(true);
    expect(isPowerOfTwoBitwise(3)).toBe(false);
    expect(isPowerOfTwoBitwise(4)).toBe(true);
    expect(isPowerOfTwoBitwise(5)).toBe(false);
    expect(isPowerOfTwoBitwise(6)).toBe(false);
    expect(isPowerOfTwoBitwise(7)).toBe(false);
    expect(isPowerOfTwoBitwise(8)).toBe(true);
    expect(isPowerOfTwoBitwise(10)).toBe(false);
    expect(isPowerOfTwoBitwise(12)).toBe(false);
    expect(isPowerOfTwoBitwise(16)).toBe(true);
    expect(isPowerOfTwoBitwise(31)).toBe(false);
    expect(isPowerOfTwoBitwise(64)).toBe(true);
    expect(isPowerOfTwoBitwise(1024)).toBe(true);
    expect(isPowerOfTwoBitwise(1023)).toBe(false);
  });
});