source: mainline/uspace/lib/c/test/stdlib.c@ 099c834

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 099c834 was 099c834, checked in by Jiri Svoboda <jiri@…>, 7 years ago

atexit, exit, _Exit, at_quick_exit, quick_exit, tests for strtol and friends.

  • Property mode set to 100644
File size: 5.7 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 formatted input (scanf family)
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 wchar_t is defined */
51 wchar_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/** atoi function */
66PCUT_TEST(atoi)
67{
68 int i;
69
70 i = atoi(" \t42");
71 PCUT_ASSERT_TRUE(i == 42);
72}
73
74/** atol function */
75PCUT_TEST(atol)
76{
77 long li;
78
79 li = atol(" \t42");
80 PCUT_ASSERT_TRUE(li == 42);
81}
82
83/** atoll function */
84PCUT_TEST(atoll)
85{
86 long long lli;
87
88 lli = atoll(" \t42");
89 PCUT_ASSERT_TRUE(lli == 42);
90}
91
92/** strtol function */
93PCUT_TEST(strtol)
94{
95 long li;
96 char *ep;
97
98 li = strtol(" \t42x", &ep, 10);
99 PCUT_ASSERT_TRUE(li == 42);
100 PCUT_ASSERT_TRUE(*ep == 'x');
101}
102
103/** strtol function with auto-detected base 10 */
104PCUT_TEST(strtol_dec_auto)
105{
106 long li;
107 char *ep;
108
109 li = strtol(" \t42x", &ep, 0);
110 PCUT_ASSERT_TRUE(li == 42);
111 PCUT_ASSERT_TRUE(*ep == 'x');
112}
113
114/** strtol function with octal number */
115PCUT_TEST(strtol_oct)
116{
117 long li;
118 char *ep;
119
120 li = strtol(" \t052x", &ep, 8);
121 PCUT_ASSERT_TRUE(li == 052);
122 PCUT_ASSERT_TRUE(*ep == 'x');
123}
124
125/** strtol function with octal number with prefix */
126#include <stdio.h>
127PCUT_TEST(strtol_oct_prefix)
128{
129 long li;
130 char *ep;
131
132 li = strtol(" \t052x", &ep, 0);
133 printf("li=%ld (0%lo)\n", li, li);
134 PCUT_ASSERT_TRUE(li == 052);
135 PCUT_ASSERT_TRUE(*ep == 'x');
136}
137
138/** strtol function with hex number */
139PCUT_TEST(strtol_hex)
140{
141 long li;
142 char *ep;
143
144 li = strtol(" \t2ax", &ep, 16);
145 PCUT_ASSERT_TRUE(li == 0x2a);
146 PCUT_ASSERT_TRUE(*ep == 'x');
147}
148
149/** strtol function with hex number with hex prefix */
150PCUT_TEST(strtol_hex_prefixed)
151{
152 long li;
153 char *ep;
154
155 li = strtol(" \t0x2ax", &ep, 0);
156 PCUT_ASSERT_TRUE(li == 0x2a);
157 PCUT_ASSERT_TRUE(*ep == 'x');
158}
159
160/** strtol function with base 16 and number with 0x prefix */
161PCUT_TEST(strtol_base16_prefix)
162{
163 long li;
164 char *ep;
165
166 li = strtol(" \t0x1y", &ep, 16);
167 printf("li=%ld\n", li);
168 PCUT_ASSERT_TRUE(li == 1);
169 PCUT_ASSERT_TRUE(*ep == 'y');
170}
171
172/** strtol function with base 36 number */
173PCUT_TEST(strtol_base36)
174{
175 long li;
176 char *ep;
177
178 li = strtol(" \tz1.", &ep, 36);
179 PCUT_ASSERT_TRUE(li == 35 * 36 + 1);
180 PCUT_ASSERT_TRUE(*ep == '.');
181}
182
183/** rand function */
184PCUT_TEST(rand)
185{
186 int i;
187 int r;
188
189 for (i = 0; i < 100; i++) {
190 r = rand();
191 PCUT_ASSERT_TRUE(r >= 0);
192 PCUT_ASSERT_TRUE(r <= RAND_MAX);
193 }
194
195 PCUT_ASSERT_TRUE(RAND_MAX >= 32767);
196}
197
198/** srand function */
199PCUT_TEST(srand)
200{
201 int r1;
202 int r2;
203
204 srand(1);
205 r1 = rand();
206 srand(1);
207 r2 = rand();
208
209 PCUT_ASSERT_INT_EQUALS(r2, r1);
210
211 srand(42);
212 r1 = rand();
213 srand(42);
214 r2 = rand();
215
216 PCUT_ASSERT_INT_EQUALS(r2, r1);
217}
218
219/** Just make sure we have memory allocation function prototypes */
220PCUT_TEST(malloc)
221{
222 void *p;
223
224#if 0
225 // TODO
226 p = aligned_alloc(4, 8);
227 PCUT_ASSERT_NOT_NULL(p);
228 free(p);
229#endif
230 p = calloc(4, 4);
231 PCUT_ASSERT_NOT_NULL(p);
232 free(p);
233
234 p = malloc(4);
235 PCUT_ASSERT_NOT_NULL(p);
236 p = realloc(p, 2);
237 PCUT_ASSERT_NOT_NULL(p);
238 free(p);
239}
240
241/** Just check abort() is defined */
242PCUT_TEST(abort)
243{
244 if (0)
245 abort();
246}
247
248static void dummy_exit_handler(void)
249{
250}
251
252/** atexit function */
253PCUT_TEST(atexit)
254{
255 int rc;
256
257 rc = atexit(dummy_exit_handler);
258 PCUT_ASSERT_INT_EQUALS(0, rc);
259}
260
261/** exit function -- just make sure it is declared */
262PCUT_TEST(exit)
263{
264 if (0)
265 exit(0);
266}
267
268/** at_quick_exit function */
269PCUT_TEST(at_quick_exit)
270{
271 int rc;
272
273 rc = at_quick_exit(dummy_exit_handler);
274 PCUT_ASSERT_INT_EQUALS(0, rc);
275}
276
277/** quick_exit function -- just make sure it is declared */
278PCUT_TEST(quick_exit)
279{
280 if (0)
281 quick_exit(0);
282}
283
284/** Integer division */
285PCUT_TEST(div_func)
286{
287 div_t d;
288
289 d = div(41, 7);
290 PCUT_ASSERT_INT_EQUALS(5, d.quot);
291 PCUT_ASSERT_INT_EQUALS(6, d.rem);
292}
293
294/** Long integer division */
295PCUT_TEST(ldiv_func)
296{
297 ldiv_t d;
298
299 d = ldiv(41, 7);
300 PCUT_ASSERT_INT_EQUALS(5, d.quot);
301 PCUT_ASSERT_INT_EQUALS(6, d.rem);
302}
303
304/** Long long integer division */
305PCUT_TEST(lldiv_func)
306{
307 lldiv_t d;
308
309 d = lldiv(41, 7);
310 PCUT_ASSERT_INT_EQUALS(5, d.quot);
311 PCUT_ASSERT_INT_EQUALS(6, d.rem);
312}
313
314PCUT_EXPORT(stdlib);
Note: See TracBrowser for help on using the repository browser.