Esempio n. 1
0
def test_lots():
    N = 1000
    random.seed(17)
    for i in range(N):
        x = random.randint(0, 0xFFFFFFFFFFFFFF)
        y = codec8.encode(x)
        z = codec8.decode(y.__iter__())
        assert x == z
Esempio n. 2
0
def putMeta(f, itm):
    if type(itm) == type(int(1)) or type(itm) == type(long(1)):
        if itm >= 0:
            f.write('Z')
            bs = bytearray(encode(itm))
            f.write(bs)
        else:
            f.write('z')
            bs = bytearray(encode(-itm))
            f.write(bs)
    elif type(itm) == type(float(0)):
        if itm >= 0.0:
            (m, e) = math.frexp(itm)
            m = long(m * 2**40)
            e -= 40
            if e >= 0:
                f.write('F')
                bs = bytearray(encode(m))
                f.write(bs)
                bs = bytearray(encode(e))
                f.write(bs)
            else:
                f.write('G')
                bs = bytearray(encode(m))
                f.write(bs)
                bs = bytearray(encode(-e))
                f.write(bs)
        else:
            (m, e) = math.frexp(-itm)
            m = long(m * 2**40)
            e -= 40
            if e >= 0:
                f.write('f')
                bs = bytearray(encode(m))
                f.write(bs)
                bs = bytearray(encode(e))
                f.write(bs)
            else:
                f.write('g')
                bs = bytearray(encode(m))
                f.write(bs)
                bs = bytearray(encode(-e))
                f.write(bs)
    elif type(itm) == type(''):
        f.write('S')
        bs = bytearray(encode(len(itm)))
        f.write(bs)
        f.write(itm)
    elif type(itm) == type(tuple([])):
        f.write('T')
        bs = bytearray(encode(len(itm)))
        f.write(bs)
        for i in xrange(len(itm)):
            putMeta(f, itm[i])
    elif type(itm) == type({}):
        f.write('D')
        bs = bytearray(encode(len(itm)))
        f.write(bs)
        for (k,v) in itm.items():
            putMeta(f, k)
            putMeta(f, v)
    else:
        raise BadMetaData(itm)
Esempio n. 3
0
def test_dec_128():
    x = 0
    y = codec8.encode(x)
    z = codec8.decode(y.__iter__())
    assert x == z
Esempio n. 4
0
def test_enc_0():
    x = 0
    y = codec8.encode(x)
    assert len(y) == 1
    assert y[0] == 0
Esempio n. 5
0
def test_enc_128():
    x = 128
    y = codec8.encode(x)
    assert len(y) == 2
    assert y[0] == 129
    assert y[1] == 0
Esempio n. 6
0
def test_enc_127():
    x = 127
    y = codec8.encode(x)
    assert len(y) == 1
    assert y[0] == 127