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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f9df7b9 was 7f9df7b9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Remove unnecessary symbol renaming from libposix.

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