main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <string>
  2. #include <sstream>
  3. #include <cassert>
  4. #include <climits>
  5. #include <stdlib.h>
  6. #include "jsonElement.hh"
  7. #include "streamConsumer.hh"
  8. const char testJson[] = "{\"widget\": {\"debug\": \"on\",\"window\": {\"title\": \"Sample Konfabulator Widget\",\"name\": \"main_window\",\"width\": 500,\"height\": 500},\"image\": { \"src\": \"Images/Sun.png\",\"name\": \"sun1\",\"hOffset\": 250,\"vOffset\": 250,\"alignment\": \"center\"},\"text\": {\"data\": \"Click Here\",\"size\": 36,\"style\": \"bold\",\"name\": \"text1\",\"hOffset\": 250,\"vOffset\": 100,\"alignment\": \"center\",\"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"}}}";
  9. StreamConsumer *toJson(std::string str)
  10. {
  11. std::stringstream input(str);
  12. return (new StreamConsumer(input))->read();
  13. }
  14. void checkArray()
  15. {
  16. //Check array
  17. StreamConsumer *root = toJson("[true, 42, \"coucou\", 12.34, false, -12, -12.42]");
  18. JSonArray *arr = dynamic_cast<JSonArray *>(root->getRoot());
  19. assert(arr != nullptr);
  20. JSonArray::const_iterator it = arr->cbegin();
  21. assert((dynamic_cast<JSonPrimitive<bool> *> (*it)) != nullptr);
  22. assert(((JSonPrimitive<bool> *)(*it))->getValue() == true);
  23. it++;
  24. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  25. assert(((JSonPrimitive<int> *)(*it))->getValue() == 42);
  26. it++;
  27. assert((dynamic_cast<JSonPrimitive<std::string> *> (*it)) != nullptr);
  28. assert(((JSonPrimitive<std::string> *)(*it))->getValue() == "coucou");
  29. it++;
  30. assert((dynamic_cast<JSonPrimitive<double> *> (*it)) != nullptr);
  31. assert(((JSonPrimitive<double> *)(*it))->getValue() == 12.34);
  32. it++;
  33. assert((dynamic_cast<JSonPrimitive<bool> *> (*it)) != nullptr);
  34. assert(((JSonPrimitive<bool> *)(*it))->getValue() == false);
  35. it++;
  36. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  37. assert(((JSonPrimitive<int> *)(*it))->getValue() == -12);
  38. it++;
  39. assert((dynamic_cast<JSonPrimitive<double> *> (*it)) != nullptr);
  40. double value = ((JSonPrimitive<double> *)(*it))->getValue();
  41. assert(value == -12.42);
  42. delete root;
  43. }
  44. void checkTypes()
  45. {
  46. //Check basic types
  47. StreamConsumer *root = toJson("{\"test\":\"value\"}");
  48. assert(dynamic_cast<JSonObject*>(root->getRoot()) != nullptr);
  49. delete root;
  50. root = toJson("[\"test\",\"value\"]");
  51. assert(dynamic_cast<JSonArray*>(root->getRoot()) != nullptr);
  52. delete root;
  53. root = toJson("\"test\"");
  54. assert(dynamic_cast<JSonPrimitive<std::string> *>(root->getRoot()) != nullptr);
  55. delete root;
  56. root = toJson("42");
  57. assert(dynamic_cast<JSonPrimitive<int> *>(root->getRoot()) != nullptr);
  58. delete root;
  59. root = toJson("42.2");
  60. assert(dynamic_cast<JSonPrimitive<double> *>(root->getRoot()) != nullptr);
  61. assert((((JSonPrimitive<double> *)(root->getRoot()))->getValue()) == 42.2);
  62. delete root;
  63. root = toJson(std::to_string((long long)LLONG_MAX));
  64. assert(dynamic_cast<JSonPrimitive<long long> *>(root->getRoot()) != nullptr);
  65. delete root;
  66. root = toJson("true");
  67. assert(dynamic_cast<JSonPrimitive<bool> *>(root->getRoot()) != nullptr);
  68. delete root;
  69. }
  70. void checkObject()
  71. {
  72. //Check Obj
  73. StreamConsumer *root = toJson("{\"bool\":true, \"int\":42, \"str\":\"coucou\", \"double\":12.34, \"arrayOfInt\":[1, 2, 3, 4.5]}");
  74. assert(dynamic_cast<JSonObject *>(root->getRoot()) != nullptr);
  75. assert(((JSonObject *)(root->getRoot()))->size() == 5);
  76. const JSonElement *tmp = ((JSonObject *)(root->getRoot()))->get("bool");
  77. assert((dynamic_cast<const JSonPrimitive<bool> *> (tmp)) != nullptr);
  78. assert((dynamic_cast<const JSonPrimitive<bool> *> (tmp))->getValue() == true);
  79. tmp = ((JSonObject *)(root->getRoot()))->get("int");
  80. assert((dynamic_cast<const JSonPrimitive<int> *> (tmp)) != nullptr);
  81. assert((dynamic_cast<const JSonPrimitive<int> *> (tmp))->getValue() == 42);
  82. tmp = ((JSonObject *)(root->getRoot()))->get("str");
  83. assert((dynamic_cast<const JSonPrimitive<std::string> *> (tmp)) != nullptr);
  84. assert((dynamic_cast<const JSonPrimitive<std::string> *> (tmp))->getValue() == "coucou");
  85. tmp = ((JSonObject *)(root->getRoot()))->get("double");
  86. assert((dynamic_cast<const JSonPrimitive<double> *> (tmp)) != nullptr);
  87. assert((dynamic_cast<const JSonPrimitive<double> *> (tmp))->getValue() == 12.34);
  88. tmp = ((JSonObject *)(root->getRoot()))->get("arrayOfInt");
  89. const JSonArray *arr2 = dynamic_cast<const JSonArray *> (tmp);
  90. assert(arr2 != nullptr);
  91. assert(arr2->size() == 4);
  92. JSonArray::const_iterator it = arr2->cbegin();
  93. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  94. assert(((JSonPrimitive<int> *)(*it))->getValue() == 1);
  95. it++;
  96. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  97. assert(((JSonPrimitive<int> *)(*it))->getValue() == 2);
  98. it++;
  99. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  100. assert(((JSonPrimitive<int> *)(*it))->getValue() == 3);
  101. it++;
  102. assert((dynamic_cast<JSonPrimitive<double> *> (*it)) != nullptr);
  103. assert(((JSonPrimitive<double> *)(*it))->getValue() == 4.5);
  104. delete root;
  105. }
  106. void checkSample()
  107. {
  108. StreamConsumer *root = toJson(testJson);
  109. root->getRoot();
  110. delete root;
  111. }
  112. int main()
  113. {
  114. checkTypes();
  115. checkArray();
  116. checkObject();
  117. checkSample();
  118. exit(EXIT_SUCCESS);
  119. }