tst_putwc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Simple test of putwc in the C locale.
  2. Copyright (C) 2000-2026 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. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <error.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <wchar.h>
  20. static const char outname[] = OBJPFX "tst_putwc.temp";
  21. /* Prototype for our test function. */
  22. int do_test (void);
  23. #define TEST_FUNCTION do_test ()
  24. /* This defines the `main' function and some more. */
  25. #include <test-skeleton.c>
  26. int
  27. do_test (void)
  28. {
  29. const wchar_t str[] = L"This is a test of putwc\n";
  30. wchar_t buf[100];
  31. size_t n = 0;
  32. FILE *fp;
  33. int res = 0;
  34. add_temp_file (outname);
  35. fp = fopen (outname, "w+");
  36. if (fp == NULL)
  37. error (EXIT_FAILURE, errno, "cannot open temporary file");
  38. for (n = 0; str[n] != L'\0'; ++n)
  39. putwc (str[n], fp);
  40. /* First try reading after rewinding. */
  41. rewind (fp);
  42. wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  43. n = 0;
  44. while (! feof (fp) && n < sizeof (buf) - 1)
  45. {
  46. buf[n] = getwc (fp);
  47. if (buf[n] == WEOF)
  48. break;
  49. ++n;
  50. }
  51. buf[n] = L'\0';
  52. if (wcscmp (buf, L"This is a test of putwc\n") != 0)
  53. {
  54. puts ("first comparison failed");
  55. res = 1;
  56. }
  57. /* Now close the file, open it again, and read again. */
  58. if (fclose (fp) != 0)
  59. {
  60. printf ("failure during fclose: %m\n");
  61. res = 1;
  62. }
  63. fp = fopen (outname, "r");
  64. if (fp == NULL)
  65. {
  66. printf ("cannot reopen file: %m\n");
  67. return 1;
  68. }
  69. /* We can remove the file now. */
  70. remove (outname);
  71. wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  72. n = 0;
  73. while (! feof (fp) && n < sizeof (buf) - 1)
  74. {
  75. buf[n] = getwc (fp);
  76. if (buf[n] == WEOF)
  77. break;
  78. ++n;
  79. }
  80. buf[n] = L'\0';
  81. if (wcscmp (buf, L"This is a test of putwc\n") != 0)
  82. {
  83. puts ("second comparison failed");
  84. res = 1;
  85. }
  86. if (fclose (fp) != 0)
  87. {
  88. printf ("failure during fclose: %m\n");
  89. res = 1;
  90. }
  91. /* Next test: write a bit more than a few bytes. */
  92. fp = fopen (outname, "w");
  93. if (fp == NULL)
  94. error (EXIT_FAILURE, errno, "cannot open temporary file");
  95. for (n = 0; n < 4098; ++n)
  96. putwc (n & 255, fp);
  97. fclose (fp);
  98. return res;
  99. }