ARB
arb_error.cxx
Go to the documentation of this file.
1 // ========================================================= //
2 // //
3 // File : test_arb_error.cxx //
4 // Purpose : test header class ARB_ERROR //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in Jan 22 //
7 // http://www.arb-home.de/ //
8 // //
9 // ========================================================= //
10 
11 // --------------------------------------------------------------------------------
12 
13 #ifdef UNIT_TESTS
14 #ifndef TEST_UNIT_H
15 #include <test_unit.h>
16 #endif
17 
18 #include <arb_error.h>
19 #include <arb_msg.h>
20 #include <ErrorOrType.h>
21 
22 static const char *SOME_ERROR = "whatever";
23 
24 #if defined(ASSERTION_USED)
25 
26 static void drop_unused_ARB_ERROR() {
27  ARB_ERROR e0;
28 }
29 static void drop_ARB_ERROR() {
30  ARB_ERROR e1 = SOME_ERROR;
31 }
32 static void overwrite_ARB_ERROR() {
33  ARB_ERROR e1 = SOME_ERROR;
34  ARB_ERROR e2 = SOME_ERROR;
35 
36  e1 = e2; // expect this command to trigger assertion
37  ARB_SIGSEGV(true); // -> this is never reached
38 }
39 
40 #endif
41 
42 static ARB_ERROR forwardError(ARB_ERROR err) {
43  return err;
44 }
45 static ARB_ERROR annotateError(ARB_ERROR err) {
46  if (err) {
47  ARB_ERROR ann_err = GBS_global_string("annotated '%s'", err.deliver());
48  err = ann_err;
49  }
50  return err;
51 }
52 
53 void TEST_misuse_ARB_ERROR_crashtest() {
54  TEST_EXPECT_CODE_ASSERTION_FAILS(drop_unused_ARB_ERROR);
55  TEST_EXPECT_CODE_ASSERTION_FAILS(drop_ARB_ERROR);
56  TEST_EXPECT_CODE_ASSERTION_FAILS(overwrite_ARB_ERROR);
57 }
58 void TEST_class_ARB_ERROR() {
59  // unused error:
60  {
61  ARB_ERROR e0;
63  }
64 
65  // simple error delivery:
66  {
67  ARB_ERROR e1 = SOME_ERROR;
68  TEST_EXPECT_EQUAL(e1.deliver(), SOME_ERROR);
69  }
70 
71  // deliver reassigned error (copy ctor):
72  {
73  ARB_ERROR e1 = SOME_ERROR;
74  ARB_ERROR e2(e1);
75  TEST_EXPECT_EQUAL(e2.deliver(), SOME_ERROR);
76  }
77  // deliver reassigned error (also copy ctor):
78  {
79  ARB_ERROR e1 = SOME_ERROR;
80  ARB_ERROR e2 = e1; // assign in definition == copy-ctor
81  TEST_EXPECT_EQUAL(e2.deliver(), SOME_ERROR);
82  }
83  // deliver reassigned error (op=):
84  {
85  ARB_ERROR e1 = SOME_ERROR;
86  ARB_ERROR e2;
87  e2 = e1; // real assignment
88  TEST_EXPECT_EQUAL(e2.deliver(), SOME_ERROR);
89  }
90 
91  // deliver error forwarded through function:
92  {
93  ARB_ERROR e0;
94  ARB_ERROR e1 = SOME_ERROR;
95  ARB_ERROR f0 = forwardError(e0);
96  ARB_ERROR f1 = forwardError(e1);
98  TEST_EXPECT_EQUAL(f1.deliver(), SOME_ERROR);
99  }
100  // forward and annotate error:
101  {
102  ARB_ERROR e0;
103  ARB_ERROR e1 = SOME_ERROR;
104  ARB_ERROR a0 = annotateError(e0);
105  ARB_ERROR a1 = annotateError(e1);
107  TEST_EXPECT_EQUAL(a1.deliver(), "annotated 'whatever'");
108  }
109 }
110 
111 typedef ErrorOr<int> ErrorOrInt;
112 
113 #if defined(ASSERTION_USED)
114 
115 static void drop_ErrorOr0() {
116  ErrorOrInt e(NULp, 42);
117 }
118 static void drop_ErrorOr1() {
119  ErrorOrInt e(SOME_ERROR, 0);
120 }
121 static void retrieve_unchecked_ErrorOr0() {
122  ErrorOrInt e(NULp, 21);
123  int v = e.getValue(); // getValue w/o previous mandatory hasError
124  TEST_EXPECT_EQUAL(v, 21);
125 }
126 static void retrieve_unchecked_ErrorOr1() {
127  ErrorOrInt e(SOME_ERROR, 0);
128  ARB_ERROR ae = e.getError(); // getError w/o previous mandatory hasError
130 }
131 
132 #endif
133 
134 static ErrorOrInt generateErrorOr(int i) {
135  return ErrorOrInt(NULp, i);
136 }
137 static ErrorOrInt generateErrorOr(const char *msg) {
138  return ErrorOrInt(msg, -1);
139 }
140 
141 void TEST_misuse_ErrorOr_crashtest() {
142  TEST_EXPECT_CODE_ASSERTION_FAILS(drop_ErrorOr0);
143  TEST_EXPECT_CODE_ASSERTION_FAILS(drop_ErrorOr1);
144  TEST_EXPECT_CODE_ASSERTION_FAILS(retrieve_unchecked_ErrorOr0);
145  TEST_EXPECT_CODE_ASSERTION_FAILS(retrieve_unchecked_ErrorOr1);
146 }
147 void TEST_ErrorOr() {
148  // simple uses (create, check, use + destroy):
149  {
150  ErrorOrInt e(SOME_ERROR, 0);
151  TEST_EXPECT(e.hasError());
152  TEST_EXPECT_EQUAL(e.getError().deliver(), SOME_ERROR);
153  }
154  {
155  ErrorOrInt v(NULp, 7);
156  TEST_EXPECT(v.hasValue());
157  TEST_EXPECT_EQUAL(v.getValue(), 7);
158  }
159 
160  // test copy-construction:
161  {
162  ErrorOrInt e(SOME_ERROR, 0);
163  ErrorOrInt c = e;
164  TEST_EXPECT(c.hasError());
165  TEST_EXPECT_EQUAL(c.getError().deliver(), SOME_ERROR);
166  }
167  {
168  ErrorOrInt v(NULp, 17);
169  ErrorOrInt c = v;
170  TEST_EXPECT(c.hasValue());
171  TEST_EXPECT_EQUAL(c.getValue(), 17);
172  }
173  {
174  ErrorOrInt v = generateErrorOr(17);
175  TEST_EXPECT(v.hasValue());
176  TEST_EXPECT_EQUAL(v.getValue(), 17);
177  }
178  {
179  ErrorOrInt e = generateErrorOr(SOME_ERROR);
180  TEST_EXPECT(e.hasError());
181  TEST_EXPECT_EQUAL(e.getError().deliver(), SOME_ERROR);
182  }
183 }
184 
185 
186 #endif // UNIT_TESTS
187 
188 // --------------------------------------------------------------------------------
189 
190 
191 
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
#define ARB_SIGSEGV(backtrace)
Definition: arb_assert.h:174
#define TEST_EXPECT(cond)
Definition: test_unit.h:1328
GB_ERROR deliver() const
Definition: arb_error.h:116
#define TEST_REJECT_NULL(n)
Definition: test_unit.h:1325
#define TEST_EXPECT_NULL(n)
Definition: test_unit.h:1322
#define TEST_EXPECT_CODE_ASSERTION_FAILS(cb)
Definition: test_unit.h:1252
#define NULp
Definition: cxxforward.h:116
#define TEST_EXPECT_EQUAL(expr, want)
Definition: test_unit.h:1294