test_newlib.c 7.3 KB

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