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), )
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")), )
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)
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)
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)
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)
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)
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)
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)
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)
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)
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, )
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, )
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, )
def test_short_float(self): self.assertEqual(b'\x62\x4d\x5d', encode(4.5)) self.assertEqual(b'\x62\xb4\xd5', encode(-4.5))
def test_encode_none(self): self.assertEqual(b"\x02", encode(None))
def test_negative_infinity(self): self.assertEqual(b'\x04', encode(float('-inf')))
def test_range(self): self.assertEqual(b'\xc3\x20\x21\x01\x21\x02', encode(range(3)))
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()))
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()))
def test_infinity(self): self.assertEqual(b"\x03", encode(float("inf")))
def test_encode_bytes(self): if PY3: self.assertEqual(b"\xa4test", encode(b"test"))
def test_encode_unicode(self): self.assertEqual(b"\x84test", encode("test"))
def test_negative_infinity(self): self.assertEqual(b"\x04", encode(float("-inf")))
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"))
def test_encode_string(self): self.assertEqual(b"\x84test", encode("test"))
def test_nan(self): self.assertEqual(b"\x05", encode(float("nan")))
def test_zero_decimal(self): self.assertEqual(b'\x60', encode(Decimal('0.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')))
def test_zero_float(self): self.assertEqual(b"\x60", encode(0.0))
def test_simple_list(self): self.assertEqual(b'\xc3\x85jelly\x83jam\x86butter', encode(['jelly', 'jam', 'butter']))
def test_encode_true(self): self.assertEqual(b"\x01", encode(True))
def test_tuple(self): self.assertEqual(b'\xc3\x20\x21\x01\x21\x02', encode((0, 1, 2)))
def test_encode_none(self): self.assertEqual(b'\x02', encode(None))
def test_infinity(self): self.assertEqual(b'\x03', encode(float('inf')))
def test_encode_false(self): self.assertEqual(b'\x00', encode(False))
def test_nan(self): self.assertEqual(b'\x05', encode(float('nan')))
def test_encode_false(self): self.assertEqual(b"\x00", encode(False))
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'))
def test_int_float(self): self.assertEqual(b'\x61\x4d', encode(4.0)) self.assertEqual(b'\x61\xb4', encode(-4.0))
def test_int_float(self): self.assertEqual(b"\x61\x4d", encode(4.0)) self.assertEqual(b"\x61\xb4", encode(-4.0))
def test_range(self): self.assertEqual(b"\xc3\x20\x21\x01\x21\x02", encode(range(3)))
def test_encode_true(self): self.assertEqual(b'\x01', encode(True))
def test_leading_zero_float(self): self.assertEqual(b"\x62\xd2\x5d", encode(0.25)) self.assertEqual(b"\x62\xbd\x25", encode(-0.25))
def test_encode_string(self): self.assertEqual(b'\x84test', encode('test'))
def test_encode_unicode(self): self.assertEqual(b'\x84test', encode('test'))
def test_short_float(self): self.assertEqual(b"\x62\x4d\x5d", encode(4.5)) self.assertEqual(b"\x62\xb4\xd5", encode(-4.5))
def test_tuple(self): self.assertEqual(b"\xc3\x20\x21\x01\x21\x02", encode((0, 1, 2)))
def test_encode_bytes(self): if PY3: self.assertEqual(b'\xa4test', encode(b'test'))
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))
def test_zero_float(self): self.assertEqual(b'\x60', encode(0.0))
def test_zero_decimal(self): self.assertEqual(b"\x60", encode(Decimal("0.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))
def test_int_decimal(self): self.assertEqual(b"\x61\x4d", encode(Decimal("4.0"))) self.assertEqual(b"\x61\xb4", encode(Decimal("-4.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))
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")))
def test_int_decimal(self): self.assertEqual(b'\x61\x4d', encode(Decimal('4.0'))) self.assertEqual(b'\x61\xb4', encode(Decimal('-4.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")))
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')))
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))