File: sprintf_flt-big.c

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (158 lines) | stat: -rw-r--r-- 5,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Test of sprintf(), float version, very long strings.
   $Id: sprintf_flt-big.c,v 1.2 2007/03/10 04:44:34 dmix Exp $	*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progmem.h"

PROGMEM static const struct sprf_s {
    char fmt[8];
    float val;
    char pattern[260];
} t[] = {

    { "%255e",		1.234567,
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                   1.234567e+00"	},
    { "%255.7e",	1.2345678,
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                  1.2345678e+00"	},
    { "%+255e",		1.234567,
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                  +1.234567e+00"	},
    { "%0255e",		1.234567,
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000001.234567e+00"	},
    { "%+0255e",	1.234567,
	"+0000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000001.234567e+00"	},
    { "%+0255e",	-1.234567,
	"-0000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000001.234567e+00"	},
    { "%-255e",		1.234567,
	"1.234567e+00                    "
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                               "	},

    { "%255.0f",	1.25,
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                              1"	},
    { "%.255f",		1.25,
	"1.250000000000000000000000000000"
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"000000000000000000000000000000000"	},
    { "%0255f",		1.25,
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"00000000000000000000000000000000"	/* 32 */
	"000000000000000000000001.250000"	},
    { "%-255f",		1.25,
	"1.250000                        "
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                                "	/* 32 */
	"                               "	},

    /* the smallest nonzero	*/
    { "%.45f",		0x1p-149f,
	"0.000000000000000000000000000000000000000000001"	},
    { "%.50f",		0x1p-149f,
	"0.00000000000000000000000000000000000000000000140130"	},

};

void run_sprf (const struct sprf_s *pt, int testno)
{
    static char s[300];
    int n;
    int code;

#ifdef	__AVR__
    n = sprintf_P (s, pt->fmt, pgm_read_dword (& pt->val));
#else
    n = sprintf (s, pt->fmt, pt->val);
#endif
    if (n != (int)strlen_P (pt->pattern))
	code = testno + 1000;
    else if (strcmp_P (s, pt->pattern))
	code = testno;
    else
	return;
#if  !defined(__AVR__)
    printf ("\ntestno %3d: expect: %3d, \"%s\","
	    "\n            output: %3d, \"%s\"\n",
	    testno, strlen(pt->pattern), pt->pattern, n, s);
    exit (code < 256 ? testno : 255);
#elif	defined(DEBUG)
    exit ((int)s);
#endif
    exit (code);
}

int main ()
{
    int i;
    for (i = 0; (unsigned)i != sizeof(t)/sizeof(t[0]); i++)
	run_sprf (t+i, i+1);
    return 0;
}