source: mainline/uspace/app/tester/print/print6.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[82d062d8]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
[34b9299]29#include <stdio.h>
30#include "../tester.h"
31
32#include <str.h>
33
34const char *test_print6(void)
35{
36 struct {
37 double val;
38 const char *fmt;
39 const char *exp_str;
40 const char *warn_str;
41 } pat[] = {
[1b20da0]42 /*
43 * Generic
[34b9299]44 */
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 },
[1b20da0]50 { 1e307, "%g", "1e+307", 0 },
51 { 0.09999999999999999, "%g", "9.999999999999999e-02", 0 },
52 { 0.099999999999999999, "%g", "0.1", 0 },
[34b9299]53
[82d062d8]54 /*
55 * gcc and msvc convert "3.4567e-317" to different binary
56 * doubles.
57 */
[1b20da0]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 },
[34b9299]62
63 /* Special */
[1b20da0]64 { 1e300 * 1e300, "%g", "inf", 0 },
[3bacee1]65 { -1.0 / (1e300 * 1e300), "%g", "-0", 0 },
[34b9299]66
[1b20da0]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 },
[34b9299]71
72 /* rounding w/ trailing zero removal */
[1b20da0]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 },
[34b9299]78
79 /*
[1b20da0]80 * Scientific
[34b9299]81 */
[1b20da0]82 { 1e05, "%e", "1.000000e+05", 0 },
[34b9299]83
84 /* full padding */
[82d062d8]85
86 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
87 { 1e-1, "%+010.3e", "+1.000e-01", 0 },
[1b20da0]88 { 1e-1, "%+10.3e", "+1.000e-01", 0 },
[82d062d8]89 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_LEFTALIGNED */
90 { 1e-1, "%+-10.3e", "+1.000e-01", 0 },
[34b9299]91
92 /* padding */
93
[82d062d8]94 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
95 { 1e-1, "%+010.2e", "+01.00e-01", 0 },
[1b20da0]96 { 1e-1, "%+10.2e", " +1.00e-01", 0 },
[82d062d8]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 },
[1b20da0]105 { 1e-1, "%10.2e", " 1.00e-01", 0 },
[34b9299]106
107 /* padding fractionals */
[82d062d8]108
109 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
110 { 1.08e29, "%+010.3e", "+1.080e+29", 0 },
[1b20da0]111 { 1.08e29, "%+10.3e", "+1.080e+29", 0 },
[82d062d8]112 /* __PRINTF_FLAG_SHOWPLUS | __PRINTF_FLAG_ZEROPADDED */
113 { 1.08e29, "%+011.2e", "+001.08e+29", 0 },
[1b20da0]114 { 1.085e29, "%11.2e", " 1.09e+29", 0 },
[34b9299]115
116 /* rounding */
[82d062d8]117
[1b20da0]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 },
[82d062d8]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 },
[a35b458]128
[34b9299]129 /* special */
[82d062d8]130
[1b20da0]131 { 1e300 * 1e300, "%10.5e", " inf", 0 },
[3bacee1]132 { -1.0 / (1e300 * 1e300), "%10.2e", " -0.00e+00", 0 },
[82d062d8]133 /* __PRINTF_FLAG_BIGCHARS */
134 { 1e300 * 1e300, "%10.5E", " INF", 0 },
135 /* __PRINTF_FLAG_BIGCHARS */
[3bacee1]136 { -1.0 / (1e300 * 1e300), "%10.2E", " -0.00E+00", 0 },
[34b9299]137
138 /*
139 * Fixed
140 */
[a35b458]141
[34b9299]142 /* padding */
[82d062d8]143
144 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
145 { 1e-1, "% 010.3f", " 00000.100", 0 },
146 /*
147 * __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED |
148 * __PRINTF_FLAG_LEFTALIGNED
149 */
150 { 1e-1, "% 0-10.3f", " 0.100 ", 0 },
151 /* __PRINTF_FLAG_SPACESIGN | __PRINTF_FLAG_ZEROPADDED */
152 { 1e-1, "% 010.3f", " 00000.100", 0 },
[1b20da0]153 { 1e-1, "%10.3f", " 0.100", 0 },
[34b9299]154
155 /* rounding */
[82d062d8]156
[1b20da0]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 },
[34b9299]165
[82d062d8]166 /*
167 * The compiler will go for closer 0.10..055 instead of
168 * 0.09..917
169 */
[1b20da0]170 { 0.1, "%+10.20f", "+0.10000000000000000550", 0 },
[34b9299]171 /* Next closest to 0.1 */
[82d062d8]172 { 0.0999999999999999917, "%+10.20f", "+0.09999999999999999170",
[3bacee1]173 0 },
[82d062d8]174 { 0.0999999999999999917, "%+10f", " +0.100000", 0 },
175 { 0.0999999999999998945, "%10.20f", "0.09999999999999989450",
[3bacee1]176 0 },
[34b9299]177 };
178
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 };
[a35b458]183
[82d062d8]184 TPRINTF("Test printing of floating point numbers via "
185 "printf(\"%%f\"):\n");
[a35b458]186
[34b9299]187 for (int i = 0; i < patterns_len; ++i) {
[a35b458]188
[34b9299]189 snprintf(buf, buf_size, pat[i].fmt, pat[i].val);
[a35b458]190
[82d062d8]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);
[34b9299]194 } else {
[82d062d8]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);
[34b9299]198 } else {
199 ++failed;
[82d062d8]200 TPRINTF("ERR: %s |%s| != |%s|\n",
201 pat[i].fmt, buf, pat[i].exp_str);
[34b9299]202 }
203 }
204 }
[a35b458]205
[34b9299]206 if (failed) {
207 return "Unexpectedly misprinted floating point numbers.";
208 } else {
209 return 0;
210 }
211}
Note: See TracBrowser for help on using the repository browser.