* config/rs6000/rs6000.c (rs6000_deligitimze_address): Do not
[official-gcc.git] / libgo / testsuite / gotest
blobbbe77cce09d5649efb3fa865bf964893eff86e5e
1 #!/bin/sh
2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # Using all the *_test.go files in the current directory, write out a file
7 # _testmain.go that runs all its tests. Compile everything and run the
8 # tests.
9 # If files are named on the command line, use them instead of *_test.go.
11 # Makes egrep,grep work better in general if we put them
12 # in ordinary C mode instead of what the current language is.
13 unset LANG
14 export LC_ALL=C
15 export LC_CTYPE=C
17 GC=${GC:-gccgo}
18 GL=${GL:-${GC-gccgo}}
19 GOLIBS=${GOLIBS:-}
20 export GC GL GOLIBS
22 NM=${NM:-nm}
24 # srcdir is where the source files are found. basedir is where the
25 # source file paths are relative to.
26 # gofiles are the test files. pkgfiles are the source files.
27 srcdir=.
28 basedir=.
29 gofiles=""
30 pkgfiles=""
31 loop=true
32 keep=false
33 pkgpath=
34 prefix=
35 dejagnu=no
36 GOARCH=""
37 timeout=240
38 testname=""
39 trace=false
40 while $loop; do
41 case "x$1" in
42 x--srcdir)
43 srcdir=$2
44 shift
45 shift
47 x--srcdir=*)
48 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
49 shift
51 x--basedir)
52 basedir=$2
53 shift
54 shift
56 x--basedir=*)
57 basedir=`echo $1 | sed -e 's/^--basedir=//'`
58 shift
60 x--pkgpath)
61 pkgpath=$2
62 shift
63 shift
65 x--pkgpath=*)
66 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
67 shift
69 x--prefix)
70 prefix=$2
71 shift
72 shift
74 x--prefix=*)
75 prefix=`echo $1 | sed -e 's/^--prefix=//'`
76 shift
78 x--keep)
79 keep=true
80 shift
82 x--pkgfiles)
83 pkgfiles=$2
84 shift
85 shift
87 x--pkgfiles=*)
88 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
89 shift
91 x--dejagnu)
92 dejagnu=$2
93 shift
94 shift
96 x--dejagnu=*)
97 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
98 shift
100 x--goarch)
101 GOARCH=$2
102 shift
103 shift
105 x--goarch=*)
106 GOARCH=`echo $1 | sed -e 's/^--goarch=//'`
107 shift
109 x--timeout)
110 timeout=$2
111 shift
112 shift
114 x--timeout=*)
115 timeout=`echo $1 | sed -e 's/^--timeout=//'`
116 shift
118 x--testname)
119 testname=$2
120 shift
121 shift
123 x--testname=*)
124 testname=`echo $1 | sed -e 's/^--testname=//'`
125 shift
127 x--trace)
128 trace=true
129 shift
131 x-*)
132 loop=false
135 loop=false
138 gofiles="$gofiles $1"
139 shift
141 esac
142 done
144 DIR=gotest$$
145 rm -rf $DIR
146 mkdir $DIR
148 cd $DIR
149 mkdir test
150 cd test
152 if test $keep = false; then
153 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
154 else
155 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
158 case "$srcdir" in
162 srcdir="../../$srcdir"
164 esac
166 SRCDIR=$srcdir
167 export SRCDIR
169 case "$basedir" in
173 basedir="../../$basedir"
175 esac
177 # Link all the files/directories in srcdir into our working directory,
178 # so that the tests do not have to refer to srcdir to find test data.
179 ln -s $srcdir/* .
181 # Some tests refer to a ../testdata directory.
182 if test -e $srcdir/../testdata; then
183 rm -f ../testdata
184 abssrcdir=`cd $srcdir && pwd`
185 ln -s $abssrcdir/../testdata ../testdata
188 # Copy the .go files because io/utils_test.go expects a regular file.
189 case "x$gofiles" in
191 case "x$pkgfiles" in
193 for f in `cd $srcdir; ls *.go`; do
194 rm -f $f;
195 cp $srcdir/$f .
196 done
199 for f in $pkgfiles; do
200 if test -f $basedir/$f; then
201 b=`basename $f`
202 rm -f $b
203 cp $basedir/$f $b
204 elif test -f ../../$f; then
205 b=`basename $f`
206 rm -f $b
207 cp ../../$f $b
208 else
209 echo "file $f not found" 1>&2
210 exit 1
212 done
213 for f in `cd $srcdir; ls *_test.go`; do
214 rm -f $f
215 cp $srcdir/$f .
216 done
218 esac
221 for f in $gofiles; do
222 b=`basename $f`
223 rm -f $b
224 cp $basedir/$f $b
225 done
226 case "x$pkgfiles" in
228 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
229 rm -f $f
230 cp $srcdir/$f .
231 done
234 for f in $pkgfiles; do
235 if test -f $basedir/$f; then
236 b=`basename $f`
237 rm -f $b
238 cp $basedir/$f $b
239 elif test -f ../../$f; then
240 b=`basename $f`
241 rm -f $b
242 cp ../../$f $b
243 else
244 echo "file $f not found" 1>&2
245 exit 1
247 done
249 esac
251 esac
253 # Some tests expect the _obj directory created by the gc Makefiles.
254 mkdir _obj
256 # Some tests expect the _test directory created by the gc Makefiles.
257 mkdir _test
259 case "x$gofiles" in
261 gofiles=`ls *_test.go 2>/dev/null`
262 esac
264 case "x$gofiles" in
266 echo 'no test files found' 1>&2
267 exit 1
268 esac
270 # Run any commands given in sources, like
271 # // gotest: $GC foo.go
272 # to build any test-only dependencies.
273 holdGC="$GC"
274 GC="$GC -g -c -I ."
275 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
276 GC="$holdGC"
278 case "x$pkgfiles" in
280 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
283 for f in $pkgfiles; do
284 pkgbasefiles="$pkgbasefiles `basename $f`"
285 done
287 esac
289 case "x$pkgfiles" in
291 echo 'no source files found' 1>&2
292 exit 1
294 esac
296 # Split $gofiles into external gofiles (those in *_test packages)
297 # and internal ones (those in the main package).
298 for f in $gofiles; do
299 package=`grep '^package[ ]' $f | sed 1q`
300 case "$package" in
301 *_test)
302 xgofiles="$xgofiles $f"
305 ngofiles="$ngofiles $f"
307 esac
308 done
309 gofiles=$ngofiles
311 # External $O file
312 xofile=""
313 havex=false
314 if [ "x$xgofiles" != "x" ]; then
315 xofile="_xtest_.o"
316 havex=true
319 set -e
321 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
323 pkgpatharg=
324 xpkgpatharg=
325 prefixarg=
326 if test -n "$pkgpath"; then
327 pkgpatharg="-fgo-pkgpath=$pkgpath"
328 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
329 elif test -n "$prefix"; then
330 prefixarg="-fgo-prefix=$prefix"
333 if test "$trace" = "true"; then
334 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
336 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
338 if $havex; then
339 mkdir -p `dirname $package`
340 cp _gotest_.o `dirname $package`/lib`basename $package`.a
341 if test "$trace" = "true"; then
342 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
344 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
347 # They all compile; now generate the code to call them.
349 testname() {
350 # Remove the package from the name used with the -test option.
351 echo $1 | sed 's/^.*\.//'
354 localname() {
355 # The package main has been renamed to __main__ when imported.
356 # Adjust its uses.
357 echo $1 | sed 's/^main\./__main__./'
361 text="T"
362 case "$GOARCH" in
363 ppc64) text="D" ;;
364 esac
366 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
368 # test functions are named TestFoo
369 # the grep -v eliminates methods and other special names
370 # that have multiple dots.
371 pattern='Test([^a-z].*)?'
372 # The -p option tells GNU nm not to sort.
373 # The -v option tells Solaris nm to sort by value.
374 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
375 if [ "x$tests" = x ]; then
376 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
377 exit 2
379 # benchmarks are named BenchmarkFoo.
380 pattern='Benchmark([^a-z].*)?'
381 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
383 # examples are named ExampleFoo
384 pattern='Example([^a-z].*)?'
385 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
387 # package spec
388 echo 'package main'
389 echo
390 # imports
391 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
392 echo 'import "./_gotest_"'
394 if $havex; then
395 echo 'import "./_xtest_"'
397 echo 'import "testing"'
398 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
399 # test array
400 echo
401 echo 'var tests = []testing.InternalTest {'
402 for i in $tests
404 n=$(testname $i)
405 j=$(localname $i)
406 echo ' {"'$n'", '$j'},'
407 done
408 echo '}'
410 # benchmark array
411 # The comment makes the multiline declaration
412 # gofmt-safe even when there are no benchmarks.
413 echo 'var benchmarks = []testing.InternalBenchmark{ //'
414 for i in $benchmarks
416 n=$(testname $i)
417 j=$(localname $i)
418 echo ' {"'$n'", '$j'},'
419 done
420 echo '}'
422 # examples array
423 echo 'var examples = []testing.InternalExample{ //'
424 # This doesn't work because we don't pick up the output.
425 #for i in $examples
427 # n=$(testname $i)
428 # j=$(localname $i)
429 # echo ' {"'$n'", '$j', ""},'
430 #done
431 echo '}'
433 # body
434 echo \
436 var matchPat string
437 var matchRe *__regexp__.Regexp
439 func matchString(pat, str string) (result bool, err error) {
440 if matchRe == nil || matchPat != pat {
441 matchPat = pat
442 matchRe, err = __regexp__.Compile(matchPat)
443 if err != nil {
444 return
447 return matchRe.MatchString(str), nil
450 func main() {
451 testing.Main(matchString, tests, benchmarks, examples)
453 }>_testmain.go
455 case "x$dejagnu" in
456 xno)
457 if test "$trace" = "true"; then
458 echo ${GC} -g -c _testmain.go
460 ${GC} -g -c _testmain.go
462 if test "$trace" = "true"; then
463 echo ${GL} *.o ${GOLIBS}
465 ${GL} *.o ${GOLIBS}
467 if test "$trace" = "true"; then
468 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
470 ./a.out -test.short -test.timeout=${timeout}s "$@" &
471 pid=$!
472 (sleep `expr $timeout + 10`
473 echo > gotest-timeout
474 echo "timed out in gotest" 1>&2
475 kill -9 $pid) &
476 alarmpid=$!
477 wait $pid
478 status=$?
479 if ! test -f gotest-timeout; then
480 kill $alarmpid
482 exit $status
484 xyes)
485 rm -rf ../../testsuite/*.o
486 files=`echo *`
487 for f in $files; do
488 if test "$f" = "_obj" || test "$f" = "_test"; then
489 continue
491 rm -rf ../../testsuite/$f
492 if test -f $f; then
493 cp $f ../../testsuite/
494 else
495 ln -s ../$DIR/test/$f ../../testsuite/
497 done
498 cd ../../testsuite
499 rm -rf _obj _test
500 mkdir _obj _test
501 if test "$testname" != ""; then
502 GOTESTNAME="$testname"
503 export GOTESTNAME
505 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
506 # Useful when using make check-target-libgo
507 cat libgo.log >> libgo-all.log
508 cat libgo.sum >> libgo-all.sum
509 rm -rf $files
511 esac