python: Add test for python3 and fix found problems.

This commit is contained in:
INADA Naoki 2010-09-02 02:16:28 +09:00
parent 2146f5f623
commit 8d0d2bd3fc
6 changed files with 266 additions and 9 deletions

View File

@ -12,7 +12,7 @@ cdef extern from "Python.h":
cdef unsigned long long PyLong_AsUnsignedLongLong(object o) cdef unsigned long long PyLong_AsUnsignedLongLong(object o)
cdef bint PyBool_Check(object o) cdef bint PyBool_Check(object o)
cdef bint PyMapping_Check(object o) cdef bint PyDict_Check(object o)
cdef bint PySequence_Check(object o) cdef bint PySequence_Check(object o)
cdef bint PyLong_Check(object o) cdef bint PyLong_Check(object o)
cdef bint PyInt_Check(object o) cdef bint PyInt_Check(object o)
@ -69,13 +69,14 @@ cdef class Packer(object):
def __dealloc__(self): def __dealloc__(self):
free(self.pk.buf); free(self.pk.buf);
cdef int __pack(self, object o) except -1: cdef int _pack(self, object o) except -1:
cdef long long llval cdef long long llval
cdef unsigned long long ullval cdef unsigned long long ullval
cdef long longval cdef long longval
cdef double fval cdef double fval
cdef char* rawval cdef char* rawval
cdef int ret cdef int ret
cdef dict d
if o is None: if o is None:
ret = msgpack_pack_nil(&self.pk) ret = msgpack_pack_nil(&self.pk)
@ -109,19 +110,20 @@ cdef class Packer(object):
ret = msgpack_pack_raw(&self.pk, len(o)) ret = msgpack_pack_raw(&self.pk, len(o))
if ret == 0: if ret == 0:
ret = msgpack_pack_raw_body(&self.pk, rawval, len(o)) ret = msgpack_pack_raw_body(&self.pk, rawval, len(o))
elif PyMapping_Check(o): elif PyDict_Check(o):
ret = msgpack_pack_map(&self.pk, len(o)) d = o
ret = msgpack_pack_map(&self.pk, len(d))
if ret == 0: if ret == 0:
for k,v in o.iteritems(): for k,v in d.items():
ret = self.__pack(k) ret = self._pack(k)
if ret != 0: break if ret != 0: break
ret = self.__pack(v) ret = self._pack(v)
if ret != 0: break if ret != 0: break
elif PySequence_Check(o): elif PySequence_Check(o):
ret = msgpack_pack_array(&self.pk, len(o)) ret = msgpack_pack_array(&self.pk, len(o))
if ret == 0: if ret == 0:
for v in o: for v in o:
ret = self.__pack(v) ret = self._pack(v)
if ret != 0: break if ret != 0: break
else: else:
# TODO: Serialize with defalt() like simplejson. # TODO: Serialize with defalt() like simplejson.
@ -130,7 +132,7 @@ cdef class Packer(object):
def pack(self, object obj): def pack(self, object obj):
cdef int ret cdef int ret
ret = self.__pack(obj) ret = self._pack(obj)
if ret: if ret:
raise TypeError raise TypeError
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length) buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)

102
python/test3/test_case.py Normal file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env python
# coding: utf-8
from nose import main
from nose.tools import *
from msgpack import packs, unpacks
def check(length, obj):
v = packs(obj)
assert_equal(len(v), length, "%r length should be %r but get %r" % (obj, length, len(v)))
assert_equal(unpacks(v), obj)
def test_1():
for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1,
-((1<<5)-1), -(1<<5)]:
check(1, o)
def test_2():
for o in [1 << 7, (1 << 8) - 1,
-((1<<5)+1), -(1<<7)
]:
check(2, o)
def test_3():
for o in [1 << 8, (1 << 16) - 1,
-((1<<7)+1), -(1<<15)]:
check(3, o)
def test_5():
for o in [1 << 16, (1 << 32) - 1,
-((1<<15)+1), -(1<<31)]:
check(5, o)
def test_9():
for o in [1 << 32, (1 << 64) - 1,
-((1<<31)+1), -(1<<63),
1.0, 0.1, -0.1, -1.0]:
check(9, o)
def check_raw(overhead, num):
check(num + overhead, b" " * num)
def test_fixraw():
check_raw(1, 0)
check_raw(1, (1<<5) - 1)
def test_raw16():
check_raw(3, 1<<5)
check_raw(3, (1<<16) - 1)
def test_raw32():
check_raw(5, 1<<16)
def check_array(overhead, num):
check(num + overhead, (None,) * num)
def test_fixarray():
check_array(1, 0)
check_array(1, (1 << 4) - 1)
def test_array16():
check_array(3, 1 << 4)
check_array(3, (1<<16)-1)
def test_array32():
check_array(5, (1<<16))
def match(obj, buf):
assert_equal(packs(obj), buf)
assert_equal(unpacks(buf), obj)
def test_match():
cases = [
(None, b'\xc0'),
(False, b'\xc2'),
(True, b'\xc3'),
(0, b'\x00'),
(127, b'\x7f'),
(128, b'\xcc\x80'),
(256, b'\xcd\x01\x00'),
(-1, b'\xff'),
(-33, b'\xd0\xdf'),
(-129, b'\xd1\xff\x7f'),
({1:1}, b'\x81\x01\x01'),
(1.0, b"\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
((), b'\x90'),
(tuple(range(15)),b"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
(tuple(range(16)),b"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
({}, b'\x80'),
(dict([(x,x) for x in range(15)]), b'\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'),
(dict([(x,x) for x in range(16)]), b'\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'),
]
for v, p in cases:
match(v, p)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,14 @@
#!/usr/bin/env python
# coding: utf-8
from nose.tools import *
from msgpack import packs, unpacks
import datetime
def test_raise_on_find_unsupported_value():
assert_raises(TypeError, packs, datetime.datetime.now())
if __name__ == '__main__':
from nose import main
main()

View File

@ -0,0 +1,75 @@
#!/usr/bin/env python
# coding: utf-8
from nose import main
from nose.tools import *
from msgpack import unpacks
def check(src, should):
assert_equal(unpacks(src), should)
def testSimpleValue():
check(b"\x93\xc0\xc2\xc3",
(None, False, True,))
def testFixnum():
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff",
((0,64,127,), (-32,-16,-1,),)
)
def testFixArray():
check(b"\x92\x90\x91\x91\xc0",
((),((None,),),),
)
def testFixRaw():
check(b"\x94\xa0\xa1a\xa2bc\xa3def",
(b"", b"a", b"bc", b"def",),
)
def testFixMap():
check(
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
{False: {None: None}, True:{None:{}}},
)
def testUnsignedInt():
check(
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
b"\xce\xff\xff\xff\xff",
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
)
def testSignedInt():
check(b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
b"\xd2\xff\xff\xff\xff",
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,))
def testRaw():
check(b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
(b"", b"a", b"ab", b"", b"a", b"ab"))
def testArray():
check(b"\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00"
b"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02"
b"\xc2\xc3",
((), (None,), (False,True), (), (None,), (False,True))
)
def testMap():
check(
b"\x96"
b"\xde\x00\x00"
b"\xde\x00\x01\xc0\xc2"
b"\xde\x00\x02\xc0\xc2\xc3\xc2"
b"\xdf\x00\x00\x00\x00"
b"\xdf\x00\x00\x00\x01\xc0\xc2"
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
({}, {None: False}, {True: False, None: False}, {},
{None: False}, {True: False, None: False}))
if __name__ == '__main__':
main()

28
python/test3/test_pack.py Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python
# coding: utf-8
from nose import main
from nose.tools import *
from msgpack import packs, unpacks
def check(data):
re = unpacks(packs(data))
assert_equal(re, data)
def testPack():
test_data = [
0, 1, 127, 128, 255, 256, 65535, 65536,
-1, -32, -33, -128, -129, -32768, -32769,
1.0,
b"", b"a", b"a"*31, b"a"*32,
None, True, False,
(), ((),), ((), None,),
{None: 0},
(1<<23),
]
for td in test_data:
check(td)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,36 @@
#!/usr/bin/env python
# coding: utf-8
from msgpack import Unpacker
def test_foobar():
unpacker = Unpacker(read_size=3)
unpacker.feed(b'foobar')
assert unpacker.unpack() == ord(b'f')
assert unpacker.unpack() == ord(b'o')
assert unpacker.unpack() == ord(b'o')
assert unpacker.unpack() == ord(b'b')
assert unpacker.unpack() == ord(b'a')
assert unpacker.unpack() == ord(b'r')
try:
o = unpacker.unpack()
print(("Oops!", o))
assert 0
except StopIteration:
assert 1
else:
assert 0
unpacker.feed(b'foo')
unpacker.feed(b'bar')
k = 0
for o, e in zip(unpacker, b'foobarbaz'):
assert o == e
k += 1
assert k == len(b'foobar')
if __name__ == '__main__':
test_foobar()