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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2008-08-10 09:59:16 UTC
  • mfrom: (1.1.7 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080810095916-wwyigh3vt0e9s7ud
Tags: 1:1.6.2.cvs20080610-2
Added build-depends on texlive-extra-utils (closes: #493454)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test of scanf(): 'c' conversion directive.
 
2
   $Id: sscanf-c1.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
    /* Empty input.     */
 
74
    CHECK (-1, 1, "", "%c", v.s);
 
75
    CHECK (-1, 1, "", " %c", v.s);
 
76
    CHECK (-1, 1, " ", " %c", v.s);
 
77
    CHECK (-1, 1, " ", "  %c", v.s);
 
78
    CHECK (-1, 1, "\t\n\v\f\r", " %c", v.s);
 
79
    
 
80
    /* Normal conversion.       */
 
81
    CHECK (1, (v.s[0] == 'a'), "a", "%c", v.s);
 
82
    CHECK (3, !memcmp_P (v.s, PSTR(" \001\377"), 3),
 
83
           " \001\377", "%c%c%c", v.s, v.s + 1, v.s + 2);
 
84
    CHECK (4, !memcmp_P (v.s, PSTR("DCBA"), 4),
 
85
           "ABCD", "%c%c%c%c", v.s + 3, v.s + 2, v.s + 1, v.s);
 
86
 
 
87
    /* Input match.     */
 
88
    CHECK (1, (v.s[0] == 'q'), "%The        %q", "%%The %%%c", v.s);
 
89
 
 
90
    /* Suppress a writing.      */
 
91
    CHECK (0, (v.s[0] == FILL), "a", "%*c", v.s);
 
92
    CHECK (3, !memcmp_P(v.s, PSTR("ACD"), 3),
 
93
           "ABCD", "%c%*c%c%c", v.s, v.s + 1, v.s + 2);
 
94
 
 
95
    /* Width field.     */
 
96
    CHECK (1, (v.s[0] == 'A'), "A", "%1c", v.s);
 
97
    CHECK (1, !memcmp_P(v.s, PSTR("AB"), 2), "AB", "%2c", v.s);
 
98
    CHECK (1, !memcmp_P(v.s, PSTR("The_quick_br"), 12),
 
99
           "The_quick_brown_fox", "%12c", v.s);
 
100
    CHECK (1, !memcmp_P(v.s, PSTR("A\t D"), 4), "A\t D", "%4c", v.s);
 
101
    CHECK (2, !memcmp_P(v.s, PSTR("3412"), 4),
 
102
           "1234", "%2c%2c", v.s + 2, v.s);
 
103
 
 
104
    /* Suppress and width.      */
 
105
    CHECK (0, (v.s[0] == FILL), "A", "%*1c", v.s);
 
106
    CHECK (0, (v.s[0] == FILL), "AA", "%*2c", v.s);
 
107
 
 
108
    return 0;
 
109
}