793_stdc.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from stdc import *
  2. assert sizeof(Int8) == sizeof(UInt8) == 1
  3. assert sizeof(Int16) == sizeof(UInt16) == 2
  4. assert sizeof(Int32) == sizeof(UInt32) == 4
  5. assert sizeof(Int64) == sizeof(UInt64) == 8
  6. assert sizeof(Float) == 4
  7. assert sizeof(Double) == 8
  8. assert sizeof(Bool) == 1
  9. assert sizeof(Pointer) in (4, 8)
  10. x = Int32(42)
  11. assert x.value == 42
  12. x.value = 100
  13. assert x.value == 100
  14. Int32.read(addressof(x), 0) == 100
  15. Int32.write(addressof(x), 0, 200)
  16. assert x.value == 200
  17. # test array
  18. arr = Int32.array(3)
  19. arr[0] = 10
  20. arr[1] = 20
  21. arr[2] = 30
  22. assert arr[0] == 10
  23. assert arr[1] == 20
  24. assert arr[2] == 30
  25. # test malloc, memset, memcpy
  26. p = malloc(3 * sizeof(Int32))
  27. memset(p, 0, 3 * sizeof(Int32))
  28. memcpy(p, addressof(arr), 3 * sizeof(Int32))
  29. for i in range(3):
  30. assert arr[i] == Int32.read(p, i)
  31. assert memcmp(p, addressof(arr), 3 * sizeof(Int32)) == 0
  32. # test free
  33. free(p)
  34. # test float
  35. y = Double.array(3)
  36. y[0] = 1.1
  37. y[1] = 2.2
  38. y[2] = 3.3
  39. assert Double.read(addressof(y), 0) == 1.1
  40. assert Double.read(addressof(y), 1) == 2.2
  41. assert Double.read(addressof(y), 2) == 3.3
  42. # test read_cstr and write_cstr
  43. a = Char.array(20)
  44. write_cstr(addressof(a), "hello")
  45. assert read_cstr(addressof(a)) == "hello"
  46. a[3] = 0
  47. assert read_cstr(addressof(a)) == "hel"
  48. # test read_bytes and write_bytes
  49. assert read_bytes(addressof(a), 5) == b'hel\x00o'