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

Last change on this file since 64d38cd was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[eeb23f2d]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
[7f9df7b9]29
[eeb23f2d]30
[0d0b319]31#include <errno.h>
32
[eeb23f2d]33#include "posix/stdio.h"
34
[134ac5d]35#include <pcut/pcut.h>
[eeb23f2d]36
[6b646dc]37#define EPSILON 0.000001
[eeb23f2d]38
[3f932a7e]39PCUT_INIT;
[eeb23f2d]40
[c85a57f]41PCUT_TEST_SUITE(scanf);
[eeb23f2d]42
43
[c85a57f]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
[3bacee1]51PCUT_TEST(int_decimal)
52{
[eeb23f2d]53 int number;
[7f9df7b9]54 int rc = sscanf("4242", "%d", &number);
[eeb23f2d]55 PCUT_ASSERT_INT_EQUALS(1, rc);
56 PCUT_ASSERT_INT_EQUALS(4242, number);
57}
58
[3bacee1]59PCUT_TEST(int_negative_decimal)
60{
[eeb23f2d]61 int number;
[7f9df7b9]62 int rc = sscanf("-53", "%d", &number);
[eeb23f2d]63 PCUT_ASSERT_INT_EQUALS(1, rc);
64 PCUT_ASSERT_INT_EQUALS(-53, number);
65}
66
[6b646dc]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
[3bacee1]73PCUT_TEST(int_misc)
74{
[6b646dc]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
[7f9df7b9]87 int rc = sscanf(
[3bacee1]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);
[6b646dc]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
[3bacee1]107PCUT_TEST(double_misc)
108{
[6b646dc]109 float f;
110 double d;
111 long double ld;
112
[7f9df7b9]113 int rc = sscanf(
[3bacee1]114 "\n \t\t1.0 -0x555.AP10 1234.5678e12",
115 "%f %lf %Lf",
116 &f, &d, &ld);
[6b646dc]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
[3bacee1]125PCUT_TEST(str_misc)
126{
[6b646dc]127 char str[20];
128 char *pstr;
129
[7f9df7b9]130 int rc = sscanf(
[3bacee1]131 "\n\n\thello world \n",
132 "%5s %ms",
133 str, &pstr);
[6b646dc]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
[3bacee1]143PCUT_TEST(str_matchers)
144{
[6b646dc]145 char scanset[20];
146 char *pscanset;
147
[7f9df7b9]148 int rc = sscanf(
[3bacee1]149 "\n\n\th-e-l-l-o world-] \n",
150 " %9[-eh-o] %m[^]-]",
151 scanset, &pscanset);
[6b646dc]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
[3bacee1]161PCUT_TEST(char_misc)
162{
[6b646dc]163 char seq[20];
164 char *pseq;
165
[7f9df7b9]166 int rc = sscanf(
[3bacee1]167 "\n\n\thello world \n",
168 " %5c %mc",
169 seq, &pseq);
[6b646dc]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
[c85a57f]183#endif
184
[eeb23f2d]185PCUT_EXPORT(scanf);
Note: See TracBrowser for help on using the repository browser.