Beispiel #1
0
 def test_sixteen_byte_int(self):
     self.assertEqual(
         b"\x30\x10\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         encode(0x4000000000000000000000000000000),
     )
     self.assertEqual(
         b"\x50\x10\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         encode(-0x4000000000000000000000000000000),
     )
Beispiel #2
0
 def test_long_decimal(self):
     self.assertEqual(
         b"\x70\x11\x15\x2d\x79\x82\x31\x23\x45\x67\x89\x01\x23\x45\x67\x89\x01\x20\x0d",
         encode(Decimal("152.79823123456789012345678901200")),
     )
     self.assertEqual(
         b"\x70\x11\xb1\x52\xd7\x98\x23\x12\x34\x56\x78\x90\x12\x34\x56\x78\x90\x12\x00",
         encode(Decimal("-152.79823123456789012345678901200")),
     )
Beispiel #3
0
 def test_encode_long_string(self):
     contents = 'a' * 2100
     encoded = encode(contents)
     token, length = unpack_from('!BH', encoded, 0)
     self.assertEqual(0x98, token)
     self.assertEqual(2100, length)
     self.assertEqual(encoded[3:], b'a' * 2100)
Beispiel #4
0
 def test_encode_long_string(self):
     contents = "a" * 2100
     encoded = encode(contents)
     token, length = unpack_from("!BH", encoded, 0)
     self.assertEqual(0x98, token)
     self.assertEqual(2100, length)
     self.assertEqual(encoded[3:], b"a" * 2100)
Beispiel #5
0
def cycle():
    sample = {
        "countries": [
            {"code": "us", "name": "United States"},
            {"code": "ca", "name": "Canada"},
            {"code": "mx", "name": "Mexico"},
        ],
        "region": 3,
        "burned": False,
        "dimensions": {"thickness": 0.7, "width": 4.5},
        "name": "the best",
        "toast": True,
        "toppings": ["jelly", "jam", "butter"],
    }
    # j = json.JSONEncoder(sort_keys=True)
    for i in range(1000):
        encode(sample, sort_keys=True)
Beispiel #6
0
 def test_encode_long_binary(self):
     if PY3:
         contents = b"a" * 2100
         encoded = encode(contents)
         token, length = unpack_from("!BH", encoded, 0)
         self.assertEqual(0xB8, token)
         self.assertEqual(2100, length)
         self.assertEqual(encoded[3:], contents)
Beispiel #7
0
 def test_encode_long_binary(self):
     if PY3:
         contents = b'a' * 2100
         encoded = encode(contents)
         token, length = unpack_from('!BH', encoded, 0)
         self.assertEqual(0xb8, token)
         self.assertEqual(2100, length)
         self.assertEqual(encoded[3:], contents)
Beispiel #8
0
 def test_repeating_keys(self):
     encoded = encode(
         {
             "countries": [
                 {"code": "us", "name": "United States"},
                 {"code": "ca", "name": "Canada"},
                 {"code": "mx", "name": "Mexico"}
             ],
             "region": 3,
         }, sort_keys=True)
     self.assertEqual(b'\xe2\x09countries\xc3\xe2\x04code\x82us\x04name\x8DUnited States\xe2\x81\x82ca\x82\x86Canada\xe2\x81\x82mx\x82\x86Mexico\x06region\x21\x03', encoded)
Beispiel #9
0
def cycle():
    sample = {
        "countries": [
            {"code": "us", "name": "United States"},
            {"code": "ca", "name": "Canada"},
            {"code": "mx", "name": "Mexico"}
        ],
        "region": 3,
        "burned": False,
        "dimensions": {
            "thickness": 0.7,
            "width": 4.5
        },
        "name": "the best",
        "toast": True,
        "toppings": ["jelly", "jam", "butter"]
    }
    # j = json.JSONEncoder(sort_keys=True)
    for i in range(1000):
        encode(sample, sort_keys=True)
Beispiel #10
0
 def test_simple_dict(self):
     encoded = encode(
         {
             "burned": False,
             "dimensions": {
                 "thickness": 0.7,
                 "width": 4.5
             },
             "name": "the best",
             "toast": True,
             "toppings": ["jelly", "jam", "butter"]
         }, sort_keys=True)
     self.assertEqual(b'\xe5\x06burned\x00\x0adimensions\xe2\x09thickness\x61\xd7\x05width\x62\x4d\x5d\x04name\x88the best\x05toast\x01\x08toppings\xc3\x85jelly\x83jam\x86butter', encoded)
Beispiel #11
0
 def test_dict_with_long_strings(self):
     encoded = encode(
         {
             "burned": False,
             "dimensions": {
                 "thickness": 0.7,
                 "width": 4.5
             },
             "name": "a" * 0x5000,
             "toast": True,
             "toppings": ["j" * 0x600, "k" * 0x672, "l" * 0x600]
         }, sort_keys=True)
     self.assertEqual(b'\xe5\x06burned\x00\x0adimensions\xe2\x09thickness\x61\xd7\x05width\x62\x4d\x5d\x04name\x98\x50\x00' + b'a' * 0x5000 + b'\x05toast\x01\x08toppings\xc3\x96\x00' + b'j' * 0x600 + b'\x96\x72' + b'k' * 0x672 + b'\x96\x00' + b'l' * 0x600, encoded)
Beispiel #12
0
 def test_simple_dict(self):
     encoded = encode(
         {
             "burned": False,
             "dimensions": {"thickness": 0.7, "width": 4.5},
             "name": "the best",
             "toast": True,
             "toppings": ["jelly", "jam", "butter"],
         },
         sort_keys=True,
     )
     self.assertEqual(
         b"\xe5\x06burned\x00\x0adimensions\xe2\x09thickness\x61\xd7\x05width\x62\x4d\x5d\x04name\x88the best\x05toast\x01\x08toppings\xc3\x85jelly\x83jam\x86butter",
         encoded,
     )
Beispiel #13
0
 def test_repeating_keys(self):
     encoded = encode(
         {
             "countries": [
                 {"code": "us", "name": "United States"},
                 {"code": "ca", "name": "Canada"},
                 {"code": "mx", "name": "Mexico"},
             ],
             "region": 3,
         },
         sort_keys=True,
     )
     self.assertEqual(
         b"\xe2\x09countries\xc3\xe2\x04code\x82us\x04name\x8DUnited States\xe2\x81\x82ca\x82\x86Canada\xe2\x81\x82mx\x82\x86Mexico\x06region\x21\x03",
         encoded,
     )
Beispiel #14
0
 def test_dict_with_long_strings(self):
     encoded = encode(
         {
             "burned": False,
             "dimensions": {"thickness": 0.7, "width": 4.5},
             "name": "a" * 0x5000,
             "toast": True,
             "toppings": ["j" * 0x600, "k" * 0x672, "l" * 0x600],
         },
         sort_keys=True,
     )
     self.assertEqual(
         b"\xe5\x06burned\x00\x0adimensions\xe2\x09thickness\x61\xd7\x05width\x62\x4d\x5d\x04name\x98\x50\x00"
         + b"a" * 0x5000
         + b"\x05toast\x01\x08toppings\xc3\x96\x00"
         + b"j" * 0x600
         + b"\x96\x72"
         + b"k" * 0x672
         + b"\x96\x00"
         + b"l" * 0x600,
         encoded,
     )
Beispiel #15
0
 def test_short_float(self):
     self.assertEqual(b'\x62\x4d\x5d', encode(4.5))
     self.assertEqual(b'\x62\xb4\xd5', encode(-4.5))
Beispiel #16
0
 def test_encode_none(self):
     self.assertEqual(b"\x02", encode(None))
Beispiel #17
0
 def test_negative_infinity(self):
     self.assertEqual(b'\x04', encode(float('-inf')))
Beispiel #18
0
 def test_range(self):
     self.assertEqual(b'\xc3\x20\x21\x01\x21\x02', encode(range(3)))
Beispiel #19
0
 def test_generator_list(self):
     def sample_generator():
         for x in range(3):
             yield x
     self.assertEqual(b'\x0c\x20\x21\x01\x21\x02\x0f', encode(sample_generator()))
Beispiel #20
0
    def test_generator_list(self):
        def sample_generator():
            for x in range(3):
                yield x

        self.assertEqual(b"\x0c\x20\x21\x01\x21\x02\x0f", encode(sample_generator()))
Beispiel #21
0
 def test_infinity(self):
     self.assertEqual(b"\x03", encode(float("inf")))
Beispiel #22
0
 def test_encode_bytes(self):
     if PY3:
         self.assertEqual(b"\xa4test", encode(b"test"))
Beispiel #23
0
 def test_encode_unicode(self):
     self.assertEqual(b"\x84test", encode("test"))
Beispiel #24
0
 def test_negative_infinity(self):
     self.assertEqual(b"\x04", encode(float("-inf")))
Beispiel #25
0
 def test_encode_medium_length_string(self):
     self.assertEqual(b"\x90\x20Now is the time for all good men", encode("Now is the time for all good men"))
Beispiel #26
0
 def test_encode_string(self):
     self.assertEqual(b"\x84test", encode("test"))
Beispiel #27
0
 def test_nan(self):
     self.assertEqual(b"\x05", encode(float("nan")))
Beispiel #28
0
 def test_zero_decimal(self):
     self.assertEqual(b'\x60', encode(Decimal('0.0')))
Beispiel #29
0
 def test_leading_zero_decimal(self):
     self.assertEqual(b'\x62\xd2\x5d', encode(Decimal('0.25')))
     self.assertEqual(b'\x62\xbd\x25', encode(Decimal('-0.25')))
Beispiel #30
0
 def test_zero_float(self):
     self.assertEqual(b"\x60", encode(0.0))
Beispiel #31
0
 def test_simple_list(self):
     self.assertEqual(b'\xc3\x85jelly\x83jam\x86butter', encode(['jelly', 'jam', 'butter']))
Beispiel #32
0
 def test_encode_true(self):
     self.assertEqual(b"\x01", encode(True))
Beispiel #33
0
 def test_tuple(self):
     self.assertEqual(b'\xc3\x20\x21\x01\x21\x02', encode((0, 1, 2)))
Beispiel #34
0
 def test_encode_none(self):
     self.assertEqual(b'\x02', encode(None))
Beispiel #35
0
 def test_infinity(self):
     self.assertEqual(b'\x03', encode(float('inf')))
Beispiel #36
0
 def test_encode_false(self):
     self.assertEqual(b'\x00', encode(False))
Beispiel #37
0
 def test_nan(self):
     self.assertEqual(b'\x05', encode(float('nan')))
Beispiel #38
0
 def test_encode_false(self):
     self.assertEqual(b"\x00", encode(False))
Beispiel #39
0
 def test_encode_medium_length_string(self):
     self.assertEqual(b'\x90\x20Now is the time for all good men', encode('Now is the time for all good men'))
Beispiel #40
0
 def test_int_float(self):
     self.assertEqual(b'\x61\x4d', encode(4.0))
     self.assertEqual(b'\x61\xb4', encode(-4.0))
Beispiel #41
0
 def test_int_float(self):
     self.assertEqual(b"\x61\x4d", encode(4.0))
     self.assertEqual(b"\x61\xb4", encode(-4.0))
Beispiel #42
0
 def test_range(self):
     self.assertEqual(b"\xc3\x20\x21\x01\x21\x02", encode(range(3)))
Beispiel #43
0
 def test_encode_true(self):
     self.assertEqual(b'\x01', encode(True))
Beispiel #44
0
 def test_leading_zero_float(self):
     self.assertEqual(b"\x62\xd2\x5d", encode(0.25))
     self.assertEqual(b"\x62\xbd\x25", encode(-0.25))
Beispiel #45
0
 def test_encode_string(self):
     self.assertEqual(b'\x84test', encode('test'))
Beispiel #46
0
 def test_encode_unicode(self):
     self.assertEqual(b'\x84test', encode('test'))
Beispiel #47
0
 def test_short_float(self):
     self.assertEqual(b"\x62\x4d\x5d", encode(4.5))
     self.assertEqual(b"\x62\xb4\xd5", encode(-4.5))
Beispiel #48
0
 def test_tuple(self):
     self.assertEqual(b"\xc3\x20\x21\x01\x21\x02", encode((0, 1, 2)))
Beispiel #49
0
 def test_encode_bytes(self):
     if PY3:
         self.assertEqual(b'\xa4test', encode(b'test'))
Beispiel #50
0
 def test_long_float(self):
     self.assertEqual(b"\x65\x15\x2d\x79\x82\x3d", encode(152.79823))
     self.assertEqual(b"\x65\xb1\x52\xd7\x98\x23", encode(-152.79823))
Beispiel #51
0
 def test_zero_float(self):
     self.assertEqual(b'\x60', encode(0.0))
Beispiel #52
0
 def test_zero_decimal(self):
     self.assertEqual(b"\x60", encode(Decimal("0.0")))
Beispiel #53
0
 def test_leading_zero_float(self):
     self.assertEqual(b'\x62\xd2\x5d', encode(0.25))
     self.assertEqual(b'\x62\xbd\x25', encode(-0.25))
Beispiel #54
0
 def test_int_decimal(self):
     self.assertEqual(b"\x61\x4d", encode(Decimal("4.0")))
     self.assertEqual(b"\x61\xb4", encode(Decimal("-4.0")))
Beispiel #55
0
 def test_long_float(self):
     self.assertEqual(b'\x65\x15\x2d\x79\x82\x3d', encode(152.79823))
     self.assertEqual(b'\x65\xb1\x52\xd7\x98\x23', encode(-152.79823))
Beispiel #56
0
 def test_leading_zero_decimal(self):
     self.assertEqual(b"\x62\xd2\x5d", encode(Decimal("0.25")))
     self.assertEqual(b"\x62\xbd\x25", encode(Decimal("-0.25")))
Beispiel #57
0
 def test_int_decimal(self):
     self.assertEqual(b'\x61\x4d', encode(Decimal('4.0')))
     self.assertEqual(b'\x61\xb4', encode(Decimal('-4.0')))
Beispiel #58
0
 def test_short_decimal(self):
     self.assertEqual(b"\x62\x4d\x5d", encode(Decimal("4.5")))
     self.assertEqual(b"\x62\xb4\xd5", encode(Decimal("-4.5")))
Beispiel #59
0
 def test_short_decimal(self):
     self.assertEqual(b'\x62\x4d\x5d', encode(Decimal('4.5')))
     self.assertEqual(b'\x62\xb4\xd5', encode(Decimal('-4.5')))
Beispiel #60
0
 def test_sixteen_byte_int(self):
     self.assertEqual(b'\x30\x10\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', encode(0x4000000000000000000000000000000))
     self.assertEqual(b'\x50\x10\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', encode(-0x4000000000000000000000000000000))