TestSuite.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "TestSuite.h"
  2. #include "../Source/JSONNode.h"
  3. #ifndef JSON_STDERROR
  4. #ifdef JSON_DEBUG
  5. #ifdef JSON_LIBRARY
  6. static void callback(const json_char * msg_c){
  7. json_string msg(msg_c);
  8. #else
  9. static void callback(const json_string & msg){
  10. #endif
  11. #ifdef JSON_STRING_HEADER
  12. echo("callback triggered, but can't display string");
  13. #else
  14. #ifdef JSON_UNICODE
  15. const std::string res = std::string(msg.begin(), msg.end());
  16. echo(res);
  17. #else
  18. echo(msg);
  19. #endif
  20. #endif
  21. }
  22. #endif
  23. #endif
  24. void TestSuite::TestSelf(void){
  25. UnitTest::SetPrefix("TestSuite.cpp - Self Test");
  26. #ifndef JSON_STDERROR
  27. #ifdef JSON_DEBUG
  28. #ifdef JSON_LIBRARY
  29. json_register_debug_callback(callback);
  30. #else
  31. libjson::register_debug_callback(callback);
  32. #endif
  33. #endif
  34. #endif
  35. assertUnitTest();
  36. #if defined(JSON_SAFE) && ! defined(JSON_LIBRARY)
  37. bool temp = false;
  38. JSON_ASSERT_SAFE(true, JSON_TEXT(""), temp = true;);
  39. assertFalse(temp);
  40. JSON_ASSERT_SAFE(false, JSON_TEXT(""), temp = true;);
  41. assertTrue(temp);
  42. temp = false;
  43. JSON_FAIL_SAFE(JSON_TEXT(""), temp = true;);
  44. assertTrue(temp);
  45. #endif
  46. echo("If this fails, then edit JSON_INDEX_TYPE in JSONOptions.h");
  47. assertLessThanEqualTo(sizeof(json_index_t), sizeof(void*));
  48. }
  49. //makes sure that libjson didn't leak memory somewhere
  50. void TestSuite::TestFinal(void){
  51. #ifdef JSON_UNIT_TEST
  52. UnitTest::SetPrefix("TestSuite.cpp - Memory Leak");
  53. echo("Node allocations: " << JSONNode::getNodeAllocationCount());
  54. echo("Node deallocations: " << JSONNode::getNodeDeallocationCount());
  55. assertEquals(JSONNode::getNodeAllocationCount(), JSONNode::getNodeDeallocationCount());
  56. echo("internal allocations: " << JSONNode::getInternalAllocationCount());
  57. echo("internal deallocations: " << JSONNode::getInternalDeallocationCount());
  58. assertEquals(JSONNode::getInternalAllocationCount(), JSONNode::getInternalDeallocationCount());
  59. echo("children allocations: " << JSONNode::getChildrenAllocationCount());
  60. echo("children deallocations: " << JSONNode::getChildrenDeallocationCount());
  61. assertEquals(JSONNode::getChildrenAllocationCount(), JSONNode::getChildrenDeallocationCount());
  62. #if defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
  63. echo("stl allocations: " << JSONAllocatorRelayer::getAllocationCount());
  64. echo("stl deallocations: " << JSONAllocatorRelayer::getDeallocationCount());
  65. echo("stl bytes: " << JSONAllocatorRelayer::getAllocationByteCount());
  66. assertEquals(JSONAllocatorRelayer::getAllocationCount(), JSONAllocatorRelayer::getDeallocationCount());
  67. #endif
  68. #endif
  69. }