Skip to content

Commit

Permalink
Adjust to nonstandard _strto
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed Feb 4, 2024
1 parent b46fe23 commit 3ea0141
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
38 changes: 19 additions & 19 deletions SingleSource/UnitTests/strtoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,64 @@ int main(void) {
char tricky[] = "+0xz";
errno = 0;
/* basic functionality */
assert(strtoi("123", NULL, 10) == 123);
assert(_strtoi("123", NULL, 10) == 123);
/* proper detecting of default base 10 */
assert(strtoi("456", NULL, 0) == 456);
assert(_strtoi("456", NULL, 0) == 456);
/* proper functioning to smaller base */
assert(strtoi("14", NULL, 8) == 12);
assert(_strtoi("14", NULL, 8) == 12);
/* proper autodetecting of octal */
assert(strtoi("016", NULL, 0) == 14);
assert(_strtoi("016", NULL, 0) == 14);
/* proper autodetecting of hexadecimal, lowercase 'x' */
assert(strtoi("0xFF", NULL, 0) == 255);
assert(_strtoi("0xFF", NULL, 0) == 255);
/* proper autodetecting of hexadecimal, uppercase 'X' */
assert(strtoi("0Xa1", NULL, 0) == 161);
assert(_strtoi("0Xa1", NULL, 0) == 161);
/* proper handling of border case: 0x followed by non-hexdigit */
assert(strtoi(tricky, &endptr, 0) == 0);
assert(_strtoi(tricky, &endptr, 0) == 0);
assert(endptr == tricky + 2);
/* proper handling of border case: 0 followed by non-octdigit */
assert(strtoi(tricky, &endptr, 8) == 0);
assert(_strtoi(tricky, &endptr, 8) == 0);
assert(endptr == tricky + 2);
/* errno should still be 0 */
assert(errno == 0);
/* correctly decoding zero */
assert(strtoi("0", &endptr, 0) == 0);
assert(_strtoi("0", &endptr, 0) == 0);
assert(*endptr == '\0');
assert(errno == 0);
/* overflowing subject sequence must still return proper endptr */
assert(strtoi(overflow, &endptr, 36) == INT_MIN);
assert(_strtoi(overflow, &endptr, 36) == INT_MIN);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* same for positive */
errno = 0;
assert(strtoi(overflow + 1, &endptr, 36) == INT_MAX);
assert(_strtoi(overflow + 1, &endptr, 36) == INT_MAX);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* testing skipping of leading whitespace */
assert(strtoi(" \n\v\t\f789", NULL, 0) == 789);
assert(_strtoi(" \n\v\t\f789", NULL, 0) == 789);
/* testing conversion failure */
assert(strtoi(overflow, &endptr, 10) == 0);
assert(_strtoi(overflow, &endptr, 10) == 0);
assert(endptr == overflow);
endptr = NULL;
assert(strtoi(overflow, &endptr, 0) == 0);
assert(_strtoi(overflow, &endptr, 0) == 0);
assert(endptr == overflow);
/* TODO: These tests assume two-complement, but conversion should work */
/* for one-complement and signed magnitude just as well. Anyone having */
/* a platform to test this on? */
errno = 0;
/* testing "even" overflow, i.e. base is power of two */
assert(strtoi("32767", NULL, 0) == 0x7fff);
assert(_strtoi("32767", NULL, 0) == 0x7fff);
assert(errno == 0);
errno = 0;
assert(strtoi("32768", NULL, 0) == INT_MAX);
assert(_strtoi("32768", NULL, 0) == INT_MAX);
assert(errno == ERANGE);
errno = 0;
assert(strtoi("-32767", NULL, 0) == (int)0x8001);
assert(_strtoi("-32767", NULL, 0) == (int)0x8001);
assert(errno == 0);
errno = 0;
assert(strtoi("-32768", NULL, 0) == INT_MIN);
assert(_strtoi("-32768", NULL, 0) == INT_MIN);
assert(errno == 0);
errno = 0;
assert(strtoi("-32769", NULL, 0) == INT_MIN);
assert(_strtoi("-32769", NULL, 0) == INT_MIN);
assert(errno == ERANGE);
return 0;
}
30 changes: 15 additions & 15 deletions SingleSource/UnitTests/strtosc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,56 @@ int main(void) {
char tricky[] = "+0xz";
errno = 0;
/* basic functionality */
assert(strtosc("123", NULL, 10) == 123);
assert(_strtosc("123", NULL, 10) == 123);
/* proper functioning to smaller base */
assert(strtosc("14", NULL, 8) == 12);
assert(_strtosc("14", NULL, 8) == 12);
/* proper autodetecting of octal */
assert(strtosc("016", NULL, 0) == 14);
assert(_strtosc("016", NULL, 0) == 14);
/* proper handling of border case: 0x followed by non-hexdigit */
assert(strtosc(tricky, &endptr, 0) == 0);
assert(_strtosc(tricky, &endptr, 0) == 0);
assert(endptr == tricky + 2);
/* proper handling of border case: 0 followed by non-octdigit */
assert(strtosc(tricky, &endptr, 8) == 0);
assert(_strtosc(tricky, &endptr, 8) == 0);
assert(endptr == tricky + 2);
/* errno should still be 0 */
assert(errno == 0);
/* correctly decoding zero */
assert(strtosc("0", &endptr, 0) == 0);
assert(_strtosc("0", &endptr, 0) == 0);
assert(*endptr == '\0');
assert(errno == 0);
/* overflowing subject sequence must still return proper endptr */
assert(strtosc(overflow, &endptr, 36) == SCHAR_MIN);
assert(_strtosc(overflow, &endptr, 36) == SCHAR_MIN);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* same for positive */
errno = 0;
assert(strtosc(overflow + 1, &endptr, 36) == SCHAR_MAX);
assert(_strtosc(overflow + 1, &endptr, 36) == SCHAR_MAX);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* testing conversion failure */
assert(strtosc(overflow, &endptr, 10) == 0);
assert(_strtosc(overflow, &endptr, 10) == 0);
assert(endptr == overflow);
endptr = NULL;
assert(strtosc(overflow, &endptr, 0) == 0);
assert(_strtosc(overflow, &endptr, 0) == 0);
assert(endptr == overflow);
/* TODO: These tests assume two-complement, but conversion should work */
/* for one-complement and signed magnitude just as well. Anyone having */
/* a platform to test this on? */
errno = 0;
/* testing "even" overflow, i.e. base is power of two */
assert(strtosc("127", NULL, 0) == 0x7f);
assert(_strtosc("127", NULL, 0) == 0x7f);
assert(errno == 0);
errno = 0;
assert(strtosc("128", NULL, 0) == SCHAR_MAX);
assert(_strtosc("128", NULL, 0) == SCHAR_MAX);
assert(errno == ERANGE);
errno = 0;
assert(strtosc("-127", NULL, 0) == (signed char)0x81);
assert(_strtosc("-127", NULL, 0) == (signed char)0x81);
assert(errno == 0);
errno = 0;
assert(strtosc("-128", NULL, 0) == SCHAR_MIN);
assert(_strtosc("-128", NULL, 0) == SCHAR_MIN);
assert(errno == 0);
errno = 0;
assert(strtosc("-129", NULL, 0) == SCHAR_MIN);
assert(_strtosc("-129", NULL, 0) == SCHAR_MIN);
assert(errno == ERANGE);
return 0;
}
30 changes: 15 additions & 15 deletions SingleSource/UnitTests/strtouc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,54 @@ int main(void) {
char tricky[] = "+0xz";
errno = 0;
/* basic functionality */
assert(strtouc("123", NULL, 10) == 123);
assert(_strtouc("123", NULL, 10) == 123);
/* proper functioning to smaller base */
assert(strtouc("14", NULL, 8) == 12);
assert(_strtouc("14", NULL, 8) == 12);
/* proper autodetecting of octal */
assert(strtouc("016", NULL, 0) == 14);
assert(_strtouc("016", NULL, 0) == 14);
/* proper autodetecting of hexadecimal, lowercase 'x' */
assert(strtouc("0xFF", NULL, 0) == 255);
assert(_strtouc("0xFF", NULL, 0) == 255);
/* proper autodetecting of hexadecimal, uppercase 'X' */
assert(strtouc("0Xa1", NULL, 0) == 161);
assert(_strtouc("0Xa1", NULL, 0) == 161);
/* proper handling of border case: 0x followed by non-hexdigit */
assert(strtouc(tricky, &endptr, 0) == 0);
assert(_strtouc(tricky, &endptr, 0) == 0);
assert(endptr == tricky + 2);
/* proper handling of border case: 0 followed by non-octdigit */
assert(strtouc(tricky, &endptr, 8) == 0);
assert(_strtouc(tricky, &endptr, 8) == 0);
assert(endptr == tricky + 2);
/* errno should still be 0 */
assert(errno == 0);
/* correctly decoding zero */
assert(strtouc("0", &endptr, 0) == 0);
assert(_strtouc("0", &endptr, 0) == 0);
assert(*endptr == '\0');
assert(errno == 0);
/* overflowing subject sequence must still return proper endptr */
assert(strtouc(overflow, &endptr, 36) == UCHAR_MAX);
assert(_strtouc(overflow, &endptr, 36) == UCHAR_MAX);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* same for positive */
errno = 0;
assert(strtouc(overflow + 1, &endptr, 36) == UCHAR_MAX);
assert(_strtouc(overflow + 1, &endptr, 36) == UCHAR_MAX);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* testing skipping of leading whitespace */
assert(strtouc(" \n\v\t\f123", NULL, 0) == 123);
assert(_strtouc(" \n\v\t\f123", NULL, 0) == 123);
/* testing conversion failure */
assert(strtouc(overflow, &endptr, 10) == 0);
assert(_strtouc(overflow, &endptr, 10) == 0);
assert(endptr == overflow);
endptr = NULL;
assert(strtouc(overflow, &endptr, 0) == 0);
assert(_strtouc(overflow, &endptr, 0) == 0);
assert(endptr == overflow);
/* TODO: These tests assume two-complement, but conversion should work */
/* for one-complement and signed magnitude just as well. Anyone having */
/* a platform to test this on? */
errno = 0;
/* long -> 32 bit */
/* testing "even" overflow, i.e. base is power of two */
assert(strtouc("255", NULL, 0) == UCHAR_MAX);
assert(_strtouc("255", NULL, 0) == UCHAR_MAX);
assert(errno == 0);
errno = 0;
assert(strtouc("256", NULL, 0) == UCHAR_MAX);
assert(_strtouc("256", NULL, 0) == UCHAR_MAX);
assert(errno == ERANGE);
/* TODO: test "odd" overflow, i.e. base is not power of two */
/* long -> 64 bit */
Expand Down
32 changes: 16 additions & 16 deletions SingleSource/UnitTests/strtoui.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,56 @@ int main(void) {
char tricky[] = "+0xz";
errno = 0;
/* basic functionality */
assert(strtoui("123", NULL, 10) == 123);
assert(_strtoui("123", NULL, 10) == 123);
/* proper detecting of default base 10 */
assert(strtoui("456", NULL, 0) == 456);
assert(_strtoui("456", NULL, 0) == 456);
/* proper functioning to smaller base */
assert(strtoui("14", NULL, 8) == 12);
assert(_strtoui("14", NULL, 8) == 12);
/* proper autodetecting of octal */
assert(strtoui("016", NULL, 0) == 14);
assert(_strtoui("016", NULL, 0) == 14);
/* proper autodetecting of hexadecimal, lowercase 'x' */
assert(strtoui("0xFF", NULL, 0) == 255);
assert(_strtoui("0xFF", NULL, 0) == 255);
/* proper autodetecting of hexadecimal, uppercase 'X' */
assert(strtoui("0Xa1", NULL, 0) == 161);
assert(_strtoui("0Xa1", NULL, 0) == 161);
/* proper handling of border case: 0x followed by non-hexdigit */
assert(strtoui(tricky, &endptr, 0) == 0);
assert(_strtoui(tricky, &endptr, 0) == 0);
assert(endptr == tricky + 2);
/* proper handling of border case: 0 followed by non-octdigit */
assert(strtoui(tricky, &endptr, 8) == 0);
assert(_strtoui(tricky, &endptr, 8) == 0);
assert(endptr == tricky + 2);
/* errno should still be 0 */
assert(errno == 0);
/* correctly decoding zero */
assert(strtoui("0", &endptr, 0) == 0);
assert(_strtoui("0", &endptr, 0) == 0);
assert(*endptr == '\0');
assert(errno == 0);
/* overflowing subject sequence must still return proper endptr */
assert(strtoui(overflow, &endptr, 36) == UINT_MAX);
assert(_strtoui(overflow, &endptr, 36) == UINT_MAX);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* same for positive */
errno = 0;
assert(strtoui(overflow + 1, &endptr, 36) == UINT_MAX);
assert(_strtoui(overflow + 1, &endptr, 36) == UINT_MAX);
assert(errno == ERANGE);
assert((endptr - overflow) == 53);
/* testing skipping of leading whitespace */
assert(strtoui(" \n\v\t\f789", NULL, 0) == 789);
assert(_strtoui(" \n\v\t\f789", NULL, 0) == 789);
/* testing conversion failure */
assert(strtoui(overflow, &endptr, 10) == 0);
assert(_strtoui(overflow, &endptr, 10) == 0);
assert(endptr == overflow);
endptr = NULL;
assert(strtoui(overflow, &endptr, 0) == 0);
assert(_strtoui(overflow, &endptr, 0) == 0);
assert(endptr == overflow);
/* TODO: These tests assume two-complement, but conversion should work */
/* for one-complement and signed magnitude just as well. Anyone having */
/* a platform to test this on? */
errno = 0;
/* long -> 16 bit */
/* testing "even" overflow, i.e. base is power of two */
assert(strtoui("65535", NULL, 0) == UINT_MAX);
assert(_strtoui("65535", NULL, 0) == UINT_MAX);
assert(errno == 0);
errno = 0;
assert(strtoui("65536", NULL, 0) == UINT_MAX);
assert(_strtoui("65536", NULL, 0) == UINT_MAX);
assert(errno == ERANGE);
return 0;
}

0 comments on commit 3ea0141

Please sign in to comment.