'Ensure getting first n bits of a base32-encoded DAG hash works.'

def test_hash_bits(self):

    b32 = dict(((j, i) for (i, j) in enumerate('abcdefghijklmnopqrstuvwxyz')))
    b32.update(dict(((j, i) for (i, j) in enumerate('234567', 26))))
    tests = ['35orsd4cenv743hg4i5vxha2lzayycby', '6kfqtj7dap3773rxog6kkmoweix5gpwo', 'e6h6ff3uvmjbq3azik2ckr6ckwm3depv', 'snz2juf4ij7sv77cq3vs467q6acftmur', '4eg47oedi5bbkhpoxw26v3oe6vamkfd7', 'vrwabwj6umeb5vjw6flx2rnft3j457rw']
    for test_hash in tests:
        expected = ''.join([format(b32[c], '#07b').replace('0b', '') for c in test_hash])
        for bits in (1, 2, 3, 4, 7, 8, 9, 16, 64, 117, 128, 160):
            actual_int = spack.spec.base32_prefix_bits(test_hash, bits)
            fmt = ('#0%sb' % (bits + 2))
            actual = format(actual_int, fmt).replace('0b', '')
            assert (expected[:bits] == actual)
        with pytest.raises(ValueError):
            spack.spec.base32_prefix_bits(test_hash, 161)
        with pytest.raises(ValueError):
            spack.spec.base32_prefix_bits(test_hash, 256)
