source: mainline/uspace/app/tester/print/print6.c@ 69c664e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 69c664e was 82d062d8, checked in by Jakub Jermar <jakub@…>, 13 years ago

Add a missing copyright message and cstyle.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Copyright (c) 2012 Adam Hraska
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#include <stdio.h>
30#include <unistd.h>
31#include "../tester.h"
32
33#include <str.h>
34
35const char *test_print6(void)
36{
37 struct {
38 double val;
39 const char *fmt;
40 const char *exp_str;
41 const char *warn_str;
42 } pat[] = {
43 /*
44 * Generic
45 */
46 { 2.0, "%g", "2", 0 },
47 { 0, "%g", "0", 0 },
48 { 0.1, "%g", "0.1", 0 },
49 { 9e59, "%g", "9e+59", 0 },
50 { -9e-59, "%g", "-9e-59", 0 },
51 { 1e307, "%g", "1e+307", 0 },
52 { 0.09999999999999999, "%g", "9.999999999999999e-02", 0 },
53 { 0.099999999999999999, "%g", "0.1", 0 },
54
55 /*
56 * gcc and msvc convert "3.4567e-317" to different binary
57 * doubles.
58 */
59 { 3.4567e-317, "%g", "3.4567e-317", "3.456998e-317" },
60 { 3.4567e-318, "%g", "3.4567e-318", 0 },
61 { 123456789012345.0, "%g", "123456789012345", 0 },
62 { -123456789012345.0, "%g", "-123456789012345", 0 },
63
64 /* Special */
65 { 1e300 * 1e300, "%g", "inf", 0 },
66 { -1.0 /(1e300 * 1e300), "%g", "-0", 0 },
67
68 { 1234567.8901, "%g", "1234567.8901", 0 },
69 { 1234567.80012, "%g", "1234567.80012", 0 },
70 { 112e-32, "%g", "1.12e-30", 0 },
71 { 10.0e45, "%g", "1e+46", 0 },
72
73 /* rounding w/ trailing zero removal */
74 { 0.01, "%10.6g", " 0.01", 0 },
75 { 9.495, "%10.2g", " 9.5", 0 },
76 { 9.495e30, "%10.2g", " 9.5e+30", 0 },
77 { 9.495e30, "%10g", " 9.495e+30", 0 },
78 { 9.495e30, "%10.6g", " 9.495e+30", 0 },
79
80 /*
81 * Scientific
82 */
83 { 1e05, "%e", "1.000000e+05", 0 },
84
85 /* full padding */
86
87 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
88 { 1e-1, "%+010.3e", "+1.000e-01", 0 },
89 { 1e-1, "%+10.3e", "+1.000e-01", 0 },
90 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_LEFTALIGNED */
91 { 1e-1, "%+-10.3e", "+1.000e-01", 0 },
92
93 /* padding */
94
95 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
96 { 1e-1, "%+010.2e", "+01.00e-01", 0 },
97 { 1e-1, "%+10.2e", " +1.00e-01", 0 },
98 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_LEFTALIGNED */
99 { 1e-1, "%+-10.2e", "+1.00e-01 ", 0 },
100 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
101 { 1e-1, "% 010.2e", " 01.00e-01", 0 },
102 /* __PRINTF_FLAG_ZEROPADDED */
103 { 1e-1, "%010.2e", "001.00e-01", 0 },
104 /* __PRINTF_FLAG_SPACESIGN */
105 { 1e-1, "% 10.2e", " 1.00e-01", 0 },
106 { 1e-1, "%10.2e", " 1.00e-01", 0 },
107
108 /* padding fractionals */
109
110 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
111 { 1.08e29, "%+010.3e", "+1.080e+29", 0 },
112 { 1.08e29, "%+10.3e", "+1.080e+29", 0 },
113 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
114 { 1.08e29, "%+011.2e", "+001.08e+29", 0 },
115 { 1.085e29, "%11.2e", " 1.09e+29", 0 },
116
117 /* rounding */
118
119 { 1.345e2, "%+10.2e", " +1.35e+02", 0 },
120 { 9.995e2, "%+10.2e", " +1.00e+03", 0 },
121 { -9.99499999e2, "%10.2e", " -9.99e+02", 0 },
122 { -9.99499999e2, "%10.0e", " -1e+03", 0 },
123 /* __PRINTF_FLAG_DECIMALPT */
124 { -9.99499999e2, "%#10.0e", " -1.e+03", 0 },
125 /* __PRINTF_FLAG_DECIMALPT */
126 { -1.2345006789e+231, "%#10.10e", "-1.2345006789e+231", 0 },
127 /* __PRINTF_FLAG_DECIMALPT */
128 { -1.23450067995e+231, "%#10.10e", "-1.2345006800e+231", 0 },
129
130 /* special */
131
132 { 1e300 * 1e300, "%10.5e", " inf", 0 },
133 { -1.0 /(1e300 * 1e300), "%10.2e", " -0.00e+00", 0 },
134 /* __PRINTF_FLAG_BIGCHARS */
135 { 1e300 * 1e300, "%10.5E", " INF", 0 },
136 /* __PRINTF_FLAG_BIGCHARS */
137 { -1.0 /(1e300 * 1e300), "%10.2E", " -0.00E+00", 0 },
138
139 /*
140 * Fixed
141 */
142
143 /* padding */
144
145 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
146 { 1e-1, "% 010.3f", " 00000.100", 0 },
147 /*
148 * __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED |
149 * __PRINTF_FLAG_LEFTALIGNED
150 */
151 { 1e-1, "% 0-10.3f", " 0.100 ", 0 },
152 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
153 { 1e-1, "% 010.3f", " 00000.100", 0 },
154 { 1e-1, "%10.3f", " 0.100", 0 },
155
156 /* rounding */
157
158 { -0.0, "%10.0f", " -0", 0 },
159 { -0.099, "%+10.3f", " -0.099", 0 },
160 { -0.0995, "%+10.3f", " -0.100", 0 },
161 { -0.0994, "%+10.3f", " -0.099", 0 },
162 { -99.995, "%+10.0f", " -100", 0 },
163 { 3.5, "%+10.30f", "+3.500000000000000000000000000000", 0 },
164 { 3.5, "%+10.0f", " +4", 0 },
165 { 0.1, "%+10.6f", " +0.100000", 0 },
166
167 /*
168 * The compiler will go for closer 0.10..055 instead of
169 * 0.09..917
170 */
171 { 0.1, "%+10.20f", "+0.10000000000000000550", 0 },
172 /* Next closest to 0.1 */
173 { 0.0999999999999999917, "%+10.20f", "+0.09999999999999999170",
174 0 },
175 { 0.0999999999999999917, "%+10f", " +0.100000", 0 },
176 { 0.0999999999999998945, "%10.20f", "0.09999999999999989450",
177 0 },
178 };
179
180 int patterns_len = (int)(sizeof(pat) / sizeof(pat[0]));
181 int failed = 0;
182 const int buf_size = 256;
183 char buf[256 + 1] = { 0 };
184
185 TPRINTF("Test printing of floating point numbers via "
186 "printf(\"%%f\"):\n");
187
188 for (int i = 0; i < patterns_len; ++i) {
189
190 snprintf(buf, buf_size, pat[i].fmt, pat[i].val);
191
192 if (!str_cmp(buf, pat[i].exp_str)) {
193 TPRINTF("ok: %s |%s| == |%s|\n",
194 pat[i].fmt, buf, pat[i].exp_str);
195 } else {
196 if (pat[i].warn_str && !str_cmp(buf, pat[i].warn_str)) {
197 TPRINTF("warn: %s |%s| != |%s|\n",
198 pat[i].fmt, buf, pat[i].exp_str);
199 } else {
200 ++failed;
201 TPRINTF("ERR: %s |%s| != |%s|\n",
202 pat[i].fmt, buf, pat[i].exp_str);
203 }
204 }
205 }
206
207 if (failed) {
208 return "Unexpectedly misprinted floating point numbers.";
209 } else {
210 return 0;
211 }
212}
213
Note: See TracBrowser for help on using the repository browser.