|
| 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() |
0 commit comments