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
|
/* Base test.
$Id: strtol-1.c,v 1.1 2007/02/06 12:36:58 dmix Exp $
*/
#include <stdlib.h>
#include <string.h>
#include "progmem.h"
#include "strtol.h"
PROGMEM static const struct t_s {
char s[14]; /* string to convert */
int base;
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 },
};
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_strtol (tt.s, tt.base, tt.ret, tt.err, tt.len))
exit (i+1);
}
return 0;
}
|