lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <wchar.h> |
| 4 | |
| 5 | |
| 6 | static int |
| 7 | do_test (void) |
| 8 | { |
| 9 | size_t size; |
| 10 | wchar_t *buf; |
| 11 | FILE *fp = open_wmemstream (&buf, &size); |
| 12 | if (fp == NULL) |
| 13 | { |
| 14 | puts ("open_wmemstream failed"); |
| 15 | return 1; |
| 16 | } |
| 17 | |
| 18 | off64_t off = ftello64 (fp); |
| 19 | if (off != 0) |
| 20 | { |
| 21 | puts ("initial position wrong"); |
| 22 | return 1; |
| 23 | } |
| 24 | |
| 25 | if (fseek (fp, 32768, SEEK_SET) != 0) |
| 26 | { |
| 27 | puts ("fseek failed"); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | if (fputws (L"foo", fp) == EOF) |
| 32 | { |
| 33 | puts ("fputws failed"); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | if (fclose (fp) == EOF) |
| 38 | { |
| 39 | puts ("fclose failed"); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | if (size != 32768 + 3) |
| 44 | { |
| 45 | printf ("expected size %d, got %zu\n", 32768 + 3, size); |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | for (int i = 0; i < 32768; ++i) |
| 50 | if (buf[i] != L'\0') |
| 51 | { |
| 52 | printf ("wide character at offset %d is %#x\n", |
| 53 | i, (unsigned int) buf[i]); |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | if (wmemcmp (buf + 32768, L"foo", 3) != 0) |
| 58 | { |
| 59 | puts ("written string incorrect"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | /* Mark the buffer. */ |
| 64 | wmemset (buf, L'A', size); |
| 65 | free (buf); |
| 66 | |
| 67 | /* Try again, this time with write mode enabled before the seek. */ |
| 68 | fp = open_wmemstream (&buf, &size); |
| 69 | if (fp == NULL) |
| 70 | { |
| 71 | puts ("2nd open_wmemstream failed"); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | off = ftello64 (fp); |
| 76 | if (off != 0) |
| 77 | { |
| 78 | puts ("2nd initial position wrong"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | if (fputws (L"bar", fp) == EOF) |
| 83 | { |
| 84 | puts ("2nd fputws failed"); |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | if (fseek (fp, 32768, SEEK_SET) != 0) |
| 89 | { |
| 90 | puts ("2nd fseek failed"); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | if (fputws (L"foo", fp) == EOF) |
| 95 | { |
| 96 | puts ("3rd fputws failed"); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | if (fclose (fp) == EOF) |
| 101 | { |
| 102 | puts ("2nd fclose failed"); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | if (size != 32768 + 3) |
| 107 | { |
| 108 | printf ("2nd expected size %d, got %zu\n", 32768 + 3, size); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | if (wmemcmp (buf, L"bar", 3) != 0) |
| 113 | { |
| 114 | puts ("initial string incorrect in 2nd try"); |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | for (int i = 3; i < 32768; ++i) |
| 119 | if (buf[i] != L'\0') |
| 120 | { |
| 121 | printf ("wide character at offset %d is %#x in 2nd try\n", |
| 122 | i, (unsigned int) buf[i]); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | if (wmemcmp (buf + 32768, L"foo", 3) != 0) |
| 127 | { |
| 128 | puts ("written string incorrect in 2nd try"); |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | #define TEST_FUNCTION do_test () |
| 136 | #include "../test-skeleton.c" |