1
0

723_msgpack.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. try:
  2. import msgpack
  3. except ImportError:
  4. print('msgpack is not enabled, skipping test...')
  5. exit()
  6. a = {
  7. 'a': 1,
  8. 'b': 2,
  9. 'c': None,
  10. 'd': [1, 2, 3],
  11. 'e': {
  12. 'a': 100,
  13. 'b': 2.5,
  14. 'c': None,
  15. 'd': [142, 2785, 39767],
  16. },
  17. "f": 'This is a string',
  18. 'g': [True, False, None],
  19. 'h': False
  20. }
  21. import msgpack
  22. assert msgpack.loads(b'\x01') == 1
  23. assert msgpack.loads(b'\xa11') == "1"
  24. assert msgpack.loads(b'\xcb\x00\x00\x00\x00\x00\x00\x00\x00') == 0.0
  25. assert msgpack.loads(b'\x92\x01\x02') == [1, 2]
  26. assert msgpack.loads(b'\xc0') == None
  27. assert msgpack.loads(b'\xc3') == True
  28. assert msgpack.loads(b'\xc2') == False
  29. assert msgpack.loads(b'\x80') == {}
  30. _j = msgpack.dumps(a)
  31. _a = msgpack.loads(_j)
  32. for k, v in a.items():
  33. assert (a[k] == _a[k]), f'{a[k]} != {_a[k]}'
  34. for k, v in _a.items():
  35. assert (a[k] == _a[k]), f'{a[k]} != {_a[k]}'
  36. b = [1, 2, True, None, False]
  37. _j = msgpack.dumps(b)
  38. _b = msgpack.loads(_j)
  39. assert b == _b
  40. c = 1.0
  41. _j = msgpack.dumps(c)
  42. _c = msgpack.loads(_j)
  43. assert c == _c
  44. d = True
  45. _j = msgpack.dumps(d)
  46. _d = msgpack.loads(_j)
  47. assert d == _d
  48. # assert msgpack.dumps((1,)) == '[1]'
  49. # assert msgpack.dumps((1, 2, 3)) == '[1, 2, 3]'
  50. # assert msgpack.dumps(tuple()) == '[]'
  51. assert msgpack.dumps([]) == b'\x90'
  52. assert msgpack.dumps([1, 2, 3]) == b'\x93\x01\x02\x03'
  53. assert msgpack.dumps([1]) == b'\x91\x01'
  54. try:
  55. msgpack.dumps({1: 2})
  56. assert False
  57. except TypeError:
  58. assert True
  59. try:
  60. msgpack.dumps(type)
  61. assert False
  62. except TypeError:
  63. assert True