test_newlib.c 7.6 KB

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