test_mem.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "test_mem.h"
  2. #include "lwip/mem.h"
  3. #include "lwip/stats.h"
  4. #if !LWIP_STATS || !MEM_STATS
  5. #error "This tests needs MEM-statistics enabled"
  6. #endif
  7. #if LWIP_DNS
  8. #error "This test needs DNS turned off (as it mallocs on init)"
  9. #endif
  10. /* Setups/teardown functions */
  11. static void
  12. mem_setup(void)
  13. {
  14. }
  15. static void
  16. mem_teardown(void)
  17. {
  18. }
  19. /* Test functions */
  20. /** Call mem_malloc, mem_free and mem_trim and check stats */
  21. START_TEST(test_mem_one)
  22. {
  23. #define SIZE1 16
  24. #define SIZE1_2 12
  25. #define SIZE2 16
  26. void *p1, *p2;
  27. mem_size_t s1, s2;
  28. LWIP_UNUSED_ARG(_i);
  29. fail_unless(lwip_stats.mem.used == 0);
  30. p1 = mem_malloc(SIZE1);
  31. fail_unless(p1 != NULL);
  32. fail_unless(lwip_stats.mem.used >= SIZE1);
  33. s1 = lwip_stats.mem.used;
  34. p2 = mem_malloc(SIZE2);
  35. fail_unless(p2 != NULL);
  36. fail_unless(lwip_stats.mem.used >= SIZE2 + s1);
  37. s2 = lwip_stats.mem.used;
  38. mem_trim(p1, SIZE1_2);
  39. mem_free(p2);
  40. fail_unless(lwip_stats.mem.used <= s2 - SIZE2);
  41. mem_free(p1);
  42. fail_unless(lwip_stats.mem.used == 0);
  43. }
  44. END_TEST
  45. static void malloc_keep_x(int x, int num, int size, int freestep)
  46. {
  47. int i;
  48. void* p[16];
  49. LWIP_ASSERT("invalid size", size >= 0 && size < (mem_size_t)-1);
  50. memset(p, 0, sizeof(p));
  51. for(i = 0; i < num && i < 16; i++) {
  52. p[i] = mem_malloc((mem_size_t)size);
  53. fail_unless(p[i] != NULL);
  54. }
  55. for(i = 0; i < num && i < 16; i += freestep) {
  56. if (i == x) {
  57. continue;
  58. }
  59. mem_free(p[i]);
  60. p[i] = NULL;
  61. }
  62. for(i = 0; i < num && i < 16; i++) {
  63. if (i == x) {
  64. continue;
  65. }
  66. if (p[i] != NULL) {
  67. mem_free(p[i]);
  68. p[i] = NULL;
  69. }
  70. }
  71. fail_unless(p[x] != NULL);
  72. mem_free(p[x]);
  73. }
  74. START_TEST(test_mem_random)
  75. {
  76. const int num = 16;
  77. int x;
  78. int size;
  79. int freestep;
  80. LWIP_UNUSED_ARG(_i);
  81. fail_unless(lwip_stats.mem.used == 0);
  82. for (x = 0; x < num; x++) {
  83. for (size = 1; size < 32; size++) {
  84. for (freestep = 1; freestep <= 3; freestep++) {
  85. fail_unless(lwip_stats.mem.used == 0);
  86. malloc_keep_x(x, num, size, freestep);
  87. fail_unless(lwip_stats.mem.used == 0);
  88. }
  89. }
  90. }
  91. }
  92. END_TEST
  93. /** Create the suite including all tests for this module */
  94. Suite *
  95. mem_suite(void)
  96. {
  97. testfunc tests[] = {
  98. TESTFUNC(test_mem_one),
  99. TESTFUNC(test_mem_random)
  100. };
  101. return create_suite("MEM", tests, sizeof(tests)/sizeof(testfunc), mem_setup, mem_teardown);
  102. }