File: sprintf_flt-nan.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 (90 lines) | stat: -rw-r--r-- 2,275 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
/* Test of sprintf(), float version, nonfinite numbers.
   $Id: sprintf_flt-nan.c,v 1.1 2007/02/18 13:46:34 dmix Exp $	*/

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

PROGMEM static const struct sprfu_s {
    char fmt[8];
    union {
	long lo;
	float fl;
    } val;
    char pattern[21];
} t[] = {

    { "%e",	{ 0x7f800000 }, 	"inf"	},
    { "%e",	{ 0xff800000 },		"-inf"	},
    { "%f",	{ 0x7f800000 },		"inf"	},
    { "%G",	{ 0xff800000 },		"-INF"	},

    { "%e",	{ 0x7f800001 },		"nan"	},
    { "% e",	{ 0x7f800001 },		" nan"	},
    { "%+e",	{ 0x7f800001 },		"+nan"	},
    { "%E",	{ 0x7f800001 },		"NAN"	},
    { "%5e",	{ 0x7f800001 },		"  nan"	},
    { "%+5e",	{ 0x7f800001 },		" +nan"	},
    { "%05e",	{ 0x7f800001 },		"  nan"	},
    { "%-5e",	{ 0x7f800001 },		"nan  "	},

    { "%e",	{ 0xff800001 },		"nan"	},
    { "% e",	{ 0xff800001 },		" nan"	},
    { "%+e",	{ 0xff800001 },		"+nan"	},
    { "%E",	{ 0xff800001 },		"NAN"	},
    { "%5e",	{ 0xff800001 },		"  nan"	},
    { "%+5e",	{ 0xff800001 },		" +nan"	},
    { "%05e",	{ 0xff800001 },		"  nan"	},
    { "%-5e",	{ 0xff800001 },		"nan  "	},

    { "%e",	{ 0x7fffffff },		"nan"	},
    { "%e",	{ 0xffffffff },		"nan"	},
    { "%e",	{ 0x7fc00000 },		"nan"	},
    { "%e",	{ 0xffc00000 },		"nan"	},

    { "%f",	{ 0x7f800001 },		"nan"	},
    { "%5F",	{ 0x7f800001 },		"  NAN"	},
    { "%g",	{ 0x7f800001 },		"nan"	},
    { "%5G",	{ 0x7f800001 },		"  NAN"	},
};

#ifndef	__AVR__
# define strlen_P	strlen
#endif

void run_sprf (const struct sprfu_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.fl);
#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;
}