lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <inttypes.h> |
| 2 | #include <locale.h> |
| 3 | #include <stdio.h> |
| 4 | #include <uchar.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | static int |
| 8 | do_test (void) |
| 9 | { |
| 10 | if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL) |
| 11 | { |
| 12 | puts ("cannot set locale"); |
| 13 | return 1; |
| 14 | } |
| 15 | |
| 16 | int result = 0; |
| 17 | |
| 18 | char32_t c32 = 48; |
| 19 | do |
| 20 | { |
| 21 | if (c32 >= 0xd800 && c32 <= 0xe000) |
| 22 | continue; |
| 23 | |
| 24 | char buf[20]; |
| 25 | size_t n1 = c32rtomb (buf, c32, NULL); |
| 26 | if (n1 <= 0) |
| 27 | { |
| 28 | printf ("c32rtomb for U'\\x%" PRIx32 "' failed\n", (uint32_t) c32); |
| 29 | result = 1; |
| 30 | continue; |
| 31 | } |
| 32 | |
| 33 | char32_t c32out; |
| 34 | size_t n2 = mbrtoc32 (&c32out, buf, n1, NULL); |
| 35 | if ((ssize_t) n2 < 0) |
| 36 | { |
| 37 | printf ("mbrtoc32 for U'\\x%" PRIx32 "' failed\n", (uint32_t) c32); |
| 38 | result = 1; |
| 39 | continue; |
| 40 | } |
| 41 | if (n2 != n1) |
| 42 | { |
| 43 | printf ("mbrtoc32 for U'\\x%" PRIx32 "' consumed %zu bytes, not %zu\n", |
| 44 | (uint32_t) c32, n2, n1); |
| 45 | result = 1; |
| 46 | } |
| 47 | else if (c32out != c32) |
| 48 | { |
| 49 | printf ("mbrtoc32 for U'\\x%" PRIx32 "' produced U'\\x%" PRIx32 "\n", |
| 50 | (uint32_t) c32, (uint32_t) c32out); |
| 51 | result = 1; |
| 52 | } |
| 53 | |
| 54 | char16_t c16; |
| 55 | size_t n3 = mbrtoc16 (&c16, buf, n1, NULL); |
| 56 | if (n3 != n1) |
| 57 | { |
| 58 | printf ("mbrtoc16 for U'\\x%" PRIx32 "' did not consume all bytes\n", |
| 59 | (uint32_t) c32); |
| 60 | result = 1; |
| 61 | continue; |
| 62 | } |
| 63 | if (c32 < 0x10000) |
| 64 | { |
| 65 | if (c16 != c32) |
| 66 | { |
| 67 | printf ("mbrtoc16 for U'\\x%" PRIx32 "' produce u'\\x%" PRIx16 "'\n", |
| 68 | (uint32_t) c32, (uint16_t) c16); |
| 69 | result = 1; |
| 70 | continue; |
| 71 | } |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | buf[0] = '1'; |
| 76 | char16_t c16_2; |
| 77 | size_t n4 = mbrtoc16 (&c16_2, buf, 1, NULL); |
| 78 | if (n4 != (size_t) -3) |
| 79 | { |
| 80 | printf ("second mbrtoc16 for U'\\x%" PRIx32 "' did not return -3\n", |
| 81 | (uint32_t) c32); |
| 82 | result = 1; |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (c32 != (((uint32_t) (c16 - 0xd7c0)) << 10) + (c16_2 - 0xdc00)) |
| 87 | { |
| 88 | printf ("mbrtoc16 for U'\\x%" PRIx32 "' returns U'\\x%" PRIx32 "\n", |
| 89 | (uint32_t) c32, |
| 90 | (((uint32_t) (c16 - 0xd7c0)) << 10) + (c16_2 - 0xdc00)); |
| 91 | result = 1; |
| 92 | continue; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | buf[0] = '\0'; |
| 97 | char16_t c16_nul; |
| 98 | n3 = mbrtoc16 (&c16_nul, buf, n1, NULL); |
| 99 | if (n3 != 0) |
| 100 | { |
| 101 | printf ("mbrtoc16 for '\\0' returns %zd\n", n3); |
| 102 | result = 1; |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if (c32 < 0x10000) |
| 107 | { |
| 108 | size_t n5 = c16rtomb (buf, c16, NULL); |
| 109 | if ((ssize_t) n5 < 0) |
| 110 | { |
| 111 | printf ("c16rtomb for U'\\x%" PRIx32 "' failed with %zd\n", |
| 112 | (uint32_t) c32, n5); |
| 113 | result = 1; |
| 114 | continue; |
| 115 | } |
| 116 | if (n5 != n1) |
| 117 | { |
| 118 | printf ("c16rtomb for U'\\x%" PRIx32 "' produced %zu bytes instead of %zu bytes\n", |
| 119 | (uint32_t) c32, n5, n1); |
| 120 | result = 1; |
| 121 | continue; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | while ((c32 += 0x1111) <= U'\x12000'); |
| 126 | |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | #define TEST_FUNCTION do_test () |
| 131 | #include "../test-skeleton.c" |