xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/19_diagnostics/stdexceptions.cc (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 // 2001-02-26 Benjamin Kosnik  <bkoz@redhat.com>
2 
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 
21 // 19.1 Exception classes
22 
23 #include <string>
24 #include <stdexcept>
25 #include <testsuite_hooks.h>
26 
27 // libstdc++/1972
28 void test01()
29 {
30   bool test = true;
31   std::string s("lack of sunlight, no water error");
32 
33   // 1
34   std::logic_error obj = std::logic_error(s);
35 
36   // 2
37   // std::logic_error obj((std::string)strlit);
38 
39   VERIFY( std::strcmp(obj.what(), s.data()) == 0 );
40 }
41 
42 void test02()
43 {
44   bool test = true;
45   std::string s("lack of sunlight error");
46   std::domain_error x(s);
47 
48   VERIFY( std::strcmp(x.what(), s.data()) == 0 );
49 }
50 
51 // libstdc++/2089
52 class fuzzy_logic : public std::logic_error
53 {
54 public:
55   fuzzy_logic() : std::logic_error("whoa") { }
56 };
57 
58 void test03()
59 {
60   bool test = true;
61   try
62     { throw fuzzy_logic(); }
63   catch(const fuzzy_logic& obj)
64     { VERIFY( std::strcmp("whoa", obj.what()) == 0 ); }
65   catch(...)
66     { VERIFY( false ); }
67 }
68 
69 // test copy ctors and assignment operators
70 // libstdc++/1972
71 // via Greg Bumgardner <bumgard@roguewave.com>
72 void allocate_on_stack(void)
73 {
74   const size_t num = 512;
75   __extension__ char array[num];
76   for (size_t i = 0; i < num; i++)
77     array[i]=0;
78 }
79 void test04()
80 {
81   bool test = true;
82   const std::string s("CA ISO emergency once again:immediate power down");
83   const char* strlit1 = "wish I lived in Palo Alto";
84   const char* strlit2 = "...or Santa Barbara";
85   std::runtime_error obj1(s);
86 
87   // block 01
88   {
89     const std::string s2(strlit1);
90     std::runtime_error obj2(s2);
91     obj1 = obj2;
92   }
93   allocate_on_stack();
94   VERIFY( std::strcmp(strlit1, obj1.what()) == 0 );
95 
96   // block 02
97   {
98     const std::string s3(strlit2);
99     std::runtime_error obj3 = std::runtime_error(s3);
100     obj1 = obj3;
101   }
102   allocate_on_stack();
103   VERIFY( std::strcmp(strlit2, obj1.what()) == 0 );
104 }
105 
106 int main(void)
107 {
108   test01();
109   test02();
110   test03();
111   test04();
112 
113   return 0;
114 }
115