Skip to content

Commit

Permalink
corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-bloch committed Jun 27, 2023
1 parent 00cd7c7 commit 54998dd
Showing 1 changed file with 100 additions and 60 deletions.
160 changes: 100 additions & 60 deletions libc/stdlib/stdlib_strto.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,6 @@
#include <unity_fixture.h>


static const char hugeDoubleString[] =
"17976931348623157081452742373170435679807056752584499659891747680315"
"72607800285387605895586327668781715404589535143824642343213268894641"
"82768467546703537516986049910576551282076245490090389328944075868508"
"45513394230458323690322294816580855933212334827479782620414472316873"
"817718091929988125040402618412485836.1797693134862315708145274237317"
"04356798070567525844996598917476803157260780028538760589558632766878"
"17154045895351438246423432132688946418276846754670353751698604991057"
"65512820762454900903893289440758685084551339423045832369032229481658"
"08559332123348274797826204144723168738177180919299881250404026184124"
"85836179769313486231570814527423731704356798070567525844996598917476"
"80315726078002853876058955863276687817154045895351438246423432132688"
"94641827684675467035375169860499105765512820762454900903893289440758"
"68508455133942304583236903222948165808559332123348274797826204144723"
"16873817718091929988125040402618412485836179769313486231570814527423"
"73170435679807056752584499659891747680315726078002853876058955863276"
"68781715404589535143824642343213268894641827684675467035375169860499"
"10576551282076245490090389328944075868508455133942304583236903222948"
"16580855933212334827479782620414472316873817718091929988125040402618"
"41248583617976931348623157081452742373170435679807056752584499659891"
"74768031572607800285387605895586327668781715404589535143824642343213";

static const double hugeDouble = 17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836.1797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040261841248583617976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858361797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213;

TEST_GROUP(stdlib_strto);


Expand Down Expand Up @@ -116,10 +92,6 @@ TEST(stdlib_strto, strtod_min_max)
TEST_ASSERT_EQUAL_DOUBLE(DBL_MAX, strtod("1.797693134862315e+308", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(hugeDouble, strtod(hugeDoubleString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

/* Disabled because of #703 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/703 */
#ifdef __phoenix__
TEST_IGNORE_MESSAGE("#703 issue");
Expand Down Expand Up @@ -175,25 +147,14 @@ TEST(stdlib_strto, strtof_basic_hex)

TEST(stdlib_strto, strtof_min_max)
{
const char floatMinString[] = "0.000000000000000000000000000000000000011754943508222875";
const char floatMaxString[] = "340282346638528859811704183484516925440.000000";

errno = 0;
TEST_ASSERT_EQUAL_FLOAT(FLT_MIN, strtof("1.17549435e-38", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_FLOAT(FLT_MIN, strtof(floatMinString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_FLOAT(FLT_MAX, strtof("3.40282347e+38", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_FLOAT(FLT_MAX, strtof(floatMaxString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

/* Disabled because of #703 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/703 */
#ifdef __phoenix__
TEST_IGNORE_MESSAGE("#703 issue");
Expand Down Expand Up @@ -249,43 +210,119 @@ TEST(stdlib_strto, strtold_basic_hex)

TEST(stdlib_strto, strtold_min_max)
{
/*
* Phoenix-RTOS does not currently support long double numbers.
* Consequently, long doubles are not being tested, instead, for this case, we have decided to test doubles
* https://github.com/phoenix-rtos/phoenix-rtos-tests/issues/219
*/
errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(DBL_MIN, strtold("2.2250738585072013e-308", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(hugeDouble, strtold(hugeDoubleString, NULL));
TEST_ASSERT_EQUAL_DOUBLE(DBL_MAX, strtold("1.797693134862315e+308", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

/* LDBL_MIN_MAX not declared on Phoeix_RTOS */
/* Disabled because of #703 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/703 */
#ifdef __phoenix__
TEST_IGNORE();
TEST_IGNORE_MESSAGE("#703 issue");
#endif

char str[50];
errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(DBL_MIN, strtold("0x1p-1022", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
sprintf(str, "%Le", LDBL_MIN);
TEST_ASSERT_EQUAL_DOUBLE(LDBL_MIN, strtold(str, NULL));
/* Problem on Ubuntu (host-generic) */
#ifdef __phoenix__
TEST_ASSERT_EQUAL_DOUBLE(DBL_MAX, strtold("0x1.fffffffffffffp+1023", NULL));
TEST_ASSERT_EQUAL_INT(0, errno);
}


TEST(stdlib_strto, flt_dec_notation)
{
const char floatMinString[] = "0.000000000000000000000000000000000000011754943508222875";
const char floatMaxString[] = "340282346638528859811704183484516925440.000000";

const char numString[] = "340282346638528859811704183484516925440.3402823466385288598117041834845169254401175494350822287575";

const float num = 340282346638528859811704183484516925440.3402823466385288598117041834845169254401175494350822287575;
const float num1 = 11704183484516925440.3402823466385288598117041834845169254401175494350822287575;

errno = 0;
TEST_ASSERT_EQUAL_FLOAT(FLT_MIN, strtof(floatMinString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);
#endif

errno = 0;
sprintf(str, "%Le", LDBL_MAX);
TEST_ASSERT_EQUAL_DOUBLE(LDBL_MAX, strtold(str, NULL));
TEST_ASSERT_EQUAL_FLOAT(FLT_MAX, strtof(floatMaxString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

/* Disabled because of #703 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/703 */
#ifdef __phoenix__
TEST_IGNORE_MESSAGE("#703 issue");
#endif
errno = 0;
TEST_ASSERT_EQUAL_FLOAT(num, strtof(numString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_FLOAT(num1, strtof(&numString[19], NULL));
TEST_ASSERT_EQUAL_INT(0, errno);
}


TEST(stdlib_strto, dbl_dec_notation)
{

const char numString[] =
"17976931348623157081452742373170435679807056752584499659891747680315"
"72607800285387605895586327668781715404589535143824642343213268894641"
"82768467546703537516986049910576551282076245490090389328944075868508"
"45513394230458323690322294816580855933212334827479782620414472316873"
"817718091929988125040402618412485836.1797693134862315708145274237317"
"04356798070567525844996598917476803157260780028538760589558632766878"
"17154045895351438246423432132688946418276846754670353751698604991057"
"65512820762454900903893289440758685084551339423045832369032229481658"
"08559332123348274797826204144723168738177180919299881250404026184124"
"85836179769313486231570814527423731704356798070567525844996598917476"
"80315726078002853876058955863276687817154045895351438246423432132688"
"94641827684675467035375169860499105765512820762454900903893289440758"
"68508455133942304583236903222948165808559332123348274797826204144723"
"16873817718091929988125040402618412485836179769313486231570814527423"
"73170435679807056752584499659891747680315726078002853876058955863276"
"68781715404589535143824642343213268894641827684675467035375169860499"
"10576551282076245490090389328944075868508455133942304583236903222948"
"16580855933212334827479782620414472316873817718091929988125040402618"
"41248583617976931348623157081452742373170435679807056752584499659891"
"74768031572607800285387605895586327668781715404589535143824642343213";

char buff[310] = { 0 };

const double num = 17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836.1797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040261841248583617976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858361797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213;

const double num1 = 8125040402618412485836.1797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040261841248583617976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858361797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213;

const double num2 = 17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836.17976931348623157081452742373;

errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(num, strtod(numString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(num, strtold(numString, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
sprintf(str, "%La", LDBL_MIN);
TEST_ASSERT_EQUAL_DOUBLE(LDBL_MIN, strtold(str, NULL));
TEST_ASSERT_EQUAL_DOUBLE(num1, strtod(&numString[286], NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
sprintf(str, "%La", LDBL_MAX);
TEST_ASSERT_EQUAL_DOUBLE(LDBL_MAX, strtold(str, NULL));
TEST_ASSERT_EQUAL_DOUBLE(num1, strtold(&numString[286], NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

memcpy(buff, numString, 308);

errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(num2, strtod(buff, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);

errno = 0;
TEST_ASSERT_EQUAL_DOUBLE(num2, strtold(buff, NULL));
TEST_ASSERT_EQUAL_INT(0, errno);
}

Expand Down Expand Up @@ -845,8 +882,8 @@ TEST(stdlib_strto, truncate)

TEST(stdlib_strto, truncate_whitespaces)
{
const char *str_int[] = { "\t123", "\v123", "\f123", "\n123", "\r123", "\r\n123", " 123" };
const char *str_float[] = { "\t1.23", "\v1.23", "\f1.23", "\n1.23", "\r1.23", "\r\n1.23", " 1.23" };
const char *str_int[] = { "\t123", "\v123", "\f123", "\n123", "\r123", "\r\n123", " 123", "\t\v\f\n\r123" };
const char *str_float[] = { "\t1.23", "\v1.23", "\f1.23", "\n1.23", "\r1.23", "\r\n1.23", " 1.23", "\t\v\f\n\r1.23" };

for (int i = 0; i < (sizeof(str_int) / sizeof(str_int[0])); i++) {
errno = 0;
Expand Down Expand Up @@ -1083,6 +1120,9 @@ TEST_GROUP_RUNNER(stdlib_strto)
RUN_TEST_CASE(stdlib_strto, strtold_basic_hex);
RUN_TEST_CASE(stdlib_strto, strtold_min_max);

RUN_TEST_CASE(stdlib_strto, flt_dec_notation);
RUN_TEST_CASE(stdlib_strto, dbl_dec_notation);

RUN_TEST_CASE(stdlib_strto, strtol_basic);
RUN_TEST_CASE(stdlib_strto, strtol_basic_binary);
RUN_TEST_CASE(stdlib_strto, strtol_basic_hex);
Expand Down

0 comments on commit 54998dd

Please sign in to comment.