dynamic_scoped_test.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  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. #include <gtest/gtest.h>
  17. #include "util/dynamic_scoped_ref.hpp"
  18. DynamicScopedRef<const int> d_value;
  19. void check_function(int expected)
  20. {
  21. ASSERT_EQ(*d_value.get(), expected);
  22. }
  23. TEST(DynamicScopedTest, test)
  24. {
  25. int v1 = 1;
  26. int v2 = 2;
  27. int v3 = 3;
  28. {
  29. auto guard1 = d_value.bind(v1);
  30. ASSERT_TRUE(d_value);
  31. ASSERT_EQ(*d_value, 1);
  32. check_function(1);
  33. {
  34. auto guard2 = d_value.bind(v2);
  35. ASSERT_TRUE(d_value);
  36. ASSERT_EQ(*d_value, 2);
  37. check_function(2);
  38. {
  39. auto guard3 = d_value.bind(v3);
  40. ASSERT_EQ(*d_value, 3);
  41. check_function(3);
  42. }
  43. ASSERT_TRUE(d_value);
  44. check_function(2);
  45. ASSERT_EQ(*d_value, 2);
  46. }
  47. ASSERT_TRUE(d_value);
  48. ASSERT_EQ(*d_value, 1);
  49. check_function(1);
  50. }
  51. ASSERT_FALSE(d_value);
  52. }
  53. /* EOF */