tst-preadvwritev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Tests for preadv and pwritev.
  2. Copyright (C) 2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #define __USE_MISC
  16. #include <sys/uio.h>
  17. static void do_prepare (void);
  18. #define PREPARE(argc, argv) do_prepare ()
  19. static int do_test (void);
  20. #define TEST_FUNCTION do_test ()
  21. #include "test-skeleton.c"
  22. static char *temp_filename;
  23. static int temp_fd;
  24. static void
  25. do_prepare (void)
  26. {
  27. temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
  28. if (temp_fd == -1)
  29. {
  30. printf ("cannot create temporary file: %m\n");
  31. exit (1);
  32. }
  33. }
  34. #if defined(preadv) && defined(pwritev)
  35. #define FAIL(str) \
  36. do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
  37. static int
  38. do_test_with_offset (off_t offset)
  39. {
  40. struct iovec iov[2];
  41. ssize_t ret;
  42. char buf1[32];
  43. char buf2[64];
  44. memset (buf1, 0xf0, sizeof buf1);
  45. memset (buf2, 0x0f, sizeof buf2);
  46. /* Write two buffer with 32 and 64 bytes respectively. */
  47. memset (iov, 0, sizeof iov);
  48. iov[0].iov_base = buf1;
  49. iov[0].iov_len = sizeof buf1;
  50. iov[1].iov_base = buf2;
  51. iov[1].iov_len = sizeof buf2;
  52. ret = pwritev (temp_fd, iov, 2, offset);
  53. if (ret == -1)
  54. FAIL ("first pwritev returned -1");
  55. if (ret != (sizeof buf1 + sizeof buf2))
  56. FAIL ("first pwritev returned an unexpected value");
  57. ret = pwritev (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset);
  58. if (ret == -1)
  59. FAIL ("second pwritev returned -1");
  60. if (ret != (sizeof buf1 + sizeof buf2))
  61. FAIL ("second pwritev returned an unexpected value");
  62. char buf3[32];
  63. char buf4[64];
  64. memset (buf3, 0x0f, sizeof buf3);
  65. memset (buf4, 0xf0, sizeof buf4);
  66. iov[0].iov_base = buf3;
  67. iov[0].iov_len = sizeof buf3;
  68. iov[1].iov_base = buf4;
  69. iov[1].iov_len = sizeof buf4;
  70. /* Now read two buffer with 32 and 64 bytes respectively. */
  71. ret = preadv (temp_fd, iov, 2, offset);
  72. if (ret == -1)
  73. FAIL ("first preadv returned -1");
  74. if (ret != (sizeof buf3 + sizeof buf4))
  75. FAIL ("first preadv returned an unexpected value");
  76. if (memcmp (buf1, buf3, sizeof buf1) != 0)
  77. FAIL ("first buffer from first preadv different than expected");
  78. if (memcmp (buf2, buf4, sizeof buf2) != 0)
  79. FAIL ("second buffer from first preadv different than expected");
  80. ret = preadv (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset);
  81. if (ret == -1)
  82. FAIL ("second preadv returned -1");
  83. if (ret != (sizeof buf3 + sizeof buf4))
  84. FAIL ("second preadv returned an unexpected value");
  85. /* And compare the buffers read and written to check if there are equal. */
  86. if (memcmp (buf1, buf3, sizeof buf1) != 0)
  87. FAIL ("first buffer from second preadv different than expected");
  88. if (memcmp (buf2, buf4, sizeof buf2) != 0)
  89. FAIL ("second buffer from second preadv different than expected");
  90. return 0;
  91. }
  92. #endif
  93. static int
  94. do_test (void)
  95. {
  96. #if defined(preadv) && defined(pwritev)
  97. return do_test_with_offset (0);
  98. #else
  99. return 23;
  100. #endif
  101. }