test_newlib.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "unity.h"
  7. TEST_CASE("test ctype functions", "[newlib]")
  8. {
  9. TEST_ASSERT_TRUE( isalnum('a') && isalnum('A') && isalnum('z') && isalnum('Z') && isalnum('0') && isalnum('9') );
  10. TEST_ASSERT_FALSE( isalnum('(') || isalnum('-') || isalnum(' ') || isalnum('\x81') || isalnum('.') || isalnum('\\') );
  11. TEST_ASSERT_TRUE( isalpha('a') && isalpha('A') && isalpha('z') && isalpha('Z') );
  12. TEST_ASSERT_FALSE( isalpha('0') || isalpha('9') || isalpha(')') || isalpha('\t') || isalpha(' ') || isalpha('\x81') );
  13. TEST_ASSERT_TRUE( isspace(' ') && isspace('\t') && isspace('\n') && isspace('\r') );
  14. TEST_ASSERT_FALSE( isspace('0') || isspace('9') || isspace(')') || isspace('A') || isspace('*') || isspace('\x81') || isspace('a'));
  15. }
  16. TEST_CASE("test atoX functions", "[newlib]")
  17. {
  18. TEST_ASSERT_EQUAL_INT(-2147483648, atoi("-2147483648"));
  19. TEST_ASSERT_EQUAL_INT(2147483647, atoi("2147483647"));
  20. TEST_ASSERT_EQUAL_INT(42, atoi("000000042"));
  21. TEST_ASSERT_EQUAL_INT(0, strtol("foo", NULL, 10));
  22. }
  23. TEST_CASE("test sprintf function", "[newlib]")
  24. {
  25. char *res = NULL;
  26. asprintf(&res, "%d %011i %lu %p %x %c %.4f\n", 42, 2147483647, 2147483648UL, (void *) 0x40010000, 0x40020000, 'Q', 1.0f / 137.0f);
  27. TEST_ASSERT_NOT_NULL(res);
  28. TEST_ASSERT_EQUAL_STRING(res, "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n");
  29. free(res);
  30. }
  31. TEST_CASE("test sscanf function", "[newlib]")
  32. {
  33. const char *src = "42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n";
  34. int fourty_two;
  35. int int_max;
  36. unsigned long int_max_plus_one;
  37. void *iram_ptr;
  38. int irom_ptr;
  39. char department;
  40. float inv_fine_structure_constant;
  41. 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);
  42. TEST_ASSERT_EQUAL(7, res);
  43. TEST_ASSERT_EQUAL(42, fourty_two);
  44. TEST_ASSERT_EQUAL(2147483647, int_max);
  45. TEST_ASSERT_EQUAL_UINT32(2147483648UL, int_max_plus_one);
  46. TEST_ASSERT_EQUAL(0x40010000, iram_ptr);
  47. TEST_ASSERT_EQUAL(0x40020000, irom_ptr);
  48. TEST_ASSERT_EQUAL('Q', department);
  49. TEST_ASSERT_TRUE(1.0f / inv_fine_structure_constant > 136 && 1.0f / inv_fine_structure_constant < 138);
  50. }
  51. TEST_CASE("test time functions", "[newlib]")
  52. {
  53. time_t now = 1464248488;
  54. setenv("TZ", "UTC-8", 1);
  55. struct tm *tm_utc = gmtime(&now);
  56. TEST_ASSERT_EQUAL( 28, tm_utc->tm_sec);
  57. TEST_ASSERT_EQUAL( 41, tm_utc->tm_min);
  58. TEST_ASSERT_EQUAL( 7, tm_utc->tm_hour);
  59. TEST_ASSERT_EQUAL( 26, tm_utc->tm_mday);
  60. TEST_ASSERT_EQUAL( 4, tm_utc->tm_mon);
  61. TEST_ASSERT_EQUAL(116, tm_utc->tm_year);
  62. TEST_ASSERT_EQUAL( 4, tm_utc->tm_wday);
  63. TEST_ASSERT_EQUAL(146, tm_utc->tm_yday);
  64. struct tm *tm_local = localtime(&now);
  65. TEST_ASSERT_EQUAL( 28, tm_local->tm_sec);
  66. TEST_ASSERT_EQUAL( 41, tm_local->tm_min);
  67. TEST_ASSERT_EQUAL( 15, tm_local->tm_hour);
  68. TEST_ASSERT_EQUAL( 26, tm_local->tm_mday);
  69. TEST_ASSERT_EQUAL( 4, tm_local->tm_mon);
  70. TEST_ASSERT_EQUAL(116, tm_local->tm_year);
  71. TEST_ASSERT_EQUAL( 4, tm_local->tm_wday);
  72. TEST_ASSERT_EQUAL(146, tm_local->tm_yday);
  73. }
  74. static int checkFnRom(void *fn, char *name)
  75. {
  76. int fnaddr = (int)fn;
  77. printf("%s: 0X%x\n", name, fnaddr);
  78. if ((fnaddr >= 0x40000000) && (fnaddr < 0x40070000)) {
  79. return 1;
  80. } else {
  81. return 0;
  82. }
  83. }
  84. TEST_CASE("check if ROM is used for functions", "[newlib]")
  85. {
  86. TEST_ASSERT(checkFnRom(printf, "printf"));
  87. TEST_ASSERT(checkFnRom(sscanf, "sscanf"));
  88. TEST_ASSERT(checkFnRom(atoi, "atoi"));
  89. TEST_ASSERT(checkFnRom(strtol, "strtol"));
  90. }