1#!/bin/bash
2
3UGREP=${UGREP_ABS_PATH:-../src/ugrep}
4CONFIGH=${CONFIGH_ABS_PATH:-../config.h}
5
6UG="$UGREP --color=always --sort $@"
7
8FILES="Hello.bat Hello.class Hello.java Hello.pdf Hello.sh Hello.txt"
9
10if test ! -x "$UGREP" ; then
11  echo "$UGREP not found, exiting"
12  exit 1
13fi
14
15if test ! -e "$CONFIGH" ; then
16  echo "$CONFIGH not found, exiting"
17  exit 1
18fi
19
20if ! $UG --version | head -n1 ; then
21  echo "$UGREP failed to execute, exiting"
22  exit 1
23fi
24
25if $UG -Fq 'HAVE_PCRE2 1' "$CONFIGH" ; then
26  have_pcre2=yes
27else
28  have_pcre2=no
29fi
30
31echo "Have libpcre2?" $have_pcre2
32
33if test "$have_pcre2" = "no" ; then
34  if $UG -Fq 'HAVE_BOOST_REGEX 1' "$CONFIGH" ; then
35    have_boost_regex=yes
36  else
37    have_boost_regex=no
38  fi
39
40  echo "Have libboost_regex?" $have_boost_regex
41fi
42
43if $UG -Fq 'HAVE_LIBZ 1' "$CONFIGH" ; then
44  have_libz=yes
45else
46  have_libz=no
47fi
48
49echo "Have libz?" $have_libz
50
51if $UG -Fq 'HAVE_LIBBZ2 1' "$CONFIGH" ; then
52  have_libbz2=yes
53else
54  have_libbz2=no
55fi
56
57echo "Have libbz2?" $have_libbz2
58
59if $UG -Fq 'HAVE_LIBLZMA 1' "$CONFIGH" ; then
60  have_liblzma=yes
61else
62  have_liblzma=no
63fi
64
65echo "Have liblzma?" $have_liblzma
66
67if $UG -Fq 'HAVE_LIBLZ4 1' "$CONFIGH" ; then
68  have_liblz4=yes
69else
70  have_liblz4=no
71fi
72
73echo "Have liblz4?" $have_liblz4
74
75if $UG -Fq 'HAVE_LIBZSTD 1' "$CONFIGH" ; then
76  have_libzstd=yes
77else
78  have_libzstd=no
79fi
80
81echo "Have libzstd?" $have_libzstd
82
83export GREP_COLORS='cx=hb:ms=hug:mc=ib+W:fn=h35:ln=32h:cn=1;32:bn=1;32:se=+36'
84
85function ERR() {
86  echo "Error: ugrep --sort $1 failed"
87  exit 1
88}
89
90DIFF="diff -U1 -"
91
92rm -rf dir1/ dir2/
93
94mkdir -p dir1 dir2
95
96ln -s ../Hello.java dir1
97cp Hello.sh dir1
98cp Hello.bat dir1
99cp makefile dir1
100ln -s ../Hello.java dir2
101cp Hello.sh dir2
102cp Hello.bat dir2
103ln -s ../dir2 dir1
104ln -s ../dir1 dir2
105cat > dir1/.gitignore << END
106# ignore shells
107*.sh
108# ignore dir2 (sub)directories
109**/dir2/
110END
111
112printf .
113$UG -rl                                  Hello dir1 | $DIFF out/dir.out               || ERR "-rl Hello dir1"
114printf .
115$UG -Rl                                  Hello dir1 | $DIFF out/dir-S.out             || ERR "-Rl Hello dir1"
116printf .
117$UG -Rl -Osh                             Hello dir1 | $DIFF out/dir-O.out             || ERR "-Rl -Osh Hello dir1"
118printf .
119$UG -Rl -M'#!/bin/sh'                    Hello dir1 | $DIFF out/dir-M.out             || ERR "-Rl -M'#!/bin/sh' Hello dir1"
120printf .
121$UG -Rl -tShell,make                     Hello dir1 | $DIFF out/dir-t.out             || ERR "-Rl -tShell,make Hello dir1"
122printf .
123$UG -1l                                  Hello dir1 | $DIFF out/dir-1.out             || ERR "-1l Hello dir1"
124printf .
125$UG -2l                                  Hello dir1 | $DIFF out/dir-2.out             || ERR "-2l Hello dir1"
126printf .
127$UG -Rl --include='*.sh'                 Hello dir1 | $DIFF out/dir--include.out      || ERR "-Rl --include='*.sh' Hello dir1"
128printf .
129$UG -Rl --exclude='*.sh'                 Hello dir1 | $DIFF out/dir--exclude.out      || ERR "-Rl --exclude='*.sh' Hello dir1"
130printf .
131$UG -Rl --exclude-dir='dir2'             Hello dir1 | $DIFF out/dir--exclude-dir.out  || ERR "-Rl --exclude-dir='dir2' Hello dir1"
132printf .
133$UG -Rl --include-dir='dir1'             Hello dir1 | $DIFF out/dir--include-dir.out  || ERR "-Rl --include-dir='dir1' Hello dir1"
134printf .
135$UG -Rl --exclude-dir='dir2'             Hello dir1 | $DIFF out/dir--exclude-dir.out  || ERR "-Rl --exclude-dir='dir2' Hello dir1"
136printf .
137$UG -Rl --include-from='dir1/.gitignore' Hello dir2 | $DIFF out/dir--include-from.out || ERR "-Rl --include-from='dir1/.gitignore' Hello dir2"
138printf .
139$UG -Rl --exclude-from='dir1/.gitignore' Hello dir1 | $DIFF out/dir--exclude-from.out || ERR "-Rl --exclude-from='dir1/.gitignore' Hello dir1"
140printf .
141$UG -Rl --ignore-files                   Hello dir1 | $DIFF out/dir--ignore-files.out || ERR "-Rl -ignore-files Hello Hello dir1"
142printf .
143$UG -Rl --filter='sh:head -n1'           Hello dir1 | $DIFF out/dir--filter.out       || ERR "-Rl --filter='sh:head -n1' Hello dir1"
144
145rm -rf dir1 dir2
146
147for OPS in '' '-F' '-G' ; do
148  printf .
149  $UG $OPS -iwco -f lorem lorem.utf8.txt \
150    | $DIFF "out/lorem.utf8$OPS-iwco.out" \
151    || ERR "$OPS -iwco -f lorem lorem.utf8.txt"
152  printf .
153  $UG $OPS -iwco -f lorem lorem.utf16.txt \
154    | $DIFF "out/lorem.utf8$OPS-iwco.out" \
155    || ERR "$OPS -iwco -f lorem lorem.utf16.txt"
156  printf .
157  $UG $OPS -iwco -f lorem lorem.utf32.txt \
158    | $DIFF "out/lorem.utf8$OPS-iwco.out" \
159    || ERR "$OPS -iwco -f lorem lorem.utf32.txt"
160  printf .
161  cat lorem | $UG $OPS -iwco --encoding=LATIN1 -f - lorem.latin1.txt \
162    | $DIFF "out/lorem.latin1$OPS-iwco.out" \
163    || ERR "$OPS -iwco -f lorem lorem.latin1.txt"
164done
165
166if [ "$have_pcre2" == yes ]; then
167  printf .
168  $UG -P -iwco -f lorem lorem.utf8.txt \
169    | $DIFF "out/lorem.utf8-P-iwco.out" \
170    || ERR "-P -iwco -f lorem lorem.utf8.txt"
171  printf .
172  $UG -P -iwco -f lorem lorem.utf16.txt \
173    | $DIFF "out/lorem.utf8-P-iwco.out" \
174    || ERR "-P -iwco -f lorem lorem.utf16.txt"
175  printf .
176  $UG -P -iwco -f lorem lorem.utf32.txt \
177    | $DIFF "out/lorem.utf8-P-iwco.out" \
178    || ERR "-P -iwco -f lorem lorem.utf32.txt"
179  printf .
180  cat lorem | $UG -P -iwco --encoding=LATIN1 -f - lorem.latin1.txt \
181    | $DIFF "out/lorem.latin1-P-iwco.out" \
182    || ERR "-P -iwco -f lorem lorem.latin1.txt"
183fi
184
185printf .
186$UG -Zio Lorem lorem.utf8.txt | $DIFF out/lorem_Lorem-Zio.out  || ERR "-Zio Lorem lorem.utf8.txt"
187
188printf .
189$UG -ci hello $FILES \
190    | $DIFF out/Hello_Hello-ci.out \
191    || ERR "-ci hello $FILES"
192printf .
193$UG -cj hello $FILES \
194    | $DIFF out/Hello_Hello-cj.out \
195    || ERR "-cj hello $FILES"
196
197printf .
198$UG -e Hello -e '".*?"' $FILES \
199    | $DIFF out/Hello_Hello-ee.out \
200    || ERR "-e Hello -e '\".*?\"' $FILES"
201printf .
202$UG -e Hello -N '".*?"' $FILES \
203    | $DIFF out/Hello_Hello-eN.out \
204    || ERR "-e Hello -N '\".*?\"' $FILES"
205printf .
206$UG --max-count=1 Hello $FILES \
207    | $DIFF out/Hello_Hello--max-count.out \
208    || ERR "--max-count=1 Hello $FILES"
209printf .
210$UG --max-files=1 Hello $FILES \
211    | $DIFF out/Hello_Hello--max-files.out \
212    || ERR "--max-files=1 Hello $FILES"
213printf .
214$UG --range=1,1   Hello $FILES \
215    | $DIFF out/Hello_Hello--range.out \
216    || ERR "--range=1,1 Hello $FILES"
217
218for PAT in '' 'Hello' '\w+\s+\S+' '\S\n\S' 'nomatch' ; do
219  FN=`echo "Hello_$PAT" | tr -Cd '[:alnum:]_'`
220  for OUT in '' '-I' '-W' '-X' ; do
221    for OPS in '' '-l' '-lv' '-c' '-co' '-cv' '-n' '-nkbT' '-unkbT' '-o' '-on' '-onkbT' '-ounkbT' '-v' '-nv' '-C2' '-nC2' '-vC2' '-nvC2' '-y' '-ny' '-vy' '-nvy' ; do
222      printf .
223      $UG -U $OUT $OPS "$PAT" $FILES \
224        | $DIFF "out/$FN$OUT$OPS.out" \
225        || ERR "$OUT $OPS '$PAT' $FILES"
226    done
227  done
228  for OUT in '--csv' '--json' '--xml' ; do
229    for OPS in '' '-l' '-lv' '-c' '-co' '-cv' '-n' '-v' '-nv' '-nkb' '-unkb' '-o' '-on' '-onkb' '-ounkb' ; do
230      printf .
231      $UG -U $OUT $OPS "$PAT" $FILES \
232        | $DIFF "out/$FN$OUT$OPS.out" \
233        || ERR "$OUT $OPS '$PAT' $FILES"
234    done
235  done
236  printf .
237  $UG -U --tag "$PAT" $FILES \
238    | $DIFF "out/$FN--tag.out" \
239    || ERR "--tag '$PAT' $FILES"
240  printf .
241  $UG -U --format-open='%m) %f:%~' --format='  %m) %n,%k %w-%d%~' --format-close='%~' "$PAT" $FILES \
242    | $DIFF "out/$FN--format.out" \
243    || ERR "--format-open='%m) %f:%~' --format='  %m) %n,%k %w-%d%~' --format-close='%~' '$PAT' $FILES"
244  printf .
245  $UG -U -v --format-open='%m) %f:%~' --format='  %m) %n,%k %w-%d%~' --format-close='%~' "$PAT" $FILES \
246    | $DIFF "out/$FN-v--format.out" \
247    || ERR "-v --format-open='%m) %f:%~' --format='  %m) %n,%k %w-%d%~' --format-close='%~' '$PAT' $FILES"
248  printf .
249  $UG -U -Iw "$PAT" $FILES \
250    | $DIFF "out/$FN-Iw.out" \
251    || ERR "-Iw '$PAT' $FILES"
252  printf .
253  $UG -U -Ix "$PAT" $FILES \
254    | $DIFF "out/$FN-Ix.out" \
255    || ERR "-Ix '$PAT' $FILES"
256  printf .
257  $UG -U -F  "$PAT" $FILES \
258    | $DIFF "out/$FN-F.out" \
259    || ERR "-F '$PAT' $FILES"
260  printf .
261  $UG -U -Fw "$PAT" $FILES \
262    | $DIFF "out/$FN-Fw.out" \
263    || ERR "-Fw '$PAT' $FILES"
264  printf .
265  $UG -U -Fx "$PAT" $FILES \
266    | $DIFF "out/$FN-Fx.out" \
267    || ERR "-Fx '$PAT' $FILES"
268  printf .
269  if [ "$PAT" == '\w+\s+\S+' ]; then
270    $UG -U -G  '\w\+\s\+\S\+' $FILES \
271      | $DIFF "out/$FN-G.out" \
272      || ERR "-G '\w\+\s\+\S\+' $FILES"
273    $UG -U -Gw '\w\+\s\+\S\+' $FILES \
274      | $DIFF "out/$FN-Gw.out" \
275      || ERR "-Gw '\w\+\s\+\S\+' $FILES"
276    $UG -U -Gx '\w\+\s\+\S\+' $FILES \
277      | $DIFF "out/$FN-Gx.out" \
278      || ERR "-Gx '\w\+\s\+\S\+' $FILES"
279  else
280    $UG -U -G  "$PAT" $FILES \
281      | $DIFF "out/$FN-G.out" \
282      || ERR "-G '$PAT' $FILES"
283    $UG -U -Gw "$PAT" $FILES \
284      | $DIFF "out/$FN-Gw.out" \
285      || ERR "-Gw '$PAT' $FILES"
286    $UG -U -Gx "$PAT" $FILES \
287      | $DIFF "out/$FN-Gx.out" \
288      || ERR "-Gx '$PAT' $FILES"
289  fi
290  if [ "$have_pcre2" == yes ] || [ "$have_boost_regex" == yes ]; then
291    printf .
292    $UG -U -IP  "$PAT" $FILES \
293      | $DIFF "out/$FN-IP.out" \
294      || ERR "-IP '$PAT' $FILES"
295    printf .
296    $UG -U -IPw "$PAT" $FILES \
297      | $DIFF "out/$FN-IPw.out" \
298      || ERR "-IPw '$PAT' $FILES"
299    printf .
300    $UG -U -IPx "$PAT" $FILES \
301      | $DIFF "out/$FN-IPx.out" \
302      || ERR "-IPx '$PAT' $FILES"
303  fi
304done
305
306for PAT in '' 'Hello World' 'Hello -World' 'Hello -bin' 'bin -Hello' 'bin -greeting' 'Hello -World|greeting' 'Hello -bin|greeting' 'Hello -(greeting|World)' '"a Hello" greeting' ; do
307  FN=`echo "Hello_$PAT" | tr -Cd '[:alnum:]_-'`
308  for OPS in '' '-l' '-c' '-co' '-o' '-C2' '-y' '--json' ; do
309    printf .
310    $UG -U --bool $OPS "$PAT" $FILES \
311      | $DIFF "out/$FN--bool$OPS.out" \
312      || ERR "--bool $OPS '$PAT' $FILES"
313    $UG -U --files --bool $OPS "$PAT" $FILES \
314      | $DIFF "out/$FN--files--bool$OPS.out" \
315      || ERR "--files --bool $OPS '$PAT' $FILES"
316  done
317done
318
319printf .
320$UG -U -e 'Hello' --and 'World' $FILES \
321    | $DIFF "out/Hello--and.out" \
322    || ERR "-e 'Hello' --and 'World' $FILES"
323printf .
324$UG -U -e 'Hello' --andnot 'World' $FILES \
325    | $DIFF "out/Hello--andnot.out" \
326    || ERR "-e 'Hello' --andnot 'World' $FILES"
327printf .
328$UG -U -e 'Hello' --and --not 'World' -e 'greeting' $FILES \
329    | $DIFF "out/Hello--and--not.out" \
330    || ERR "-e 'Hello' --and --not 'World' -e 'greeting' $FILES"
331
332if [ "$have_libz" == yes ]; then
333printf .
334$UG -z -c Hello archive.cpio    | $DIFF out/archive.cpio.out    || ERR "-z -c Hello archive.cpio"
335printf .
336$UG -z -c Hello archive.pax     | $DIFF out/archive.pax.out     || ERR "-z -c Hello archive.pax"
337printf .
338$UG -z -c Hello archive.tar     | $DIFF out/archive.tar.out     || ERR "-z -c Hello archive.tar"
339printf .
340$UG -z -c Hello archive.tgz     | $DIFF out/archive.tgz.out     || ERR "-z -c Hello archive.tgz"
341printf .
342$UG -z -c Hello archive.tZ      | $DIFF out/archive.tZ.out      || ERR "-z -c Hello archive.tZ"
343printf .
344$UG -z -c Hello archive.tar.zip | $DIFF out/archive.tar.zip.out || ERR "-z -c Hello archive.tar.zip"
345printf .
346$UG -z -c Hello archive.zip     | $DIFF out/archive.zip.out     || ERR "-z -c Hello archive.zip"
347if [ "$have_libbz2" == yes ]; then
348printf .
349$UG -z -c Hello archive.tbz     | $DIFF out/archive.tbz.out     || ERR "-z -c Hello archive.tbz"
350fi
351if [ "$have_liblzma" == yes ]; then
352printf .
353$UG -z -c Hello archive.tlz     | $DIFF out/archive.tlz.out     || ERR "-z -c Hello archive.tlz"
354printf .
355$UG -z -c Hello archive.txz     | $DIFF out/archive.txz.out     || ERR "-z -c Hello archive.txz"
356fi
357if [ "$have_liblz4" == yes ]; then
358printf .
359$UG -z -c Hello archive.tar.lz4 | $DIFF out/archive.tar.lz4.out || ERR "-z -c Hello archive.tar.lz4"
360fi
361if [ "$have_libzstd" == yes ]; then
362printf .
363$UG -z -c Hello archive.tzst    | $DIFF out/archive.tzst.out    || ERR "-z -c Hello archive.tzst"
364fi
365fi
366
367if [ "$have_libz" == yes ]; then
368printf .
369$UG -z -c -tShell Hello archive.cpio    | $DIFF out/archive-t.cpio.out    || ERR "-z -c -tShell Hello archive.cpio"
370printf .
371$UG -z -c -tShell Hello archive.pax     | $DIFF out/archive-t.pax.out     || ERR "-z -c -tShell Hello archive.pax"
372printf .
373$UG -z -c -tShell Hello archive.tar     | $DIFF out/archive-t.tar.out     || ERR "-z -c -tShell Hello archive.tar"
374printf .
375$UG -z -c -tShell Hello archive.tgz     | $DIFF out/archive-t.tgz.out     || ERR "-z -c -tShell Hello archive.tgz"
376printf .
377$UG -z -c -tShell Hello archive.tZ      | $DIFF out/archive-t.tZ.out      || ERR "-z -c -tShell Hello archive.tZ"
378printf .
379$UG -z -c -tShell Hello archive.tar.zip | $DIFF out/archive-t.tar.zip.out || ERR "-z -c -tShell Hello archive.tar.zip"
380printf .
381$UG -z -c -tShell Hello archive.zip     | $DIFF out/archive-t.zip.out     || ERR "-z -c -tShell Hello archive.zip"
382if [ "$have_libbz2" == yes ]; then
383printf .
384$UG -z -c -tShell Hello archive.tbz     | $DIFF out/archive-t.tbz.out     || ERR "-z -c -tShell Hello archive.tbz"
385fi
386if [ "$have_liblzma" == yes ]; then
387printf .
388$UG -z -c -tShell Hello archive.tlz     | $DIFF out/archive-t.tlz.out     || ERR "-z -c -tShell Hello archive.tlz"
389printf .
390$UG -z -c -tShell Hello archive.txz     | $DIFF out/archive-t.txz.out     || ERR "-z -c -tShell Hello archive.txz"
391fi
392if [ "$have_liblz4" == yes ]; then
393printf .
394$UG -z -c -tShell Hello archive.tar.lz4 | $DIFF out/archive-t.tar.lz4.out || ERR "-z -c -tShell Hello archive.tar.lz4"
395fi
396if [ "$have_libzstd" == yes ]; then
397printf .
398$UG -z -c -tShell Hello archive.tzst    | $DIFF out/archive-t.tzst.out    || ERR "-z -c -tShell Hello archive.tzst"
399fi
400fi
401
402if [ "$have_libz" == yes ]; then
403printf .
404$UG -z -c '' archive.gz | $DIFF out/archive.gz.out || ERR "-z -c '' archive.gz"
405for PAT in '\.' 'et' 'hendrerit' 'aliquam' 'sit amet aliquam' 'Nunc hendrerit at metus sit amet aliquam' 'adip[a-z]{1,}' 'adip[a-z]{4,}' 'adip[a-z]{6}' '[a-z]+' 'a[a-z]+' 'ad[a-z]+' 'adi[a-z]+' ; do
406  FN=`echo "archive_$PAT" | tr -Cd '[:alnum:]_'`
407  printf .
408  $UG -z -co "$PAT" archive.gz | $DIFF out/$FN-co.gz.out || ERR "-z -co '$PAT' archive.gz"
409done
410fi
411
412echo
413echo "ALL TESTS PASSED"
414