2016-11-17 03:36:10 -05:00
|
|
|
#include <stdio.h>
|
2016-11-07 05:43:29 -05:00
|
|
|
#include <stdbool.h>
|
2016-11-17 03:36:10 -05:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2017-01-08 16:54:04 -05:00
|
|
|
#include <string.h>
|
2016-11-17 03:36:10 -05:00
|
|
|
#include <time.h>
|
2017-01-08 16:54:04 -05:00
|
|
|
#include <sys/time.h>
|
2016-11-17 03:36:10 -05:00
|
|
|
#include "unity.h"
|
2016-11-07 05:43:29 -05:00
|
|
|
#include "sdkconfig.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
TEST_CASE("test ctype functions", "[newlib]")
|
|
|
|
{
|
|
|
|
TEST_ASSERT_TRUE( isalnum('a') && isalnum('A') && isalnum('z') && isalnum('Z') && isalnum('0') && isalnum('9') );
|
|
|
|
TEST_ASSERT_FALSE( isalnum('(') || isalnum('-') || isalnum(' ') || isalnum('\x81') || isalnum('.') || isalnum('\\') );
|
|
|
|
TEST_ASSERT_TRUE( isalpha('a') && isalpha('A') && isalpha('z') && isalpha('Z') );
|
|
|
|
TEST_ASSERT_FALSE( isalpha('0') || isalpha('9') || isalpha(')') || isalpha('\t') || isalpha(' ') || isalpha('\x81') );
|
|
|
|
TEST_ASSERT_TRUE( isspace(' ') && isspace('\t') && isspace('\n') && isspace('\r') );
|
|
|
|
TEST_ASSERT_FALSE( isspace('0') || isspace('9') || isspace(')') || isspace('A') || isspace('*') || isspace('\x81') || isspace('a'));
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("test atoX functions", "[newlib][ignore]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL_INT(-2147483648, atoi("-2147483648"));
|
|
|
|
TEST_ASSERT_EQUAL_INT(2147483647, atoi("2147483647"));
|
|
|
|
TEST_ASSERT_EQUAL_INT(42, atoi("000000042"));
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, strtol("foo", NULL, 10));
|
2016-11-07 05:43:29 -05:00
|
|
|
TEST_ASSERT_EQUAL(0.123443, atof("0.123443"));
|
|
|
|
TEST_ASSERT_EQUAL(0.123443f, atoff("0.123443"));
|
|
|
|
TEST_ASSERT_EQUAL(31.41238, strtod("0.3141238e2", NULL));
|
|
|
|
TEST_ASSERT_EQUAL(0.025f, strtof("0.025", NULL));
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("test sprintf function", "[newlib]")
|
|
|
|
{
|
|
|
|
char *res = NULL;
|
|
|
|
asprintf(&res, "%d %011i %lu %p %x %c %.4f\n", 42, 2147483647, 2147483648UL, (void *) 0x40010000, 0x40020000, 'Q', 1.0f / 137.0f);
|
|
|
|
TEST_ASSERT_NOT_NULL(res);
|
|
|
|
TEST_ASSERT_EQUAL_STRING(res, "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n");
|
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("test sscanf function", "[newlib]")
|
|
|
|
{
|
|
|
|
const char *src = "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n";
|
|
|
|
int fourty_two;
|
|
|
|
int int_max;
|
|
|
|
unsigned long int_max_plus_one;
|
|
|
|
void *iram_ptr;
|
|
|
|
int irom_ptr;
|
|
|
|
char department;
|
|
|
|
float inv_fine_structure_constant;
|
|
|
|
int res = sscanf(src, "%d %d %lu %p %x %c %f", &fourty_two, &int_max, &int_max_plus_one, &iram_ptr, &irom_ptr, &department, &inv_fine_structure_constant);
|
|
|
|
TEST_ASSERT_EQUAL(7, res);
|
|
|
|
TEST_ASSERT_EQUAL(42, fourty_two);
|
|
|
|
TEST_ASSERT_EQUAL(2147483647, int_max);
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(2147483648UL, int_max_plus_one);
|
|
|
|
TEST_ASSERT_EQUAL(0x40010000, iram_ptr);
|
|
|
|
TEST_ASSERT_EQUAL(0x40020000, irom_ptr);
|
|
|
|
TEST_ASSERT_EQUAL('Q', department);
|
|
|
|
TEST_ASSERT_TRUE(1.0f / inv_fine_structure_constant > 136 && 1.0f / inv_fine_structure_constant < 138);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("test time functions", "[newlib]")
|
|
|
|
{
|
|
|
|
time_t now = 1464248488;
|
|
|
|
setenv("TZ", "UTC-8", 1);
|
2019-03-29 00:38:06 -04:00
|
|
|
tzset();
|
2016-11-17 03:36:10 -05:00
|
|
|
struct tm *tm_utc = gmtime(&now);
|
|
|
|
TEST_ASSERT_EQUAL( 28, tm_utc->tm_sec);
|
|
|
|
TEST_ASSERT_EQUAL( 41, tm_utc->tm_min);
|
|
|
|
TEST_ASSERT_EQUAL( 7, tm_utc->tm_hour);
|
|
|
|
TEST_ASSERT_EQUAL( 26, tm_utc->tm_mday);
|
|
|
|
TEST_ASSERT_EQUAL( 4, tm_utc->tm_mon);
|
|
|
|
TEST_ASSERT_EQUAL(116, tm_utc->tm_year);
|
|
|
|
TEST_ASSERT_EQUAL( 4, tm_utc->tm_wday);
|
|
|
|
TEST_ASSERT_EQUAL(146, tm_utc->tm_yday);
|
|
|
|
|
|
|
|
struct tm *tm_local = localtime(&now);
|
|
|
|
TEST_ASSERT_EQUAL( 28, tm_local->tm_sec);
|
|
|
|
TEST_ASSERT_EQUAL( 41, tm_local->tm_min);
|
|
|
|
TEST_ASSERT_EQUAL( 15, tm_local->tm_hour);
|
|
|
|
TEST_ASSERT_EQUAL( 26, tm_local->tm_mday);
|
|
|
|
TEST_ASSERT_EQUAL( 4, tm_local->tm_mon);
|
|
|
|
TEST_ASSERT_EQUAL(116, tm_local->tm_year);
|
|
|
|
TEST_ASSERT_EQUAL( 4, tm_local->tm_wday);
|
|
|
|
TEST_ASSERT_EQUAL(146, tm_local->tm_yday);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-08 16:54:04 -05:00
|
|
|
TEST_CASE("test asctime", "[newlib]")
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
struct tm tm = { 0 };
|
|
|
|
tm.tm_year = 2016 - 1900;
|
|
|
|
tm.tm_mon = 0;
|
|
|
|
tm.tm_mday = 10;
|
|
|
|
tm.tm_hour = 16;
|
|
|
|
tm.tm_min = 30;
|
|
|
|
tm.tm_sec = 0;
|
|
|
|
time_t t = mktime(&tm);
|
|
|
|
const char* time_str = asctime(&tm);
|
|
|
|
strlcpy(buf, time_str, sizeof(buf));
|
|
|
|
printf("Setting time: %s", time_str);
|
|
|
|
struct timeval now = { .tv_sec = t };
|
|
|
|
settimeofday(&now, NULL);
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
time_t mtime = tv.tv_sec;
|
|
|
|
struct tm mtm;
|
|
|
|
localtime_r(&mtime, &mtm);
|
|
|
|
time_str = asctime(&mtm);
|
|
|
|
printf("Got time: %s", time_str);
|
|
|
|
TEST_ASSERT_EQUAL_STRING(buf, time_str);
|
|
|
|
}
|
|
|
|
|
2018-04-09 06:58:35 -04:00
|
|
|
static bool fn_in_rom(void *fn, const char *name)
|
2016-11-07 05:43:29 -05:00
|
|
|
{
|
|
|
|
const int fnaddr = (int)fn;
|
|
|
|
return (fnaddr >= 0x40000000) && (fnaddr < 0x40070000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("check if ROM or Flash is used for functions", "[newlib]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2019-06-05 00:34:19 -04:00
|
|
|
#if defined(CONFIG_NEWLIB_NANO_FORMAT) && !defined(CONFIG_SPIRAM)
|
2016-11-07 05:43:29 -05:00
|
|
|
TEST_ASSERT(fn_in_rom(printf, "printf"));
|
|
|
|
TEST_ASSERT(fn_in_rom(sscanf, "sscanf"));
|
|
|
|
#else
|
|
|
|
TEST_ASSERT_FALSE(fn_in_rom(printf, "printf"));
|
|
|
|
TEST_ASSERT_FALSE(fn_in_rom(sscanf, "sscanf"));
|
|
|
|
#endif
|
2019-06-05 00:34:19 -04:00
|
|
|
#if !defined(CONFIG_SPIRAM)
|
2016-11-07 05:43:29 -05:00
|
|
|
TEST_ASSERT(fn_in_rom(atoi, "atoi"));
|
|
|
|
TEST_ASSERT(fn_in_rom(strtol, "strtol"));
|
2018-06-27 02:47:05 -04:00
|
|
|
#else
|
|
|
|
TEST_ASSERT_FALSE(fn_in_rom(atoi, "atoi"));
|
|
|
|
TEST_ASSERT_FALSE(fn_in_rom(strtol, "strtol"));
|
|
|
|
#endif
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2016-11-07 05:43:29 -05:00
|
|
|
#ifndef CONFIG_NEWLIB_NANO_FORMAT
|
|
|
|
TEST_CASE("test 64bit int formats", "[newlib]")
|
|
|
|
{
|
|
|
|
char* res = NULL;
|
|
|
|
const uint64_t val = 123456789012LL;
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2016-11-07 05:43:29 -05:00
|
|
|
asprintf(&res, "%llu", val);
|
|
|
|
TEST_ASSERT_NOT_NULL(res);
|
|
|
|
TEST_ASSERT_EQUAL_STRING("123456789012", res);
|
|
|
|
|
|
|
|
uint64_t sval;
|
|
|
|
int ret = sscanf(res, "%llu", &sval);
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL(1, ret);
|
|
|
|
TEST_ASSERT_EQUAL(val, sval);
|
|
|
|
}
|
|
|
|
#else
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("test 64bit int formats", "[newlib][ignore]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2016-11-07 05:43:29 -05:00
|
|
|
char* res = NULL;
|
|
|
|
const uint64_t val = 123456789012LL;
|
|
|
|
|
|
|
|
asprintf(&res, "%llu", val);
|
|
|
|
TEST_ASSERT_NOT_NULL(res);
|
|
|
|
TEST_ASSERT_EQUAL_STRING("lu", res);
|
|
|
|
|
|
|
|
uint64_t sval;
|
|
|
|
int ret = sscanf(res, "%llu", &sval);
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL(0, ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("fmod and fmodf work as expected", "[newlib]")
|
|
|
|
{
|
|
|
|
TEST_ASSERT_EQUAL(0.1, fmod(10.1, 2.0));
|
|
|
|
TEST_ASSERT_EQUAL(0.1f, fmodf(10.1f, 2.0f));
|
|
|
|
}
|
2019-12-01 13:20:52 -05:00
|
|
|
|
|
|
|
TEST_CASE("newlib: can link 'system', 'raise'", "[newlib]")
|
|
|
|
{
|
|
|
|
printf("system: %p, raise: %p\n", &system, &raise);
|
|
|
|
}
|
2020-03-17 05:02:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("newlib: rom and toolchain localtime func gives the same result", "[newlib]")
|
|
|
|
{
|
|
|
|
// This UNIX time represents 2020-03-12 15:00:00 EDT (19:00 GMT)
|
|
|
|
// as can be verified with 'date --date @1584039600'
|
|
|
|
const time_t seconds = 1584039600;
|
|
|
|
setenv("TZ", "EST5EDT,M3.2.0,M11.1.0", 1); // America/New_York
|
|
|
|
tzset();
|
|
|
|
struct tm *tm = localtime(&seconds);
|
|
|
|
tm->tm_isdst = 1;
|
|
|
|
static char buf[32];
|
|
|
|
strftime(buf, sizeof(buf), "%F %T %Z", tm);
|
|
|
|
static char test_result[64];
|
|
|
|
sprintf(test_result, "%s (tm_isdst = %d)", buf, tm->tm_isdst);
|
|
|
|
printf("%s\n", test_result);
|
|
|
|
TEST_ASSERT_EQUAL_STRING("2020-03-12 15:00:00 EDT (tm_isdst = 1)", test_result);
|
|
|
|
}
|