tposix.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. discard """
  2. outputsub: ""
  3. """
  4. # Test Posix interface
  5. when not defined(windows):
  6. import posix
  7. import std/[assertions, syncio]
  8. var
  9. u: Utsname
  10. discard uname(u)
  11. writeLine(stdout, u.sysname)
  12. writeLine(stdout, u.nodename)
  13. writeLine(stdout, u.release)
  14. writeLine(stdout, u.machine)
  15. when not (defined(nintendoswitch) or defined(macos) or defined(macosx)):
  16. block:
  17. type Message = object
  18. value: int
  19. const MQ_PATH: cstring = "/top_level_file"
  20. const MQ_PRIORITY: cuint = 170
  21. const MQ_MESSAGE_SIZE: csize_t = csize_t(sizeof(Message))
  22. let mqd_a: posix.MqAttr = MqAttr(mq_maxmsg: 10, mq_msgsize: clong(MQ_MESSAGE_SIZE))
  23. let writable: posix.Mqd = posix.mq_open(
  24. MQ_PATH,
  25. posix.O_CREAT or posix.O_WRONLY or posix.O_NONBLOCK,
  26. posix.S_IRWXU,
  27. addr(mqd_a)
  28. )
  29. let readable: posix.Mqd = posix.mq_open(
  30. MQ_PATH,
  31. posix.O_RDONLY or posix.O_NONBLOCK,
  32. posix.S_IRWXU,
  33. addr(mqd_a)
  34. )
  35. let sent: Message = Message(value: 88)
  36. block:
  37. let success: int = writable.mq_send(
  38. cast[cstring](sent.addr),
  39. MQ_MESSAGE_SIZE,
  40. MQ_PRIORITY
  41. )
  42. doAssert success == 0, $success
  43. block:
  44. var buffer: Message
  45. var priority: cuint
  46. let bytesRead: int = readable.mq_receive(
  47. cast[cstring](buffer.addr),
  48. MQ_MESSAGE_SIZE,
  49. priority
  50. )
  51. doAssert buffer == sent
  52. doAssert bytesRead == int(MQ_MESSAGE_SIZE)