Back to Repositories

Testing Binary-to-Float Conversion Algorithms in javascript-algorithms

This test suite validates the conversion of binary bits to floating-point numbers across different precisions (16-bit, 32-bit, and 64-bit). It ensures accurate transformation of binary representations to their corresponding decimal floating-point values using Jest’s testing framework.

Test Coverage Overview

The test suite provides comprehensive coverage for floating-point conversion functions:

  • Tests bitsToFloat16 for half-precision floating-point conversion
  • Tests bitsToFloat32 for single-precision floating-point conversion
  • Tests bitsToFloat64 for double-precision floating-point conversion
  • Validates against pre-defined test cases for each precision level

Implementation Analysis

The testing approach utilizes Jest’s describe-it pattern for structured test organization. Each precision level (16, 32, 64 bits) is tested independently using array iteration and decimal comparison. The implementation leverages Jest’s toBeCloseTo matcher to handle floating-point precision differences.

Technical Details

  • Testing Framework: Jest
  • Test Data: Pre-defined test cases (testCases16Bits, testCases32Bits, testCases64Bits)
  • Precision Handling: Different epsilon values for each bit width (4, 7, and 14 decimal places)
  • Binary Conversion: String splitting and parseInt for bit array creation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Systematic test case organization by precision level
  • Proper handling of floating-point comparisons using appropriate precision
  • Consistent test structure across different bit widths
  • Efficient test data iteration and validation

trekhleb/javascript-algorithms

src/algorithms/math/binary-floating-point/__tests__/bitsToFloat.test.js

            
import { testCases16Bits, testCases32Bits, testCases64Bits } from '../testCases';
import { bitsToFloat16, bitsToFloat32, bitsToFloat64 } from '../bitsToFloat';

describe('bitsToFloat16', () => {
  it('should convert floating point binary bits to floating point decimal number', () => {
    for (let testCaseIndex = 0; testCaseIndex < testCases16Bits.length; testCaseIndex += 1) {
      const [decimal, binary] = testCases16Bits[testCaseIndex];
      const bits = binary.split('').map((bitString) => parseInt(bitString, 10));
      expect(bitsToFloat16(bits)).toBeCloseTo(decimal, 4);
    }
  });
});

describe('bitsToFloat32', () => {
  it('should convert floating point binary bits to floating point decimal number', () => {
    for (let testCaseIndex = 0; testCaseIndex < testCases32Bits.length; testCaseIndex += 1) {
      const [decimal, binary] = testCases32Bits[testCaseIndex];
      const bits = binary.split('').map((bitString) => parseInt(bitString, 10));
      expect(bitsToFloat32(bits)).toBeCloseTo(decimal, 7);
    }
  });
});

describe('bitsToFloat64', () => {
  it('should convert floating point binary bits to floating point decimal number', () => {
    for (let testCaseIndex = 0; testCaseIndex < testCases64Bits.length; testCaseIndex += 1) {
      const [decimal, binary] = testCases64Bits[testCaseIndex];
      const bits = binary.split('').map((bitString) => parseInt(bitString, 10));
      expect(bitsToFloat64(bits)).toBeCloseTo(decimal, 14);
    }
  });
});