TestAssign.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "TestSuite.h"
  2. #include "../Source/JSONNode.h"
  3. void TestSuite::TestAssigning(void){
  4. UnitTest::SetPrefix("TestAssign.cpp - Assigning");
  5. #ifdef JSON_LIBRARY
  6. //check names
  7. JSONNODE * test1 = json_new(JSON_NODE);
  8. json_set_name(test1, JSON_TEXT("hello world"));
  9. json_char * res = json_name(test1);
  10. assertCStringSame(res, JSON_TEXT("hello world"));
  11. json_free(res);
  12. //check strings
  13. json_set_a(test1, JSON_TEXT("Hello world"));
  14. assertEquals(json_type(test1), JSON_STRING);
  15. res = json_as_string(test1);
  16. assertCStringSame(res, JSON_TEXT("Hello world"));
  17. json_free(res);
  18. //check ints
  19. json_set_i(test1, 13);
  20. assertEquals(json_type(test1), JSON_NUMBER);
  21. res = json_as_string(test1);
  22. #ifdef JSON_CASTABLE
  23. assertCStringSame(res, JSON_TEXT("13"));
  24. #endif
  25. json_free(res);
  26. assertEquals(json_as_int(test1), 13);
  27. assertEquals(json_as_float(test1), 13.0f);
  28. //check doubles work
  29. json_set_f(test1, 13.7f);
  30. assertEquals(json_type(test1), JSON_NUMBER);
  31. res = json_as_string(test1);
  32. #ifdef JSON_CASTABLE
  33. assertCStringSame(res, JSON_TEXT("13.7"));
  34. #endif
  35. json_free(res);
  36. assertEquals(json_as_int(test1), 13);
  37. assertEquals(json_as_float(test1), 13.7f);
  38. //test making sure stripping the trailing period works
  39. json_set_f(test1, 13.0f);
  40. assertEquals(json_type(test1), JSON_NUMBER);
  41. res = json_as_string(test1);
  42. #ifdef JSON_CASTABLE
  43. assertCStringSame(res, JSON_TEXT("13"));
  44. #endif
  45. json_free(res);
  46. assertEquals(json_as_int(test1), 13);
  47. assertEquals(json_as_float(test1), 13.0f);
  48. //check boolean
  49. json_set_b(test1, (int)true);
  50. assertEquals(json_type(test1), JSON_BOOL);
  51. res = json_as_string(test1);
  52. #ifdef JSON_CASTABLE
  53. assertCStringSame(res, JSON_TEXT("true"));
  54. #endif
  55. json_free(res);
  56. assertEquals(json_as_bool(test1), true);
  57. //check boolean
  58. json_set_b(test1, false);
  59. assertEquals(json_type(test1), JSON_BOOL);
  60. res = json_as_string(test1);
  61. #ifdef JSON_CASTABLE
  62. assertCStringSame(res, JSON_TEXT("false"));
  63. #endif
  64. json_free(res);
  65. assertEquals(json_as_bool(test1), false);
  66. //check null
  67. json_nullify(test1);
  68. assertEquals(json_type(test1), JSON_NULL);
  69. res = json_as_string(test1);
  70. #ifdef JSON_CASTABLE
  71. assertCStringSame(res, JSON_TEXT("null"));
  72. #endif
  73. json_free(res);
  74. json_delete(test1);
  75. #else
  76. //check names
  77. JSONNode test1;
  78. test1.set_name(JSON_TEXT("hello world"));
  79. assertEquals(test1.name(), JSON_TEXT("hello world"));
  80. //check strings
  81. test1 = JSON_TEXT("Hello world");
  82. assertEquals(test1.type(), JSON_STRING);
  83. assertEquals(test1.as_string(), JSON_TEXT("Hello world"));
  84. //test chars
  85. test1 = JSON_TEXT('\0');
  86. assertEquals(test1.type(), JSON_NUMBER);
  87. #ifdef JSON_CASTABLE
  88. assertEquals(test1.as_string(), JSON_TEXT("0"));
  89. #endif
  90. assertEquals(test1.as_int(), 0);
  91. assertEquals(test1.as_float(), 0.0f);
  92. //check ints
  93. test1 = 13;
  94. assertEquals(test1.type(), JSON_NUMBER);
  95. #ifdef JSON_CASTABLE
  96. assertEquals(test1.as_string(), JSON_TEXT("13"));
  97. #endif
  98. assertEquals(test1.as_int(), 13);
  99. assertEquals(test1.as_float(), 13.0f);
  100. //check doubles work
  101. test1 = 13.7f;
  102. assertEquals(test1.type(), JSON_NUMBER);
  103. #ifdef JSON_CASTABLE
  104. assertEquals(test1.as_string(), JSON_TEXT("13.7"));
  105. #endif
  106. assertEquals(test1.as_int(), 13);
  107. assertEquals(test1.as_float(), 13.7f);
  108. //test making sure stripping hte trailing period works
  109. test1 = 13.0f;
  110. assertEquals(test1.type(), JSON_NUMBER);
  111. #ifdef JSON_CASTABLE
  112. assertEquals(test1.as_string(), JSON_TEXT("13"));
  113. #endif
  114. assertEquals(test1.as_int(), 13);
  115. assertEquals(test1.as_float(), 13.0f);
  116. //check boolean
  117. test1 = true;
  118. assertEquals(test1.type(), JSON_BOOL);
  119. #ifdef JSON_CASTABLE
  120. assertEquals(test1.as_string(), JSON_TEXT("true"));
  121. #endif
  122. assertEquals(test1.as_bool(), true);
  123. //check boolean
  124. test1 = false;
  125. assertEquals(test1.type(), JSON_BOOL);
  126. #ifdef JSON_CASTABLE
  127. assertEquals(test1.as_string(), JSON_TEXT("false"));
  128. #endif
  129. assertEquals(test1.as_bool(), false);
  130. //check null
  131. test1.nullify();
  132. assertEquals(test1.type(), JSON_NULL);
  133. #ifdef JSON_CASTABLE
  134. assertEquals(test1.as_string(), JSON_TEXT("null"));
  135. #endif
  136. #endif
  137. }