common_observe.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @file tests/common_observe.cpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2022 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include "artis-star/common/context/Context.hpp"
  26. #include "artis-star/common/observer/Observer.hpp"
  27. #include "artis-star/common/observer/View.hpp"
  28. #include "artis-star/kernel/pdevs/Dynamics.hpp"
  29. #include "artis-star/kernel/pdevs/Simulator.hpp"
  30. #include <artis-star/common/time/DoubleTime.hpp>
  31. #define BOOST_TEST_MODULE Common_Observe_Tests
  32. #include <boost/test/unit_test.hpp>
  33. /*************************************************
  34. * Tests
  35. *************************************************/
  36. class MyDynamics : public artis::pdevs::Dynamics<artis::common::DoubleTime, MyDynamics> {
  37. public:
  38. struct vars {
  39. enum values {
  40. VALUE = 0
  41. };
  42. };
  43. MyDynamics(const std::string &name,
  44. const artis::pdevs::Context<artis::common::DoubleTime, MyDynamics, artis::common::NoParameters> &context)
  45. : artis::pdevs::Dynamics<artis::common::DoubleTime, MyDynamics, artis::common::NoParameters>(name, context) {
  46. observables({{vars::VALUE, "value"}});
  47. }
  48. artis::common::event::Value observe(const artis::common::DoubleTime::type & /* t */,
  49. unsigned int index) const override {
  50. if (index == vars::VALUE) {
  51. return (int) 1;
  52. }
  53. return {};
  54. }
  55. };
  56. BOOST_AUTO_TEST_CASE(Common_Observe_TestCase_1)
  57. {
  58. artis::pdevs::Simulator<artis::common::DoubleTime, MyDynamics> model("A", {});
  59. artis::common::observer::View<artis::common::DoubleTime> view;
  60. artis::common::event::Value ref{1};
  61. view.attachModel(&model);
  62. view.selector("value", {MyDynamics::vars::VALUE});
  63. view.observe(0, true);
  64. BOOST_REQUIRE_EQUAL(view.get("value").back().second, ref);
  65. }
  66. BOOST_AUTO_TEST_CASE(Common_Observe_TestCase_2)
  67. {
  68. artis::pdevs::Simulator<artis::common::DoubleTime, MyDynamics> model("A", {});
  69. auto view = new artis::common::observer::View<artis::common::DoubleTime>();
  70. artis::common::observer::Observer<artis::common::DoubleTime> observer(&model);
  71. artis::common::event::Value ref{1};
  72. view->selector("value", {MyDynamics::vars::VALUE});
  73. observer.attachView("global", view);
  74. observer.init();
  75. observer.observe(0, 1);
  76. BOOST_REQUIRE_EQUAL(observer.views().at("global")->get("value").back().second, ref);
  77. BOOST_REQUIRE_EQUAL(observer.view("global").get("value").back().second, ref);
  78. }