node_tree_modules.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* node_tree_module.cpp - node tree adding module tests
  2. * Copyright (C) 2017-2018 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_tree/traverse.h>
  19. #include "new_node.h"
  20. using namespace rainynite;
  21. using namespace rainynite::core;
  22. class CountDepth : TREE_ELEMENT(CountDepth) {
  23. public:
  24. void added(NodeTree const& tree, NodeTree::Index index) override {
  25. auto p_idx = tree.parent(index);
  26. if (p_idx) {
  27. auto p_d = tree.get_element<CountDepth>(p_idx)->depth();
  28. depth_ = p_d + 1;
  29. } else {
  30. depth_ = 0;
  31. }
  32. }
  33. size_t depth() const {
  34. return depth_;
  35. }
  36. private:
  37. size_t depth_;
  38. };
  39. TEST_CASE("Traverse node tree", "[node]") {
  40. auto root = make_shared<Add>();
  41. auto mid = make_shared<Add>();
  42. auto leaf = mid->get_link(0);
  43. root->set_link(0, mid);
  44. auto tree = NodeTree(root);
  45. auto root_index = tree.get_root_index();
  46. auto mid_index = tree.index(root_index, 0);
  47. auto leaf_index = tree.index(mid_index, 0);
  48. CHECK(tree.get_element<CountDepth>(mid_index)->depth() == 1);
  49. CHECK(tree.get_element<CountDepth>(root_index)->depth() == 0);
  50. CHECK(tree.get_element<CountDepth>(leaf_index)->depth() == 2);
  51. }