~ubuntu-branches/ubuntu/maverick/avr-libc/maverick

« back to all changes in this revision

Viewing changes to tests/simulate/scanf/sscanf_flt-fnn.c

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2009-10-31 11:52:10 UTC
  • mfrom: (1.1.8 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091031115210-x0mlijnegkce86fk
Tags: 1:1.6.7-1
* New upstream relese (closes: #544030)
* Added lintian overrides (closes: #553265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Test of scanf(): float conversion.
2
 
   $Id: sscanf_flt-fnn.c,v 1.2.2.3 2008/03/20 21:42:36 joerg_wunsch Exp $       */
3
 
 
4
 
#include <math.h>
5
 
#include <stdio.h>
6
 
#include <stdlib.h>
7
 
#include <string.h>
8
 
#include "progmem.h"
9
 
 
10
 
#ifdef  __AVR__
11
 
# define ASSERT(expr)                   \
12
 
    do {                                \
13
 
        if (!(expr)) exit(__LINE__);    \
14
 
    } while (0)
15
 
# define EXIT(v)        exit (v)
16
 
# if defined(__AVR_ATmega128__)
17
 
  /* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
18
 
#  define PRINTF(f...)  sprintf((char *)0x2000, f)
19
 
# else
20
 
  /* small AVR */
21
 
#  define PRINTF(f...)
22
 
# endif
23
 
#else
24
 
# include <assert.h>
25
 
# define ASSERT(expr)   assert (expr)
26
 
# define EXIT(v)        exit ((v) < 256 ? (v) : 255)
27
 
# define PRINTF(f...)   printf (f)
28
 
# define sscanf_P       sscanf
29
 
# define memcmp_P       memcmp
30
 
#endif
31
 
 
32
 
/* Next variables are useful to debug the AVR.  */
33
 
int vrslt = 1;
34
 
struct {
35
 
    float x[7];
36
 
    char c[7];
37
 
} v = { {1.0}, {1} };
38
 
 
39
 
void Check (int line, int expval, int rslt)
40
 
{
41
 
    vrslt = rslt;
42
 
    if (rslt != expval) {
43
 
        PRINTF ("\nLine %d:  expect= %d, rslt= %d\n", line, expval, rslt);
44
 
        EXIT (line);
45
 
    }
46
 
}
47
 
 
48
 
/* The sscanf() is called 4 times: SRAM and FLASH format, 2 values
49
 
   to fill before run.  */
50
 
#define CHECK(expval, ass_expr, str, fmt, ...)                          \
51
 
    do {                                                                \
52
 
        PROGMEM static char fmt_p[] = fmt;                              \
53
 
        char str_s[220];                                                \
54
 
        char fmt_s[40];                                                 \
55
 
        char FILL;                                                      \
56
 
        int i;                                                          \
57
 
        int (* volatile vp)(const char *, const char *, ...);           \
58
 
                                                                        \
59
 
        strcpy_P (str_s, PSTR(str));                                    \
60
 
        strcpy_P (fmt_s, fmt_p);                                        \
61
 
                                                                        \
62
 
        for (FILL = 0; FILL < 4; FILL++) {                              \
63
 
            memset (&v, FILL, sizeof(v));                               \
64
 
            vp = (FILL & 1) ? sscanf_P : sscanf;                        \
65
 
            i = vp (str_s, (FILL & 1) ? fmt_p : fmt_s, ##__VA_ARGS__);  \
66
 
            Check (__LINE__, expval, i);                                \
67
 
            ASSERT (ass_expr);                                          \
68
 
        }                                                               \
69
 
    } while (0)
70
 
 
71
 
int all_nans (const float v[], int n)
72
 
{
73
 
    int i;
74
 
    for (i = 0; i < n; i++) {
75
 
        if (!isnan (v[i]))
76
 
            return 0;
77
 
    }
78
 
    return 1;
79
 
}
80
 
 
81
 
int main ()
82
 
{
83
 
    /* No valid digits. */
84
 
    CHECK (0, 1, ". ", "%e", & v.x[0]);
85
 
    CHECK (0, 1, "+ ", "%e", & v.x[0]);
86
 
    CHECK (0, 1, "- ", "%e", & v.x[0]);
87
 
    CHECK (0, 1, "E ", "%e", & v.x[0]);
88
 
    CHECK (0, 1, "+-123", "%e", & v.x[0]);
89
 
    CHECK (0, 1, "-+123", "%e", & v.x[0]);
90
 
    CHECK (0, 1, ".-123", "%e", & v.x[0]);
91
 
    CHECK (0, 1, ".+123", "%e", & v.x[0]);
92
 
    CHECK (0, 1, "e12", "%e", & v.x[0]);
93
 
    CHECK (0, 1, "- 1", "%e", & v.x[0]);
94
 
    CHECK (0, 1, "IN ", "%e", & v.x[0]);
95
 
    CHECK (0, 1, "NA ", "%e", & v.x[0]);
96
 
    CHECK (0, 1, "infinit ", "%e", & v.x[0]);
97
 
 
98
 
    /* NaN      */
99
 
    CHECK (
100
 
        6,
101
 
        all_nans (v.x, 6),
102
 
        "nan NAN NaN -nan +NAN -nAn",
103
 
        "%E %e %F %f %G %g",
104
 
        & v.x[0], & v.x[1], & v.x[2], & v.x[3], & v.x[4], & v.x[5]);
105
 
 
106
 
    /* Character after NaN.     */
107
 
    CHECK (
108
 
        10,
109
 
        all_nans (v.x, 6) && !memcmp_P (v.c, PSTR (".+eQ"), 4),
110
 
        "nan. nan+ nane NANQ nannan",
111
 
        "%e%c %e%c %e%c %e%c %e %e",
112
 
        & v.x[0], & v.c[0], & v.x[1], & v.c[1], & v.x[2], & v.c[2],
113
 
        & v.x[3], & v.c[3], & v.x[4], & v.x[5]);
114
 
        
115
 
    /* Inf      */
116
 
    CHECK (
117
 
        6,
118
 
        !memcmp_P (
119
 
            v.x,
120
 
            ({  static float __x[] PROGMEM = {
121
 
                    INFINITY, INFINITY, INFINITY,
122
 
                    INFINITY, -INFINITY, -INFINITY
123
 
                };
124
 
                __x;
125
 
            }),
126
 
            6 * sizeof(float)),
127
 
        "INF inf +Inf INFINITY -infinity -InFiNiTy",
128
 
        "%e %E %f %F %g %G",
129
 
        & v.x[0], & v.x[1], & v.x[2], & v.x[3], & v.x[4], & v.x[5]);
130
 
 
131
 
    /* Character after Inf.     */
132
 
    CHECK (
133
 
        9,
134
 
        !memcmp_P (
135
 
            v.x,
136
 
            ({  static float __x[] PROGMEM = {
137
 
                    INFINITY, INFINITY, INFINITY,
138
 
                    INFINITY, INFINITY, -INFINITY
139
 
                };
140
 
                __x;
141
 
            }),
142
 
            6 * sizeof(float))
143
 
        && !memcmp_P (v.c, PSTR (" s\b"), 3),
144
 
        "inf infinityinfinitys INF\b inf-inf",
145
 
        "%e%c %e%e%c %e%c %e%e",
146
 
        & v.x[0], & v.c[0],
147
 
        & v.x[1],
148
 
        & v.x[2], & v.c[1],
149
 
        & v.x[3], & v.c[2],
150
 
        & v.x[4],
151
 
        & v.x[5]);
152
 
 
153
 
    return 0;
154
 
}