Avoid undefined behavior in read_subrange_type
[deliverable/binutils-gdb.git] / gdb / unittests / scoped_restore-selftests.c
CommitLineData
9bcb1f16
PA
1/* Self tests for scoped_restore for GDB, the GNU debugger.
2
e2882c85 3 Copyright (C) 2017-2018 Free Software Foundation, Inc.
9bcb1f16
PA
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
24namespace selftests {
25namespace scoped_restore_tests {
26
27struct Base {};
28struct Derived : Base {};
29
30static 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. */
34static scoped_restore_tmpl<int>
35make_scoped_restore_global (int value)
36{
37 return make_scoped_restore (&global, value);
38}
39
40static void
41run_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
106void
107_initialize_scoped_restore_selftests ()
108{
1526853e
SM
109 selftests::register_test ("scoped_restore",
110 selftests::scoped_restore_tests::run_tests);
9bcb1f16 111}
This page took 0.336524 seconds and 4 git commands to generate.