test_newlib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. TEST_CASE("test ctype functions", "[newlib]")
  12. {
  13. TEST_ASSERT_TRUE( isalnum('a') && isalnum('A') && isalnum('z') && isalnum('Z') && isalnum('0') && isalnum('9') );
  14. TEST_ASSERT_FALSE( isalnum('(') || isalnum('-') || isalnum(' ') || isalnum('\x81') || isalnum('.') || isalnum('\\') );
  15. TEST_ASSERT_TRUE( isalpha('a') && isalpha('A') && isalpha('z') && isalpha('Z') );
  16. TEST_ASSERT_FALSE( isalpha('0') || isalpha('9') || isalpha(')') || isalpha('\t') || isalpha(' ') || isalpha('\x81') );
  17. TEST_ASSERT_TRUE( isspace(' ') && isspace('\t') && isspace('\n') && isspace('\r') );
  18. TEST_ASSERT_FALSE( isspace('0') || isspace('9') || isspace(')') || isspace('A') || isspace('*') || isspace('\x81') || isspace('a'));
  19. }
  20. TEST_CASE("test atoX functions", "[newlib][ignore]")
  21. {
  22. TEST_ASSERT_EQUAL_INT(-2147483648, atoi("-2147483648"));
  23. TEST_ASSERT_EQUAL_INT(2147483647, atoi("2147483647"));
  24. TEST_ASSERT_EQUAL_INT(42, atoi("000000042"));
  25. TEST_ASSERT_EQUAL_INT(0, strtol("foo", NULL, 10));
  26. TEST_ASSERT_EQUAL(0.123443, atof("0.123443"));
  27. TEST_ASSERT_EQUAL(0.123443f, atoff("0.123443"));
  28. TEST_ASSERT_EQUAL(31.41238, strtod("0.3141238e2", NULL));
  29. TEST_ASSERT_EQUAL(0.025f, strtof("0.025", NULL));
  30. }
  31. TEST_CASE("test sprintf function", "[newlib]")
  32. {
  33. char *res = NULL;
  34. asprintf(&res, "%d %011i %lu %p %x %c %.4f\n", 42, 2147483647, 2147483648UL, (void *) 0x40010000, 0x40020000, 'Q', 1.0f / 137.0f);
  35. TEST_ASSERT_NOT_NULL(res);
  36. TEST_ASSERT_EQUAL_STRING(res, "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n");
  37. free(res);
  38. }
  39. TEST_CASE("test sscanf function", "[newlib]")
  40. {
  41. const char *src = "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n";
  42. int fourty_two;
  43. int int_max;
  44. unsigned long int_max_plus_one;
  45. void *iram_ptr;
  46. int irom_ptr;
  47. char department;
  48. float inv_fine_structure_constant;
  49. 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);
  50. TEST_ASSERT_EQUAL(7, res);
  51. TEST_ASSERT_EQUAL(42, fourty_two);
  52. TEST_ASSERT_EQUAL(2147483647, int_max);
  53. TEST_ASSERT_EQUAL_UINT32(2147483648UL, int_max_plus_one);
  54. TEST_ASSERT_EQUAL(0x40010000, iram_ptr);
  55. TEST_ASSERT_EQUAL(0x40020000, irom_ptr);
  56. TEST_ASSERT_EQUAL('Q', department);
  57. TEST_ASSERT_TRUE(1.0f / inv_fine_structure_constant > 136 && 1.0f / inv_fine_structure_constant < 138);
  58. }
  59. TEST_CASE("test time functions", "[newlib]")
  60. {
  61. time_t now = 1464248488;
  62. setenv("TZ", "UTC-8", 1);
  63. tzset();
  64. struct tm *tm_utc = gmtime(&now);
  65. TEST_ASSERT_EQUAL( 28, tm_utc->tm_sec);
  66. TEST_ASSERT_EQUAL( 41, tm_utc->tm_min);
  67. TEST_ASSERT_EQUAL( 7, tm_utc->tm_hour);
  68. TEST_ASSERT_EQUAL( 26, tm_utc->tm_mday);
  69. TEST_ASSERT_EQUAL( 4, tm_utc->tm_mon);
  70. TEST_ASSERT_EQUAL(116, tm_utc->tm_year);
  71. TEST_ASSERT_EQUAL( 4, tm_utc->tm_wday);
  72. TEST_ASSERT_EQUAL(146, tm_utc->tm_yday);
  73. struct tm *tm_local = localtime(&now);
  74. TEST_ASSERT_EQUAL( 28, tm_local->tm_sec);
  75. TEST_ASSERT_EQUAL( 41, tm_local->tm_min);
  76. TEST_ASSERT_EQUAL( 15, tm_local->tm_hour);
  77. TEST_ASSERT_EQUAL( 26, tm_local->tm_mday);
  78. TEST_ASSERT_EQUAL( 4, tm_local->tm_mon);
  79. TEST_ASSERT_EQUAL(116, tm_local->tm_year);
  80. TEST_ASSERT_EQUAL( 4, tm_local->tm_wday);
  81. TEST_ASSERT_EQUAL(146, tm_local->tm_yday);
  82. }
  83. TEST_CASE("test asctime", "[newlib]")
  84. {
  85. char buf[64];
  86. struct tm tm = { 0 };
  87. tm.tm_year = 2016 - 1900;
  88. tm.tm_mon = 0;
  89. tm.tm_mday = 10;
  90. tm.tm_hour = 16;
  91. tm.tm_min = 30;
  92. tm.tm_sec = 0;
  93. time_t t = mktime(&tm);
  94. const char* time_str = asctime(&tm);
  95. strlcpy(buf, time_str, sizeof(buf));
  96. printf("Setting time: %s", time_str);
  97. struct timeval now = { .tv_sec = t };
  98. settimeofday(&now, NULL);
  99. struct timeval tv;
  100. gettimeofday(&tv, NULL);
  101. time_t mtime = tv.tv_sec;
  102. struct tm mtm;
  103. localtime_r(&mtime, &mtm);
  104. time_str = asctime(&mtm);
  105. printf("Got time: %s", time_str);
  106. TEST_ASSERT_EQUAL_STRING(buf, time_str);
  107. }
  108. static bool fn_in_rom(void *fn, const char *name)
  109. {
  110. const int fnaddr = (int)fn;
  111. return (fnaddr >= 0x40000000) && (fnaddr < 0x40070000);
  112. }
  113. TEST_CASE("check if ROM or Flash is used for functions", "[newlib]")
  114. {
  115. #if defined(CONFIG_NEWLIB_NANO_FORMAT) && !defined(CONFIG_SPIRAM)
  116. TEST_ASSERT(fn_in_rom(printf, "printf"));
  117. TEST_ASSERT(fn_in_rom(sscanf, "sscanf"));
  118. #else
  119. TEST_ASSERT_FALSE(fn_in_rom(printf, "printf"));
  120. TEST_ASSERT_FALSE(fn_in_rom(sscanf, "sscanf"));
  121. #endif
  122. #if !defined(CONFIG_SPIRAM)
  123. TEST_ASSERT(fn_in_rom(atoi, "atoi"));
  124. TEST_ASSERT(fn_in_rom(strtol, "strtol"));
  125. #else
  126. TEST_ASSERT_FALSE(fn_in_rom(atoi, "atoi"));
  127. TEST_ASSERT_FALSE(fn_in_rom(strtol, "strtol"));
  128. #endif
  129. }
  130. #ifndef CONFIG_NEWLIB_NANO_FORMAT
  131. TEST_CASE("test 64bit int formats", "[newlib]")
  132. {
  133. char* res = NULL;
  134. const uint64_t val = 123456789012LL;
  135. asprintf(&res, "%llu", val);
  136. TEST_ASSERT_NOT_NULL(res);
  137. TEST_ASSERT_EQUAL_STRING("123456789012", res);
  138. uint64_t sval;
  139. int ret = sscanf(res, "%llu", &sval);
  140. free(res);
  141. TEST_ASSERT_EQUAL(1, ret);
  142. TEST_ASSERT_EQUAL(val, sval);
  143. }
  144. #else
  145. TEST_CASE("test 64bit int formats", "[newlib][ignore]")
  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("lu", res);
  152. uint64_t sval;
  153. int ret = sscanf(res, "%llu", &sval);
  154. free(res);
  155. TEST_ASSERT_EQUAL(0, ret);
  156. }
  157. #endif
  158. TEST_CASE("fmod and fmodf work as expected", "[newlib]")
  159. {
  160. TEST_ASSERT_EQUAL(0.1, fmod(10.1, 2.0));
  161. TEST_ASSERT_EQUAL(0.1f, fmodf(10.1f, 2.0f));
  162. }