Remove unistd.h
[helenos.git] / uspace / app / tester / print / print6.c
blob4238d9d4499887a68e061387c003bb4262611fe1
1 /*
2 * Copyright (c) 2012 Adam Hraska
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 #include <stdio.h>
30 #include "../tester.h"
32 #include <str.h>
34 const char *test_print6(void)
36 struct {
37 double val;
38 const char *fmt;
39 const char *exp_str;
40 const char *warn_str;
41 } pat[] = {
42 /*
43 * Generic
45 { 2.0, "%g", "2", 0 },
46 { 0, "%g", "0", 0 },
47 { 0.1, "%g", "0.1", 0 },
48 { 9e59, "%g", "9e+59", 0 },
49 { -9e-59, "%g", "-9e-59", 0 },
50 { 1e307, "%g", "1e+307", 0 },
51 { 0.09999999999999999, "%g", "9.999999999999999e-02", 0 },
52 { 0.099999999999999999, "%g", "0.1", 0 },
55 * gcc and msvc convert "3.4567e-317" to different binary
56 * doubles.
58 { 3.4567e-317, "%g", "3.4567e-317", "3.456998e-317" },
59 { 3.4567e-318, "%g", "3.4567e-318", 0 },
60 { 123456789012345.0, "%g", "123456789012345", 0 },
61 { -123456789012345.0, "%g", "-123456789012345", 0 },
63 /* Special */
64 { 1e300 * 1e300, "%g", "inf", 0 },
65 { -1.0 /(1e300 * 1e300), "%g", "-0", 0 },
67 { 1234567.8901, "%g", "1234567.8901", 0 },
68 { 1234567.80012, "%g", "1234567.80012", 0 },
69 { 112e-32, "%g", "1.12e-30", 0 },
70 { 10.0e45, "%g", "1e+46", 0 },
72 /* rounding w/ trailing zero removal */
73 { 0.01, "%10.6g", " 0.01", 0 },
74 { 9.495, "%10.2g", " 9.5", 0 },
75 { 9.495e30, "%10.2g", " 9.5e+30", 0 },
76 { 9.495e30, "%10g", " 9.495e+30", 0 },
77 { 9.495e30, "%10.6g", " 9.495e+30", 0 },
80 * Scientific
82 { 1e05, "%e", "1.000000e+05", 0 },
84 /* full padding */
86 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
87 { 1e-1, "%+010.3e", "+1.000e-01", 0 },
88 { 1e-1, "%+10.3e", "+1.000e-01", 0 },
89 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_LEFTALIGNED */
90 { 1e-1, "%+-10.3e", "+1.000e-01", 0 },
92 /* padding */
94 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
95 { 1e-1, "%+010.2e", "+01.00e-01", 0 },
96 { 1e-1, "%+10.2e", " +1.00e-01", 0 },
97 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_LEFTALIGNED */
98 { 1e-1, "%+-10.2e", "+1.00e-01 ", 0 },
99 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
100 { 1e-1, "% 010.2e", " 01.00e-01", 0 },
101 /* __PRINTF_FLAG_ZEROPADDED */
102 { 1e-1, "%010.2e", "001.00e-01", 0 },
103 /* __PRINTF_FLAG_SPACESIGN */
104 { 1e-1, "% 10.2e", " 1.00e-01", 0 },
105 { 1e-1, "%10.2e", " 1.00e-01", 0 },
107 /* padding fractionals */
109 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
110 { 1.08e29, "%+010.3e", "+1.080e+29", 0 },
111 { 1.08e29, "%+10.3e", "+1.080e+29", 0 },
112 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
113 { 1.08e29, "%+011.2e", "+001.08e+29", 0 },
114 { 1.085e29, "%11.2e", " 1.09e+29", 0 },
116 /* rounding */
118 { 1.345e2, "%+10.2e", " +1.35e+02", 0 },
119 { 9.995e2, "%+10.2e", " +1.00e+03", 0 },
120 { -9.99499999e2, "%10.2e", " -9.99e+02", 0 },
121 { -9.99499999e2, "%10.0e", " -1e+03", 0 },
122 /* __PRINTF_FLAG_DECIMALPT */
123 { -9.99499999e2, "%#10.0e", " -1.e+03", 0 },
124 /* __PRINTF_FLAG_DECIMALPT */
125 { -1.2345006789e+231, "%#10.10e", "-1.2345006789e+231", 0 },
126 /* __PRINTF_FLAG_DECIMALPT */
127 { -1.23450067995e+231, "%#10.10e", "-1.2345006800e+231", 0 },
129 /* special */
131 { 1e300 * 1e300, "%10.5e", " inf", 0 },
132 { -1.0 /(1e300 * 1e300), "%10.2e", " -0.00e+00", 0 },
133 /* __PRINTF_FLAG_BIGCHARS */
134 { 1e300 * 1e300, "%10.5E", " INF", 0 },
135 /* __PRINTF_FLAG_BIGCHARS */
136 { -1.0 /(1e300 * 1e300), "%10.2E", " -0.00E+00", 0 },
139 * Fixed
142 /* padding */
144 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
145 { 1e-1, "% 010.3f", " 00000.100", 0 },
147 * __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED |
148 * __PRINTF_FLAG_LEFTALIGNED
150 { 1e-1, "% 0-10.3f", " 0.100 ", 0 },
151 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
152 { 1e-1, "% 010.3f", " 00000.100", 0 },
153 { 1e-1, "%10.3f", " 0.100", 0 },
155 /* rounding */
157 { -0.0, "%10.0f", " -0", 0 },
158 { -0.099, "%+10.3f", " -0.099", 0 },
159 { -0.0995, "%+10.3f", " -0.100", 0 },
160 { -0.0994, "%+10.3f", " -0.099", 0 },
161 { -99.995, "%+10.0f", " -100", 0 },
162 { 3.5, "%+10.30f", "+3.500000000000000000000000000000", 0 },
163 { 3.5, "%+10.0f", " +4", 0 },
164 { 0.1, "%+10.6f", " +0.100000", 0 },
167 * The compiler will go for closer 0.10..055 instead of
168 * 0.09..917
170 { 0.1, "%+10.20f", "+0.10000000000000000550", 0 },
171 /* Next closest to 0.1 */
172 { 0.0999999999999999917, "%+10.20f", "+0.09999999999999999170",
173 0 },
174 { 0.0999999999999999917, "%+10f", " +0.100000", 0 },
175 { 0.0999999999999998945, "%10.20f", "0.09999999999999989450",
176 0 },
179 int patterns_len = (int)(sizeof(pat) / sizeof(pat[0]));
180 int failed = 0;
181 const int buf_size = 256;
182 char buf[256 + 1] = { 0 };
184 TPRINTF("Test printing of floating point numbers via "
185 "printf(\"%%f\"):\n");
187 for (int i = 0; i < patterns_len; ++i) {
189 snprintf(buf, buf_size, pat[i].fmt, pat[i].val);
191 if (!str_cmp(buf, pat[i].exp_str)) {
192 TPRINTF("ok: %s |%s| == |%s|\n",
193 pat[i].fmt, buf, pat[i].exp_str);
194 } else {
195 if (pat[i].warn_str && !str_cmp(buf, pat[i].warn_str)) {
196 TPRINTF("warn: %s |%s| != |%s|\n",
197 pat[i].fmt, buf, pat[i].exp_str);
198 } else {
199 ++failed;
200 TPRINTF("ERR: %s |%s| != |%s|\n",
201 pat[i].fmt, buf, pat[i].exp_str);
206 if (failed) {
207 return "Unexpectedly misprinted floating point numbers.";
208 } else {
209 return 0;