File: strtod-1.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 (149 lines) | stat: -rw-r--r-- 3,266 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
/* Test of strtod() function. Base cases.
   $Id: strtod-1.c,v 1.1 2007/02/05 21:35:59 dmix Exp $
 */
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progmem.h"

union lofl_u {
    long lo;
    float fl;
};

volatile union lofl_u v = { .lo = 1 };

PROGMEM const struct {		/* Table of test cases.	*/
    char s[20];
    unsigned char len;
    float val;
    int eno;
} t[] = {

    /* Empty string	*/
    { "", 0,	0, 0 },
    { " ", 0,	0, 0 },
    { "\t\n\r\f\v", 0, 0, 0 },

    /* 0.0	*/
    { "0", 1,		0.0, 0 },
    { "0.0", 3,		0.0, 0 },
    { "+0", 2,		0.0, 0 },
    { "+0.0", 4,	0.0, 0 },
    
    /* -0.0	*/
    { "-0", 2,		-0.0, 0 },
    { "-0.0", 4,	-0.0, 0 },
    
    /* Leading spaces	*/
    { " \t\n\r\f\v1", 7,	1, 0 },
    
    /* Non-zero character at the end of number	*/
    { "123 ", 3, 123, 0 },
    { "123/", 3, 123, 0 },
    { "123:", 3, 123, 0 },
    { "123\x01", 3, 123, 0 },
    { "123\xff", 3, 123, 0 },
    { "123-", 3, 123, 0 },
    { "123+", 3, 123, 0 },
    { "123..", 4, 123, 0 },

    /* No valid digits	*/
    { ".", 0,	0, 0 },
    { "+", 0,	0, 0 },
    { "-", 0,	0, 0 },
    { "e", 0,	0, 0 },
    { "E", 0,	0, 0 },
    { "+-123", 0, 0, 0 },
    { "-+123", 0, 0, 0 },
    { ".-123", 0, 0, 0 },
    { ".+123", 0, 0, 0 },
    { "e12",   0, 0, 0 },
    { "- 1",   0, 0, 0 },
    { "IN",    0, 0, 0 },
    { "NA",    0, 0, 0 },
    
    /* Infinity	*/
    { "INF", 3,		INFINITY, 0 },
    { " inf", 4,	INFINITY, 0 },
    { "+Inf", 4,	INFINITY, 0 },

    { "-INF", 4,	-INFINITY, 0 },
    { "\t-inF", 5,	-INFINITY, 0 },
    
    { "INFINITY", 8,	INFINITY, 0 },
    { "-InFiNiTy", 9,	-INFINITY, 0 },

    { "INFINIT", 3,	INFINITY, 0 },
    { "infinitys", 8,	INFINITY, 0 },
    
    /* NaN	*/
    { "NAN", 3,		NAN, 0 },
    { " nan", 4,	NAN, 0 },
    { "-NaN", 4,	NAN, 0 },
    { "NANQ", 3,	NAN, 0 },
    
    /* Overflow	*/
    { "1e309", 5,	INFINITY, ERANGE },
    { "-1e309", 6,	-INFINITY, ERANGE },

    { "1e32767", 7,		INFINITY, ERANGE },
    { "1e32768", 7,		INFINITY, ERANGE },
    { "1e32769", 7,		INFINITY, ERANGE },
    { "1e9999999999", 12,	INFINITY, ERANGE },
    
    /* Underflow	*/
    { "1e-350", 6,	0.0, ERANGE },
#ifdef	__AVR__
    { "-1e-350", 7,	-0.0, ERANGE },
#else
    { "-1e-350", 7,	0.0, ERANGE },		/* ???	*/
#endif
    { "1e-32767", 8,		0.0, ERANGE },
    { "1e-32768", 8,		0.0, ERANGE },
    { "1e-32769", 8,		0.0, ERANGE },
    { "1e-9999999999", 13,	0.0, ERANGE },
    
    /* Nonregular cases	*/
    { "1234.5", 6,	1234.5, 0 },
    { "-9.876e12", 9,	-9.876e12, 0 },
};

void x_exit (int index)
{
#ifndef	__AVR__
    fprintf (stderr, "t[%d]:  %#lx\n", index - 1, v.lo);
#endif
    exit (index ? index : -1);
}

int main ()
{
    char s [sizeof(t[0].s)];
    char *p;
    union lofl_u mst;
    unsigned char len;
    int eno;
    int i;
    
    for (i = 0; i < (int) (sizeof(t) / sizeof(t[0])); i++) {
	strcpy_P (s, t[i].s);
	len = pgm_read_byte (& t[i].len);
	mst.lo = pgm_read_dword (& t[i].val);
	eno = pgm_read_word (& t[i].eno);
	
	errno = 0;
	p = 0;
	v.fl = strtod (s, &p);
	
	if (!p || (p - s) != len || errno != eno)
	    x_exit (i+1);
	if (isnan(mst.fl) && isnan(v.fl))
	    continue;
	if (v.lo != mst.lo)
	    x_exit (i+1);
    }
    return 0;
}