File: strtoul-1.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 (83 lines) | stat: -rw-r--r-- 2,223 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
/* Base test.
   $Id: strtoul-1.c,v 1.1 2007/02/07 13:42:36 dmix Exp $
 */
#include <stdlib.h>
#include <string.h>
#include "progmem.h"
#include "strtoul.h"

PROGMEM static const struct t_s {
    char s[14];		/* string to convert	*/
    int base;
    unsigned long ret;	/* result must	*/
    int err;		/* errno must	*/
    unsigned char len;	/* endptr displacement must	*/
} t[] = {
    { "", 0,	    0, 0, 0 },
    { "-", 0,	    0, 0, 0 },
    { "+", 0,	    0, 0, 0 },
    { "    ", 0,    0, 0, 0 },

#ifdef	__AVR__		/* AVR: errno is't set in case of invalid base	*/
    { "", -1,	    0, 0, 0 },
    { "0", 1,	    0, 0, 0 },
    { "123", 37,    0, 0, 0 },
#else
    { "", -1,	    0, EINVAL, 0 },
    { "0", 1,	    0, EINVAL, 0 },
    { "123", 37,    0, EINVAL, 0 },
#endif
	
    { "0", 0,	    0, 0, 1 },
    { "0 ", 0,	    0, 0, 1 },
    { " 0", 0,	    0, 0, 2 },
    { "\t0", 0,	    0, 0, 2 },
    { " \t 0", 0,   0, 0, 4 },
    { "0x0", 0,	    0, 0, 3 },
    { "0000", 0,    0, 0, 4 },
    { "+0", 0,	    0, 0, 2 },
    { "-0", 0,	    0, 0, 2 },
	
    { "0x", 0,	    0, 0, 1 },
    { "0x", 16,	    0, 0, 1 },
    { "0X", 0,	    0, 0, 1 },
    { " 0x", 0,	    0, 0, 2 },
    { "0xx", 0,	    0, 0, 1 },
    { "-0x", 0,	    0, 0, 2 },
    { "-0x", 16,    0, 0, 2 },
    { "+0x", 0,	    0, 0, 2 },
	
    { "1", 0,	    1, 0, 1 },
    { "-1", 0,	    -1, 0, 2 },

    { "210", 3,	    2*9 + 1*3 + 0,		0, 3 },
    { "a98", 11,    10*11*11L + 9*11 + 8,	0, 3 },
    { "xyz", 36,    33*36*36L + 34*36 + 35,	0, 3 },
	
    { "12345", 8,    012345, 0, 5 },
    { "12345", 10,   12345, 0, 5 },
    { "12345", 16,   0x12345, 0, 5 },
    
    { "4294967294", 0,	 0xfffffffe, 0, 10 },
    { "4294967295", 0,	 0xffffffff, 0, 10 },
    { "037777777776", 0, 0xfffffffe, 0, 12 },
    { "037777777777", 0, 0xffffffff, 0, 12 },
    { "0xfffffffe", 0,   0xfffffffe, 0, 10 },
    { "0xffffffff", 0,   0xffffffff, 0, 10 },
    
    { "0xabcdef", 0,	 0xabcdef, 0, 8 },
    { "0XABCDEF", 0,	 0xabcdef, 0, 8 },
};

int main ()
{
    struct t_s tt;
    int i;
    
    for (i = 0; i != (int)(sizeof(t)/sizeof(t[0])); i++) {
	memcpy_P (&tt, t+i, sizeof(tt));
	if (t_strtoul (tt.s, tt.base, tt.ret, tt.err, tt.len))
	    exit (i+1);
    }
    return 0;
}