link_storage.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* link_storage.cpp - test link storage
  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/named_link_storage.h>
  19. #include <core/node/make.h>
  20. #include "zero_context.h"
  21. using namespace rainynite;
  22. using namespace rainynite::core;
  23. class RealOnly :
  24. public LinkStorage<
  25. RealOnly,
  26. types::Only<double>,
  27. types::Only<double>
  28. >
  29. {
  30. DEFAULT_VALUES(0.0, 1.0)
  31. };
  32. TEST_CASE("Link storage: only", "[node]") {
  33. auto real = make_unique<RealOnly>();
  34. CHECK(real->get_link_as<double>(0)->value(zero_context()) == 0);
  35. CHECK(real->get_link_as<double>(1)->value(zero_context()) == 1);
  36. real->set_link(0, make_value<double>(1));
  37. real->set_link(1, make_value<double>(2));
  38. CHECK(real->get_link_as<double>(0)->value(zero_context()) == 1);
  39. CHECK(real->get_link_as<double>(1)->value(zero_context()) == 2);
  40. CHECK_THROWS_AS((real->set_link(0, make_value<string>())), NodeAccessError);
  41. }
  42. class RealOrString :
  43. public LinkStorage<
  44. RealOrString,
  45. types::AnyOf<double, string>
  46. >
  47. {
  48. DEFAULT_VALUES(0.0)
  49. };
  50. TEST_CASE("Link storage: any of", "[node]") {
  51. auto links = make_unique<RealOrString>();
  52. CHECK(links->get_link_as<double>(0)->value(zero_context()) == 0);
  53. links->set_link(0, make_value<double>(1.5));
  54. CHECK(links->get_link_as<double>(0)->value(zero_context()) == 1.5);
  55. links->set_link(0, make_value<string>("hello"));
  56. CHECK(links->get_link_as<string>(0)->value(zero_context()) == "hello");
  57. CHECK_THROWS_AS((links->set_link(0, make_value<int>())), NodeAccessError);
  58. }
  59. class AnyLink :
  60. public LinkStorage<
  61. AnyLink,
  62. types::Any
  63. >
  64. {
  65. DEFAULT_VALUES(0.0)
  66. };
  67. TEST_CASE("Link storage: any type", "[node]") {
  68. auto links = make_unique<RealOrString>();
  69. auto zero = make_value<double>(0);
  70. auto hello = make_value<string>("hello");
  71. links->set_link(0, zero);
  72. CHECK(links->get_link_as<double>(0)->value(zero_context()) == 0);
  73. links->set_link(0, hello);
  74. CHECK(links->get_link_as<string>(0)->value(zero_context()) == "hello");
  75. }
  76. class Named :
  77. public NamedLinkStorage<
  78. Named,
  79. types::Only<double>,
  80. types::Any
  81. >
  82. {
  83. NODE_PROPERTIES("real", "any")
  84. DEFAULT_VALUES(0.0, Nothing{})
  85. PROPERTY(real)
  86. PROPERTY(any)
  87. };
  88. TEST_CASE("Named link storage", "[node]") {
  89. auto links = make_unique<Named>();
  90. CHECK_THROWS_AS(links->get_property("invalid"), NodeAccessError);
  91. CHECK(links->real_value<double>(zero_context()) == 0);
  92. CHECK(links->get_property_value<Nothing>("any", zero_context()));
  93. links->set_property("real", make_value<double>(1));
  94. CHECK(links->get_link_as<double>(0)->value(zero_context()) == 1);
  95. CHECK(links->get_property_as<double>("real")->value(zero_context()) == 1);
  96. links->set_property("any", make_value<double>(2));
  97. CHECK(links->get_link_as<double>(1)->value(zero_context()) == 2);
  98. CHECK(links->get_property_as<double>("any")->value(zero_context()) == 2);
  99. links->set_property("any", make_value<string>("hello"));
  100. CHECK(links->get_property_as<string>("any")->value(zero_context()) == "hello");
  101. }