File: sprintf_std-int.c

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (119 lines) | stat: -rw-r--r-- 3,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Test of sprintf(), a set of decimal numbers.
   Is sutable for all variants: MIN, STD and FLT.
   $Id: sprintf_std-int.c,v 1.1.2.1 2008/03/20 21:42:32 joerg_wunsch Exp $	*/

#ifndef __AVR__
# define PRINTFLN(line, fmt, ...)	\
    printf("\nLine %2d: " fmt "\n", line, ##__VA_ARGS__)
# define EXIT(code)	exit ((code) < 255 ? (code) : 255)
# define sprintf_P	sprintf
#else
# if defined(__AVR_ATmega128__)
  /* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
#  define PRINTFLN(line, fmt, ...)	\
    sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
# else
   /* small AVR */
#  define PRINTFLN(args...)
# endif
# define EXIT	exit
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progmem.h"

void Check (int line,
            const char *expstr,
	    int retval, const char *retstr)
{
    int code;

    if (retval != (int)strlen (retstr))
	code = 1000 + line;
    else if (strcmp_P (retstr, expstr))
	code = line;
    else
	return;
    PRINTFLN (line, "expect: %3d, \"%s\",\n%8s output: %3d, \"%s\"\n",
	      strlen(expstr), expstr, " ", retval, retstr);
#ifdef	DEBUG
    code = (int)retstr;
#endif
    EXIT (code);
}

/* 'vp' is used to avoid gcc warnings about format string.	*/
#define CHECK(expstr, fmt, ...)	do {				\
    char s[260];						\
    int i;							\
    int (* volatile vp)(char *, const char *, ...) = sprintf_P;	\
    memset (s, 0, sizeof(s));					\
    i = vp (s, PSTR(fmt), ##__VA_ARGS__);			\
    Check (__LINE__, PSTR(expstr), i, s);			\
} while (0)

#define CHECK_2(fmt, val)	do {	\
    char s[20];				\
    int i;				\
    i = sprintf_P (s, PSTR(fmt), val);	\
    if (i <= 0 || i > 12)		\
	EXIT (__LINE__);		\
    if (val != strtoul(s, 0, 0))	\
	EXIT (__LINE__ + 1000);		\
} while (0)

int main ()
{
    unsigned long b1, b2;

    CHECK ("012345678", "%d%d%d%d%d%d%d%d%d", 0,1,2,3,4,5,6,7,8);
    CHECK ("9 10 11", "%d %d %d", 9, 10, 11);
    CHECK ("99 100 101", "%d %d %d", 99, 100, 101);
    CHECK ("999 1000 1001", "%d %d %d", 999, 1000, 1001);
    CHECK ("9999 10000 10001", "%d %d %d", 9999, 10000, 10001);
    CHECK ("99999 100000 100001", "%ld %ld %ld", 99999L, 100000L, 100001L);
    CHECK ("999999 1000000 1000001", "%ld %ld %ld",
	   999999L, 1000000L, 1000001L);
    CHECK ("9999999 10000000 10000001", "%ld %ld %ld",
	   9999999L, 10000000L, 10000001L);
    CHECK ("99999999 100000000 100000001", "%ld %ld %ld",
           99999999L, 100000000L, 100000001L);
    CHECK ("999999999 1000000000 1000000001", "%ld %ld %ld",
	   999999999L, 1000000000L, 1000000001L);
    CHECK ("2000000000 3000000000 4000000000 4294967295", "%lu %lu %lu %lu",
	   2000000000ul, 3000000000ul, 4000000000ul, 0xffffffff);

    /* Check a set of numbers.	*/
    for (b1 = 1; b1; b1 <<= 1) {
	for (b2 = 1; b2; b2 <<= 1) {
	    unsigned long x;
	    x = b1 | b2;
	    CHECK_2 ("%ld", x);
	    CHECK_2 ("%lu", x);
	    CHECK_2 ("%#lo", x);
	    CHECK_2 ("%#lx", x);
	    CHECK_2 ("%#lX", x);
	    x -= 1;
	    CHECK_2 ("%ld", x);
	    CHECK_2 ("%lu", x);
	    CHECK_2 ("%#lo", x);
	    CHECK_2 ("%#lx", x);
	    CHECK_2 ("%#lX", x);
	    x = ~(b1 | b2);
	    CHECK_2 ("%ld", x);
	    CHECK_2 ("%lu", x);
	    CHECK_2 ("%#lo", x);
	    CHECK_2 ("%#lx", x);
	    CHECK_2 ("%#lX", x);
	    x += 1;
	    CHECK_2 ("%ld", x);
	    CHECK_2 ("%lu", x);
	    CHECK_2 ("%#lo", x);
	    CHECK_2 ("%#lx", x);
	    CHECK_2 ("%#lX", x);
	}
    }
    return 0;
}