summaryrefslogtreecommitdiff
path: root/tests/foad1
blob: aa25f122fcd134b9601f3446720b150f8e0b5487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#! /bin/sh
# Test various combinations of command-line options.
#
# Copyright (C) 2001, 2006, 2009-2019 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
#
# This set of tests was started by Julian Foad.

. "${srcdir=.}/init.sh"; path_prepend_ ../src

failures=0

# grep_test INPUT EXPECTED_OUTPUT PATTERN_AND_OPTIONS...
# Run "grep" with the given INPUT, pattern and options, and check that
# the output is EXPECTED_OUTPUT.  If not, print a message and set 'failures'.
# "/" represents a newline within INPUT and EXPECTED_OUTPUT.
grep_test ()
{
  INPUT="$1"
  EXPECT="$2"
  shift 2
  OUTPUT=$(printf %s "$INPUT" | tr "/" "\n" | grep "$@" | tr "\n" "/")
  if test "$OUTPUT" != "$EXPECT" || test "$VERBOSE" = "yes"; then
    echo "Testing:  grep $@"
    test "$LC_ALL" != C && test "$LC_ALL" != "" && echo "  LC_ALL: \"$LC_ALL\""
    echo "  input:  \"$INPUT\""
    echo "  output: \"$OUTPUT\""
  fi
  if test "$OUTPUT" != "$EXPECT"; then
    echo "  expect: \"$EXPECT\""
    echo "FAIL"
    failures=1
  fi
}


# Test "--only-matching" ("-o") option

# "-o" with "-i" should output an exact copy of the matching input text.
grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "word" -o -i
grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "Word" -o -i
grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "WORD" -o -i

# Should display the line number (-n), octet offset (-b), or file name
# (-H) of every match, not just of the first match on each input line.
# Check it both with and without -i because of the separate code paths.
# Also check what it does when lines of context are specified.
grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n
grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n -i
grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n -3 2>/dev/null
grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b
grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b -i
grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b -3 2>/dev/null
grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b
grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b -i
grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b -3 2>/dev/null
grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H
grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H -i
grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H -3 2>/dev/null

# Combination of -h and -H
grep_test "wA wB/" "wA wB/" "w."
grep_test "wA wB/" "wA wB/" "w." -h
grep_test "wA wB/" "wA wB/" "w." -H -h
grep_test "wA wB/" "(standard input):wA wB/" "w." -H
grep_test "wA wB/" "(standard input):wA wB/" "w." -h -H

# End of a previous match should not match a "start of ..." expression.
grep_test "word_word/" "word_/" "^word_*" -o
grep_test "wordword/" "word/" "\<word" -o


# Test "--color" option

CB=""
CE=""

# "--color" with "-i" should output an exact copy of the matching input text.
grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "word" --color=always -i
grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "Word" --color=always -i
grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "WORD" --color=always -i

# End of a previous match should not match a "start of ..." expression.
grep_test "word_word/" "${CB}word_${CE}word/" "^word_*" --color=always
grep_test "wordword/" "${CB}word${CE}word/" "\<word" --color=always


# Test combination of "-m" with "-A" and anchors.
# Based on a report from Pavol Gono.
grep_test "4/40/"  "4/40/"  "^4$" -m1 -A99
grep_test "4/04/"  "4/04/"  "^4$" -m1 -A99
grep_test "4/444/" "4/444/" "^4$" -m1 -A99
grep_test "4/40/"  "4/40/"  "^4"  -m1 -A99
grep_test "4/04/"  "4/04/"  "^4"  -m1 -A99
grep_test "4/444/" "4/444/" "^4"  -m1 -A99
grep_test "4/40/"  "4/40/"  "4$"  -m1 -A99
grep_test "4/04/"  "4/04/"  "4$"  -m1 -A99
grep_test "4/444/" "4/444/" "4$"  -m1 -A99


# Test for "-F -w" bugs.  Thanks to Gordon Lack for these two.
grep_test "A/CX/B/C/" "A/B/C/" -wF -e A -e B -e C
grep_test "LIN7C 55327/" "" -wF -e 5327 -e 5532

# Test for non-empty matches following empty ones.
grep_test 'xyz/' 'y/' -o 'y*'
grep_test 'xyz/' "x${CB}y${CE}z/" --color=always 'y*'

# Test for increasing/decreasing-length word matches,
# for independence from pattern order within the pattern list,
# and for preferring the longest match at a given position.
x0='a bb ccc dddd/'
x1='dddd ccc bb a/'
x2='bcd abcd abc bc bcd abc/'
x3='bc abcd bc/'
y0="a ${CB}bb${CE} ${CB}ccc${CE} dddd/"
y1="dddd ${CB}ccc${CE} ${CB}bb${CE} a/"
y2="bcd abcd abc ${CB}bc${CE} bcd abc/"
y3="${CB}bc${CE} abcd ${CB}bc${CE}/"
grep_test "$x0" "$y0" -E --color=always -e bb -e cc -e ccc
grep_test "$x0" "$y0" -F --color=always -e bb -e cc -e ccc
grep_test "$x0" "$y0" -E --color=always -e bb -e ccc -e cc
grep_test "$x0" "$y0" -F --color=always -e bb -e ccc -e cc
grep_test "$x0" "$y0" -E -w --color=always -e bb -e ccc
grep_test "$x0" "$y0" -F -w --color=always -e bb -e ccc
grep_test "$x0" "$y0" -E -w --color=always -e ccc -e bb
grep_test "$x0" "$y0" -F -w --color=always -e ccc -e bb
grep_test "$x1" "$y1" -E -w --color=always -e bb -e ccc
grep_test "$x1" "$y1" -F -w --color=always -e bb -e ccc
grep_test "$x1" "$y1" -E -w --color=always -e ccc -e bb
grep_test "$x1" "$y1" -F -w --color=always -e ccc -e bb
grep_test "$x2" "$y2" -E -w --color=always bc
grep_test "$x2" "$y2" -F -w --color=always bc
grep_test "$x3" "$y3" -E -w --color=always bc
grep_test "$x3" "$y3" -F -w --color=always bc

# Bug#25655
grep_test .tar/ .tar/ -e '\.tar' -e '\.tbz'
grep_test .tar/ .tar/ -o -e '\.tar' -e 'tar'
grep_test '$*.[^\/' '$*.[^\/' -o -e '\$\*\.\[\^\\' -e abc
grep_test '$*.[^\/(+?{|/' '$*.[^\/(+?{|/' -o -E \
          -e '\$\*\.\[\^\\' -e '\(\+\?\{\|'

# Skip the rest of the tests - known to fail. TAA.
Exit $failures

# The rest of this file is meant to be executed under this locale.
LC_ALL=cs_CZ.UTF-8; export LC_ALL
# If the UTF-8 locale doesn't work, skip these tests silently.
locale -k LC_CTYPE 2>/dev/null | grep -q "charmap.*UTF-8" || Exit $failures

# Test character class erroneously matching a '[' character.
grep_test "[/" "" "[[:alpha:]]" -E

for mode in F G E; do
  # Hint:  pipe the output of these tests in
  #        "| LESS= LESSCHARSET=ascii less".
  # LETTER N WITH TILDE is U+00F1 and U+00D1.
  # LETTER Y WITH DIAERESIS is U+00FF and U+0178.
  grep_test 'añÿb/AÑŸB/' 'ñÿ/ÑŸ/' 'ñÿ' -o -i -$mode
  grep_test 'añÿb/AÑŸB/' 'ñÿ/ÑŸ/' 'ÑŸ' -o -i -$mode
  grep_test 'añÿb/AÑŸB/' "a${CB}ñÿ${CE}b/A${CB}ÑŸ${CE}B/" 'ñÿ' --color=always -i -$mode
  grep_test 'añÿb/AÑŸB/' "a${CB}ñÿ${CE}b/A${CB}ÑŸ${CE}B/" 'ÑŸ' --color=always -i -$mode

  # POSIX (about -i):  ... each character in the string is matched
  # against the pattern, not only the character, but also its case
  # counterpart (if any), shall be matched.
  # The following were chosen because of their trickiness due to the
  # differing UTF-8 octet length of their counterpart and to the
  # non-reflexivity of their mapping.
  # Beware of homographs!  Look carefully at the actual octets.

  # lc(U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE) = U+0069 LATIN SMALL LETTER I
  grep_test 'aİb/' "a${CB}İ${CE}b/" 'i' --color=always -i -$mode
  grep_test 'aib/' ''               'İ' --color=always -i -$mode
  grep_test 'aİb/' ''               'I' --color=always -i -$mode
  # uc(U+0131 LATIN SMALL LETTER DOTLESS I)          = U+0049 LATIN CAPITAL LETTER I
  grep_test 'aıb/' "a${CB}ı${CE}b/" 'I' --color=always -i -$mode
  grep_test 'aIb/' ''               'ı' --color=always -i -$mode
  grep_test 'aıb/' ''               'i' --color=always -i -$mode
  # uc(U+017F LATIN SMALL LETTER LONG S)             = U+0053 LATIN CAPITAL LETTER S
  grep_test 'aſb/' "a${CB}ſ${CE}b/" 'S' --color=always -i -$mode
  grep_test 'aSb/' ''               'ſ' --color=always -i -$mode
  grep_test 'aſb/' ''               's' --color=always -i -$mode
  # uc(U+1FBE GREEK PROSGEGRAMMENI)                  = U+0399 GREEK CAPITAL LETTER IOTA
  grep_test 'aιb/' "a${CB}ι${CE}b/" 'Ι' --color=always -i -$mode
  grep_test 'aΙb/' ''               'ι' --color=always -i -$mode
  grep_test 'aιb/' ''               'ι' --color=always -i -$mode
  # lc(U+2126 OHM SIGN)                              = U+03C9 GREEK SMALL LETTER OMEGA
  grep_test 'aΩb/' "a${CB}Ω${CE}b/" 'ω' --color=always -i -$mode
  grep_test 'aωb/' ''               'Ω' --color=always -i -$mode
  grep_test 'aΩb/' ''               'Ω' --color=always -i -$mode
  # lc(U+212A KELVIN SIGN)                           = U+006B LATIN SMALL LETTER K
  grep_test 'aKb/' "a${CB}K${CE}b/" 'k' --color=always -i -$mode
  grep_test 'akb/' ''               'K' --color=always -i -$mode
  grep_test 'aKb/' ''               'K' --color=always -i -$mode
  # lc(U+212B ANGSTROM SIGN)                         = U+00E5 LATIN SMALL LETTER A WITH RING ABOVE
  grep_test 'aÅb/' "a${CB}Å${CE}b/" 'å' --color=always -i -$mode
  grep_test 'aåb/' ''               'Å' --color=always -i -$mode
  grep_test 'aÅb/' ''               'Å' --color=always -i -$mode
done


# Any tests inserted right here will be performed under an UTF-8 locale.
# Insert them before LC_ALL is set above to avoid this.
# Leave this comment last.

Exit $failures