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

« back to all changes in this revision

Viewing changes to tests/simulate/printf/vsnprintf_all.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
 
/* $Id: vsnprintf_all.c,v 1.1.2.1 2008/03/20 21:42:32 joerg_wunsch Exp $ */
2
 
 
3
 
#ifndef __AVR__
4
 
# define PRINTFLN(line, fmt, ...)       \
5
 
    printf("\nLine %2d: " fmt "\n", line, ##__VA_ARGS__)
6
 
# define EXIT(code)     exit ((code) < 255 ? (code) : 255)
7
 
# define vsnprintf_P    vsnprintf
8
 
#else
9
 
# if defined(__AVR_ATmega128__)
10
 
  /* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
11
 
#  define PRINTFLN(line, fmt, ...)      \
12
 
    sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
13
 
# else
14
 
   /* small AVR */
15
 
#  define PRINTFLN(args...)
16
 
# endif
17
 
# define EXIT   exit
18
 
#endif
19
 
 
20
 
#define L_PRINTFLN(args...)     PRINTFLN (__LINE__, args)
21
 
#define L_EXIT()                EXIT (__LINE__)
22
 
 
23
 
#include <stdarg.h>
24
 
#include <stdio.h>
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
#include "progmem.h"
28
 
 
29
 
#ifdef  L_progmem
30
 
# define vsnprintf      vsnprintf_P
31
 
# define PFMT(s)        PSTR(s)
32
 
#else
33
 
# define PFMT(s)        s
34
 
#endif
35
 
 
36
 
void Check (int line,
37
 
            int expval, const char *expstr,
38
 
            char *buf, size_t size, const char *fmt, ...)
39
 
{
40
 
    va_list ap;
41
 
    int retval;
42
 
    int code;
43
 
 
44
 
    va_start (ap, fmt);
45
 
    retval = vsnprintf (buf, size, fmt, ap);
46
 
    va_end (ap);
47
 
 
48
 
    if (retval != expval)
49
 
        code = line;
50
 
    else if (size && strcmp_P (buf, expstr))
51
 
        code = 1000 + line;
52
 
    else
53
 
        return;
54
 
    PRINTFLN (line, "expect: %3d, \"%s\",\n%8s output: %3d, \"%s\"\n",
55
 
              strlen(expstr), expstr, " ", retval, size ? buf : "");
56
 
#ifdef  DEBUG
57
 
    code = (int)retstr;
58
 
#endif
59
 
    EXIT (code);
60
 
}
61
 
 
62
 
 
63
 
#define FILLBUF 0x55    /* Before call of function to test
64
 
                           buf[] is filled with nonzero value.  */
65
 
#define CHECK(expval, expstr, buf, size, fmt, ...)      \
66
 
    memset (buf, FILLBUF, sizeof(buf));                 \
67
 
    Check (__LINE__, expval, PSTR(expstr),              \
68
 
           buf, size, PFMT(fmt), ##__VA_ARGS__)
69
 
 
70
 
int main ()
71
 
{
72
 
    char s[260];
73
 
    int i;
74
 
 
75
 
    /* size == 0        */
76
 
    CHECK (0, "", s+10, 0, "");
77
 
 
78
 
    /* bug #19280: snprintf(s,0,...) write to s[-1]     */
79
 
    memset (s, FILLBUF, sizeof(s));
80
 
    Check (__LINE__, 3, PSTR(""), s+10, 0, PFMT("foo"));
81
 
    for (i = 0; i != (int)sizeof(s); i++) {
82
 
        if ((unsigned char)s[i] != FILLBUF)
83
 
            L_EXIT ();
84
 
    }
85
 
 
86
 
    /* size == 1        */
87
 
    s[0] = 1;
88
 
    CHECK (0, "", s, 1, "");
89
 
    CHECK (3, "", s, 1, "foo");
90
 
 
91
 
    /* size == 2        */
92
 
    CHECK (0, "", s, 2, "");
93
 
    CHECK (1, ".", s, 2, ".");
94
 
    CHECK (2, "a", s, 2, "aa");
95
 
    CHECK (5, "1", s, 2, "%d", 12345);
96
 
 
97
 
    /* big size */
98
 
    CHECK (6, "-12345", s, sizeof(s), "%d", -12345);
99
 
    CHECK (5, "54321", s, ~0u >> 1, "%u", 54321);               /* 32767 */
100
 
    CHECK (4, "abcd", s, ~(~0u >> 1), "%x", 0xabcd);            /* 32768 */
101
 
    CHECK (3, "123", s, ~0u, "%o", 0123);                       /* 65535 */
102
 
 
103
 
    return 0;
104
 
}