Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.cp / try_catch.cc
CommitLineData
335928ac
MC
1/* This test script is part of GDB, the GNU debugger.
2
88b9d363 3 Copyright 2002-2022 Free Software Foundation, Inc.
335928ac
MC
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
a9762ec7 7 the Free Software Foundation; either version 3 of the License, or
335928ac
MC
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
a9762ec7 14
335928ac 15 You should have received a copy of the GNU General Public License
47d48711 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9bba8c8f
MC
17
18#include <exception>
19#include <stdexcept>
20#include <string>
f53b3eeb 21#include <string.h>
9bba8c8f 22
76565097 23enum region { oriental, egyptian, greek, etruscan, roman };
9bba8c8f 24
76565097
DC
25// Test one.
26class gnu_obj_1
27{
28public:
29 typedef region antiquities;
30 const bool test;
31 const int key1;
32 long key2;
9bba8c8f 33
76565097 34 antiquities value;
9bba8c8f 35
76565097
DC
36 gnu_obj_1(antiquities a, long l): test(true), key1(5), key2(l), value(a) {}
37};
9bba8c8f 38
76565097
DC
39// Test two.
40template<typename T>
41class gnu_obj_2: public virtual gnu_obj_1
42{
43public:
44 antiquities value_derived;
45
46 gnu_obj_2(antiquities b): gnu_obj_1(oriental, 7), value_derived(b) { }
47};
9bba8c8f 48
76565097
DC
49// Test three.
50template<typename T>
51class gnu_obj_3
52{
53public:
54 typedef region antiquities;
55 gnu_obj_2<int> data;
9bba8c8f 56
76565097
DC
57 gnu_obj_3(antiquities b): data(etruscan) { }
58};
9bba8c8f
MC
59
60int main()
61{
9bba8c8f
MC
62 bool test = true;
63 const int i = 5;
64 int j = i;
65 gnu_obj_2<long> test2(roman);
66 gnu_obj_3<long> test3(greek);
67
68 // 1
69 try
70 {
71 ++j;
76565097 72 throw gnu_obj_1(egyptian, 4589); // marker 1-throw
9bba8c8f
MC
73 }
74 catch (gnu_obj_1& obj)
75 {
76 ++j;
76565097 77 if (obj.value != egyptian) // marker 1-catch
9bba8c8f
MC
78 test &= false;
79 if (obj.key2 != 4589)
80 test &= false;
81 }
82 catch (...)
83 {
84 j = 0;
85 test &= false;
86 }
87
88 // 2
89 try
90 {
76565097 91 ++j; // marker 2-start
9bba8c8f
MC
92 try
93 {
76565097 94 ++j; // marker 2-next
9bba8c8f
MC
95 try
96 {
97 ++j;
76565097 98 throw gnu_obj_1(egyptian, 4589); // marker 2-throw
9bba8c8f
MC
99 }
100 catch (gnu_obj_1& obj)
101 {
102 ++j;
76565097 103 if (obj.value != egyptian) // marker 2-catch
9bba8c8f
MC
104 test &= false;
105 if (obj.key2 != 4589)
106 test &= false;
107 }
108 }
109 catch (gnu_obj_1& obj)
110 {
111 ++j;
112 if (obj.value != egyptian)
113 test &= false;
114 if (obj.key2 != 4589)
115 test &= false;
116 }
117 }
118 catch (...)
119 {
120 j = 0;
121 test &= false;
122 }
123
124 // 3 use standard library
125 using namespace std;
126 try
127 {
128 if (j < 100)
76565097 129 throw invalid_argument("gdb.1"); // marker 3-throw
9bba8c8f
MC
130 }
131 catch (exception& obj)
132 {
f53b3eeb 133 if (strcmp (obj.what(), "gdb.1") != 0) // marker 3-catch
9bba8c8f
MC
134 test &= false;
135 }
f53b3eeb 136 return 0; // marker test-complete
9bba8c8f 137}
This page took 2.635667 seconds and 4 git commands to generate.