struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / strto.c
bloba511dc9bbf731c6fd8ea6ae4fe0230d959a95beb
1 /** strto.c
2 */
3 #include <testfwk.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <limits.h>
8 #include <errno.h>
10 const char string0[] = "-2";
11 const char string1[] = "-9999999999999999999999";
12 const char string2[] = "9999999999999999999999";
13 const char string3[] = "-2test";
14 const char string4[] = "test";
15 const char string5[] = "023test";
16 const char string6[] = "-0x23test";
18 void
19 testStrto(void)
21 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) && !defined(__SDCC_mcs51) // Lack of memory
22 char *e;
24 ASSERT(strtoul("", 0, 10) == 0);
25 ASSERT(strtoul("2", 0, 10) == 2);
26 ASSERT(strtoul("3", 0, 10) == 3);
27 ASSERT(strtoul("23", 0, 10) == 23);
29 ASSERT(strtoul("23", 0, 0) == 23);
30 ASSERT(strtoul("023", 0, 0) == 023);
31 ASSERT(strtoul("0x23", 0, 0) == 0x23);
33 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202300L // C2X introduces binary prefix.
34 ASSERT(strtoul("0b11", 0, 0) == 0b11);
35 ASSERT(strtoul("0b11", 0, 2) == 0b11);
36 #endif
38 ASSERT(strtoul("+23", 0, 0) == +23);
39 ASSERT(strtoul("+023", 0, 0) == +023);
40 ASSERT(strtoul("+0x23", 0, 0) == +0x23);
42 ASSERT(strtol("-42", 0, 0) == -42);
43 ASSERT(strtol("-042", 0, 0) == -042);
44 ASSERT(strtol("-0x42", 0, 0) == -0x42);
45 ASSERT(strtol("-0x42", 0, 16) == -0x42);
47 #if ULONG_MAX == 4294967295
48 errno = 0;
49 strtoul("4294967296", 0, 10);
50 ASSERT(errno == ERANGE);
51 #endif
53 errno = 0;
54 ASSERT(strtol(string1, &e, 10) == LONG_MIN);
55 ASSERT(errno == ERANGE);
56 ASSERT(e == string1 + strlen(string1));
58 errno = 0;
59 ASSERT(strtol(string2, &e, 10) == LONG_MAX);
60 ASSERT(errno == ERANGE);
61 ASSERT(e == string2 + strlen(string2));
63 errno = 0;
64 ASSERT(strtoul(string0, &e, 10) == (unsigned long int)(-2));
65 ASSERT(errno != ERANGE);
66 ASSERT(e == string0 + strlen(string0));
68 errno = 0;
69 ASSERT(strtoul(string2, &e, 10) == ULONG_MAX);
70 ASSERT(errno == ERANGE);
71 ASSERT(e == string2 + strlen(string2));
73 errno = 0;
74 ASSERT(strtol(string3, &e, 10) == -2);
75 ASSERT(errno != ERANGE);
76 ASSERT(e == string3 + 2);
78 errno = 0;
79 ASSERT(strtol(string4, &e, 0) == 0);
80 ASSERT(errno != ERANGE);
81 ASSERT(e == string4);
83 errno = 0;
84 ASSERT(strtol(string5, &e, 0) == 023);
85 ASSERT(errno != ERANGE);
86 ASSERT(e == string5 + 3);
88 errno = 0;
89 ASSERT(strtol(string6, &e, 0) == -0x23);
90 ASSERT(errno != ERANGE);
91 ASSERT(e == string6 + 5);
92 #endif