c 2010 python software foundation adapted from python s libtestteststrtod py by mark dickinson more test cases for deccheck py exact halfway cases with a small number of significant digits upper smallest integer 2545k lower smallest odd number 2535k select a random odd n in 2535k 2545k then n 10k gives a halfway case with small number of significant digits remove any additional powers of 5 try numbers of the form n 2p2 10e p2 0 until n 2p2 has more than 20 significant digits same again but with extra trailing zeros try numbers of the form n 5p2 10e p5 p5 0 with n 5p5 1020 same again but with extra trailing zeros test halfway cases for the roundhalftoeven rule bit pattern for a random finite positive or 0 0 float convert bit pattern to a number of the form m 2e add 0 5 ulps convert to a decimal string m 2e m 5e 10e boundaries expressed as triples n e u where n10e is an approximation to the boundary value and u10e is 1ulp test values close to 21075 the underflow boundary similar to boundarytests except that the random error doesn t scale with n make 0 more likely to be chosen than other digits put together random short valid strings d d e squares tough cases for ln etc c 2010 python software foundation adapted from python s lib test test_strtod py by mark dickinson more test cases for deccheck py exact halfway cases with a small number of significant digits upper smallest integer 2 54 5 k lower smallest odd number 2 53 5 k select a random odd n in 2 53 5 k 2 54 5 k then n 10 k gives a halfway case with small number of significant digits remove any additional powers of 5 try numbers of the form n 2 p2 10 e p2 0 until n 2 p2 has more than 20 significant digits same again but with extra trailing zeros try numbers of the form n 5 p2 10 e p5 p5 0 with n 5 p5 10 20 same again but with extra trailing zeros test halfway cases for the round half to even rule bit pattern for a random finite positive or 0 0 float convert bit pattern to a number of the form m 2 e add 0 5 ulps convert to a decimal string m 2 e m 5 e 10 e boundaries expressed as triples n e u where n 10 e is an approximation to the boundary value and u 10 e is 1ulp a power of 2 boundary 1 0 overflow boundary 2 1024 normal subnormal 2 1022 zero test values close to 2 1075 the underflow boundary similar to boundary_tests except that the random error doesn t scale with n make 0 more likely to be chosen than other digits put together random short valid strings d d e squares tough cases for ln etc
import random

TEST_SIZE = 2


def test_short_halfway_cases():
    
    for k in 0, 5, 10, 15, 20:
        
        upper = -(-2**54//5**k)
        
        lower = -(-2**53//5**k)
        if lower % 2 == 0:
            lower += 1
        for i in range(10 * TEST_SIZE):
            
            
            
            n, e = random.randrange(lower, upper, 2), k

            
            while n % 5 == 0:
                n, e = n // 5, e + 1
            assert n % 10 in (1, 3, 7, 9)

            
            
            digits, exponent = n, e
            while digits < 10**20:
                s = '{}e{}'.format(digits, exponent)
                yield s
                
                s = '{}e{}'.format(digits * 10**40, exponent - 40)
                yield s
                digits *= 2

            
            
            digits, exponent = n, e
            while digits < 10**20:
                s = '{}e{}'.format(digits, exponent)
                yield s
                
                s = '{}e{}'.format(digits * 10**40, exponent - 40)
                yield s
                digits *= 5
                exponent -= 1

def test_halfway_cases():
    
    for i in range(1000):
        for j in range(TEST_SIZE):
            
            bits = random.randrange(2047*2**52)

            
            e, m = divmod(bits, 2**52)
            if e:
                m, e = m + 2**52, e - 1
            e -= 1074

            
            m, e = 2*m + 1, e - 1

            
            if e >= 0:
                digits = m << e
                exponent = 0
            else:
                
                digits = m * 5**-e
                exponent = e
            s = '{}e{}'.format(digits, exponent)
            yield s

def test_boundaries():
    
    
    
    boundaries = [
        (10000000000000000000, -19, 1110),   
        (17976931348623159077, 289, 1995),   
        (22250738585072013831, -327, 4941),  
        (0, -327, 4941),                     
        ]
    for n, e, u in boundaries:
        for j in range(1000):
            for i in range(TEST_SIZE):
                digits = n + random.randrange(-3*u, 3*u)
                exponent = e
                s = '{}e{}'.format(digits, exponent)
                yield s
            n *= 10
            u *= 10
            e -= 1

def test_underflow_boundary():
    
    
    
    for exponent in range(-400, -320):
        base = 10**-exponent // 2**1075
        for j in range(TEST_SIZE):
            digits = base + random.randrange(-1000, 1000)
            s = '{}e{}'.format(digits, exponent)
            yield s

def test_bigcomp():
    for ndigs in 5, 10, 14, 15, 16, 17, 18, 19, 20, 40, 41, 50:
        dig10 = 10**ndigs
        for i in range(100 * TEST_SIZE):
            digits = random.randrange(dig10)
            exponent = random.randrange(-400, 400)
            s = '{}e{}'.format(digits, exponent)
            yield s

def test_parsing():
    
    digits = '000000123456789'
    signs = ('+', '-', '')

    
    
    for i in range(1000):
        for j in range(TEST_SIZE):
            s = random.choice(signs)
            intpart_len = random.randrange(5)
            s += ''.join(random.choice(digits) for _ in range(intpart_len))
            if random.choice([True, False]):
                s += '.'
                fracpart_len = random.randrange(5)
                s += ''.join(random.choice(digits)
                             for _ in range(fracpart_len))
            else:
                fracpart_len = 0
            if random.choice([True, False]):
                s += random.choice(['e', 'E'])
                s += random.choice(signs)
                exponent_len = random.randrange(1, 4)
                s += ''.join(random.choice(digits)
                             for _ in range(exponent_len))

            if intpart_len + fracpart_len:
                yield s

test_particular = [
     
    '1.00000000100000000025',
    '1.0000000000000000000000000100000000000000000000000' 
    '00025',
    '1.0000000000000000000000000000000000000000000010000' 
    '0000000000000000000000000000000000000000025',
    '1.0000000000000000000000000000000000000000000000000' 
    '000001000000000000000000000000000000000000000000000' 
    '000000000025',
    '0.99999999900000000025',
    '0.9999999999999999999999999999999999999999999999999' 
    '999000000000000000000000000000000000000000000000000' 
    '000025',
    '0.9999999999999999999999999999999999999999999999999' 
    '999999999999999999999999999999999999999999999999999' 
    '999999999999999999999999999999999999999990000000000' 
    '000000000000000000000000000000000000000000000000000' 
    '000000000000000000000000000000000000000000000000000' 
    '0000000000000000000000000000025',

    '1.0000000000000000000000000000000000000000000000000' 
    '000000000000000000000000000000000000000000000000000' 
    '100000000000000000000000000000000000000000000000000' 
    '000000000000000000000000000000000000000000000000001',
    '1.0000000000000000000000000000000000000000000000000' 
    '000000000000000000000000000000000000000000000000000' 
    '500000000000000000000000000000000000000000000000000' 
    '000000000000000000000000000000000000000000000000005',
    '1.0000000000000000000000000000000000000000000000000' 
    '000000000100000000000000000000000000000000000000000' 
    '000000000000000000250000000000000002000000000000000' 
    '000000000000000000000000000000000000000000010000000' 
    '000000000000000000000000000000000000000000000000000' 
    '0000000000000000001',
    '1.0000000000000000000000000000000000000000000000000' 
    '000000000100000000000000000000000000000000000000000' 
    '000000000000000000249999999999999999999999999999999' 
    '999999999999979999999999999999999999999999999999999' 
    '999999999999999999999900000000000000000000000000000' 
    '000000000000000000000000000000000000000000000000000' 
    '00000000000000000000000001',

    '0.9999999999999999999999999999999999999999999999999' 
    '999999999900000000000000000000000000000000000000000' 
    '000000000000000000249999999999999998000000000000000' 
    '000000000000000000000000000000000000000000010000000' 
    '000000000000000000000000000000000000000000000000000' 
    '0000000000000000001',
    '0.9999999999999999999999999999999999999999999999999' 
    '999999999900000000000000000000000000000000000000000' 
    '000000000000000000250000001999999999999999999999999' 
    '999999999999999999999999999999999990000000000000000' 
    '000000000000000000000000000000000000000000000000000' 
    '1',

    
    '1.000000000000000000000000000000000000000000000000' 
    '00000000000000000000000000000000000000000000000000' 
    '00100000000000000000000000000000000000000000000000' 
    '00000000000000000000000000000000000000000000000000' 
    '0001',
    '0.999999999999999999999999999999999999999999999999' 
    '99999999999999999999999999999999999999999999999999' 
    '99899999999999999999999999999999999999999999999999' 
    '99999999999999999999999999999999999999999999999999' 
    '99999999999999999999999999999999999999999999999999' 
    '9999'
    ]


TESTCASES = [
      [x for x in test_short_halfway_cases()],
      [x for x in test_halfway_cases()],
      [x for x in test_boundaries()],
      [x for x in test_underflow_boundary()],
      [x for x in test_bigcomp()],
      [x for x in test_parsing()],
      test_particular
]

def un_randfloat():
    for i in range(1000):
        l = random.choice(TESTCASES[:6])
        yield random.choice(l)
    for v in test_particular:
        yield v

def bin_randfloat():
    for i in range(1000):
        l1 = random.choice(TESTCASES)
        l2 = random.choice(TESTCASES)
        yield random.choice(l1), random.choice(l2)

def tern_randfloat():
    for i in range(1000):
        l1 = random.choice(TESTCASES)
        l2 = random.choice(TESTCASES)
        l3 = random.choice(TESTCASES)
        yield random.choice(l1), random.choice(l2), random.choice(l3)