dynamic_node.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* dynamic_node.cpp - test dynamic nodes
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <catch.hpp>
  18. #include <core/node_info.h>
  19. #include <core/node/node.h>
  20. #include <core/context.h>
  21. using namespace rainynite;
  22. using namespace rainynite::core;
  23. shared_ptr<Context> zero_context() {
  24. static auto instance = make_shared<Context>();
  25. return instance;
  26. }
  27. AbstractReference make_time_list_0_1() {
  28. auto time_list = make_node_with_name<Node<vector<double>>>("TimeList/Real");
  29. time_list->get_property("step")->set_any(Time(1.0));
  30. time_list->get_property("period")->set_any(TimePeriod(Time(0.0), Time(2.0)));
  31. time_list->set_property("source", make_node_with_name<AbstractValue>("Linear"));
  32. auto untyped = make_node_with_name<Node<Nothing>>("ToUntypedList");
  33. untyped->set_property("source", time_list);
  34. return untyped;
  35. }
  36. TEST_CASE("Test ApplyToList node", "[node]") {
  37. auto apply = make_node_with_name<Node<vector<double>>>("ApplyToList/Real");
  38. auto add = make_node_with_name<AbstractNode>("Add/Real");
  39. add->get_property("a")->set_any(0.5);
  40. apply->set_property("source", dynamic_pointer_cast<AbstractValue>(std::move(add)));
  41. apply->get_property("property_name")->set_any(string("b"));
  42. auto args = apply->get_property("dynamic_arguments");
  43. REQUIRE(args != nullptr);
  44. auto list = dynamic_cast<UntypedListValue*>(args.get());
  45. REQUIRE(list != nullptr);
  46. CHECK(apply->value(zero_context()) == vector<double>{});
  47. SECTION("Simple") {
  48. list->push_back(make_value<double>(1.0));
  49. CHECK(apply->value(zero_context()) == vector<double>{1.5});
  50. list->push_back(make_value<double>(3.0));
  51. CHECK((apply->value(zero_context()) == vector<double>{1.5, 3.5}));
  52. }
  53. SECTION("More complex") {
  54. // This isn't really required..
  55. auto time_map = make_node_with_name<Node<double>>("TimeMap/Real");
  56. time_map->get_property("offset")->set_any(Time(1.0));
  57. time_map->set_property("source", make_node_with_name<AbstractValue>("Linear"));
  58. REQUIRE(time_map->value(zero_context()) == 1.0);
  59. list->push_back(time_map);
  60. CHECK(apply->value(zero_context()) == vector<double>{1.5});
  61. }
  62. SECTION("Preserve context") {
  63. auto time_list = make_time_list_0_1();
  64. apply->set_property("dynamic_arguments", time_list);
  65. auto exp = vector<double>{0.5, 1.5};
  66. CHECK(apply->value(zero_context()) == exp);
  67. }
  68. }
  69. TEST_CASE("Test DynamicListZip node", "[node]") {
  70. auto zip = make_node_with_name<Node<vector<double>>>("DynamicListZip/Real");
  71. zip->get_property("node_type")->set_any(string("Add/Real"));
  72. auto args = zip->get_property("arguments_list");
  73. auto list_of_lists = dynamic_cast<UntypedListValue*>(args.get());
  74. SECTION("Simple") {
  75. auto a_list = make_shared<UntypedListValue>();
  76. auto b_list = make_shared<UntypedListValue>();
  77. list_of_lists->push_back(a_list);
  78. list_of_lists->push_back(b_list);
  79. CHECK(zip->value(zero_context()) == vector<double>{});
  80. a_list->push_back(make_value<double>(1.0));
  81. b_list->push_back(make_value<double>(1.5));
  82. CHECK(zip->value(zero_context()) == vector<double>{2.5});
  83. a_list->push_back(make_value<double>(2.0));
  84. b_list->push_back(make_value<double>(3.5));
  85. CHECK((zip->value(zero_context()) == vector<double>{2.5, 5.5}));
  86. }
  87. SECTION("Preserve context") {
  88. auto t_list = make_time_list_0_1();
  89. list_of_lists->push_back(t_list);
  90. list_of_lists->push_back(t_list);
  91. auto exp = vector<double>{0, 2};
  92. CHECK(zip->value(zero_context()) == exp);
  93. }
  94. }