xref: /openbsd/regress/usr.bin/fmt/fmt.sh (revision 274d7c50)
1#!/bin/sh
2#
3# Copyright (c) 2015, 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
17test_fmt()
18{
19	expect=`echo -n "$3" ; echo .`
20	result=`echo -n "$2" | LC_ALL=en_US.UTF-8 fmt $1 2>&1 ; echo .`
21	if [ "$result" != "$expect" ]; then
22		echo "LC_ALL=en_US.UTF-8 fmt $1 \"$2\":"
23		echo -n "$2" | hexdump -C
24		echo "expect: $expect"
25		echo -n "$expect" | hexdump -C
26		echo "result: $result"
27		echo -n "$result" | hexdump -C
28		exit 1
29	fi
30	[ -n "$4" ] && expect=`echo -n "$4" ; echo .`
31	result=`echo -n "$2" | LC_ALL=C fmt $1 2>&1 ; echo .`
32	if [ "$result" != "$expect" ]; then
33		echo "LC_ALL=C fmt $1 \"$2\":"
34		echo -n "$2" | hexdump -C
35		echo "expect: $expect"
36		echo -n "$expect" | hexdump -C
37		echo "result: $result"
38		echo -n "$result" | hexdump -C
39		exit 1
40	fi
41}
42
43# paragraph handling: function process_stream()
44test_fmt "" "a\nb\n" "a b\n"
45test_fmt "" "a\n\nb\n" "a\n\nb\n"
46test_fmt "" "a\n.b\n" "a\n.b\n"
47test_fmt "-n" "a\n.b\n" "a .b\n"
48test_fmt "" "a\nb\n c\n d\n" "a b\n c d\n"
49test_fmt "" " a\n b\nc\nd\n" " a b\nc d\n"
50test_fmt "" " a\nb\nc\n" " a\nb c\n"
51test_fmt "" " a\n\tb\n        c\n\td\n e\n" " a\n        b c d\n e\n"
52test_fmt "-l 8" " a\n\tb\n        c\n\td\n e\n" " a\n\tb c d\n e\n"
53
54# The -p option seems to be broken.
55# Apparently, it allows the *second* line of a paragraph
56# to have a different indentation, not the first as documented.
57# The following tests demonstrate the current behaviour:
58test_fmt "-p" " a\nb\nc\n" " a b\nc\n"
59test_fmt "-p" "a\n b\nc\n" "a b c\n"
60test_fmt "-p" "a\nb\n c\nd\ne\n" "a b\n c d\ne\n"
61
62# mail header handling: function process_stream()
63test_fmt "-m 6 6" "X: a\n  b\n" "X: a b\n"
64test_fmt "-m 6 6" "X: a\n b\n" "X: a b\n"
65test_fmt "-m 3 6" "a\nX: b\n" "a X:\nb\n"
66
67# The -m option seems to be broken.
68# The following tests demonstrate the current behaviour:
69# If a mail header is too long, it gets wrapped, but the
70# indentation is missing from the continuation line:
71test_fmt "-m 3 6" "X: a b\n" "X: a\nb\n"
72test_fmt "-m 6 6" "X: a\n  b c\n" "X: a b\nc\n"
73test_fmt "-m 6 6" "X: a\n b c\n" "X: a b\nc\n"
74test_fmt "-m 3 6" "a\n\nX: b c\n" "a\n\nX: b\nc\n"
75test_fmt "-m 3 6" "a\n\nX: b\n c\n" "a\n\nX: b\nc\n"
76
77# in-line whitespace handling: function output_word()
78test_fmt "" "a  b\n" "a  b\n"
79test_fmt "-s" "a  b\n" "a b\n"
80test_fmt "" "a.\nb\n" "a.  b\n"
81test_fmt "" "a. b\n" "a. b\n"
82test_fmt "-s" "a. b\n" "a.  b\n"
83
84# line breaking: function output_word()
85test_fmt "2 4" "a\nb\nc\n" "a b\nc\n"
86test_fmt "2 4" "longish\na\nb\n" "longish\na b\n"
87test_fmt "2 4" "a\nlongish\nb\nc\n" "a\nlongish\nb c\n"
88test_fmt "2 4" "aa\nb\nc\nd\n" "aa\nb c\nd\n"
89
90# centering: function center_stream()
91test_fmt "-c 4" "a\n b\n\tc\n" "  a\n  b\n  c\n"
92test_fmt "-c 4" "aa\n bb\n\tcc\n" " aa\n bb\n cc\n"
93test_fmt "-c 4" "aaa\n bbb\n\tccc\n" " aaa\n bbb\n ccc\n"
94test_fmt "-c 4" "aaaa\n bbbb\n\tcccc\n" "aaaa\nbbbb\ncccc\n"
95
96# control characters in the input stream: function get_line()
97test_fmt "" "a\ab\n" "ab\n"
98test_fmt "" "a\bb\n" "b\n"
99test_fmt "" "a\tb\n" "a       b\n"
100test_fmt "" ".a\ab\n" ".a\ab\n"
101test_fmt "" ".a\bb\n" ".a\bb\n"
102test_fmt "" ".a\tb\n" ".a\tb\n"
103test_fmt "" " .a\ab\n" " .ab\n"
104test_fmt "" " .a\bb\n" " .b\n"
105test_fmt "" " .a\tb\n" " .a     b\n"
106test_fmt "-n" ".a\ab\n" ".ab\n"
107test_fmt "-n" ".a\bb\n" ".b\n"
108test_fmt "-n" ".a\tb\n" ".a      b\n"
109
110# input corner cases: function get_line()
111test_fmt "" "" ""
112test_fmt "" " " ""
113test_fmt "" "\t" ""
114test_fmt "" "a" "a\n"
115test_fmt "" "a " "a\n"
116test_fmt "" "a\t" "a\n"
117test_fmt "" " \n" "\n"
118test_fmt "" "a \n" "a\n"
119test_fmt "" "a\t\n" "a\n"
120
121#utf-8
122test_fmt "14" \
123    "pöüöüöü üüp\n" \
124    "pöüöüöü üüp\n" \
125    "pöüöüöü\nüüp\n"
126
127
128exit 0
129