Skip to content

Commit ca0bda0

Browse files
author
Naoki INADA
committed
Add some test cases.
1 parent 3a6f662 commit ca0bda0

2 files changed

Lines changed: 129 additions & 0 deletions

File tree

test/test_case.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
from nose import main
5+
from nose.tools import *
6+
from msgpack import packs, unpacks
7+
8+
def check(length, obj):
9+
v = packs(obj)
10+
assert_equal(len(v), length)
11+
assert_equal(unpacks(v), obj)
12+
13+
def test_1():
14+
for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1,
15+
-((1<<5)-1), -(1<<5)]:
16+
check(1, o)
17+
18+
def test_2():
19+
for o in [1 << 7, (1 << 8) - 1,
20+
-((1<<5)+1), -(1<<7)
21+
]:
22+
check(2, o)
23+
24+
def test_3():
25+
for o in [1 << 8, (1 << 16) - 1,
26+
-((1<<7)+1), -(1<<15)]:
27+
check(3, o)
28+
29+
def test_5():
30+
for o in [1 << 16, (1 << 32) - 1,
31+
-((1<<15)+1), -(1<<31)]:
32+
check(5, o)
33+
34+
def test_9():
35+
for o in [1 << 32, (1 << 64) - 1,
36+
-((1<<31)+1), -(1<<63),
37+
1.0, 0.1, -0.1, -1.0]:
38+
check(9, o)
39+
40+
41+
def check_raw(overhead, num):
42+
check(num + overhead, " " * num)
43+
44+
def test_fixraw():
45+
check_raw(1, 0)
46+
check_raw(1, (1<<5) - 1)
47+
48+
def test_raw16():
49+
check_raw(3, 1<<5)
50+
check_raw(3, (1<<16) - 1)
51+
52+
def test_raw32():
53+
check_raw(5, 1<<16)
54+
55+
56+
def check_array(overhead, num):
57+
check(num + overhead, [None] * num)
58+
59+
def test_fixarray():
60+
check_array(1, 0)
61+
check_array(1, (1 << 4) - 1)
62+
63+
def test_array16():
64+
check_array(3, 1 << 4)
65+
check_array(3, (1<<16)-1)
66+
67+
def test_array32():
68+
check_array(5, (1<<16))
69+
70+
71+
def match(obj, buf):
72+
assert_equal(packs(obj), buf)
73+
assert_equal(unpacks(buf), obj)
74+
75+
def test_match():
76+
cases = [
77+
(None, '\xc0'),
78+
(False, '\xc2'),
79+
(True, '\xc3'),
80+
(0, '\x00'),
81+
(127, '\x7f'),
82+
(128, '\xcc\x80'),
83+
(256, '\xcd\x01\x00'),
84+
(-1, '\xff'),
85+
(-33, '\xd0\xdf'),
86+
(-129, '\xd1\xff\x7f'),
87+
({1:1}, '\x81\x01\x01'),
88+
(1.0, "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
89+
([], '\x90'),
90+
(range(15),"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
91+
(range(16),"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
92+
({}, '\x80'),
93+
(dict([(x,x) for x in range(15)]), "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"),
94+
(dict([(x,x) for x in range(16)]), "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"),
95+
]
96+
97+
for v, p in cases:
98+
match(v, p)
99+
100+
if __name__ == '__main__':
101+
main()

test/test_pack.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
from nose import main
5+
from nose.tools import *
6+
7+
from msgpack import packs, unpacks
8+
9+
def check(data):
10+
re = unpacks(packs(data))
11+
assert_equal(re, data)
12+
13+
def testPack():
14+
test_data = [
15+
0, 1, 127, 128, 255, 256, 65535, 65536,
16+
-1, -32, -33, -128, -129, -32768, -32769,
17+
1.0,
18+
"", "a", "a"*31, "a"*32,
19+
None, True, False,
20+
[], [[]], [[], None],
21+
{None: 0},
22+
(1<<23),
23+
]
24+
for td in test_data:
25+
check(td)
26+
27+
if __name__ == '__main__':
28+
main()

0 commit comments

Comments
 (0)