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

« back to all changes in this revision

Viewing changes to tests/simulate/scanf/sscanf-c2.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(): 'c' conversion directive.
2
 
   $Id: sscanf-c2.c,v 1.1.2.3 2008/03/20 21:42:33 joerg_wunsch Exp $    */
3
 
 
4
 
#include <stdio.h>
5
 
#include <stdlib.h>
6
 
#include <string.h>
7
 
#include "progmem.h"
8
 
 
9
 
#ifdef  __AVR__
10
 
# define ASSERT(expr)                   \
11
 
    do {                                \
12
 
        if (!(expr)) exit(__LINE__);    \
13
 
    } while (0)
14
 
# define EXIT(v)        exit (v)
15
 
# if defined(__AVR_ATmega128__)
16
 
  /* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
17
 
#  define PRINTF(f...)  sprintf((char *)0x2000, f)
18
 
# else
19
 
  /* small AVR */
20
 
#  define PRINTF(f...)
21
 
# endif
22
 
#else
23
 
# include <assert.h>
24
 
# define ASSERT(expr)   assert (expr)
25
 
# define EXIT(v)        exit ((v) < 256 ? (v) : 255)
26
 
# define PRINTF(f...)   printf (f)
27
 
# define sscanf_P       sscanf
28
 
# define memcmp_P       memcmp
29
 
#endif
30
 
 
31
 
/* Next variables are useful to debug the AVR.  */
32
 
int vrslt = 1;
33
 
struct {
34
 
    char s[12];
35
 
} v = { {1} };
36
 
 
37
 
void Check (int line, int expval, int rslt)
38
 
{
39
 
    vrslt = rslt;
40
 
    if (rslt != expval) {
41
 
        PRINTF ("\nLine %d:  expect= %d, rslt= %d\n", line, expval, rslt);
42
 
        EXIT (line);
43
 
    }
44
 
}
45
 
 
46
 
/* The sscanf() is called 4 times: SRAM and FLASH format, 2 values
47
 
   to fill before run.  */
48
 
#define CHECK(expval, ass_expr, str, fmt, ...)                          \
49
 
    do {                                                                \
50
 
        PROGMEM static char fmt_p[] = fmt;                              \
51
 
        char str_s[220];                                                \
52
 
        char fmt_s[40];                                                 \
53
 
        char FILL;                                                      \
54
 
        int i;                                                          \
55
 
        int (* volatile vp)(const char *, const char *, ...);           \
56
 
                                                                        \
57
 
        ASSERT (sizeof(str_s) >= sizeof(str));                          \
58
 
        ASSERT (sizeof(fmt_s) >= sizeof(fmt_p));                        \
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 main ()
72
 
{
73
 
    /* Zero width.      */
74
 
#ifdef  __AVR__
75
 
    CHECK (0, (v.s[0] == FILL), "A", "%0c", v.s);
76
 
#else
77
 
    CHECK (1, (v.s[0] == 'A'), "A", "%0c", v.s);
78
 
#endif
79
 
 
80
 
    /* Left width digit is 0.   */
81
 
    CHECK (1, (v.s[0] == 'A'), "A", "%01c", v.s);
82
 
    CHECK (1, !memcmp_P(v.s, PSTR("AB"), 2), "AB", "%02c", v.s);
83
 
 
84
 
    /* Invalid symbol after '%'.        */
85
 
    CHECK (0, (v.s[0] == FILL), "A", "% c", v.s);
86
 
    CHECK (0, (v.s[0] == FILL), "A", "%-c", v.s);
87
 
    CHECK (0, (v.s[0] == FILL), "A", "%+c", v.s);
88
 
    CHECK (0, (v.s[0] == FILL), "A", "%.c", v.s);
89
 
    CHECK (0, (v.s[0] == FILL), "A", "%#c", v.s);
90
 
 
91
 
    /* The length modifier.     */
92
 
    CHECK (1, (v.s[0] == 'A' && v.s[1] == FILL), "A", "%hc", v.s);
93
 
#ifdef  __AVR__
94
 
    CHECK (1, (v.s[0] == 'A' && v.s[1] == FILL), "A", "%lc", v.s);
95
 
#else
96
 
    CHECK (1, (v.s[0] == 'A' && v.s[1] == 0), "A", "%lc", v.s);
97
 
#endif
98
 
 
99
 
    return 0;
100
 
}