예제 #1
0
def test_invalid():
    with pytest.raises(TypeError):
        cbors.loadb(1)
    with pytest.raises(ValueError):
        cbors.loadb(b"foo")

    class foo:
        pass

    with pytest.raises(TypeError):
        cbors.dumpb(foo())
    with pytest.raises(TypeError):
        cbors.dumpb({"foo": foo()})
예제 #2
0
def test_rfc():
    examples = [
        (0, 0x00),
        (1, 0x01),
        (10, 0x0A),
        (23, 0x17),
        (24, 0x1818),
        (25, 0x1819),
        (100, 0x1864),
        (1000, 0x1903E8),
        (1000000, 0x1A000F4240),
        (1000000000000, 0x1B000000E8D4A51000),
        (18446744073709551615, 0x1BFFFFFFFFFFFFFFFF),
        (-1, 0x20),
        (-10, 0x29),
        (-100, 0x3863),
        (-1000, 0x3903E7),
        (0.0, 0xF90000),
        (-0.0, 0xF98000),
        (1.0, 0xF93C00),
        (1.1, 0xFB3FF199999999999A),
        (1.5, 0xF93E00),
        (65504.0, 0xF97BFF),
        (100000.0, 0xFA47C35000),
        (3.4028234663852886e38, 0xFA7F7FFFFF),
        (1.0e300, 0xFB7E37E43C8800759C),
        (5.960464477539063e-8, 0xF90001),
        (0.00006103515625, 0xF90400),
        (-4.0, 0xF9C400),
        (-4.1, 0xFBC010666666666666),
        (float("inf"), 0xF97C00),
        (float("nan"), 0xF97E00),
        (float("-inf"), 0xF9FC00),
        (False, 0xF4),
        (True, 0xF5),
        (None, 0xF6),
        ("", 0x60),
        ("a", 0x6161),
        ("IETF", 0x6449455446),
        ('"\\', 0x62225C),
        ("\u00fc", 0x62C3BC),
        ("\u6c34", 0x63E6B0B4),
        ([], 0x80),
        ([1, 2, 3], 0x83010203),
        ([1, [2, 3], [4, 5]], 0x8301820203820405),
        (
            list(range(1, 26)),
            0x98190102030405060708090A0B0C0D0E0F101112131415161718181819,
        ),
        ({}, 0xA0),
        ({1: 2, 3: 4}, 0xA201020304),
        ({"a": 1, "b": [2, 3]}, 0xA26161016162820203),
        (["a", {"b": "c"}], 0x826161A161626163),
        ({c: c.upper() for c in "abcde"}, 0xA56161614161626142616361436164614461656145),
    ]

    for v, h in examples:
        e = bytes.fromhex("{:02x}".format(h))
        assert cbors.dumpb(v) == e
        assert_equal(v, cbors.loadb(e))
예제 #3
0
def test_input_types():
    cbors.loadb(b"\x01")
    cbors.loadb(bytearray(b"\x01"))
    with pytest.raises(TypeError):
        cbors.loadb(1)
예제 #4
0
def test_decode_inverts_encode(v):
    assert_equal(v, cbors.loadb(cbors.dumpb(v)))
예제 #5
0
파일: test_cbors.py 프로젝트: moreati/cbors
def test_rfc():
    examples = [
        (0, 0x00),
        (1, 0x01),
        (10, 0x0a),
        (23, 0x17),
        (24, 0x1818),
        (25, 0x1819),
        (100, 0x1864),
        (1000, 0x1903e8),
        (1000000, 0x1a000f4240),
        (1000000000000, 0x1b000000e8d4a51000),
        (18446744073709551615, 0x1bffffffffffffffff),
        (-1, 0x20),
        (-10, 0x29),
        (-100, 0x3863),
        (-1000, 0x3903e7),
        (0.0, 0xf90000),
        (-0.0, 0xf98000),
        (1.0, 0xf93c00),
        (1.1, 0xfb3ff199999999999a),
        (1.5, 0xf93e00),
        (65504.0, 0xf97bff),
        (100000.0, 0xfa47c35000),
        (3.4028234663852886e+38, 0xfa7f7fffff),
        (1.0e+300, 0xfb7e37e43c8800759c),
        (5.960464477539063e-8, 0xf90001),
        (0.00006103515625, 0xf90400),
        (-4.0, 0xf9c400),
        (-4.1, 0xfbc010666666666666),
        (float('inf'), 0xf97c00),
        (float('nan'), 0xf97e00),
        (float('-inf'), 0xf9fc00),
        (False, 0xf4),
        (True, 0xf5),
        (None, 0xf6),
        ("", 0x60),
        ("a", 0x6161),
        ("IETF", 0x6449455446),
        ("\"\\", 0x62225c),
        ("\u00fc", 0x62c3bc),
        ("\u6c34", 0x63e6b0b4),
        ([], 0x80),
        ([1, 2, 3], 0x83010203),
        ([1, [2, 3], [4, 5]], 0x8301820203820405),
        (list(range(1, 26)),
         0x98190102030405060708090a0b0c0d0e0f101112131415161718181819),
        ({}, 0xa0),
        ({
            1: 2,
            3: 4
        }, 0xa201020304),
        ({
            "a": 1,
            "b": [2, 3]
        }, 0xa26161016162820203),
        (["a", {
            "b": "c"
        }], 0x826161a161626163),
        ({c: c.upper()
          for c in 'abcde'}, 0xa56161614161626142616361436164614461656145),
    ]

    for v, h in examples:
        e = bytes.fromhex('{:02x}'.format(h))
        assert cbors.dumpb(v) == e
        assert_equal(v, cbors.loadb(e))