1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <ctype.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <sys/time.h>
9 #include "unity.h"
10 #include "sdkconfig.h"
11 #include "soc/soc.h"
12 
13 TEST_CASE("test ctype functions", "[newlib]")
14 {
15     TEST_ASSERT_TRUE( isalnum('a') && isalnum('A') && isalnum('z') && isalnum('Z') && isalnum('0') && isalnum('9') );
16     TEST_ASSERT_FALSE( isalnum('(') || isalnum('-') || isalnum(' ') || isalnum('\x81') || isalnum('.') || isalnum('\\') );
17     TEST_ASSERT_TRUE( isalpha('a') && isalpha('A') && isalpha('z') && isalpha('Z') );
18     TEST_ASSERT_FALSE( isalpha('0') || isalpha('9') || isalpha(')') || isalpha('\t') || isalpha(' ') || isalpha('\x81') );
19     TEST_ASSERT_TRUE( isspace(' ') && isspace('\t') && isspace('\n') && isspace('\r') );
20     TEST_ASSERT_FALSE( isspace('0') || isspace('9') || isspace(')') || isspace('A') || isspace('*') || isspace('\x81') || isspace('a'));
21 }
22 
23 TEST_CASE("test atoX functions", "[newlib]")
24 {
25     TEST_ASSERT_EQUAL_INT(-2147483648, atoi("-2147483648"));
26     TEST_ASSERT_EQUAL_INT(2147483647, atoi("2147483647"));
27     TEST_ASSERT_EQUAL_INT(42, atoi("000000042"));
28     TEST_ASSERT_EQUAL_INT(0, strtol("foo", NULL, 10));
29     TEST_ASSERT_EQUAL(0.123443, atof("0.123443"));
30     TEST_ASSERT_EQUAL(0.123443f, atoff("0.123443"));
31     TEST_ASSERT_EQUAL(31.41238, strtod("0.3141238e2", NULL));
32     TEST_ASSERT_EQUAL(0.025f, strtof("0.025", NULL));
33 }
34 
35 TEST_CASE("test sprintf function", "[newlib]")
36 {
37     char *res = NULL;
38     asprintf(&res, "%d %011i %lu %p %x %c %.4f\n", 42, 2147483647, 2147483648UL, (void *) 0x40010000, 0x40020000, 'Q', 1.0f / 137.0f);
39     TEST_ASSERT_NOT_NULL(res);
40     TEST_ASSERT_EQUAL_STRING(res, "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n");
41     free(res);
42 }
43 
44 TEST_CASE("test sscanf function", "[newlib]")
45 {
46     const char *src = "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n";
47     int fourty_two;
48     int int_max;
49     unsigned long int_max_plus_one;
50     void *iram_ptr;
51     int irom_ptr;
52     char department;
53     float inv_fine_structure_constant;
54     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);
55     TEST_ASSERT_EQUAL(7, res);
56     TEST_ASSERT_EQUAL(42, fourty_two);
57     TEST_ASSERT_EQUAL(2147483647, int_max);
58     TEST_ASSERT_EQUAL_UINT32(2147483648UL, int_max_plus_one);
59     TEST_ASSERT_EQUAL(0x40010000, iram_ptr);
60     TEST_ASSERT_EQUAL(0x40020000, irom_ptr);
61     TEST_ASSERT_EQUAL('Q', department);
62     TEST_ASSERT_TRUE(1.0f / inv_fine_structure_constant > 136 && 1.0f / inv_fine_structure_constant < 138);
63 }
64 
65 TEST_CASE("test time functions", "[newlib]")
66 {
67     time_t now = 1464248488;
68     setenv("TZ", "UTC-8", 1);
69     tzset();
70     struct tm *tm_utc = gmtime(&now);
71     TEST_ASSERT_EQUAL( 28, tm_utc->tm_sec);
72     TEST_ASSERT_EQUAL( 41, tm_utc->tm_min);
73     TEST_ASSERT_EQUAL(  7, tm_utc->tm_hour);
74     TEST_ASSERT_EQUAL( 26, tm_utc->tm_mday);
75     TEST_ASSERT_EQUAL(  4, tm_utc->tm_mon);
76     TEST_ASSERT_EQUAL(116, tm_utc->tm_year);
77     TEST_ASSERT_EQUAL(  4, tm_utc->tm_wday);
78     TEST_ASSERT_EQUAL(146, tm_utc->tm_yday);
79 
80     struct tm *tm_local = localtime(&now);
81     TEST_ASSERT_EQUAL( 28, tm_local->tm_sec);
82     TEST_ASSERT_EQUAL( 41, tm_local->tm_min);
83     TEST_ASSERT_EQUAL( 15, tm_local->tm_hour);
84     TEST_ASSERT_EQUAL( 26, tm_local->tm_mday);
85     TEST_ASSERT_EQUAL(  4, tm_local->tm_mon);
86     TEST_ASSERT_EQUAL(116, tm_local->tm_year);
87     TEST_ASSERT_EQUAL(  4, tm_local->tm_wday);
88     TEST_ASSERT_EQUAL(146, tm_local->tm_yday);
89 
90 }
91 
92 
93 TEST_CASE("test asctime", "[newlib]")
94 {
95     char buf[64];
96     struct tm tm = { 0 };
97     tm.tm_year = 2016 - 1900;
98     tm.tm_mon = 0;
99     tm.tm_mday = 10;
100     tm.tm_hour = 16;
101     tm.tm_min = 30;
102     tm.tm_sec = 0;
103     time_t t = mktime(&tm);
104     const char* time_str = asctime(&tm);
105     strlcpy(buf, time_str, sizeof(buf));
106     printf("Setting time: %s", time_str);
107     struct timeval now = { .tv_sec = t };
108     settimeofday(&now, NULL);
109 
110     struct timeval tv;
111     gettimeofday(&tv, NULL);
112     time_t mtime = tv.tv_sec;
113     struct tm mtm;
114     localtime_r(&mtime, &mtm);
115     time_str = asctime(&mtm);
116     printf("Got time: %s", time_str);
117     TEST_ASSERT_EQUAL_STRING(buf, time_str);
118 }
119 
fn_in_rom(void * fn)120 static bool fn_in_rom(void *fn)
121 {
122     const int fnaddr = (int)fn;
123     return (fnaddr >= SOC_IROM_MASK_LOW && fnaddr < SOC_IROM_MASK_HIGH);
124 }
125 
126 
127 TEST_CASE("check if ROM or Flash is used for functions", "[newlib]")
128 {
129 #if defined(CONFIG_NEWLIB_NANO_FORMAT)
130     TEST_ASSERT(fn_in_rom(vfprintf));
131 #else
132     TEST_ASSERT_FALSE(fn_in_rom(vfprintf));
133 #endif // CONFIG_NEWLIB_NANO_FORMAT
134 
135 #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_NEWLIB_NANO_FORMAT)
136     TEST_ASSERT(fn_in_rom(sscanf));
137 #else
138     TEST_ASSERT_FALSE(fn_in_rom(sscanf));
139 #endif // CONFIG_IDF_TARGET_ESP32 && CONFIG_NEWLIB_NANO_FORMAT
140 
141 #if defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_SPIRAM)
142     TEST_ASSERT(fn_in_rom(atoi));
143     TEST_ASSERT(fn_in_rom(strtol));
144 #elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C3)
145     /* S3 and C3 always use these from ROM */
146     TEST_ASSERT(fn_in_rom(atoi));
147     TEST_ASSERT(fn_in_rom(strtol));
148 #else
149     /* S2 do not have these in ROM */
150     TEST_ASSERT_FALSE(fn_in_rom(atoi));
151     TEST_ASSERT_FALSE(fn_in_rom(strtol));
152 #endif // defined(CONFIG_IDF_TARGET_ESP32) && !defined(CONFIG_SPIRAM)
153 }
154 
155 #ifndef CONFIG_NEWLIB_NANO_FORMAT
156 TEST_CASE("test 64bit int formats", "[newlib]")
157 {
158     char* res = NULL;
159     const uint64_t val = 123456789012LL;
160 
161     asprintf(&res, "%llu", val);
162     TEST_ASSERT_NOT_NULL(res);
163     TEST_ASSERT_EQUAL_STRING("123456789012", res);
164 
165     uint64_t sval;
166     int ret = sscanf(res, "%llu", &sval);
167     free(res);
168 
169     TEST_ASSERT_EQUAL(1, ret);
170     TEST_ASSERT_EQUAL(val, sval);
171 }
172 #else // CONFIG_NEWLIB_NANO_FORMAT
173 TEST_CASE("test 64bit int formats", "[newlib]")
174 {
175     char* res = NULL;
176     const uint64_t val = 123456789012LL;
177 
178     asprintf(&res, "%llu", val);
179     TEST_ASSERT_NOT_NULL(res);
180     TEST_ASSERT_EQUAL_STRING("lu", res);
181 
182     uint64_t sval;
183     int ret = sscanf(res, "%llu", &sval);
184     free(res);
185 
186     TEST_ASSERT_EQUAL(0, ret);
187 }
188 #endif // CONFIG_NEWLIB_NANO_FORMAT
189 
190 
191 TEST_CASE("fmod and fmodf work as expected", "[newlib]")
192 {
193     TEST_ASSERT_EQUAL(0.1, fmod(10.1, 2.0));
194     TEST_ASSERT_EQUAL(0.1f, fmodf(10.1f, 2.0f));
195 }
196 
197 TEST_CASE("newlib: can link 'system', 'raise'", "[newlib]")
198 {
199     printf("system: %p, raise: %p\n", &system, &raise);
200 }
201 
202 
203 TEST_CASE("newlib: rom and toolchain localtime func gives the same result", "[newlib]")
204 {
205     // This UNIX time represents 2020-03-12 15:00:00 EDT (19:00 GMT)
206     // as can be verified with 'date --date @1584039600'
207     const time_t seconds = 1584039600;
208     setenv("TZ", "EST5EDT,M3.2.0,M11.1.0", 1); // America/New_York
209     tzset();
210     struct tm *tm = localtime(&seconds);
211     tm->tm_isdst = 1;
212     static char buf[32];
213     strftime(buf, sizeof(buf), "%F %T %Z", tm);
214     static char test_result[64];
215     sprintf(test_result, "%s (tm_isdst = %d)", buf, tm->tm_isdst);
216     printf("%s\n", test_result);
217     TEST_ASSERT_EQUAL_STRING("2020-03-12 15:00:00 EDT (tm_isdst = 1)", test_result);
218 }
219