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

int main ()
{
    PROGMEM static struct t_s {
	char s[35];		/* string to convert	*/
	int base;
	unsigned long ret;	/* result must	*/
	int err;		/* errno must	*/
	unsigned char len;	/* endptr displacement must	*/
    } t[] = {
	
	{ "0", 2,	0, 0, 1 },
	{ "1", 2,	1, 0, 1 },
	{ "-1", 2,	-1, 0, 2 },
	{ "10101010", 2,	0xaa, 0, 8 },

	{ "1111111111111111111111111111110", 2,	   0x7ffffffe, 0, 31 },
	{ "1111111111111111111111111111111", 2,    0x7fffffff, 0, 31 },
	{ "10000000000000000000000000000000", 2,   0x80000000, 0, 32 },
	{ "10000000000000000000000000000001", 2,   0x80000001, 0, 32 },
	{ "11111111111111111111111111111111", 2,   0xffffffff, 0, 32 },
	{ "100000000000000000000000000000000", 2,  0xffffffff, ERANGE, 33 },
	{ "100000000000000000000000000000001", 2,  0xffffffff, ERANGE, 33 },

	{ "-1111111111111111111111111111111", 2,   0x80000001, 0, 32 },
	{ "-10000000000000000000000000000000", 2,  0x80000000, 0, 33 },
	{ "-10000000000000000000000000000001", 2,  0x7fffffff, 0, 33 },
	{ "-10000000000000000000000000000010", 2,  0x7ffffffe, 0, 33 },
	{ "-11111111111111111111111111111111", 2,  0x00000001, 0, 33 },
	{ "-100000000000000000000000000000000", 2, 0xffffffff, ERANGE, 34 },
	{ "-100000000000000000000000000000001", 2, 0xffffffff, ERANGE, 34 },
	
    };
    struct t_s tt;
    int i;
    
    for (i = 0; i != (int)(sizeof(t)/sizeof(t[0])); i++) {
	struct t_s *p;
	memcpy_P (&tt, t + i, sizeof(tt));
	p = &tt;
	if (t_strtoul (p->s, p->base, p->ret, p->err, p->len))
	    exit (i+1);
    }
    return 0;
}