~ubuntu-branches/ubuntu/wily/avr-libc/wily-proposed

« back to all changes in this revision

Viewing changes to tests/simulate/scanf/sscanf-s2.c

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2009-10-31 11:52:10 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091031115210-crjd42sn6ezrj52c
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(): 's' conversion directive.
2
 
   $Id: sscanf-s2.c,v 1.1.2.3 2008/03/20 21:42:35 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 c;
35
 
    char s[8];
36
 
} v[6] = { {1, {1}} };
37
 
 
38
 
void Check (int line, int expval, int rslt)
39
 
{
40
 
    vrslt = rslt;
41
 
    if (rslt != expval) {
42
 
        PRINTF ("\nLine %d:  expect= %d, rslt= %d\n", line, expval, rslt);
43
 
        EXIT (line);
44
 
    }
45
 
}
46
 
 
47
 
/* The sscanf() is called 4 times: SRAM and FLASH format, 2 values
48
 
   to fill before run.  */
49
 
#define CHECK(expval, ass_expr, str, fmt, ...)                          \
50
 
    do {                                                                \
51
 
        PROGMEM static char fmt_p[] = fmt;                              \
52
 
        char str_s[220];                                                \
53
 
        char fmt_s[40];                                                 \
54
 
        char FILL;                                                      \
55
 
        int i;                                                          \
56
 
        int (* volatile vp)(const char *, const char *, ...);           \
57
 
                                                                        \
58
 
        ASSERT (sizeof(str_s) >= sizeof(str));                          \
59
 
        ASSERT (sizeof(fmt_s) >= sizeof(fmt_p));                        \
60
 
        strcpy_P (str_s, PSTR(str));                                    \
61
 
        strcpy_P (fmt_s, fmt_p);                                        \
62
 
                                                                        \
63
 
        for (FILL = 0; FILL < 4; FILL++) {                              \
64
 
            memset (&v, FILL, sizeof(v));                               \
65
 
            vp = (FILL & 1) ? sscanf_P : sscanf;                        \
66
 
            i = vp (str_s, (FILL & 1) ? fmt_p : fmt_s, ##__VA_ARGS__);  \
67
 
            Check (__LINE__, expval, i);                                \
68
 
            ASSERT (ass_expr);                                          \
69
 
        }                                                               \
70
 
    } while (0)
71
 
 
72
 
int main ()
73
 
{
74
 
    /* Width field.     */
75
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%1s", v[0].s);
76
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "ABCD", "%1s", v[0].s);
77
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("AB")), "ABCD", "%2s", v[0].s);
78
 
    CHECK (2, !memcmp_P(v[0].s, PSTR("CD\000AB\000"), 6),
79
 
           "ABCD", "%2s%2s", v[0].s + 3, v[0].s);
80
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("The_quick_b")),
81
 
           "The_quick_brown_fox", "%11s", v[0].s);
82
 
 
83
 
    /* Suppress and width.      */
84
 
    CHECK (0, (v[0].s[0] == FILL), "A", "%*1s", v[0].s);
85
 
    CHECK (0, (v[0].s[0] == FILL), "AA", "%*2s", v[0].s);
86
 
 
87
 
    /* Split long string.       */
88
 
    CHECK (
89
 
        6,
90
 
        !strcmp_P (v[0].s, PSTR("ABCDE"))
91
 
        && !strcmp_P (v[1].s, PSTR("FGHIJ"))
92
 
        && !strcmp_P (v[2].s, PSTR("KLMNO"))
93
 
        && !strcmp_P (v[3].s, PSTR("PQRST"))
94
 
        && !strcmp_P (v[4].s, PSTR("UVWXY"))
95
 
        && !strcmp_P (v[5].s, PSTR("Z")),
96
 
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
97
 
        "%5s%5s %5s  %5s\t%5s\n%5s",
98
 
        v[0].s, v[1].s, v[2].s, v[3].s, v[4].s, v[5].s);
99
 
 
100
 
    /* Zero width.      */
101
 
#ifdef  __AVR__
102
 
    CHECK (0, (v[0].s[0] == FILL), "A", "%0s", v[0].s);
103
 
#else
104
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%0s", v[0].s);  /* ??? */
105
 
#endif
106
 
 
107
 
    /* Left width digit is 0.   */
108
 
    CHECK (
109
 
        2,
110
 
        !strcmp_P(v[0].s, PSTR("A")) && !strcmp_P(v[1].s, PSTR("BC")),
111
 
        "ABCD",
112
 
        "%01s %02s", v[0].s, v[1].s);
113
 
 
114
 
    /* Invalid symbol after '%'.        */
115
 
    CHECK (0, (v[0].s[0] == FILL), "A", "% s", v[0].s);
116
 
    CHECK (0, (v[0].s[0] == FILL), "A", "%-s", v[0].s);
117
 
    CHECK (0, (v[0].s[0] == FILL), "A", "%+s", v[0].s);
118
 
    CHECK (0, (v[0].s[0] == FILL), "A", "%.s", v[0].s);
119
 
    CHECK (0, (v[0].s[0] == FILL), "A", "%#s", v[0].s);
120
 
 
121
 
    /* The length modifier.     */
122
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%hs", v[0].s);
123
 
    CHECK (1, !strcmp_P(v[0].s, PSTR("A")), "A", "%ls", v[0].s);
124
 
 
125
 
    return 0;
126
 
}