]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/infra/testsuite/cyginfra/tassert7.cxx
Initial revision
[karo-tx-redboot.git] / tools / src / infra / testsuite / cyginfra / tassert7.cxx
1 //==========================================================================
2 //
3 //      tassert7.cxx
4 //
5 //      Assertion test case                                                                
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //                                                                          
10 // ----------------------------------------------------------------------------
11 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
12 //
13 // This file is part of the eCos host tools.
14 //
15 // This program is free software; you can redistribute it and/or modify it 
16 // under the terms of the GNU General Public License as published by the Free 
17 // Software Foundation; either version 2 of the License, or (at your option) 
18 // any later version.
19 // 
20 // This program is distributed in the hope that it will be useful, but WITHOUT 
21 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
22 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
23 // more details.
24 // 
25 // You should have received a copy of the GNU General Public License along with
26 // this program; if not, write to the Free Software Foundation, Inc., 
27 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 //
29 // ----------------------------------------------------------------------------
30 //                                                                          
31 //####COPYRIGHTEND####
32 //==========================================================================
33 //#####DESCRIPTIONBEGIN####                                             
34 //
35 // Author(s):           bartv
36 // Contributors:        bartv
37 // Date:                1998-12-23
38 // Purpose:
39 // Description:         This routine checks the invariant assertions.
40 //                      The entry tests will have been taken care of by
41 //                      tassert6.cxx, but it is also necessary to check
42 //                      the exit tests.
43 //
44 //####DESCRIPTIONEND####
45 //==========================================================================
46
47
48 #define CYG_DECLARE_HOST_ASSERTION_SUPPORT
49 #define CYGDBG_USE_ASSERTS
50 #define CYGDBG_INFRA_DEBUG_PRECONDITIONS
51 #define CYGDBG_INFRA_DEBUG_POSTCONDITIONS
52 #define CYGDBG_INFRA_DEBUG_LOOP_INVARIANTS
53 #define CYGDBG_INFRA_DEBUG_INVARIANTS
54
55 #include <cyg/infra/testcase.h>
56 #include <cyg/infra/cyg_ass.h>
57 #include <cstdlib>
58 #include <csetjmp>
59 #include <cstring>
60
61 // This is used to "recover" from an assertion failure
62 static jmp_buf setjmp_buffer;
63
64 // The number of assertions that have triggered.
65 static int failed_assertions = 0;
66
67 // The number of assertions that have been triggered.
68 static int counter = 0;
69
70 // Are objects currently valid?
71 static bool check_this_should_fail = false;
72
73 static const char message[] = "so long and thanks for all the fish";
74
75 class dummy {
76   private:
77     int         random;
78
79   public:
80     dummy() {
81         random = rand();
82     }
83     ~dummy() {
84         random = 0;
85     }
86
87     void invariant1();
88     void invariant2();
89     static void invariant3(dummy&);
90     static void invariant4(dummy&);
91     static void invariant5(dummy*);
92     static void invariant6(dummy*);
93     bool check_this(cyg_assert_class_zeal) const;
94 };
95
96 bool
97 dummy::check_this(cyg_assert_class_zeal zeal) const
98 {
99     // The default zeal should be cyg_quick.
100     switch(zeal) {
101     case cyg_quick:
102         return !check_this_should_fail;
103         
104     case cyg_system_test:
105     case cyg_extreme:
106     case cyg_thorough:
107     case cyg_trivial:
108     case cyg_none:
109         CYG_TEST_FAIL_FINISH("incorrect default zeal passed to check_this() member function");
110         break;
111     default:
112         CYG_TEST_FAIL_FINISH("invalid zeal passed to check_this() member function");
113         break;
114     }
115     return false;
116 }
117
118 void
119 dummy::invariant1(void)
120 {
121     CYG_INVARIANT_THIS(dummy, message);
122     check_this_should_fail = true;
123 }
124
125 void
126 dummy::invariant2(void)
127 {
128     CYG_INVARIANT_THISC(dummy);
129     check_this_should_fail = true;
130 }
131
132 void
133 dummy::invariant3(dummy& obj)
134 {
135     CYG_INVARIANT_CLASSO(dummy, obj, message);
136     check_this_should_fail = true;
137 }
138
139 void
140 dummy::invariant4(dummy& obj)
141 {
142     CYG_INVARIANT_CLASSOC(dummy, obj);
143     check_this_should_fail = true;
144 }
145
146 void
147 dummy::invariant5(dummy* obj)
148 {
149     CYG_INVARIANT_CLASS(dummy, obj, message);
150     check_this_should_fail = true;
151 }
152
153 void
154 dummy::invariant6(dummy* obj)
155 {
156     CYG_INVARIANT_CLASSC(dummy, obj);
157     check_this_should_fail = true;
158 }
159
160 extern "C"
161 bool
162 failure_handler(const char* fn, const char* file, cyg_uint32 line, const char* msg)
163 {
164     if (false == check_this_should_fail) {
165         CYG_TEST_FAIL("assertion triggered when everything should be ok");
166     }
167     failed_assertions++;
168     counter++;
169     longjmp(setjmp_buffer, 1);
170     return true;
171 }
172
173 int
174 main(int argc, char **argv)
175 {
176     dummy object;
177
178     cyg_assert_install_failure_handler(&failure_handler);
179     setjmp(setjmp_buffer);
180     
181     for ( bool done = false; !done; counter++ ) {
182         check_this_should_fail = false;
183         
184         switch(counter) {
185         case 0:
186             object.invariant1();
187             CYG_TEST_FAIL("CYG_INVARIANT_THIS() test should not have returned");
188             break;
189             
190         case 1:
191             object.invariant2();
192             CYG_TEST_FAIL("CYG_INVARIANT_THISC() test should not have returned");
193             break;
194             
195         case 2:
196             dummy::invariant3(object);
197             CYG_TEST_FAIL("CYG_INVARIANT_CLASSO() test should not have returned");
198             break;
199             
200         case 3:
201             dummy::invariant4(object);
202             CYG_TEST_FAIL("CYG_INVARIANT_CLASSOC() test should not have returned");
203             break;
204             
205         case 4:
206             dummy::invariant5(&object);
207             CYG_TEST_FAIL("CYG_INVARIANT_CLASS() test should not have returned");
208             break;
209             
210         case 5:
211             dummy::invariant6(&object);
212             CYG_TEST_FAIL("CYG_INVARIANT_CLASSC() test should not have returned");
213             break;
214
215         default:
216             done = true;
217             counter--;  // About to get incremented again...
218             break;
219         }
220     }
221
222     if (failed_assertions != 6) {
223         CYG_TEST_FAIL("Broken test case, not all assertions have been tried");
224     }
225     
226     if (failed_assertions == counter) {
227         CYG_TEST_PASS("All assertions trigger successfully");
228     } else {
229         CYG_TEST_FAIL("Not all assertions trigger");
230     }
231     return 0;
232 }