xref: /openbsd/regress/usr.bin/fold/fold.sh (revision 771fbea0)
1#!/bin/sh
2#
3# Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17FOLD=/usr/bin/fold
18
19# Arguments of the test function:
20# 1. command line arguments for fold(1)
21# 2. standard input for fold, backslash-encoded
22# 3. expected standard output, backslash-encoded
23# 4. expected standard output of "fold -b", backslash-encoded
24#    (optional, by default the same as argument 3.)
25test_fold()
26{
27	expect=`echo -n "$3" ; echo .`
28	result=`echo -n "$2" | $FOLD $1 2>&1 ; echo .`
29	if [ "$result" != "$expect" ]; then
30		echo "fold $1 \"$2\":"
31		echo -n "$2" | hexdump -C
32		echo "expect: $expect"
33		echo -n "$expect" | hexdump -C
34		echo "result: $result"
35		echo -n "$result" | hexdump -C
36		exit 1
37	fi
38	[ -n "$4" ] && expect=`echo -n "$4" ; echo .`
39	result=`echo -n "$2" | $FOLD -b $1 2>&1 ; echo .`
40	if [ "$result" != "$expect" ]; then
41		echo "fold -b $1 \"$2\":"
42		echo -n "$2" | hexdump -C
43		echo "expect: $expect"
44		echo -n "$expect" | hexdump -C
45		echo "result: $result"
46		echo -n "$result" | hexdump -C
47		exit 1
48	fi
49}
50
51export LC_ALL=C
52
53test_fold "" "" ""
54
55# newline
56test_fold "" "\n" "\n"
57test_fold "" "\n\n" "\n\n"
58test_fold "-w 1" "\n\n" "\n\n"
59test_fold "-w 2" "1\n12\n123" "1\n12\n12\n3"
60test_fold "-w 2" "12345" "12\n34\n5"
61test_fold "-w 2" "12345\n" "12\n34\n5\n"
62
63# backspace
64test_fold "-w 2" "123" "12\n3"
65test_fold "-w 2" "1\b234" "1\b23\n4" "1\b\n23\n4"
66test_fold "-w 2" "\b1234" "\b12\n34" "\b1\n23\n4"
67test_fold "-w 2" "12\b\b345" "12\b\b34\n5" "12\n\b\b\n34\n5"
68test_fold "-w 2" "12\r3" "12\r3" "12\n\r3"
69
70# tabulator
71test_fold "-w 2" "1\t9" "1\n\t\n9" "1\t\n9"
72test_fold "-w 8" "0\t123456789" "0\t\n12345678\n9" "0\t123456\n789"
73test_fold "-w 9" "1\t9\b\b89012" "1\t9\b\b89\n012" "1\t9\b\b8901\n2"
74
75# split after last blank
76test_fold "-sw 4" "1 23 45" "1 \n23 \n45"
77test_fold "-sw 3" "1234 56" "123\n4 \n56"
78
79# invalid characters
80test_fold "-w 3" "1\037734" "1\03773\n4"
81test_fold "-w 3" "1\000734" "1\00073\n4"
82test_fold "-w 3" "1\000034" "1\00003\n4"
83
84export LC_ALL=en_US.UTF-8
85
86# double width characters
87test_fold "-w 4" "1\0343\0201\020145" "1\0343\0201\02014\n5" \
88		"1\0343\0201\0201\n45"
89test_fold "-w 3" "\0343\0201\0201\0343\0201\020134" \
90		"\0343\0201\0201\n\0343\0201\02013\n4" \
91		"\0343\0201\0201\n\0343\0201\0201\n34"
92test_fold "-w 2" "\0343\0201\0201\b23" "\0343\0201\0201\b2\n3" \
93		"\0343\0201\0201\n\b2\n3"
94test_fold "-w 1" "1\0343\0201\02014" "1\n\0343\0201\0201\n4"
95
96# zero width characters
97test_fold "-w 3" "1a\0314\020034" "1a\0314\02003\n4" "1a\n\0314\02003\n4"
98test_fold "-w 2" "1a\0314\02003" "1a\0314\0200\n3" "1a\n\0314\0200\n3"
99
100# four byte UTF-8 encoding
101test_fold "-w 3" "1\0360\0220\0200\020034" "1\0360\0220\0200\02003\n4" \
102		"1\n\0360\0220\0200\0200\n34"
103
104# invalid UTF-8
105test_fold "-w 3" "\0343\0201\0201\0201\0201\0201\0201\0201\n" \
106		"\0343\0201\0201\0201\n\0201\0201\0201\n\0201\n" \
107		"\0343\0201\0201\n\0201\0201\0201\n\0201\0201\n"
108test_fold "-w 2" "\0343\0343\0201\0201\n" "\0343\n\0343\0201\0201\n"
109
110exit 0
111