test_newlib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. struct tm *tm_utc = gmtime(&now);
  64. TEST_ASSERT_EQUAL( 28, tm_utc->tm_sec);
  65. TEST_ASSERT_EQUAL( 41, tm_utc->tm_min);
  66. TEST_ASSERT_EQUAL( 7, tm_utc->tm_hour);
  67. TEST_ASSERT_EQUAL( 26, tm_utc->tm_mday);
  68. TEST_ASSERT_EQUAL( 4, tm_utc->tm_mon);
  69. TEST_ASSERT_EQUAL(116, tm_utc->tm_year);
  70. TEST_ASSERT_EQUAL( 4, tm_utc->tm_wday);
  71. TEST_ASSERT_EQUAL(146, tm_utc->tm_yday);
  72. struct tm *tm_local = localtime(&now);
  73. TEST_ASSERT_EQUAL( 28, tm_local->tm_sec);
  74. TEST_ASSERT_EQUAL( 41, tm_local->tm_min);
  75. TEST_ASSERT_EQUAL( 15, tm_local->tm_hour);
  76. TEST_ASSERT_EQUAL( 26, tm_local->tm_mday);
  77. TEST_ASSERT_EQUAL( 4, tm_local->tm_mon);
  78. TEST_ASSERT_EQUAL(116, tm_local->tm_year);
  79. TEST_ASSERT_EQUAL( 4, tm_local->tm_wday);
  80. TEST_ASSERT_EQUAL(146, tm_local->tm_yday);
  81. }
  82. TEST_CASE("test asctime", "[newlib]")
  83. {
  84. char buf[64];
  85. struct tm tm = { 0 };
  86. tm.tm_year = 2016 - 1900;
  87. tm.tm_mon = 0;
  88. tm.tm_mday = 10;
  89. tm.tm_hour = 16;
  90. tm.tm_min = 30;
  91. tm.tm_sec = 0;
  92. time_t t = mktime(&tm);
  93. const char* time_str = asctime(&tm);
  94. strlcpy(buf, time_str, sizeof(buf));
  95. printf("Setting time: %s", time_str);
  96. struct timeval now = { .tv_sec = t };
  97. settimeofday(&now, NULL);
  98. struct timeval tv;
  99. gettimeofday(&tv, NULL);
  100. time_t mtime = tv.tv_sec;
  101. struct tm mtm;
  102. localtime_r(&mtime, &mtm);
  103. time_str = asctime(&mtm);
  104. printf("Got time: %s", time_str);
  105. TEST_ASSERT_EQUAL_STRING(buf, time_str);
  106. }
  107. static bool fn_in_rom(void *fn, char *name)
  108. {
  109. const int fnaddr = (int)fn;
  110. return (fnaddr >= 0x40000000) && (fnaddr < 0x40070000);
  111. }
  112. TEST_CASE("check if ROM or Flash is used for functions", "[newlib]")
  113. {
  114. #ifdef CONFIG_NEWLIB_NANO_FORMAT
  115. TEST_ASSERT(fn_in_rom(printf, "printf"));
  116. TEST_ASSERT(fn_in_rom(sscanf, "sscanf"));
  117. #else
  118. TEST_ASSERT_FALSE(fn_in_rom(printf, "printf"));
  119. TEST_ASSERT_FALSE(fn_in_rom(sscanf, "sscanf"));
  120. #endif
  121. TEST_ASSERT(fn_in_rom(atoi, "atoi"));
  122. TEST_ASSERT(fn_in_rom(strtol, "strtol"));
  123. }
  124. #ifndef CONFIG_NEWLIB_NANO_FORMAT
  125. TEST_CASE("test 64bit int formats", "[newlib]")
  126. {
  127. char* res = NULL;
  128. const uint64_t val = 123456789012LL;
  129. asprintf(&res, "%llu", val);
  130. TEST_ASSERT_NOT_NULL(res);
  131. TEST_ASSERT_EQUAL_STRING("123456789012", res);
  132. uint64_t sval;
  133. int ret = sscanf(res, "%llu", &sval);
  134. free(res);
  135. TEST_ASSERT_EQUAL(1, ret);
  136. TEST_ASSERT_EQUAL(val, sval);
  137. }
  138. #else
  139. TEST_CASE("test 64bit int formats", "[newlib][ignore]")
  140. {
  141. char* res = NULL;
  142. const uint64_t val = 123456789012LL;
  143. asprintf(&res, "%llu", val);
  144. TEST_ASSERT_NOT_NULL(res);
  145. TEST_ASSERT_EQUAL_STRING("lu", res);
  146. uint64_t sval;
  147. int ret = sscanf(res, "%llu", &sval);
  148. free(res);
  149. TEST_ASSERT_EQUAL(0, ret);
  150. }
  151. #endif
  152. TEST_CASE("fmod and fmodf work as expected", "[newlib]")
  153. {
  154. TEST_ASSERT_EQUAL(0.1, fmod(10.1, 2.0));
  155. TEST_ASSERT_EQUAL(0.1f, fmodf(10.1f, 2.0f));
  156. }