1#!/bin/sh
2# Validate yes buffer handling
3
4# Copyright (C) 2015-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ yes
21
22# Check basic operation
23test "$(yes | head -n1)" = 'y' || fail=1
24
25# Check various single item sizes, with the most important
26# size being BUFSIZ used for the local buffer to yes(1).
27# Note a \n is added, so actual sizes required internally
28# are 1 more than the size used here.
29for size in 1 1999 4095 4096 8191 8192 16383 16384; do
30  printf "%${size}s\n" '' > out.1
31  yes "$(printf %${size}s '')" | head -n2 | uniq > out.2
32  compare out.1 out.2 || fail=1
33done
34
35# Check the many small items case,
36# both fitting and overflowing the internal buffer.
37# First check that 4000 arguments supported.
38if test 4000 -eq $(sh -c 'echo $#' 0 $(seq 4000)); then
39  for i in 100 4000; do
40    seq $i | paste -s -d ' ' | sed p > out.1
41    yes $(seq $i) | head -n2 > out.2
42    compare out.1 out.2 || fail=1
43  done
44fi
45
46# Check a single appropriate diagnostic is output on write error
47if test -w /dev/full && test -c /dev/full; then
48  # The single output diagnostic expected,
49  # (without the possibly varying :strerror(ENOSPC) suffix).
50  printf '%s\n' "yes: standard output" > exp
51
52  for size in 1 16384; do
53    returns_ 1 yes "$(printf %${size}s '')" >/dev/full 2>errt || fail=1
54    sed 's/\(yes:.*\):.*/\1/' errt > err
55    compare exp err || fail=1
56  done
57fi
58
59Exit $fail
60