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