serialize.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * serialize.cpp - serializing tests
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <catch.hpp>
  19. #include <iostream>
  20. #include <core/types.h>
  21. #include <core/node/node.h>
  22. #include <core/node/property.h>
  23. #include <core/serialize/json.h>
  24. #include <core/serialize/node_writer.h>
  25. #include <core/serialize/node_reader.h>
  26. using namespace rainynite;
  27. using namespace rainynite::core;
  28. namespace rainynite::core {
  29. namespace serialize {
  30. template class AutoValueToString<double>;
  31. }
  32. class Add : public Node<double> {
  33. public:
  34. Add() {
  35. init<double>(a, 0);
  36. init<double>(b, 0);
  37. }
  38. public:
  39. double get(shared_ptr<Context> context) const override {
  40. auto a = get_a()->get(context);
  41. auto b = get_b()->get(context);
  42. return a+b;
  43. }
  44. NODE_PROPERTY(a, double);
  45. NODE_PROPERTY(b, double);
  46. };
  47. REGISTER_NODE(Add);
  48. }
  49. TEST_CASE("Dumb json serialize", "[serialize,node]") {
  50. std::ostringstream stream;
  51. auto writer = serialize::DumbJsonWriter<serialize::NodeWriter, AbstractReference>(stream);
  52. AbstractReference three = make_value<double>(3);
  53. auto add = make_node<Add>();
  54. add->set_property("a", three);
  55. add->set_property("b", three);
  56. AbstractReference add_a = add;
  57. writer.object(add_a);
  58. std::cout << stream.str() << std::endl;
  59. stream.str("");
  60. auto list = make_shared<ListValue<double>>();
  61. list->new_id();
  62. push_value(list, 1.5);
  63. writer.object(list);
  64. std::cout << stream.str() << std::endl;
  65. }
  66. TEST_CASE("Read double", "[serialize]") {
  67. auto x = parse_primitive_type_to<double>("Real", "3.5");
  68. CHECK(x == 3.5);
  69. }
  70. class DummyReader {};
  71. TEST_CASE("Deserialize", "[serialize,node]") {
  72. auto uuid_gen = boost::uuids::random_generator();
  73. serialize::NodeDeserializer<DummyReader> s;
  74. s.object_start(uuid_gen());
  75. s.type("Add");
  76. s.key("a");
  77. s.object_start(uuid_gen());
  78. s.type("Value<Real>");
  79. s.object_value_start();
  80. s.value_string("1");
  81. s.object_value_end();
  82. s.object_end();
  83. s.key("b");
  84. s.object_start(uuid_gen());
  85. s.type("Value<Real>");
  86. s.object_value_start();
  87. s.value_string("2");
  88. s.object_value_end();
  89. s.object_end();
  90. s.object_end();
  91. auto add = s.get_root();
  92. CHECK(add->get_type() == typeid(double));
  93. CHECK(any_cast<double>(add->get_any({})) == 3);
  94. }