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

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

Flip error constants to positive values, and update libposix for the change.

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