test_newlib.c 7.1 KB

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