tst_putwc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Simple test of putwc in the C locale.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <wchar.h>
  21. static const char outname[] = OBJPFX "tst_putwc.temp";
  22. /* Prototype for our test function. */
  23. int do_test (void);
  24. #define TEST_FUNCTION do_test ()
  25. /* This defines the `main' function and some more. */
  26. #include <test-skeleton.c>
  27. int
  28. do_test (void)
  29. {
  30. const wchar_t str[] = L"This is a test of putwc\n";
  31. wchar_t buf[100];
  32. size_t n = 0;
  33. FILE *fp;
  34. int res = 0;
  35. add_temp_file (outname);
  36. fp = fopen (outname, "w+");
  37. if (fp == NULL)
  38. error (EXIT_FAILURE, errno, "cannot open temporary file");
  39. for (n = 0; str[n] != L'\0'; ++n)
  40. putwc (str[n], fp);
  41. /* First try reading after rewinding. */
  42. rewind (fp);
  43. wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  44. n = 0;
  45. while (! feof (fp) && n < sizeof (buf) - 1)
  46. {
  47. buf[n] = getwc (fp);
  48. if (buf[n] == WEOF)
  49. break;
  50. ++n;
  51. }
  52. buf[n] = L'\0';
  53. if (wcscmp (buf, L"This is a test of putwc\n") != 0)
  54. {
  55. puts ("first comparison failed");
  56. res = 1;
  57. }
  58. /* Now close the file, open it again, and read again. */
  59. if (fclose (fp) != 0)
  60. {
  61. printf ("failure during fclose: %m\n");
  62. res = 1;
  63. }
  64. fp = fopen (outname, "r");
  65. if (fp == NULL)
  66. {
  67. printf ("cannot reopen file: %m\n");
  68. return 1;
  69. }
  70. /* We can remove the file now. */
  71. remove (outname);
  72. wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  73. n = 0;
  74. while (! feof (fp) && n < sizeof (buf) - 1)
  75. {
  76. buf[n] = getwc (fp);
  77. if (buf[n] == WEOF)
  78. break;
  79. ++n;
  80. }
  81. buf[n] = L'\0';
  82. if (wcscmp (buf, L"This is a test of putwc\n") != 0)
  83. {
  84. puts ("second comparison failed");
  85. res = 1;
  86. }
  87. if (fclose (fp) != 0)
  88. {
  89. printf ("failure during fclose: %m\n");
  90. res = 1;
  91. }
  92. /* Next test: write a bit more than a few bytes. */
  93. fp = fopen (outname, "w");
  94. if (fp == NULL)
  95. error (EXIT_FAILURE, errno, "cannot open temporary file");
  96. for (n = 0; n < 4098; ++n)
  97. putwc (n & 255, fp);
  98. fclose (fp);
  99. return res;
  100. }