1 /* Self tests for scoped_restore for GDB, the GNU debugger.
3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbsupport/selftest.h"
22 #include "gdbsupport/scoped_restore.h"
25 namespace scoped_restore_tests {
28 struct Derived : Base {};
32 /* Check that we can return a scoped_restore from a function. Below
33 we'll make sure this does the right thing. */
34 static scoped_restore_tmpl<int>
35 make_scoped_restore_global (int value)
37 return make_scoped_restore (&global, value);
43 /* Test that single-argument make_scoped_restore restores the
44 original value on scope exit. */
48 scoped_restore restore = make_scoped_restore (&integer);
49 SELF_CHECK (integer == 0);
52 SELF_CHECK (integer == 0);
55 /* Same, with two-argument make_scoped_restore. */
59 scoped_restore restore = make_scoped_restore (&integer, 1);
60 SELF_CHECK (integer == 1);
62 SELF_CHECK (integer == 0);
65 /* Test releasing a scoped_restore. */
69 scoped_restore restore = make_scoped_restore (&integer, 1);
70 SELF_CHECK (integer == 1);
73 /* The overridden value should persist. */
74 SELF_CHECK (integer == 1);
77 /* Test creating a scoped_restore with a value of a type convertible
83 scoped_restore restore = make_scoped_restore (&base, &derived);
85 SELF_CHECK (base == &derived);
87 SELF_CHECK (base == nullptr);
90 /* Test calling a function that returns a scoped restore. Makes
91 sure that if the compiler emits a call to the copy ctor, that we
92 still do the right thing. */
95 SELF_CHECK (global == 0);
96 scoped_restore restore = make_scoped_restore_global (1);
97 SELF_CHECK (global == 1);
99 SELF_CHECK (global == 0);
103 } /* namespace scoped_restore_tests */
104 } /* namespace selftests */
107 _initialize_scoped_restore_selftests ()
109 selftests::register_test ("scoped_restore",
110 selftests::scoped_restore_tests::run_tests);