main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <iostream>
  2. #include <cstdlib> //for malloc, realloc, and free
  3. #include "TestSuite.h"
  4. #include "../../libjson.h"
  5. void DoTests(void);
  6. void DoTests(void){
  7. TestSuite::TestStreams();
  8. TestSuite::TestValidator();
  9. TestSuite::TestString();
  10. TestSuite::TestConverters();
  11. #ifdef JSON_BINARY
  12. TestSuite::TestBase64();
  13. #endif
  14. TestSuite::TestReferenceCounting();
  15. TestSuite::TestConstructors();
  16. TestSuite::TestAssigning();
  17. TestSuite::TestEquality();
  18. TestSuite::TestInequality();
  19. TestSuite::TestChildren();
  20. TestSuite::TestFunctions();
  21. TestSuite::TestIterators();
  22. TestSuite::TestInspectors();
  23. TestSuite::TestNamespace();
  24. #ifdef JSON_WRITE_PRIORITY
  25. TestSuite::TestWriter();
  26. #endif
  27. #ifdef JSON_COMMENTS
  28. TestSuite::TestComments();
  29. #endif
  30. #ifdef JSON_MUTEX_CALLBACKS
  31. TestSuite::TestMutex();
  32. TestSuite::TestThreading();
  33. #endif
  34. TestSuite::TestSharedString();
  35. TestSuite::TestFinal();
  36. }
  37. #ifdef JSON_MEMORY_CALLBACKS
  38. long mallocs = 0;
  39. long reallocs = 0;
  40. long frees = 0;
  41. long bytes = 0;
  42. //used to check load
  43. size_t maxBytes = 0;
  44. size_t currentBytes = 0;
  45. #ifdef JSON_LIBRARY
  46. #define MEMTYPE unsigned long
  47. #else
  48. #define MEMTYPE size_t
  49. #endif
  50. #include <map>
  51. #include <vector>
  52. std::map<void *, MEMTYPE> mem_mapping;
  53. std::vector<size_t> bytesallocated;
  54. void * testmal(MEMTYPE siz);
  55. void * testmal(MEMTYPE siz){
  56. ++mallocs;
  57. bytes += (long)siz;
  58. currentBytes += siz;
  59. if (currentBytes > maxBytes) maxBytes = currentBytes;
  60. bytesallocated.push_back(currentBytes);
  61. void * res = std::malloc(siz);
  62. mem_mapping[res] = siz;
  63. return res;
  64. }
  65. void testfree(void * ptr);
  66. void testfree(void * ptr){
  67. ++frees;
  68. std::map<void *, MEMTYPE>::iterator i = mem_mapping.find(ptr);
  69. if (i != mem_mapping.end()){ //globals
  70. currentBytes -= mem_mapping[ptr];
  71. mem_mapping.erase(ptr);
  72. }
  73. bytesallocated.push_back(currentBytes);
  74. std::free(ptr);
  75. }
  76. void * testreal(void * ptr, MEMTYPE siz);
  77. void * testreal(void * ptr, MEMTYPE siz){
  78. ++reallocs;
  79. std::map<void *, MEMTYPE>::iterator i = mem_mapping.find(ptr);
  80. if (i != mem_mapping.end()){ //globals
  81. currentBytes -= mem_mapping[ptr];
  82. mem_mapping.erase(ptr);
  83. }
  84. currentBytes += siz;
  85. if (currentBytes > maxBytes) maxBytes = currentBytes;
  86. bytesallocated.push_back(currentBytes);
  87. void * res = std::realloc(ptr, siz);
  88. mem_mapping[res] = siz;
  89. return res;
  90. }
  91. void doMemTests(void);
  92. void doMemTests(void){
  93. #ifdef JSON_LIBRARY
  94. json_register_memory_callbacks(testmal, testreal, testfree);
  95. #else
  96. libjson::register_memory_callbacks(testmal, testreal, testfree);
  97. #endif
  98. DoTests();
  99. echo("mallocs: " << mallocs);
  100. echo("frees: " << frees);
  101. echo("reallocs: " << reallocs);
  102. echo("bytes: " << bytes << " (" << (int)(bytes / 1024) << " KB)");
  103. echo("max bytes at once: " << maxBytes << " (" << (int)(maxBytes / 1024) << " KB)");
  104. std::vector<size_t>::iterator i = bytesallocated.begin();
  105. std::vector<size_t>::iterator e = bytesallocated.end();
  106. size_t bbytes = 0;
  107. for(; i != e; ++i){
  108. bbytes += *i;
  109. }
  110. bbytes = (size_t)(((double)bbytes) / ((double)bytesallocated.size()));
  111. echo("avg bytes at once: " << bbytes << " (" << (int)(bbytes / 1024) << " KB)");
  112. echo("still allocated: " << currentBytes << " (" << (int)(currentBytes / 1024) << " KB) (Global variables)");
  113. assertEquals(mallocs, frees);
  114. }
  115. #endif
  116. #include "RunTestSuite2.h"
  117. int main () {
  118. UnitTest::StartTime();
  119. TestSuite::TestSelf();
  120. DoTests();
  121. #ifdef JSON_MEMORY_CALLBACKS
  122. doMemTests();
  123. #endif
  124. RunTestSuite2::RunTests();
  125. UnitTest::SaveTo("out.html");
  126. return 0;
  127. }