source: mainline/uspace/lib/c/test/uchar.c

Last change on this file was 696b405, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Implement standard character conversion functions from <uchar.h> and <wchar.h>

These are more somewhat more flexible than the existing functions in <str.h> since
they can process incomplete stream and save unfinished character for next call.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[696b405]1/*
2 * Copyright (c) 2025 Jiří Zárevúcky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <mem.h>
30#include <pcut/pcut.h>
31#include <stdio.h>
32#include <uchar.h>
33
34PCUT_INIT;
35
36PCUT_TEST_SUITE(uchar);
37
38PCUT_TEST(mbrtoc16)
39{
40 mbstate_t mbstate;
41 memset(&mbstate, 0, sizeof(mbstate));
42
43 size_t ret;
44 const char *s = u8"a𐎣";
45 char16_t out[] = u"AAAAA";
46 const char16_t expected[] = u"a𐎣";
47
48 ret = mbrtoc16(&out[0], s, MB_CUR_MAX, &mbstate);
49 PCUT_ASSERT_INT_EQUALS(1, ret);
50 PCUT_ASSERT_INT_EQUALS(expected[0], out[0]);
51
52 s += ret;
53
54 ret = mbrtoc16(&out[1], s, MB_CUR_MAX, &mbstate);
55 PCUT_ASSERT_INT_EQUALS(4, ret);
56 PCUT_ASSERT_INT_EQUALS(expected[1], out[1]);
57
58 s += ret;
59
60 ret = mbrtoc16(&out[2], s, MB_CUR_MAX, &mbstate);
61 PCUT_ASSERT_INT_EQUALS(-3, ret);
62 PCUT_ASSERT_INT_EQUALS(expected[2], out[2]);
63
64 ret = mbrtoc16(&out[3], s, MB_CUR_MAX, &mbstate);
65 PCUT_ASSERT_INT_EQUALS(0, ret);
66 PCUT_ASSERT_INT_EQUALS(expected[3], out[3]);
67}
68
69PCUT_TEST(c16rtomb)
70{
71 mbstate_t mbstate;
72 memset(&mbstate, 0, sizeof(mbstate));
73
74 const char16_t in[] = u"aβℷ𐎣";
75 char out[] = "AAAAAAAAAAAAAAAAA";
76
77 char *s = out;
78
79 for (size_t i = 0; i < sizeof(in) / sizeof(char16_t); i++) {
80 s += c16rtomb(s, in[i], &mbstate);
81 }
82
83 const char expected[] = u8"aβℷ𐎣";
84 PCUT_ASSERT_STR_EQUALS(expected, out);
85 PCUT_ASSERT_INT_EQUALS(s - out, sizeof(expected));
86}
87
88PCUT_TEST(mbrtoc32)
89{
90 mbstate_t mbstate;
91 memset(&mbstate, 0, sizeof(mbstate));
92
93 size_t ret;
94 char32_t c = 0;
95 const char *s = u8"aβℷ𐎣";
96
97 ret = mbrtoc32(&c, s, MB_CUR_MAX, &mbstate);
98 PCUT_ASSERT_INT_EQUALS(ret, 1);
99 PCUT_ASSERT_INT_EQUALS(c, U'a');
100
101 s += ret;
102
103 ret = mbrtoc32(&c, s, 1, &mbstate);
104 PCUT_ASSERT_INT_EQUALS(ret, -2);
105 PCUT_ASSERT_INT_EQUALS(c, U'a');
106
107 s += 1;
108
109 ret = mbrtoc32(&c, s, MB_CUR_MAX, &mbstate);
110 PCUT_ASSERT_INT_EQUALS(ret, 1);
111 PCUT_ASSERT_INT_EQUALS(c, U'β');
112
113 s += ret;
114
115 ret = mbrtoc32(&c, s, MB_CUR_MAX, &mbstate);
116 PCUT_ASSERT_INT_EQUALS(ret, 3);
117 PCUT_ASSERT_INT_EQUALS(c, U'ℷ');
118
119 s += ret;
120
121 ret = mbrtoc32(&c, s, 3, &mbstate);
122 PCUT_ASSERT_INT_EQUALS(ret, -2);
123 PCUT_ASSERT_INT_EQUALS(c, U'ℷ');
124
125 s += 3;
126
127 ret = mbrtoc32(&c, s, MB_CUR_MAX, &mbstate);
128 PCUT_ASSERT_INT_EQUALS(ret, 1);
129 PCUT_ASSERT_INT_EQUALS(c, U'𐎣');
130
131 s += ret;
132
133 ret = mbrtoc32(&c, s, MB_CUR_MAX, &mbstate);
134 PCUT_ASSERT_INT_EQUALS(ret, 0);
135 PCUT_ASSERT_INT_EQUALS(c, 0);
136}
137
138PCUT_TEST(c32rtomb)
139{
140 mbstate_t mbstate;
141 memset(&mbstate, 0, sizeof(mbstate));
142
143 char out[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
144 char *s = out;
145
146 _Static_assert(sizeof(out) > 5 * MB_CUR_MAX);
147
148 s += c32rtomb(s, U'a', &mbstate);
149 PCUT_ASSERT_INT_EQUALS(s - out, 1);
150
151 s += c32rtomb(s, U'β', &mbstate);
152 PCUT_ASSERT_INT_EQUALS(s - out, 3);
153
154 s += c32rtomb(s, U'ℷ', &mbstate);
155 PCUT_ASSERT_INT_EQUALS(s - out, 6);
156
157 s += c32rtomb(s, U'𐎣', &mbstate);
158 PCUT_ASSERT_INT_EQUALS(s - out, 10);
159
160 s += c32rtomb(s, 0, &mbstate);
161 PCUT_ASSERT_INT_EQUALS(s - out, 11);
162
163 const char expected[] = u8"aβℷ𐎣";
164 PCUT_ASSERT_INT_EQUALS(memcmp(out, expected, sizeof(expected)), 0);
165}
166
167PCUT_EXPORT(uchar);
Note: See TracBrowser for help on using the repository browser.