File: strtol-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 (44 lines) | stat: -rw-r--r-- 1,275 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
/* Binary conversion.
   $Id: strtol-3.c,v 1.1 2007/02/06 12:36:58 dmix Exp $
 */
#include <stdlib.h>
#include <string.h>
#include "progmem.h"
#include "strtol.h"

int main ()
{
    PROGMEM static struct t_s {
	char s[34];		/* string to convert	*/
	int base;
	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,  0x7fffffff, ERANGE, 32 },
	{ "10000000000000000000000000000001", 2,  0x7fffffff, ERANGE, 32 },
	{ "-1111111111111111111111111111111", 2,  0x80000001, 0, 32 },
	{ "-10000000000000000000000000000000", 2, 0x80000000, 0, 33 },
	{ "-10000000000000000000000000000001", 2, 0x80000000, ERANGE, 33 },
	{ "-10000000000000000000000000000010", 2, 0x80000000, ERANGE, 33 },
	
    };
    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_strtol (p->s, p->base, p->ret, p->err, p->len))
	    exit (i+1);
    }
    return 0;
}