scoped_restore-selftests.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Self tests for scoped_restore for GDB, the GNU debugger.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbsupport/selftest.h"
  16. #include "gdbsupport/scoped_restore.h"
  17. namespace selftests {
  18. namespace scoped_restore_tests {
  19. struct Base {};
  20. struct Derived : Base {};
  21. static int global;
  22. /* Check that we can return a scoped_restore from a function. Below
  23. we'll make sure this does the right thing. */
  24. static scoped_restore_tmpl<int>
  25. make_scoped_restore_global (int value)
  26. {
  27. return make_scoped_restore (&global, value);
  28. }
  29. static void
  30. run_tests ()
  31. {
  32. /* Test that single-argument make_scoped_restore restores the
  33. original value on scope exit. */
  34. {
  35. int integer = 0;
  36. {
  37. scoped_restore restore = make_scoped_restore (&integer);
  38. SELF_CHECK (integer == 0);
  39. integer = 1;
  40. }
  41. SELF_CHECK (integer == 0);
  42. }
  43. /* Same, with two-argument make_scoped_restore. */
  44. {
  45. int integer = 0;
  46. {
  47. scoped_restore restore = make_scoped_restore (&integer, 1);
  48. SELF_CHECK (integer == 1);
  49. }
  50. SELF_CHECK (integer == 0);
  51. }
  52. /* Test releasing a scoped_restore. */
  53. {
  54. int integer = 0;
  55. {
  56. scoped_restore restore = make_scoped_restore (&integer, 1);
  57. SELF_CHECK (integer == 1);
  58. restore.release ();
  59. }
  60. /* The overridden value should persist. */
  61. SELF_CHECK (integer == 1);
  62. }
  63. /* Test creating a scoped_restore with a value of a type convertible
  64. to T. */
  65. {
  66. Base *base = nullptr;
  67. Derived derived;
  68. {
  69. scoped_restore restore = make_scoped_restore (&base, &derived);
  70. SELF_CHECK (base == &derived);
  71. }
  72. SELF_CHECK (base == nullptr);
  73. }
  74. /* Test calling a function that returns a scoped restore. Makes
  75. sure that if the compiler emits a call to the copy ctor, that we
  76. still do the right thing. */
  77. {
  78. {
  79. SELF_CHECK (global == 0);
  80. scoped_restore restore = make_scoped_restore_global (1);
  81. SELF_CHECK (global == 1);
  82. }
  83. SELF_CHECK (global == 0);
  84. }
  85. }
  86. } /* namespace scoped_restore_tests */
  87. } /* namespace selftests */
  88. void _initialize_scoped_restore_selftests ();
  89. void
  90. _initialize_scoped_restore_selftests ()
  91. {
  92. selftests::register_test ("scoped_restore",
  93. selftests::scoped_restore_tests::run_tests);
  94. }