1 | # This file is sourced by init.sh, *before* its initialization.
|
---|
2 |
|
---|
3 | # This goes hand in hand with the "9>&2;" in tests/Makefile.am's
|
---|
4 | # TESTS_ENVIRONMENT definition.
|
---|
5 | stderr_fileno_=9
|
---|
6 |
|
---|
7 | # Map settings of "none" to the empty string.
|
---|
8 | test _"$LOCALE_FR" = _none && LOCALE_FR=
|
---|
9 | test _"$LOCALE_FR_UTF8" = _none && LOCALE_FR_UTF8=
|
---|
10 |
|
---|
11 | # Unset key environment variables.
|
---|
12 | if (FOO=FOO; unset FOO) >/dev/null 2>&1; then
|
---|
13 | as_unset=unset
|
---|
14 | else
|
---|
15 | as_unset=false
|
---|
16 | fi
|
---|
17 |
|
---|
18 | # Derive this list by searching for string literals as the first
|
---|
19 | # argument to getenv:
|
---|
20 | # git grep getenv|perl -nle '/\bgetenv *\("(.+?)"\)/ and print $1'|sort -u grep
|
---|
21 | vars_='
|
---|
22 | GREP_COLOR
|
---|
23 | GREP_COLORS
|
---|
24 | TERM
|
---|
25 | '
|
---|
26 | envvar_check_fail=0
|
---|
27 | for v_ in $vars_
|
---|
28 | do
|
---|
29 | $as_unset $v_
|
---|
30 | if eval test \"\${$v_+set}\" = set; then
|
---|
31 | echo "$0: the $v_ environment variable is set --" \
|
---|
32 | ' unset it and rerun this test' >&2
|
---|
33 | envvar_check_fail=1
|
---|
34 | fi
|
---|
35 | done
|
---|
36 |
|
---|
37 | test "$envvar_check_fail" = 1 && fail_ "failed to unset the above envvars"
|
---|
38 |
|
---|
39 | require_timeout_()
|
---|
40 | {
|
---|
41 | ( timeout 10s true ) > /dev/null 2>&1 \
|
---|
42 | || skip_ your system lacks the timeout program
|
---|
43 | returns_ 1 timeout 10s false \
|
---|
44 | || skip_ your system has a non-GNU timeout program
|
---|
45 | returns_ 124 timeout 0.01 sleep 0.02 \
|
---|
46 | || skip_ "'timeout 0.01 sleep 0.02' did not time out"
|
---|
47 | }
|
---|
48 |
|
---|
49 | require_pcre_()
|
---|
50 | {
|
---|
51 | echo . | grep -P . 2>err || {
|
---|
52 | test $? -eq 1 && fail_ PCRE available, but does not work.
|
---|
53 | skip_ no PCRE support
|
---|
54 | }
|
---|
55 | compare /dev/null err || fail_ PCRE available, but stderr not empty.
|
---|
56 | }
|
---|
57 |
|
---|
58 | # Some tests would fail without this particular locale.
|
---|
59 | # If the locale is not available, just skip the test.
|
---|
60 | require_en_utf8_locale_()
|
---|
61 | {
|
---|
62 | path_prepend_ .
|
---|
63 | case $(get-mb-cur-max en_US.UTF-8) in
|
---|
64 | [3456]) ;;
|
---|
65 | *) skip_ 'en_US.UTF-8 locale not found' ;;
|
---|
66 | esac
|
---|
67 | }
|
---|
68 |
|
---|
69 | require_tr_utf8_locale_()
|
---|
70 | {
|
---|
71 | path_prepend_ .
|
---|
72 | case $(get-mb-cur-max tr_TR.UTF-8) in
|
---|
73 | [3456]) ;;
|
---|
74 | *) skip_ 'tr_TR.UTF-8 locale not found' ;;
|
---|
75 | esac
|
---|
76 | }
|
---|
77 |
|
---|
78 | require_ru_RU_koi8_r()
|
---|
79 | {
|
---|
80 | path_prepend_ .
|
---|
81 | case $(get-mb-cur-max ru_RU.KOI8-R) in
|
---|
82 | 1) ;;
|
---|
83 | *) skip_ 'ru_RU.KOI8-R locale not found' ;;
|
---|
84 | esac
|
---|
85 | }
|
---|
86 |
|
---|
87 | require_compiled_in_MB_support()
|
---|
88 | {
|
---|
89 | require_en_utf8_locale_
|
---|
90 | printf 'é' | LC_ALL=en_US.UTF-8 grep '[[:lower:]]' \
|
---|
91 | || skip_ this test requires MBS support
|
---|
92 | }
|
---|
93 |
|
---|
94 | require_unibyte_locale()
|
---|
95 | {
|
---|
96 | path_prepend_ .
|
---|
97 | for loc in C en_US; do
|
---|
98 | for encoding in '' .iso88591 .iso885915 .ISO8859-1 .ISO8859-15; do
|
---|
99 | locale=$loc$encoding
|
---|
100 | MB_CUR_MAX=$(get-mb-cur-max $locale 2>/dev/null) &&
|
---|
101 | test "$MB_CUR_MAX" -eq 1 &&
|
---|
102 | LC_ALL=$locale &&
|
---|
103 | export LC_ALL &&
|
---|
104 | return
|
---|
105 | done
|
---|
106 | done
|
---|
107 | skip_ 'no unibyte locale found'
|
---|
108 | }
|
---|
109 |
|
---|
110 | # Define hi_res_time_ to a function that prints the current time
|
---|
111 | # as a floating point number with greater than 1-second resolution.
|
---|
112 | # Otherwise, skip the requiring test.
|
---|
113 | require_hi_res_time_()
|
---|
114 | {
|
---|
115 | local cmd
|
---|
116 | for cmd in 'date +%s.%N' \
|
---|
117 | 'perl -le "use Time::HiRes qw(time); print scalar time()"'; do
|
---|
118 | case $($cmd) in
|
---|
119 | *.[0-9]*) eval 'hi_res_time_() { '"$cmd"'; }'; break;;
|
---|
120 | esac
|
---|
121 | done
|
---|
122 | type hi_res_time_ || skip_ no high-resolution timer support
|
---|
123 | }
|
---|
124 |
|
---|
125 | require_JP_EUC_locale_()
|
---|
126 | {
|
---|
127 | local locale=ja_JP.eucJP
|
---|
128 | path_prepend_ .
|
---|
129 | case $(get-mb-cur-max $locale) in
|
---|
130 | [23])
|
---|
131 | LC_ALL=$locale &&
|
---|
132 | export LC_ALL &&
|
---|
133 | return
|
---|
134 | ;;
|
---|
135 | *) ;;
|
---|
136 | esac
|
---|
137 |
|
---|
138 | skip_ "$locale locale not found"
|
---|
139 | }
|
---|
140 |
|
---|
141 | expensive_()
|
---|
142 | {
|
---|
143 | if test "$RUN_EXPENSIVE_TESTS" != yes; then
|
---|
144 | skip_ 'expensive: disabled by default
|
---|
145 | This test is relatively expensive, so it is disabled by default.
|
---|
146 | To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
|
---|
147 | environment variable set to yes. E.g.,
|
---|
148 |
|
---|
149 | env RUN_EXPENSIVE_TESTS=yes make check
|
---|
150 |
|
---|
151 | or use the shortcut target of the toplevel Makefile,
|
---|
152 |
|
---|
153 | make check-expensive
|
---|
154 | '
|
---|
155 | fi
|
---|
156 | }
|
---|
157 |
|
---|
158 | # Like printf with a single argument, but that argument must be a
|
---|
159 | # sequence of four-byte strings \xHH where each H is a hexadecimal byte.
|
---|
160 | hex_printf_()
|
---|
161 | {
|
---|
162 | local octal_fmt=$(printf '\\%o' \
|
---|
163 | $(printf '%s\n' "$1" \
|
---|
164 | | sed 's,\\x\([0-9abcdefABCDEF][0-9abcdefABCDEF]\), 0x\1,g'))
|
---|
165 | printf "$octal_fmt"
|
---|
166 | }
|
---|
167 |
|
---|
168 | # Wrap tr so that it always runs in the C locale.
|
---|
169 | # Otherwise, in a multibyte locale, GNU tr (which is not multibyte-aware
|
---|
170 | # as of 2014-11-08), would work differently than others. For example,
|
---|
171 | # this command, which was written with unibyte GNU tr in mind,
|
---|
172 | # LC_ALL=ja_JP.eucJP tr AB '\244\263'
|
---|
173 | # would act like this with the multibyte tr from HP-UX and Solaris:
|
---|
174 | # LC_ALL=ja_JP.eucJP tr A '\244\263'
|
---|
175 | tr() { LC_ALL=C env -- tr "$@"; }
|
---|
176 |
|
---|
177 | # Usage: user_time_ EXPECTED_EXIT_STATUS CMD ...
|
---|
178 | # If CMD ... exits with the expected exit status, print the elapsed
|
---|
179 | # child "user" time (not "system" time) in milliseconds and return 0.
|
---|
180 | # Otherwise, diagnose the exit status mismatch and return nonzero.
|
---|
181 | user_time_()
|
---|
182 | {
|
---|
183 | $PERL -le '
|
---|
184 | my $expected_exit_status = $ARGV[0];
|
---|
185 | shift @ARGV;
|
---|
186 |
|
---|
187 | system (@ARGV);
|
---|
188 | my ($user, $system, $child_user, $child_system) = times;
|
---|
189 |
|
---|
190 | my $me = q('"$ME_"');
|
---|
191 | $? == -1
|
---|
192 | and die qq($me: failed to exec ") . join (" ", @ARGV) . qq(": $!\n);
|
---|
193 | my $rc = $?;
|
---|
194 | my $sig = ($rc & 127);
|
---|
195 | $sig and die "$me: child died with signal $sig\n";
|
---|
196 | $rc >>= 8;
|
---|
197 | $rc == $expected_exit_status
|
---|
198 | or die "$me: bad exit status: expected $expected_exit_status; got $rc\n";
|
---|
199 |
|
---|
200 | # Print milliseconds of child user time.
|
---|
201 | $child_user *= 1000;
|
---|
202 | print int ($child_user + 0.5)' "$@"
|
---|
203 | }
|
---|
204 |
|
---|
205 | # yes is not portable, fake it with $AWK
|
---|
206 | yes() { line=${*-y} ${AWK-awk} 'BEGIN{for (;;) print ENVIRON["line"]}'; }
|
---|
207 |
|
---|
208 | # Some systems lack seq.
|
---|
209 | # A limited replacement for seq: handle 1 or 2 args; increment must be 1
|
---|
210 | if ! type seq > /dev/null 2>&1; then
|
---|
211 | seq()
|
---|
212 | {
|
---|
213 | case $# in
|
---|
214 | 1) start=1 final=$1;;
|
---|
215 | 2) start=$1 final=$2;;
|
---|
216 | *) echo you lose 1>&2; exit 1;;
|
---|
217 | esac
|
---|
218 | awk 'BEGIN{for(i='$start';i<='$final';i++) print i}' < /dev/null
|
---|
219 | }
|
---|
220 | fi
|
---|