tst-preadvwritev.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #define FAIL(str) \
  35. do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
  36. static int
  37. do_test_with_offset (off_t offset)
  38. {
  39. struct iovec iov[2];
  40. ssize_t ret;
  41. char buf1[32];
  42. char buf2[64];
  43. memset (buf1, 0xf0, sizeof buf1);
  44. memset (buf2, 0x0f, sizeof buf2);
  45. /* Write two buffer with 32 and 64 bytes respectively. */
  46. memset (iov, 0, sizeof iov);
  47. iov[0].iov_base = buf1;
  48. iov[0].iov_len = sizeof buf1;
  49. iov[1].iov_base = buf2;
  50. iov[1].iov_len = sizeof buf2;
  51. ret = pwritev (temp_fd, iov, 2, offset);
  52. if (ret == -1)
  53. FAIL ("first pwritev returned -1");
  54. if (ret != (sizeof buf1 + sizeof buf2))
  55. FAIL ("first pwritev returned an unexpected value");
  56. ret = pwritev (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset);
  57. if (ret == -1)
  58. FAIL ("second pwritev returned -1");
  59. if (ret != (sizeof buf1 + sizeof buf2))
  60. FAIL ("second pwritev returned an unexpected value");
  61. char buf3[32];
  62. char buf4[64];
  63. memset (buf3, 0x0f, sizeof buf3);
  64. memset (buf4, 0xf0, sizeof buf4);
  65. iov[0].iov_base = buf3;
  66. iov[0].iov_len = sizeof buf3;
  67. iov[1].iov_base = buf4;
  68. iov[1].iov_len = sizeof buf4;
  69. /* Now read two buffer with 32 and 64 bytes respectively. */
  70. ret = preadv (temp_fd, iov, 2, offset);
  71. if (ret == -1)
  72. FAIL ("first preadv returned -1");
  73. if (ret != (sizeof buf3 + sizeof buf4))
  74. FAIL ("first preadv returned an unexpected value");
  75. if (memcmp (buf1, buf3, sizeof buf1) != 0)
  76. FAIL ("first buffer from first preadv different than expected");
  77. if (memcmp (buf2, buf4, sizeof buf2) != 0)
  78. FAIL ("second buffer from first preadv different than expected");
  79. ret = preadv (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset);
  80. if (ret == -1)
  81. FAIL ("second preadv returned -1");
  82. if (ret != (sizeof buf3 + sizeof buf4))
  83. FAIL ("second preadv returned an unexpected value");
  84. /* And compare the buffers read and written to check if there are equal. */
  85. if (memcmp (buf1, buf3, sizeof buf1) != 0)
  86. FAIL ("first buffer from second preadv different than expected");
  87. if (memcmp (buf2, buf4, sizeof buf2) != 0)
  88. FAIL ("second buffer from second preadv different than expected");
  89. return 0;
  90. }
  91. static int
  92. do_test (void)
  93. {
  94. return do_test_with_offset (0);
  95. }