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