1 /* Self tests for scoped_restore for GDB, the GNU debugger.
3 Copyright (C) 2017-2024 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/>. */
20 #include "gdbsupport/selftest.h"
21 #include "gdbsupport/scoped_restore.h"
24 namespace scoped_restore_tests
{
27 struct Derived
: Base
{};
31 /* Check that we can return a scoped_restore from a function. Below
32 we'll make sure this does the right thing. */
33 static scoped_restore_tmpl
<int>
34 make_scoped_restore_global (int value
)
36 return make_scoped_restore (&global
, value
);
42 /* Test that single-argument make_scoped_restore restores the
43 original value on scope exit. */
47 scoped_restore restore
= make_scoped_restore (&integer
);
48 SELF_CHECK (integer
== 0);
51 SELF_CHECK (integer
== 0);
54 /* Same, with two-argument make_scoped_restore. */
58 scoped_restore restore
= make_scoped_restore (&integer
, 1);
59 SELF_CHECK (integer
== 1);
61 SELF_CHECK (integer
== 0);
64 /* Test releasing a scoped_restore. */
68 scoped_restore restore
= make_scoped_restore (&integer
, 1);
69 SELF_CHECK (integer
== 1);
72 /* The overridden value should persist. */
73 SELF_CHECK (integer
== 1);
76 /* Test creating a scoped_restore with a value of a type convertible
82 scoped_restore restore
= make_scoped_restore (&base
, &derived
);
84 SELF_CHECK (base
== &derived
);
86 SELF_CHECK (base
== nullptr);
89 /* Test calling a function that returns a scoped restore. Makes
90 sure that if the compiler emits a call to the copy ctor, that we
91 still do the right thing. */
94 SELF_CHECK (global
== 0);
95 scoped_restore restore
= make_scoped_restore_global (1);
96 SELF_CHECK (global
== 1);
98 SELF_CHECK (global
== 0);
102 } /* namespace scoped_restore_tests */
103 } /* namespace selftests */
105 void _initialize_scoped_restore_selftests ();
107 _initialize_scoped_restore_selftests ()
109 selftests::register_test ("scoped_restore",
110 selftests::scoped_restore_tests::run_tests
);