test_mem.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #if LWIP_DNS
  30. fail("This test needs DNS turned off (as it mallocs on init)");
  31. #endif
  32. fail_unless(lwip_stats.mem.used == 0);
  33. p1 = mem_malloc(SIZE1);
  34. fail_unless(p1 != NULL);
  35. fail_unless(lwip_stats.mem.used >= SIZE1);
  36. s1 = lwip_stats.mem.used;
  37. p2 = mem_malloc(SIZE2);
  38. fail_unless(p2 != NULL);
  39. fail_unless(lwip_stats.mem.used >= SIZE2 + s1);
  40. s2 = lwip_stats.mem.used;
  41. mem_trim(p1, SIZE1_2);
  42. mem_free(p2);
  43. fail_unless(lwip_stats.mem.used <= s2 - SIZE2);
  44. mem_free(p1);
  45. fail_unless(lwip_stats.mem.used == 0);
  46. }
  47. END_TEST
  48. /** Create the suite including all tests for this module */
  49. Suite *
  50. mem_suite(void)
  51. {
  52. TFun tests[] = {
  53. test_mem_one
  54. };
  55. return create_suite("MEM", tests, sizeof(tests)/sizeof(TFun), mem_setup, mem_teardown);
  56. }