]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/unittests/scoped_restore-selftests.c
Update copyright year range in all GDB files.
[thirdparty/binutils-gdb.git] / gdb / unittests / scoped_restore-selftests.c
1 /* Self tests for scoped_restore for GDB, the GNU debugger.
2
3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "selftest.h"
22 #include "common/scoped_restore.h"
23
24 namespace selftests {
25 namespace scoped_restore_tests {
26
27 struct Base {};
28 struct Derived : Base {};
29
30 static int global;
31
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)
36 {
37 return make_scoped_restore (&global, value);
38 }
39
40 static void
41 run_tests ()
42 {
43 /* Test that single-argument make_scoped_restore restores the
44 original value on scope exit. */
45 {
46 int integer = 0;
47 {
48 scoped_restore restore = make_scoped_restore (&integer);
49 SELF_CHECK (integer == 0);
50 integer = 1;
51 }
52 SELF_CHECK (integer == 0);
53 }
54
55 /* Same, with two-argument make_scoped_restore. */
56 {
57 int integer = 0;
58 {
59 scoped_restore restore = make_scoped_restore (&integer, 1);
60 SELF_CHECK (integer == 1);
61 }
62 SELF_CHECK (integer == 0);
63 }
64
65 /* Test releasing a scoped_restore. */
66 {
67 int integer = 0;
68 {
69 scoped_restore restore = make_scoped_restore (&integer, 1);
70 SELF_CHECK (integer == 1);
71 restore.release ();
72 }
73 /* The overridden value should persist. */
74 SELF_CHECK (integer == 1);
75 }
76
77 /* Test creating a scoped_restore with a value of a type convertible
78 to T. */
79 {
80 Base *base = nullptr;
81 Derived derived;
82 {
83 scoped_restore restore = make_scoped_restore (&base, &derived);
84
85 SELF_CHECK (base == &derived);
86 }
87 SELF_CHECK (base == nullptr);
88 }
89
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. */
93 {
94 {
95 SELF_CHECK (global == 0);
96 scoped_restore restore = make_scoped_restore_global (1);
97 SELF_CHECK (global == 1);
98 }
99 SELF_CHECK (global == 0);
100 }
101 }
102
103 } /* namespace scoped_restore_tests */
104 } /* namespace selftests */
105
106 void
107 _initialize_scoped_restore_selftests ()
108 {
109 selftests::register_test ("scoped_restore",
110 selftests::scoped_restore_tests::run_tests);
111 }