main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 StreamConsumer::read(input);
  13. }
  14. void checkArray()
  15. {
  16. //Check array
  17. StreamConsumer *root = toJson("[true, 42, \"coucou\", 12.34, false]");
  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<float> *> (*it)) != nullptr);
  31. assert(((JSonPrimitive<float> *)(*it))->getValue() == 12.34f);
  32. it++;
  33. assert((dynamic_cast<JSonPrimitive<bool> *> (*it)) != nullptr);
  34. assert(((JSonPrimitive<bool> *)(*it))->getValue() == false);
  35. delete root;
  36. }
  37. void checkTypes()
  38. {
  39. //Check basic types
  40. StreamConsumer *root = toJson("{\"test\":\"value\"}");
  41. assert(dynamic_cast<JSonObject*>(root->getRoot()) != nullptr);
  42. delete root;
  43. root = toJson("[\"test\",\"value\"]");
  44. assert(dynamic_cast<JSonArray*>(root->getRoot()) != nullptr);
  45. delete root;
  46. root = toJson("\"test\"");
  47. assert(dynamic_cast<JSonPrimitive<std::string> *>(root->getRoot()) != nullptr);
  48. delete root;
  49. root = toJson("42");
  50. assert(dynamic_cast<JSonPrimitive<int> *>(root->getRoot()) != nullptr);
  51. delete root;
  52. root = toJson("42.2");
  53. assert(dynamic_cast<JSonPrimitive<float> *>(root->getRoot()) != nullptr);
  54. delete root;
  55. root = toJson(std::to_string((long long)LLONG_MAX));
  56. assert(dynamic_cast<JSonPrimitive<long long> *>(root->getRoot()) != nullptr);
  57. delete root;
  58. root = toJson("true");
  59. assert(dynamic_cast<JSonPrimitive<bool> *>(root->getRoot()) != nullptr);
  60. delete root;
  61. }
  62. void checkObject()
  63. {
  64. //Check Obj
  65. StreamConsumer *root = toJson("{\"bool\":true, \"int\":42, \"str\":\"coucou\", \"float\":12.34, \"arrayOfInt\":[1, 2, 3, 4.5]}");
  66. assert(dynamic_cast<JSonObject *>(root->getRoot()) != nullptr);
  67. assert(((JSonObject *)(root->getRoot()))->size() == 5);
  68. const JSonElement *tmp = ((JSonObject *)(root->getRoot()))->get("bool");
  69. assert((dynamic_cast<const JSonPrimitive<bool> *> (tmp)) != nullptr);
  70. assert((dynamic_cast<const JSonPrimitive<bool> *> (tmp))->getValue() == true);
  71. tmp = ((JSonObject *)(root->getRoot()))->get("int");
  72. assert((dynamic_cast<const JSonPrimitive<int> *> (tmp)) != nullptr);
  73. assert((dynamic_cast<const JSonPrimitive<int> *> (tmp))->getValue() == 42);
  74. tmp = ((JSonObject *)(root->getRoot()))->get("str");
  75. assert((dynamic_cast<const JSonPrimitive<std::string> *> (tmp)) != nullptr);
  76. assert((dynamic_cast<const JSonPrimitive<std::string> *> (tmp))->getValue() == "coucou");
  77. tmp = ((JSonObject *)(root->getRoot()))->get("float");
  78. assert((dynamic_cast<const JSonPrimitive<float> *> (tmp)) != nullptr);
  79. assert((dynamic_cast<const JSonPrimitive<float> *> (tmp))->getValue() == 12.34f);
  80. tmp = ((JSonObject *)(root->getRoot()))->get("arrayOfInt");
  81. const JSonArray *arr2 = dynamic_cast<const JSonArray *> (tmp);
  82. assert(arr2 != nullptr);
  83. assert(arr2->size() == 4);
  84. JSonArray::const_iterator it = arr2->cbegin();
  85. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  86. assert(((JSonPrimitive<int> *)(*it))->getValue() == 1);
  87. it++;
  88. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  89. assert(((JSonPrimitive<int> *)(*it))->getValue() == 2);
  90. it++;
  91. assert((dynamic_cast<JSonPrimitive<int> *> (*it)) != nullptr);
  92. assert(((JSonPrimitive<int> *)(*it))->getValue() == 3);
  93. it++;
  94. assert((dynamic_cast<JSonPrimitive<float> *> (*it)) != nullptr);
  95. assert(((JSonPrimitive<float> *)(*it))->getValue() == 4.5f);
  96. delete root;
  97. }
  98. void checkSample()
  99. {
  100. StreamConsumer *root = toJson(testJson);
  101. root->getRoot();
  102. }
  103. int main(int ac, char **av)
  104. {
  105. checkTypes();
  106. checkArray();
  107. checkObject();
  108. checkSample();
  109. exit(EXIT_SUCCESS);
  110. }