rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-error.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #ifdef HAVE_UNISTD_H
4 #include <unistd.h>
5 #endif
6 
7 #include <iostream>
8 #include <fstream>
9 #include <string>
10 #include <cassert>
11 #include <cerrno>
12 
13 #include "asserts.h"
14 #include "error.h"
15 
16 void test1(void)
17 {
18  error_instance ei;
19 
20  assert(ei.what() == "");
21 #ifdef __GNUC__
22  assert(ei.where() == "");
23 #endif
24  assert(ei.file() == "");
25  assert(ei.line() == 0);
26 }
27 
28 void test2(void)
29 {
30  error e(0);
31 
32  e = ERROR(0,"Simulated error instance");
33  assert(e.num() == 0);
34  assert(!e.internal());
35  assert(e.size() == 1);
36  assert(e[0].what() == "Simulated error instance");
37 
38  e = INTERNAL_ERROR(0,"Simulated internal error instance");
39  assert(e.num() == 0);
40  assert(e.internal());
41  assert(e.size() == 1);
42  assert(e[0].what() == "Simulated internal error instance");
43 }
44 
45 void test3(void)
46 {
47  error e(0);
48 
49  e = ERROR(1,"Simulated error instance");
50  assert(e.num() == 1);
51  assert(!e.internal());
52  assert(e.size() == 1);
53  assert(e[0].what() == "Simulated error instance");
54 
55  e = INTERNAL_ERROR(0,"Simulated internal error instance");
56  assert(e.num() == 0);
57  assert(e.internal());
58  assert(e.size() == 1);
59  assert(e[0].what() == "Simulated internal error instance");
60 
61  e = INTERNAL_ERROR(1,"Simulated internal error instance");
62  assert(e.num() == 1);
63  assert(e.internal());
64  assert(e.size() == 1);
65  assert(e[0].what() == "Simulated internal error instance");
66 }
67 
68 void test4b(void)
69 {
70  throw(ERROR(1,"Simulated error for test4b"));
71 }
72 
73 void test4a(void)
74 {
75  try {
76  test4b();
77  }
78  catch(error e) {
79  e.push_back(ERROR_INSTANCE("Failure in test4a"));
80  throw(e);
81  }
82 }
83 
84 void test4(void)
85 {
86  bool thrown = false;
87 
88  try {
89  test4a();
90  }
91  catch(error e) {
92  e.push_back(ERROR_INSTANCE("Failure in test4"));
93  assert(e.num() == 1);
94  assert(!e.internal());
95  assert(e.size() == 3);
96  assert(e[0].what() == "Simulated error for test4b");
97  assert(e[1].what() == "Failure in test4a");
98  assert(e[2].what() == "Failure in test4");
99  thrown = true;
100  }
101 
102  assert(thrown);
103 }
104 
105 void test5b(void)
106 {
107  throw(INTERNAL_ERROR(1,"Simulated error for test5b"));
108 }
109 
110 void test5a(void)
111 {
112  try {
113  test5b();
114  }
115  catch(error e) {
116  e.push_back(ERROR_INSTANCE("Failure in test5a"));
117  throw(e);
118  }
119 }
120 
121 void test5(void)
122 {
123  bool thrown = false;
124 
125  try {
126  test5a();
127  }
128  catch(error e) {
129  e.push_back(ERROR_INSTANCE("Failure in test5"));
130  assert(e.num() == 1);
131  assert(e.internal());
132  assert(e.size() == 3);
133  assert(e[0].what() == "Simulated error for test5b");
134  assert(e[1].what() == "Failure in test5a");
135  assert(e[2].what() == "Failure in test5");
136  thrown = true;
137  }
138 
139  assert(thrown);
140 }
141 
142 void test6(void)
143 {
144  error e1(0), e2(0);
145 
146  e1.push_back(ERROR_INSTANCE("Testing:"));
147  e1.push_back(ERROR_INSTANCE("One..."));
148  e1.push_back(ERROR_INSTANCE("Two..."));
149  e1.push_back(ERROR_INSTANCE("Three..."));
150 
151  assert(e1.size() == 4);
152  assert(e1[0].what() == "Testing:");
153  assert(e1[1].what() == "One...");
154  assert(e1[2].what() == "Two...");
155  assert(e1[3].what() == "Three...");
156 
157  e2 = e1;
158 
159  assert(e2.size() == 4);
160  assert(e2[0].what() == "Testing:");
161  assert(e2[1].what() == "One...");
162  assert(e2[2].what() == "Two...");
163  assert(e2[3].what() == "Three...");
164 }
165 
166 #ifndef ASSUME_STL_MEMORY_EXCEPTION
167 void test7a(void)
168 {
169  std::string str = "a";
170 
171  while (true) {
172  TRY(str += str,"Could not append to string");
173  }
174 }
175 
176 void test7(void)
177 {
178  bool thrown = false;
179  bool identified = false;
180 
181  try {
182  test7a();
183  }
184  catch(error e) {
185  // std::cerr << e;
186  thrown = true;
187  identified = true;
188  }
189  catch(...) {
190  // std::cerr << err_unknown;
191  assert(errno == ENOMEM);
192  thrown = true;
193  }
194 
195  assert(thrown);
196  assert(identified);
197 }
198 #endif
199 
200 void test8(void)
201 {
202  error e(err_nomem);
203 
204  assert(e.num() == ENOMEM);
205  assert(e.size() == 1);
206  assert(e[0].what() == "Out of memory");
207 }
208 
209 int main(int argc, char const * argv[])
210 {
211  try {
212  test1();
213  test2();
214  test3();
215  test4();
216  test5();
217  test6();
218 #ifndef ASSUME_STL_MEMORY_EXCEPTION
219 #ifdef HAVE_EXCEPTION_ON_ALLOC_FAILURE
220  test7();
221 #endif
222 #endif
223  test8();
224  }
225  catch(error e) {
226  std::cerr << e;
227  assert(0);
228  }
229  catch(...) {
230  std::cerr << err_unknown;
231  assert(0);
232  }
233  return(0);
234 }
#define err_nomem
Definition: error.h:116
void test3(void)
Definition: test-error.cc:45
Instance of a single error containing a descriptive error message and the location in the file that t...
Definition: error.h:17
void push_back(const error_instance &a_e)
Definition: error.cc:215
void num(int a_i)
Definition: error.cc:205
void test7(void)
Definition: test-exec.cc:332
const uint16 line(void) const
Definition: error.cc:121
const std::string what(void) const
Definition: error.cc:104
void test6(void)
Definition: test-error.cc:142
const std::string file(void) const
Definition: error.cc:116
int main(int argc, char const *argv[])
Definition: test-error.cc:209
#define err_unknown
Definition: error.h:114
void test8(void)
Definition: test-error.cc:200
void internal(bool a_i)
Definition: error.cc:195
void test4a(void)
Definition: test-error.cc:73
An error class.
Definition: error.h:72
void test4b(void)
Definition: test-error.cc:68
#define TRY(code, es)
Definition: error.h:126
#define INTERNAL_ERROR(e, s)
Definition: error.h:123
void test5a(void)
Definition: test-error.cc:110
void test2(void)
Definition: test-error.cc:28
#define ERROR_INSTANCE(s)
Definition: error.h:67
void test4(void)
Definition: test-error.cc:84
void test1(void)
Definition: test-error.cc:16
void test5b(void)
Definition: test-error.cc:105
#define ERROR(e, s)
Definition: error.h:120
void test5(void)
Definition: test-error.cc:121