793_stdc.py 1.4 KB

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