99_bufan.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import c
  2. c_int = c.refl("int")
  3. assert c_int.name() == "int"
  4. assert c_int.__repr__() == '<ctype \'int\'>'
  5. # ------------------------------------------------
  6. c_int_1 = c.refl("int")
  7. c_struct_1 = c_int_1()
  8. assert (c_int_1() == c_int_1())
  9. assert (c_struct_1 == c_struct_1) == True
  10. # ------------------------------------------------
  11. assert c.void_p.from_hex('0x2568b60').hex() == '0x2568b60'
  12. # ------------------------------------------------
  13. class HexAddress:
  14. def __init__(self, address):
  15. if not address.startswith("0x"): # 确保地址以0x开头
  16. raise ValueError("Address should start with '0x'.")
  17. self.address = address[2:] # 去除0x前缀,并保存十六进制字符串
  18. def __str__(self):
  19. return "0x" + self.address
  20. def __add__(self, other):
  21. if isinstance(other, int):
  22. return HexAddress(hex(int(self.address, 16) + other)) # 将字符串地址转为整数进行运算
  23. elif isinstance(other, HexAddress):
  24. return HexAddress(hex(int(self.address, 16) + int(other.address, 16))) # 将字符串地址转为整数进行运算
  25. else:
  26. raise TypeError("Unsupported operand type for +: HexAddress and {}".format(type(other)))
  27. def __sub__(self, other):
  28. if isinstance(other, int):
  29. return HexAddress(hex(int(self.address, 16) - other)) # 将字符串地址转为整数进行运算
  30. elif isinstance(other, HexAddress):
  31. return HexAddress(hex(int(self.address, 16) - int(other.address, 16))) # 将字符串地址转为整数进行运算
  32. else:
  33. raise TypeError("Unsupported operand type for -: HexAddress and {}".format(type(other)))
  34. c_void_1 = c.malloc(8)
  35. assert (c_void_1 + 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) + 8)).hex()
  36. assert (c_void_1 - 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) - 8)).hex()
  37. # ------------------------------------------------
  38. # 此处测试并不完全
  39. c_void_1 = c.malloc(8)
  40. c_void_1.read_bytes(5)
  41. c_void_1.write_bytes(c_void_1.read_bytes(5))
  42. # ------------------------------------------------
  43. c_void_1 = c.malloc(32)
  44. my_struct2 = c.struct(c_void_1, 32)
  45. data_str = "Hello, World!"
  46. my_struct3 = c.struct(data_str)
  47. data_bytes = bytes([1,2,3])
  48. my_struct4 = c.struct(data_bytes)
  49. try:
  50. c.struct(True)
  51. raise Exception('c.struct 的构造方法未能触发 TypeError("expected int, str or bytes")')
  52. except TypeError:
  53. pass
  54. try:
  55. c.struct(1,2,3)
  56. raise Exception('c.struct 的构造方法未能触发 TypeError("expected 1 or 2 arguments")')
  57. except TypeError:
  58. pass
  59. try:
  60. x = c.refl("int")['a']
  61. except KeyError:
  62. pass
  63. # ------------------------------------------------
  64. my_struct1 = c.struct(16)
  65. assert my_struct1.size() == 16
  66. # 对 c.struct 的 copy 方法的测试不完全
  67. assert my_struct1.copy().size() == 16
  68. data_str = "Hello, World!"
  69. my_struct3 = c.struct(data_str)
  70. assert my_struct3.to_string() == data_str
  71. data_bytes = bytes([1,2,3])
  72. my_struct4 = c.struct(data_bytes)
  73. assert my_struct4.to_bytes() == data_bytes
  74. # ------------------------------------------------
  75. # 此处测试并不完全
  76. c_void_1 = c.malloc(16)
  77. my_struct1 = c.struct(16)
  78. c_void_1.read_struct('long')
  79. c_void_1.write_struct(my_struct1)