xref: /freebsd/contrib/netbsd-tests/usr.bin/cut/t_cut.sh (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1# $NetBSD: t_cut.sh,v 1.1 2012/03/17 16:33:13 jruoho Exp $
2#
3# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28h_run()
29{
30	file="${1}"; shift
31	opts="${*}"
32
33	for fields in 1 2 3 1-2 2,3 4 1-3,4-7 1,2-7
34	do
35		opts="-f ${fields} $@"
36		echo "----- test: cut ${opts} $(basename $file) -----"
37		cut $opts "$file" || atf_fail "command failed: cut ${opts} $file"
38	done
39}
40
41h_check()
42{
43	diff -Nru "$1" "$2" || atf_fail "files $1 and $2 differ"
44}
45
46atf_test_case basic
47basic_head()
48{
49	atf_set "descr" "Checks basic functionality"
50}
51basic_body()
52{
53	h_run "$(atf_get_srcdir)/d_cut.in" > out
54	h_check out "$(atf_get_srcdir)/d_basic.out"
55}
56
57atf_test_case sflag
58sflag_head()
59{
60	atf_set "descr" "Checks -s flag"
61}
62sflag_body()
63{
64	h_run "$(atf_get_srcdir)/d_cut.in" -s > out
65	h_check out "$(atf_get_srcdir)/d_sflag.out"
66}
67
68atf_test_case dflag
69dflag_head()
70{
71	atf_set "descr" "Checks -d flag"
72}
73dflag_body()
74{
75	h_run "$(atf_get_srcdir)/d_cut.in" -d ":" > out
76	h_check out "$(atf_get_srcdir)/d_dflag.out"
77}
78
79atf_test_case dsflag
80dsflag_head()
81{
82	atf_set "descr" "Checks -s and -d flags combined"
83}
84dsflag_body()
85{
86	h_run "$(atf_get_srcdir)/d_cut.in" -d ":" -s > out
87	h_check out "$(atf_get_srcdir)/d_dsflag.out"
88}
89
90atf_test_case latin1
91latin1_head()
92{
93	atf_set "descr" "Checks support for non-ascii characters"
94}
95latin1_body()
96{
97	export LC_ALL=C
98
99	atf_check -o inline:"bar\nBar\nBAr\nBAR\n" \
100		cut -b 6,7,8 "$(atf_get_srcdir)/d_latin1.in"
101
102	atf_check -o inline:"bar\nBar\nBAr\nBAR\n" \
103		cut -c 6,7,8 "$(atf_get_srcdir)/d_latin1.in"
104}
105
106atf_test_case utf8
107utf8_head()
108{
109	atf_set "descr" "Checks support for multibyte characters"
110}
111utf8_body()
112{
113	export LC_ALL=en_US.UTF-8
114
115	atf_check -o inline:":ba\n:Ba\n:BA\n:BA\n" \
116		cut -b 6,7,8 "$(atf_get_srcdir)/d_utf8.in"
117
118	atf_check -o inline:"bar\nBar\nBAr\nBAR\n" \
119		cut -c 6,7,8 "$(atf_get_srcdir)/d_utf8.in"
120}
121
122atf_init_test_cases()
123{
124	atf_add_test_case basic
125	atf_add_test_case sflag
126	atf_add_test_case dflag
127	atf_add_test_case dsflag
128	atf_add_test_case latin1
129	atf_add_test_case utf8
130}
131