tst-fopenloc2.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <wchar.h>
  6. static const struct
  7. {
  8. const char *enc;
  9. const char *data;
  10. size_t datalen;
  11. const wchar_t *expected;
  12. size_t expectedlen;
  13. } tests[] =
  14. {
  15. { "UCS-4LE", "a\0\0\0b\0\0\0", 8, L"ab", 2 },
  16. { "UCS-4BE", "\0\0\0a\0\0\0b", 8, L"ab", 2 },
  17. };
  18. #define ntests (sizeof (tests) / sizeof (tests[0]))
  19. static int do_test (void);
  20. #define TEST_FUNCTION do_test ()
  21. static void prepare (void);
  22. #define PREPARE(argc, argv) prepare ();
  23. #include "../test-skeleton.c"
  24. static int fd;
  25. static char *tmpname;
  26. static void
  27. prepare (void)
  28. {
  29. fd = create_temp_file ("tst-fopenloc2", &tmpname);
  30. if (fd == -1)
  31. {
  32. puts ("cannot open temp file");
  33. exit (1);
  34. }
  35. }
  36. static int
  37. do_test (void)
  38. {
  39. for (int i = 0; i < ntests; ++i)
  40. {
  41. if (ftruncate (fd, 0) != 0)
  42. {
  43. printf ("ftruncate in round %d failed\n", i + 1);
  44. return 1;
  45. }
  46. if (TEMP_FAILURE_RETRY (write (fd, tests[i].data, tests[i].datalen))
  47. != tests[i].datalen)
  48. {
  49. printf ("write in round %d failed\n", i + 1);
  50. return 1;
  51. }
  52. if (lseek (fd, 0, SEEK_SET) != 0)
  53. {
  54. printf ("lseek in round %d failed\n", i + 1);
  55. return 1;
  56. }
  57. char *ccs;
  58. if (asprintf (&ccs, "r,ccs=%s", tests[i].enc) == -1)
  59. {
  60. printf ("asprintf in round %d failed\n", i + 1);
  61. return 1;
  62. }
  63. FILE *fp = fopen (tmpname, ccs);
  64. if (fp == NULL)
  65. {
  66. printf ("fopen in round %d failed\n", i + 1);
  67. return 1;
  68. }
  69. #define LINELEN 100
  70. wchar_t line[LINELEN];
  71. if (fgetws (line, LINELEN, fp) != line)
  72. {
  73. printf ("fgetws in round %d failed\n", i + 1);
  74. return 1;
  75. }
  76. if (wcslen (line) != tests[i].expectedlen)
  77. {
  78. printf ("round %d: expected length %zu, got length %zu\n",
  79. i + 1, tests[i].expectedlen, wcslen (line));
  80. return 1;
  81. }
  82. if (wcscmp (tests[i].expected, line) != 0)
  83. {
  84. printf ("round %d: expected L\"%ls\", got L\"%ls\"\n",
  85. i + 1, tests[i].expected, line);
  86. return 1;
  87. }
  88. fclose (fp);
  89. free (ccs);
  90. }
  91. close (fd);
  92. return 0;
  93. }