source: mainline/uspace/lib/c/test/stdlib.c@ dd7df1c

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dd7df1c was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
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/** @addtogroup libc
30 * @{
31 */
32/**
33 * @file
34 * @brief Test General utilities (stdlib.h)
35 */
36
37#include <pcut/pcut.h>
38#include <stdlib.h>
39
40PCUT_INIT;
41
42PCUT_TEST_SUITE(stdlib);
43
44PCUT_TEST(decls)
45{
46 /* Make sure size_t is defined */
47 size_t sz = 0;
48 (void) sz;
49
50 /* Make sure char32_t is defined */
51 char32_t wc = L'\0';
52 (void) wc;
53
54 /* Make sure EXIT_FAILURE and EXIT_SUCCESS are defined */
55 if (0)
56 exit(EXIT_FAILURE);
57 if (0)
58 exit(EXIT_SUCCESS);
59
60 /* Make sure NULL is defined */
61 void *ptr = NULL;
62 (void) ptr;
63}
64
65/** strtold function */
66#include <stdio.h>
67PCUT_TEST(strtold)
68{
69 long double ld;
70 const char *str = " \t4.2e1@";
71 char *endptr;
72
73 ld = strtold(str, &endptr);
74 printf("ld=%.10lf\n", (double)ld);
75 PCUT_ASSERT_TRUE(ld == 42.0);
76}
77
78/** rand function */
79PCUT_TEST(rand)
80{
81 int i;
82 int r;
83
84 for (i = 0; i < 100; i++) {
85 r = rand();
86 PCUT_ASSERT_TRUE(r >= 0);
87 PCUT_ASSERT_TRUE(r <= RAND_MAX);
88 }
89
90 PCUT_ASSERT_TRUE(RAND_MAX >= 32767);
91}
92
93/** srand function */
94PCUT_TEST(srand)
95{
96 int r1;
97 int r2;
98
99 srand(1);
100 r1 = rand();
101 srand(1);
102 r2 = rand();
103
104 PCUT_ASSERT_INT_EQUALS(r2, r1);
105
106 srand(42);
107 r1 = rand();
108 srand(42);
109 r2 = rand();
110
111 PCUT_ASSERT_INT_EQUALS(r2, r1);
112}
113
114/** Just make sure we have memory allocation function prototypes */
115PCUT_TEST(malloc)
116{
117 void *p;
118
119#if 0
120 // TODO
121 p = aligned_alloc(4, 8);
122 PCUT_ASSERT_NOT_NULL(p);
123 free(p);
124#endif
125 p = calloc(4, 4);
126 PCUT_ASSERT_NOT_NULL(p);
127 free(p);
128
129 p = malloc(4);
130 PCUT_ASSERT_NOT_NULL(p);
131 p = realloc(p, 2);
132 PCUT_ASSERT_NOT_NULL(p);
133 free(p);
134}
135
136/** Just check abort() is defined */
137PCUT_TEST(abort)
138{
139 if (0)
140 abort();
141}
142
143static void dummy_exit_handler(void)
144{
145}
146
147/** atexit function */
148PCUT_TEST(atexit)
149{
150 int rc;
151
152 rc = atexit(dummy_exit_handler);
153 PCUT_ASSERT_INT_EQUALS(0, rc);
154}
155
156/** exit function -- just make sure it is declared */
157PCUT_TEST(exit)
158{
159 if (0)
160 exit(0);
161}
162
163/** at_quick_exit function */
164PCUT_TEST(at_quick_exit)
165{
166 int rc;
167
168 rc = at_quick_exit(dummy_exit_handler);
169 PCUT_ASSERT_INT_EQUALS(0, rc);
170}
171
172/** quick_exit function -- just make sure it is declared */
173PCUT_TEST(quick_exit)
174{
175 if (0)
176 quick_exit(0);
177}
178
179/** getenv function */
180PCUT_TEST(getenv)
181{
182 char *s;
183
184 s = getenv("FOO");
185 PCUT_ASSERT_NULL(s);
186}
187
188/** Test availability of command processor */
189PCUT_TEST(system_null)
190{
191 int rc;
192
193 rc = system(NULL);
194 PCUT_ASSERT_INT_EQUALS(0, rc);
195}
196
197/** Test running a command */
198PCUT_TEST(system_cmd)
199{
200 int rc;
201
202 /* This should fail as system is just a stub */
203 rc = system("/app/bdsh");
204 PCUT_ASSERT_INT_EQUALS(1, rc);
205}
206
207/** Comparison function for bsearch test */
208static int test_compar(const void *a, const void *b)
209{
210 const int *ia, *ib;
211
212 ia = (const int *)a;
213 ib = (const int *)b;
214
215 return *ia - *ib;
216}
217
218PCUT_TEST(bsearch)
219{
220 int numbers[] = { 1, 2, 6, 7, 7, 10, 100, 120 };
221 int k;
222 void *r;
223
224 k = 0;
225 r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
226 test_compar);
227 PCUT_ASSERT_NULL(r);
228
229 k = 1;
230 r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
231 test_compar);
232 PCUT_ASSERT_NOT_NULL(r);
233 PCUT_ASSERT_INT_EQUALS(1, *(int *)r);
234
235 k = 3;
236 r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
237 test_compar);
238 PCUT_ASSERT_NULL(r);
239
240 k = 6;
241 r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
242 test_compar);
243 PCUT_ASSERT_NOT_NULL(r);
244 PCUT_ASSERT_INT_EQUALS(6, *(int *)r);
245
246 k = 7;
247 r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
248 test_compar);
249 PCUT_ASSERT_NOT_NULL(r);
250 PCUT_ASSERT_INT_EQUALS(7, *(int *)r);
251
252 k = 200;
253 r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
254 test_compar);
255 PCUT_ASSERT_NULL(r);
256}
257
258/** abs function of positive number */
259PCUT_TEST(abs_pos)
260{
261 int i;
262
263 i = abs(1);
264 PCUT_ASSERT_TRUE(i == 1);
265}
266
267/** abs function of negative number */
268PCUT_TEST(abs_neg)
269{
270 int i;
271
272 i = abs(-1);
273 PCUT_ASSERT_TRUE(i == 1);
274}
275
276/** labs function of positive number */
277PCUT_TEST(labs_pos)
278{
279 long li;
280
281 li = labs(1);
282 PCUT_ASSERT_TRUE(li == 1);
283}
284
285/** labs function of negative number */
286PCUT_TEST(labs_neg)
287{
288 long li;
289
290 li = labs(-1);
291 PCUT_ASSERT_TRUE(li == 1);
292}
293
294/** llabs function of positive number */
295PCUT_TEST(llabs_pos)
296{
297 long long lli;
298
299 lli = llabs(1);
300 PCUT_ASSERT_TRUE(lli == 1);
301}
302
303/** llabs function of negative number */
304PCUT_TEST(llabs_neg)
305{
306 long long lli;
307
308 lli = llabs(-1);
309 PCUT_ASSERT_TRUE(lli == 1);
310}
311
312/** Integer division */
313PCUT_TEST(div_func)
314{
315 div_t d;
316
317 d = div(41, 7);
318 PCUT_ASSERT_INT_EQUALS(5, d.quot);
319 PCUT_ASSERT_INT_EQUALS(6, d.rem);
320}
321
322/** Long integer division */
323PCUT_TEST(ldiv_func)
324{
325 ldiv_t d;
326
327 d = ldiv(41, 7);
328 PCUT_ASSERT_INT_EQUALS(5, d.quot);
329 PCUT_ASSERT_INT_EQUALS(6, d.rem);
330}
331
332/** Long long integer division */
333PCUT_TEST(lldiv_func)
334{
335 lldiv_t d;
336
337 d = lldiv(41, 7);
338 PCUT_ASSERT_INT_EQUALS(5, d.quot);
339 PCUT_ASSERT_INT_EQUALS(6, d.rem);
340}
341
342PCUT_EXPORT(stdlib);
Note: See TracBrowser for help on using the repository browser.