Skip to content

Instantly share code, notes, and snippets.

@kezipe
Created August 21, 2026 18:07
Show Gist options
  • Select an option

  • Save kezipe/aabe7f8bdf2d8033cc8e23abccaa5348 to your computer and use it in GitHub Desktop.

Select an option

Save kezipe/aabe7f8bdf2d8033cc8e23abccaa5348 to your computer and use it in GitHub Desktop.
IEEE 754 Float32 Binary String Conversion Tests
import Foundation
import Testing
@Suite("IEEE 754 Float32 Binary String Conversion Tests")
struct IEEE754Float32ConversionTests {
// MARK: - Helper to derive exact 32-bit binary strings
private func bitPatternString(of value: Float) -> String {
let pattern = value.bitPattern
let binary = String(pattern, radix: 2)
return String(repeating: "0", count: max(0, 32 - binary.count)) + binary
}
// MARK: - Standard Numerical Values
@Test("Converts Standard Positive and Negative Floats")
func testStandardValues() throws {
// Zero representations
#expect(convert(bitPatternString(of: 0.0)) == 0.0)
#expect(convert(bitPatternString(of: -0.0)).isZero)
#expect(convert(bitPatternString(of: -0.0)).sign == .minus)
// Basic Integers
#expect(convert(bitPatternString(of: 1.0)) == 1.0)
#expect(convert(bitPatternString(of: -1.0)) == -1.0)
#expect(convert(bitPatternString(of: 42.0)) == 42.0)
// Fractions / Decimals
#expect(convert(bitPatternString(of: 0.5)) == 0.5)
#expect(convert(bitPatternString(of: -0.15625)) == -0.15625)
}
// MARK: - Explicit 32-Bit Pattern Verification
@Test("Converts Hardcoded 32-Bit IEEE 754 Strings")
func testExplicit32BitPatterns() throws {
// +1.0 -> Sign: 0 | Exponent: 127 (01111111) | Mantissa: 0...0 (23 zeros)
let posOneBits = "0" + "01111111" + String(repeating: "0", count: 23)
#expect(convert(posOneBits) == 1.0)
// -2.0 -> Sign: 1 | Exponent: 128 (10000000) | Mantissa: 0...0 (23 zeros)
let negTwoBits = "1" + "10000000" + String(repeating: "0", count: 23)
#expect(convert(negTwoBits) == -2.0)
// +0.15625 (5/32) -> Sign: 0 | Exponent: 124 (01111100) | Mantissa: 010...0
let fractionBits = "0" + "01111100" + "010" + String(repeating: "0", count: 20)
#expect(convert(fractionBits) == 0.15625)
// Old test
// Pi approximation (~3.1415927) -> 0x40490fdb
let piBits = "0" + "10000000" + "10010010000111111011011"
#expect(convert(piBits) == Float.pi)
// New Test
#expect(convert(bitPatternString(of: Float.pi)) == Float.pi)
}
// MARK: - Special Values (Infinity & NaN)
@Test("Converts Special Values (Infinities and NaN)")
func testSpecialValues() throws {
// +Infinity: Exponent all 1s (255), Mantissa all 0s
let posInfBits = "0" + String(repeating: "1", count: 8) + String(repeating: "0", count: 23)
let posInf = convert(posInfBits)
#expect(posInf.isInfinite)
#expect(posInf > 0)
// -Infinity: Sign 1, Exponent all 1s (255), Mantissa all 0s
let negInfBits = "1" + String(repeating: "1", count: 8) + String(repeating: "0", count: 23)
let negInf = convert(negInfBits)
#expect(negInf.isInfinite)
#expect(negInf < 0)
// NaN: Exponent all 1s, Mantissa non-zero
let qNaNBits = "0" + String(repeating: "1", count: 8) + "1" + String(repeating: "0", count: 22)
#expect(convert(qNaNBits).isNaN)
}
// MARK: - Limits and Subnormals
@Test("Handles Extreme Magnitude Bounds and Subnormals")
func testLimitsAndSubnormals() throws {
// Greatest Finite Value (~3.4028235e+38)
#expect(convert(bitPatternString(of: Float.greatestFiniteMagnitude)) == Float.greatestFiniteMagnitude)
// Smallest Normal Value (~1.1754944e-38)
#expect(convert(bitPatternString(of: Float.leastNormalMagnitude)) == Float.leastNormalMagnitude)
// Smallest Subnormal Value (~1.4e-45) -> Exponent 0, Mantissa 00...01
#expect(convert(bitPatternString(of: Float.leastNonzeroMagnitude)) == Float.leastNonzeroMagnitude)
}
// MARK: - Input Validation & Error Handling
@Test("Rejects Invalid Input Strings", arguments: [
"", // Empty
"010101", // Too short (< 32 bits)
String(repeating: "1", count: 33), // Too long (> 32 bits) - e.g., accidental Double string
"01010101010101012010101010101010", // Contains non-binary char ('2')
"abcdefghijklmnopqrstuvwxyz123456" // Non-binary string
])
func testInvalidInput(input: String) {
#expect(convert(input) == 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment