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

« back to all changes in this revision

Viewing changes to tests/simulate/printf/sprintf_std-inv.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 sprintf_std(), invalid format.
2
 
   $Id: sprintf_std-inv.c,v 1.1.2.1 2008/03/20 21:42:32 joerg_wunsch Exp $      */
3
 
 
4
 
#ifndef __AVR__
5
 
# define PRINTFLN(line, fmt, ...)       \
6
 
    printf("\nLine %2d: " fmt "\n", line, ##__VA_ARGS__)
7
 
# define EXIT(code)     exit ((code) < 255 ? (code) : 255)
8
 
# define sprintf_P      sprintf
9
 
#else
10
 
# if defined(__AVR_ATmega128__)
11
 
  /* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
12
 
#  define PRINTFLN(line, fmt, ...)      \
13
 
    sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
14
 
# else
15
 
   /* small AVR */
16
 
#  define PRINTFLN(args...)
17
 
# endif
18
 
# define EXIT   exit
19
 
#endif
20
 
 
21
 
#include <stdio.h>
22
 
#include <stdlib.h>
23
 
#include <string.h>
24
 
#include "progmem.h"
25
 
 
26
 
void Check (int line,
27
 
            int explen,
28
 
            const char *expstr,
29
 
            int retval, const char *retstr)
30
 
{
31
 
    int code;
32
 
 
33
 
    if (retval != explen)
34
 
        code = 1000 + line;
35
 
    else if (strcmp_P (retstr, expstr))
36
 
        code = line;
37
 
    else
38
 
        return;
39
 
    PRINTFLN (line, "expect: %3d, \"%s\",\n%8s output: %3d, \"%s\"\n",
40
 
              explen, expstr, " ", retval, retstr);
41
 
#ifdef  DEBUG
42
 
    code = (int)retstr;
43
 
#endif
44
 
    EXIT (code);
45
 
}
46
 
 
47
 
/* 'vp' is used to avoid gcc warnings about format string.      */
48
 
#define CHECK(explen, expstr, fmt, ...) do {                    \
49
 
    char s[260];                                                \
50
 
    int i;                                                      \
51
 
    int (* volatile vp)(char *, const char *, ...) = sprintf_P; \
52
 
    memset (s, 0, sizeof(s));                                   \
53
 
    i = vp (s, PSTR(fmt), ##__VA_ARGS__);                       \
54
 
    Check (__LINE__, explen, PSTR(expstr), i, s);               \
55
 
} while (0)
56
 
 
57
 
int main ()
58
 
{
59
 
#ifdef  __AVR__         /* PRINTF_STD */
60
 
    /* Float numbers: are skipped       */
61
 
    CHECK (1, "?", "%e", 0.0);
62
 
    CHECK (23, "? e ? f ? g ? E ? F ? G",
63
 
           "%e %c %e %c %e %c %e %c %e %c %e %c",
64
 
           1.0, 'e', 2.0, 'f', 3.0, 'g', 4.0, 'E', 5.0, 'F', 6.0, 'G');
65
 
    CHECK (19, "? 10 ? 11 ? 12 ? 13",
66
 
           "%+e %d %-f %d % g %d %0F %d",
67
 
           10.0, 10, 11.0, 11, 12.0, 12, 13.0, 13);
68
 
 
69
 
    /* Width is work.   */
70
 
    CHECK (17, " ?.  ?.   ?.    ?",
71
 
           "%2e.%3.1f.%+4g.% 5E", 1.0, 2.0, 3.0, 4.0);
72
 
 
73
 
    /* Left pad is work.        */
74
 
    CHECK (6, "? .?  ", "%-2F.%-03G", 5.0, 6.0);
75
 
#endif
76
 
 
77
 
    return 0;
78
 
}