# $NetBSD: t_grep.sh,v 1.8 2024/11/23 09:38:02 rillig Exp $
#
# Copyright (c) 2008, 2009, 2021, 2024 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
atf_test_case word_locale
word_locale_head()
{
atf_set "descr" "Checks word search with locale"
}
word_locale_body()
{
echo "array[]" > "input"
# In the default locale, word search works.
atf_check -o file:"input" \
env LC_ALL=C grep "array" "input"
atf_check -o file:"input" \
env LC_ALL=C grep -w "array" "input"
# XXX: In an UTF-8 locale, GNU Grep treats '[' as a word character.
atf_check -s exit:1 -o empty \
env LC_ALL="C.UTF-8" grep -w "array" "input"
}
atf_test_case word_in_line
word_in_line_head()
{
atf_set "descr" "Checks word search in different locations of a line"
}
word_in_line_body()
{
# See usr.bin/grep/util.c, "Check for whole word match", which
# looks suspiciously wrong. And indeed, NetBSD grep does not
# survive this test. GNU Grep does.
echo "begin middle end" > "input"
# A word at the beginning of a line is found.
atf_check -o file:"input" \
env LC_ALL=C grep -w "begin" "input"
# A word in the middle of a line is found.
atf_check -o file:"input" \
env LC_ALL=C grep -w "middle" "input"
# A word at the end of a line is found.
atf_check -o file:"input" \
env LC_ALL=C grep -w "end" "input"
# A subword at the beginning of a line is not found.
atf_check -s exit:1 -o empty \
env LC_ALL=C grep -w "be" "input"
# A subword in the middle of a line is not found.
atf_check -s exit:1 -o empty \
env LC_ALL=C grep -w "mid" "input"
atf_check -s exit:1 -o empty \
env LC_ALL=C grep -w "dle" "input"
# A subword at the end of a line is not found.
atf_check -s exit:1 -o empty \
env LC_ALL=C grep -w "nd" "input"
}
atf_test_case word_in_line_utf8
word_in_line_utf8_head()
{
atf_set "descr" "Checks word search at the beginning of a line"
}
word_in_line_utf8_body()
{
# See usr.bin/grep/util.c, "Check for whole word match", which
# looks suspiciously wrong. And indeed, NetBSD grep does not
# survive this test. GNU Grep does.
echo "begin middle end" > "input"
# A word at the beginning of a line is found.
atf_check -o file:"input" \
env LC_ALL="C.UTF-8" grep -w "begin" "input"
# A word in the middle of a line is found.
atf_check -o file:"input" \
env LC_ALL="C.UTF-8" grep -w "middle" "input"
# A word at the end of a line is found.
atf_check -o file:"input" \
env LC_ALL="C.UTF-8" grep -w "end" "input"
# A subword at the beginning of a line is not found.
atf_check -s exit:1 -o empty \
env LC_ALL="C.UTF-8" grep -w "be" "input"
# A subword in the middle of a line is not found.
atf_check -s exit:1 -o empty \
env LC_ALL="C.UTF-8" grep -w "mid" "input"
atf_check -s exit:1 -o empty \
env LC_ALL="C.UTF-8" grep -w "dle" "input"
# A subword at the end of a line is not found.
atf_check -s exit:1 -o empty \
env LC_ALL="C.UTF-8" grep -w "nd" "input"
}
atf_test_case begin_end
begin_end_head()
{
atf_set "descr" "Checks handling of line beginnings and ends"
}
begin_end_body()
{
atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
grep ^Front "$(atf_get_srcdir)/d_input"
# The line '__bss_end__' must not occur in the output.
atf_check -o inline:'__bss_start__\nhello\n' \
grep -Fvx -e _end -e __bss_end__ input
# Listing the most specific pattern first works around PR bin/58849.
atf_check -o inline:'__bss_start__\nhello\n' \
grep -Fvx -e __bss_end__ -e _end input
}