Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory_test.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*!
  2. * \file test/memory_test.cpp
  3. * Memory testing Toolkit Unit test
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "greatest.h"
  12. #include <memory_test.h>
  13. extern rbtree_t *MEMORY_INFO;
  14. bool check_red_black_tree(rbtree_t *current, bool valid, bool strict);
  15. int *i, *j, *k;
  16. float *l, *m, *n;
  17. void integerTeardown(void *) {
  18. free(i);
  19. free(j);
  20. free(k);
  21. }
  22. void floatTeardown(void *) {
  23. free(l);
  24. free(m);
  25. free(n);
  26. }
  27. TEST singleInteger() {
  28. i = new int;
  29. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  30. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), sizeof(int));
  31. delete i;
  32. i = NULL;
  33. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  34. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
  35. PASS();
  36. }
  37. TEST arrayInteger() {
  38. i = new int[3];
  39. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  40. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 3 * sizeof(int));
  41. delete [] i;
  42. i = NULL;
  43. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  44. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
  45. PASS();
  46. }
  47. TEST arrayCombinedInteger() {
  48. i = new int[3];
  49. j = new int;
  50. k = new int[3];
  51. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  52. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 7 * sizeof(int));
  53. delete [] i;
  54. delete j;
  55. delete [] k;
  56. i = NULL;
  57. j = NULL;
  58. k = NULL;
  59. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  60. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
  61. PASS();
  62. }
  63. SUITE(integerSuite) {
  64. SET_TEARDOWN(integerTeardown, NULL);
  65. RUN_TEST(singleInteger);
  66. RUN_TEST(arrayInteger);
  67. RUN_TEST(arrayCombinedInteger);
  68. }
  69. TEST singleFloat() {
  70. l = new float;
  71. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  72. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), sizeof(float));
  73. delete l;
  74. l = NULL;
  75. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  76. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
  77. PASS();
  78. }
  79. TEST arrayFloat() {
  80. l = new float[3];
  81. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  82. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 3 * sizeof(float));
  83. delete [] l;
  84. l = NULL;
  85. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  86. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
  87. PASS();
  88. }
  89. TEST arrayCombinedFloat() {
  90. l = new float[3];
  91. m = new float;
  92. n = new float[3];
  93. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  94. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 7 * sizeof(float));
  95. delete [] l;
  96. delete m;
  97. delete [] n;
  98. l = NULL;
  99. m = NULL;
  100. n = NULL;
  101. ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
  102. ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
  103. PASS();
  104. }
  105. SUITE(floatSuite) {
  106. SET_TEARDOWN(floatTeardown, NULL);
  107. RUN_TEST(singleFloat);
  108. RUN_TEST(arrayFloat);
  109. RUN_TEST(arrayCombinedFloat);
  110. }
  111. GREATEST_MAIN_DEFS();
  112. int main(int argc, char *argv[]) {
  113. GREATEST_MAIN_BEGIN();
  114. RUN_SUITE(integerSuite);
  115. RUN_SUITE(floatSuite);
  116. GREATEST_MAIN_END();
  117. }