]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/infra/testsuite/cyginfra/assert.exp
Initial revision
[karo-tx-redboot.git] / tools / src / infra / testsuite / cyginfra / assert.exp
1 #===============================================================================
2 #
3 #    assert.exp
4 #
5 #    Assertion test cases
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-11-25
38 #
39 #####DESCRIPTIONEND####
40 #===============================================================================
41
42 # ----------------------------------------------------------------------------
43 # Start with the simple tests.
44 ${tool}_load tassert1
45 ${tool}_load tassert2
46 ${tool}_load tassert3
47 ${tool}_load tassert4
48
49 # tassert5 is not buildable under Linux. It is a C program linked with
50 # a C++ library, and there are dependencies on the default new and
51 # delete operators which are not satisfied.
52
53 if { [string match "cl*" $::hosttest_data(CC)] } {
54     ${tool}_load tassert5
55 } else {
56     unsupported "using the infrastructure from C code"
57 }
58
59 ${tool}_load tassert6
60 ${tool}_load tassert7
61
62 # ----------------------------------------------------------------------------
63 # tassert8 is a bit more complicated. It involves an assertion which
64 # is not caught in any way by the application code. Therefore the
65 # output of the program has to be analysed to make sure it is
66 # reasonable. There is also going to be a dump file that needs
67 # to be analysed and cleaned up.
68
69 proc tassert8_filter { name result output } {
70
71     set all_ok 1
72     
73     if { $result == 0 } {
74         fail "testcase $name should have a non-zero exit code"
75         set all_ok 0
76     }
77
78     # Convert the output to a list of lines.
79     set lines [split $output "\n"]
80
81     # The first line of interest should contain the phrase
82     # "Assertion failure" and the string embedded in tassert8.cxx
83     while { 1 } {
84         if { [llength $lines] == 0 } {
85             set all_ok 0
86             fail "No \"Assertion failure\" message detected in program output"
87             break
88         }
89         set line  [lindex $lines 0]
90         set lines [lreplace $lines 0 0]
91         if { [regexp -nocase -- {^assertion failure.*it seemed like a good idea at the time$} $line] } {
92             break
93         }
94     }
95     
96     # The next line should indicate the file and the line number
97     if { [llength $lines] == 0 } {
98         set all_ok 0
99         fail "No file name or line number information"
100     } else {
101         set line  [lindex $lines 0]
102         set lines [lreplace $lines 0 0]
103         if { [regexp -nocase -- {^file .*tassert8.cxx.*line number [0-9]+$} $line] == 0} {
104             fail "Output did not contain the expected filename and linenumber"
105         }
106     }
107     # There may or may not be a line containing the function name.
108     # This should not be checked, it depends on compiler support
109     # for __PRETTY_FUNCTION__. The next line of interest is
110     # "Writing additional output to xxx", where xxx is a filename.
111     while { 1 } {
112         if { [llength $lines] == 0 } {
113             set all_ok 0
114             fail "Output did not specify where the dump information was stored"
115             break
116         } else {
117             set line  [lindex $lines 0]
118             set lines [lreplace $lines 0 0]
119             set dummy ""
120             set match ""
121             if { [regexp -- {^Writing additional output to (.*)$} $line dummy match] } {
122                 tassert8_process_dump $match all_ok
123                 break
124             }
125         }
126     }
127     
128     if { $all_ok } {
129         pass "Assertions generate sensible output"
130     }
131
132     return 0
133 }
134
135 proc tassert8_process_dump { filename all_ok_arg } {
136
137     upvar $all_ok_arg all_ok
138     set realname [hosttest_translate_existing_filename $filename]
139     if { $realname == "" } {
140         set all_ok 0
141         fail "Unable to find assertion dump file $filename"
142         return
143     }
144     set lines {}
145     set status [ catch {
146         set fd   [open $realname r]
147         set data [read $fd]
148         close $fd
149         set lines [split $data "\n"]
150         if { [llength $lines] == 0 } {
151             set all_ok 0
152             fail "The assertion dump file $realname contains no data"
153         }
154     } message ]
155     if { $status != 0 } {
156         set all_ok 0
157         fail "Unable to open assertion output file $realname, $message"
158     } 
159     set status [ catch { file delete $realname } message ]
160     if { $status != 0 } {
161         warning "Unable to delete assertion dump file $realname, $message" 0
162     }
163     if { [llength $lines] == 0 } {
164         return
165     }
166
167     # We have some data to process. The information should include
168     # the following:
169     # 1) a line Assertion failure msg
170     # 2) a line with the filename and the linenumber
171     # 3) optionally a line with the function name. This depends on
172     #    compiler support.
173     # 4) information from callback1
174     # 5) information from callback2
175     #
176     # The relative order of (4) and (5) is not defined.
177     while { 1 } {
178         if { [llength $lines] == 0 } {
179             set all_ok 0
180             fail "No \"Assertion failure\" message detected in output file"
181             break
182         }
183         set line  [lindex $lines 0]
184         set lines [lreplace $lines 0 0]
185         if { [regexp -nocase -- {^assertion failure.*it seemed like a good idea at the time$} $line] } {
186             break
187         }
188     }
189     
190     if { [llength $lines] == 0 } {
191         set all_ok 0
192         fail "No file name or line number information"
193     } else {
194         set line  [lindex $lines 0]
195         set lines [lreplace $lines 0 0]
196         if { [regexp -nocase -- {^file .*tassert8.cxx.*line number [0-9]+$} $line] == 0} {
197             set all_ok 0
198             fail "Output did not contain the expected filename and linenumber"
199         }
200     }
201
202     set seen_callback1 0
203     set seen_callback2 0
204     while { [llength $lines] > 0 } {
205         
206         set line  [lindex $lines 0]
207         set lines [lreplace $lines 0 0]
208
209         if { [regexp -nocase -- {^\# \{\{\{.*callback1.*$} $line] } {
210             if { $seen_callback1 != 0 } {
211                 set all_ok 0
212                 fail "Output contains multiple occurrences of callback1"
213                 continue
214             }
215             set seen_callback1 1
216
217             while { [llength $lines] > 0 } {
218                 set line  [lindex   $lines 0]
219                 set lines [lreplace $lines 0 0]
220
221                 if { [regexp -nocase -- {^\# \}\}\}.*$} $line] } {
222                     break
223                 }
224
225                 # callback1 should not generate any output so only blank lines
226                 # are acceptable
227                 if { [regexp -nocase -- {^ *$} $line] != 1} {
228                     set all_ok 0
229                     fail "Unexpected data in callback1 output: $line"
230                     # Do not repeat this failure message. This break will
231                     # do near enough the right thing.
232                     break
233                 }
234             }
235             
236         } elseif  { [regexp -nocase -- {^\# \{\{\{.*callback2.*$} $line] } {
237             if { $seen_callback2 != 0 } {
238                 set all_ok 0
239                 fail "Output contains multiple occurrences of callback2"
240             }
241             set seen_callback2 1
242
243             while { [llength $lines] > 0 } {
244                 set line  [lindex   $lines 0]
245                 set lines [lreplace $lines 0 0]
246
247                 if { [regexp -nocase -- {^\# \}\}\}.*$} $line] } {
248                     break
249                 }
250
251                 # callback2 is allowed to generate blank lines and
252                 # fixed lines.
253                 if { [regexp -nocase -- {^ *$} $line] == 1 } {
254                     continue
255                 }
256                 if { $line == "callback2 output" } {
257                     continue
258                 }
259                 set all_ok 0
260                 fail "Unexpected data in callback2 output: $line"
261             }
262             
263         } 
264     }
265     if { ($seen_callback1 == 0) || ($seen_callback2 == 0) } {
266         set all_ok 0
267         fail "Output did not contain all the callback information"
268     }
269 }
270
271 hosttest_run_test_with_filter tassert8 tassert8_filter {} {} {} cyginfra {}
272
273 # ----------------------------------------------------------------------------
274 # Strictly speaking this is not an assertion test. However there are some
275 # support routines in hosttest.exp which are tried closely to the
276 # implementation of the assertion code, and it is worthwhile checking
277 # these. The tassert8 testcase can be reused for this.
278
279 proc tassert9_filter { name result output } {
280
281     if { [hosttest_assert_check $result $output] == 0 } {
282         fail "testcase did not generate a recognised assertion"
283         return
284     }
285
286     set output [hosttest_assert_read_dump $output]
287     if { $output == "" } {
288         fail "testcase did not generate a recognised assertion dump"
289         return
290     }
291
292     set all_ok 1
293     
294     set callback1_output [hosttest_assert_extract_callback $output "callback1"]
295     set callback2_output [hosttest_assert_extract_callback $output "callback2"]
296
297     # Callback1 output should be empty, all blank lines should have been filtered
298     # out.
299     if { $callback1_output != "" } {
300         set all_ok 0
301         fail "callback1 output should be empty"
302     }
303     set lines [split $callback2_output "\n"]
304     if { [llength $lines] == 0 } {
305         set all_ok 0
306         fail "callback2 should have produced some output"
307     } elseif { [llength $lines] < 10} {
308         set all_ok 0
309         fail "callback2 is supposted to have at least ten lines of output"
310     } else {
311         # There should be ten lines of output, possibly followed by
312         # some blanks.
313         for { set i 0 } { $i < 10 } { incr i } {
314             set line  [lindex $lines 0]
315             set lines [lreplace $lines 0 0]
316             if { $line != "callback2 output" } {
317                 set all_ok 0
318                 fail "incorrect output from callback2"
319                 break
320             }
321         }
322
323         while { [llength $lines] > 0 } {
324             set line  [lindex $lines 0]
325             set lines [lreplace $lines 0 0]
326             if { [regexp -- {^ *$} $line] != 1 } {
327                 set all_ok 0
328                 fail "callback2 output contains unexpected data"
329                 break
330             }
331         }
332     }
333
334     if { $all_ok } {
335         pass "assertion output and dump file format match test harness expectations"
336     }
337     return 0
338 }
339
340 hosttest_run_test_with_filter tassert9 tassert9_filter tassert8.cxx {} {} cyginfra {}
341