TestRefCounting.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "TestSuite.h"
  2. void TestSuite::TestReferenceCounting(void){
  3. UnitTest::SetPrefix("TestRefCounting.cpp - Reference Counting");
  4. #ifdef JSON_LIBRARY
  5. #else
  6. JSONNode test1;
  7. #ifdef JSON_UNIT_TEST
  8. assertNotNull(test1.internal);
  9. #ifdef JSON_REF_COUNT
  10. assertEquals(test1.internal -> refcount, 1);
  11. #endif
  12. #endif
  13. //copy ctor, should simply increment the reference counter
  14. JSONNode test2 = JSONNode(test1);
  15. #ifdef JSON_REF_COUNT
  16. #ifdef JSON_UNIT_TEST
  17. assertEquals(test1.internal, test2.internal);
  18. #endif
  19. assertEquals(test1, test2);
  20. #ifdef JSON_UNIT_TEST
  21. assertEquals(test1.internal -> refcount, 2);
  22. #endif
  23. #else
  24. #ifdef JSON_UNIT_TEST
  25. assertNotEquals(test1.internal, test2.internal);
  26. #endif
  27. assertEquals(test1, test2);
  28. #endif
  29. //assignment operator, should simply increment the reference counter
  30. JSONNode test3 = test2;
  31. #ifdef JSON_UNIT_TEST
  32. #ifdef JSON_REF_COUNT
  33. assertEquals(test1.internal, test3.internal);
  34. assertEquals(test2.internal, test3.internal);
  35. assertEquals(test1.internal -> refcount, 3);
  36. #else
  37. assertNotEquals(test1.internal, test3.internal);
  38. assertNotEquals(test2.internal, test3.internal);
  39. #endif
  40. #endif
  41. //assigning something, it should copy now
  42. test2 = "hello";
  43. #ifdef JSON_UNIT_TEST
  44. #ifdef JSON_REF_COUNT
  45. assertEquals(test1.internal, test3.internal);
  46. assertNotEquals(test2.internal, test3.internal);
  47. assertEquals(test1.internal -> refcount, 2);
  48. assertEquals(test2.internal -> refcount, 1);
  49. #else
  50. assertNotEquals(test1.internal, test3.internal);
  51. assertNotEquals(test2.internal, test3.internal);
  52. #endif
  53. #endif
  54. //assigning something, it should copy now
  55. test1 = 15;
  56. #ifdef JSON_UNIT_TEST
  57. assertNotEquals(test1.internal, test3.internal);
  58. #ifdef JSON_REF_COUNT
  59. assertEquals(test1.internal -> refcount, 1);
  60. assertEquals(test3.internal -> refcount, 1);
  61. #endif
  62. #endif
  63. test1 = test2;
  64. #ifdef JSON_REF_COUNT
  65. #ifdef JSON_UNIT_TEST
  66. assertEquals(test1.internal, test2.internal);
  67. assertEquals(test1.internal -> refcount, 2);
  68. #endif
  69. #else
  70. #ifdef JSON_UNIT_TEST
  71. assertNotEquals(test1.internal, test2.internal);
  72. #endif
  73. assertEquals(test1, test2);
  74. #endif
  75. test1.set_name(JSON_TEXT("hello world"));
  76. #ifdef JSON_UNIT_TEST
  77. assertNotEquals(test1.internal, test2.internal);
  78. #ifdef JSON_REF_COUNT
  79. assertEquals(test1.internal -> refcount, 1);
  80. assertEquals(test1.internal -> refcount, 1);
  81. #endif
  82. #endif
  83. //test tree copying and partial tree copying
  84. UnitTest::SetPrefix("TestRefCounting.cpp - Partial Copy");
  85. test1 = JSONNode(JSON_NODE);
  86. test1.push_back(JSONNode(JSON_NODE));
  87. test1.push_back(JSONNode(JSON_TEXT(""), 5));
  88. assertEquals(test1.size(), 2);
  89. test2 = test1;
  90. #ifdef JSON_UNIT_TEST
  91. #ifdef JSON_REF_COUNT
  92. assertEquals(test1.internal -> refcount, 2);
  93. assertEquals(test1.internal, test2.internal);
  94. #else
  95. assertNotEquals(test1.internal, test2.internal);
  96. #endif
  97. #endif
  98. #ifdef JSON_READ_PRIORITY
  99. assertEquals(test1, libjson::parse(JSON_TEXT("{\"\":{},\"\":5}")));
  100. assertEquals(test1, test1);
  101. assertEquals(libjson::parse(JSON_TEXT("{\"\":{},\"\":5}")), libjson::parse(JSON_TEXT("{\"\":{},\"\":5}")));
  102. TestSuite::testParsingItself(test1);
  103. #endif
  104. test2[1] = 15;
  105. assertEquals(test1[1], 5);
  106. assertEquals(test2[1], 15);
  107. test1 = test2;
  108. #ifdef JSON_UNIT_TEST
  109. #ifdef JSON_REF_COUNT
  110. assertEquals(test1.internal, test2.internal);
  111. #else
  112. assertNotEquals(test1.internal, test2.internal);
  113. #endif
  114. #endif
  115. test1[0].push_back(JSONNode(JSON_TEXT(""), 1));
  116. test1[0].push_back(JSONNode(JSON_TEXT(""), 2));
  117. assertEquals(test1[0].size(), 2);
  118. assertEquals(test2[0].size(), 0);
  119. TestSuite::testParsingItself(test1);
  120. TestSuite::testParsingItself(test2);
  121. #endif
  122. }