xref: /aosp_15_r20/external/ltp/testcases/commands/wc/wc01.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2016 Fujitsu Ltd.
4# Author: Xiao Yang <[email protected]>
5#
6# Test wc command with some basic options.
7
8TST_CNT=12
9TST_SETUP=setup
10TST_TESTFUNC=do_test
11TST_NEEDS_TMPDIR=1
12TST_NEEDS_CMDS="wc"
13
14setup()
15{
16	echo "hello world" > ltp_wc
17
18	echo "This is a test" >> ltp_wc
19}
20
21wc_test()
22{
23	local wc_opt=$1
24	local wc_file=$2
25	local std_out=$3
26
27	local wc_cmd="wc $wc_opt $wc_file"
28
29	eval $wc_cmd > temp 2>&1
30	if [ $? -ne 0 ]; then
31		grep -q -E "unknown option|invalid option|unrecognized option" temp
32		if [ $? -eq 0 ]; then
33			tst_res TCONF "$wc_cmd not supported."
34		else
35			tst_res TFAIL "$wc_cmd failed."
36		fi
37		return
38	fi
39
40	if [ $# -gt 1 ]; then
41		local act_out=`cat temp | awk '{printf $1}'`
42		if [ $act_out -ne $std_out ]; then
43			tst_res TFAIL "$wc_cmd got mismatched data."
44			return
45		fi
46	fi
47
48	tst_res TPASS "wc passed with $wc_opt option."
49}
50
51do_test()
52{
53	case $1 in
54	1) wc_test "-c" ltp_wc 27;;
55	2) wc_test "--bytes" ltp_wc 27;;
56	3) wc_test "-l" ltp_wc 2;;
57	4) wc_test "--lines" ltp_wc 2;;
58	5) wc_test "-L" ltp_wc 14;;
59	6) wc_test "--max-line-length" ltp_wc 14;;
60	7) wc_test "-w" ltp_wc 6;;
61	8) wc_test "--words" ltp_wc 6;;
62	9) wc_test "-m" ltp_wc 27;;
63	10) wc_test "--chars" ltp_wc 27;;
64	11) wc_test "--help";;
65	12) wc_test "--version";;
66	esac
67}
68
69. tst_test.sh
70tst_run
71