99_cffi_2.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import c
  2. assert c.NULL == c.void_p(0)
  3. # ------------------------------------------------
  4. # 此处测试并不完全
  5. c_void_1 = c.malloc(8)
  6. c_void_1.read_bytes(5)
  7. c_void_1.write_bytes(c_void_1.read_bytes(5))
  8. # ------------------------------------------------
  9. c_void_1 = c.malloc(32)
  10. my_struct2 = c_void_1.read_struct(32)
  11. assert my_struct2.sizeof() == 32
  12. data_bytes = bytes([1,2,3])
  13. my_struct4 = c.struct(data_bytes)
  14. try:
  15. c.struct(True)
  16. raise Exception('c.struct 的构造方法未能触发 TypeError("expected int or bytes")')
  17. except TypeError:
  18. pass
  19. try:
  20. c.struct(1,2,3)
  21. raise Exception('c.struct 的构造方法未能触发 TypeError("expected 1 or 2 arguments")')
  22. except TypeError:
  23. pass
  24. # ------------------------------------------------
  25. my_struct1 = c.struct(16)
  26. assert my_struct1.sizeof() == 16
  27. # 对 c.struct 的 copy 方法的测试不完全
  28. assert my_struct1.copy().sizeof() == 16
  29. data_bytes = bytes([1,2,3])
  30. my_struct4 = c.struct(data_bytes)
  31. assert my_struct4.addr().read_bytes(
  32. my_struct4.sizeof()
  33. ) == data_bytes
  34. # ------------------------------------------------
  35. # 此处测试并不完全
  36. c_void_1 = c.malloc(16)
  37. my_struct1 = c.struct(16)
  38. c_void_1.write_struct(my_struct1)
  39. assert c_void_1.read_struct(16) == my_struct1
  40. from c import array, int_
  41. a = array(10, item_size=4)
  42. assert a.item_count == 10
  43. assert a.item_size == 4
  44. a[4] = int_(123)
  45. assert a[4] == int_(123)