source: mainline/uspace/lib/posix/test/scanf.c@ c72cd20

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

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2014 Vojtech Horky
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
30
31#include <errno.h>
32
33#include "posix/stdio.h"
34
35#include <pcut/pcut.h>
36
37#define EPSILON 0.000001
38
39PCUT_INIT;
40
41PCUT_TEST_SUITE(scanf);
42
43
44#ifndef UARCH_sparc64
45
46/*
47 * We need some floating point functions for scanf() imlementation
48 * that are not yet available for SPARC-64.
49 */
50
51PCUT_TEST(int_decimal)
52{
53 int number;
54 int rc = sscanf("4242", "%d", &number);
55 PCUT_ASSERT_INT_EQUALS(1, rc);
56 PCUT_ASSERT_INT_EQUALS(4242, number);
57}
58
59PCUT_TEST(int_negative_decimal)
60{
61 int number;
62 int rc = sscanf("-53", "%d", &number);
63 PCUT_ASSERT_INT_EQUALS(1, rc);
64 PCUT_ASSERT_INT_EQUALS(-53, number);
65}
66
67/*
68 * The following tests were copied from stdio/scanf.c where they were
69 * commented-out. We ought to convert them to more independent tests
70 * eventually.
71 */
72
73PCUT_TEST(int_misc)
74{
75 unsigned char uhh;
76 signed char shh;
77 unsigned short uh;
78 short sh;
79 unsigned udef;
80 int sdef;
81 unsigned long ul;
82 long sl;
83 unsigned long long ull;
84 long long sll;
85 void *p;
86
87 int rc = sscanf(
88 "\n j tt % \t -121314 98765 aqw 0765 0x77 0xABCDEF88 -99 884",
89 " j tt %%%3hhd%1hhu%3hd %3hu%u aqw%n %lo%llx %p %li %lld",
90 &shh, &uhh, &sh, &uh, &udef, &sdef, &ul, &ull, &p, &sl, &sll);
91
92 PCUT_ASSERT_INT_EQUALS(10, rc);
93
94 PCUT_ASSERT_INT_EQUALS(-12, shh);
95 PCUT_ASSERT_INT_EQUALS(1, uhh);
96 PCUT_ASSERT_INT_EQUALS(314, sh);
97 PCUT_ASSERT_INT_EQUALS(987, uh);
98 PCUT_ASSERT_INT_EQUALS(65, udef);
99 PCUT_ASSERT_INT_EQUALS(28, sdef);
100 PCUT_ASSERT_INT_EQUALS(0765, ul);
101 PCUT_ASSERT_INT_EQUALS(0x77, ull);
102 PCUT_ASSERT_INT_EQUALS(0xABCDEF88, (long long) (uintptr_t) p);
103 PCUT_ASSERT_INT_EQUALS(-99, sl);
104 PCUT_ASSERT_INT_EQUALS(884, sll);
105}
106
107PCUT_TEST(double_misc)
108{
109 float f;
110 double d;
111 long double ld;
112
113 int rc = sscanf(
114 "\n \t\t1.0 -0x555.AP10 1234.5678e12",
115 "%f %lf %Lf",
116 &f, &d, &ld);
117
118 PCUT_ASSERT_INT_EQUALS(3, rc);
119
120 PCUT_ASSERT_DOUBLE_EQUALS(1.0, f, EPSILON);
121 PCUT_ASSERT_DOUBLE_EQUALS(-0x555.AP10, d, EPSILON);
122 PCUT_ASSERT_DOUBLE_EQUALS(1234.5678e12, ld, EPSILON);
123}
124
125PCUT_TEST(str_misc)
126{
127 char str[20];
128 char *pstr;
129
130 int rc = sscanf(
131 "\n\n\thello world \n",
132 "%5s %ms",
133 str, &pstr);
134
135 PCUT_ASSERT_INT_EQUALS(2, rc);
136
137 PCUT_ASSERT_STR_EQUALS("hello", str);
138 PCUT_ASSERT_STR_EQUALS("world", pstr);
139
140 free(pstr);
141}
142
143PCUT_TEST(str_matchers)
144{
145 char scanset[20];
146 char *pscanset;
147
148 int rc = sscanf(
149 "\n\n\th-e-l-l-o world-] \n",
150 " %9[-eh-o] %m[^]-]",
151 scanset, &pscanset);
152
153 PCUT_ASSERT_INT_EQUALS(2, rc);
154
155 PCUT_ASSERT_STR_EQUALS("h-e-l-l-o", scanset);
156 PCUT_ASSERT_STR_EQUALS("world", pscanset);
157
158 free(pscanset);
159}
160
161PCUT_TEST(char_misc)
162{
163 char seq[20];
164 char *pseq;
165
166 int rc = sscanf(
167 "\n\n\thello world \n",
168 " %5c %mc",
169 seq, &pseq);
170
171 PCUT_ASSERT_INT_EQUALS(2, rc);
172
173 /* Manually terminate the strings. */
174 seq[5] = 0;
175 pseq[1] = 0;
176
177 PCUT_ASSERT_STR_EQUALS("hello", seq);
178 PCUT_ASSERT_STR_EQUALS("w", pseq);
179
180 free(pseq);
181}
182
183#endif
184
185PCUT_EXPORT(scanf);
Note: See TracBrowser for help on using the repository browser.