xref: /freebsd/usr.bin/uniq/tests/uniq_test.sh (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1#
2# Copyright (c) 2024 Klara, Inc.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7atf_check_uniq() {
8	atf_check uniq "$@" input actual
9	atf_check diff -u actual expected
10	atf_check uniq "$@" - actual <input
11	atf_check diff -u actual expected
12	atf_check -o file:expected uniq "$@" input
13	atf_check -o file:expected uniq "$@" <input
14	atf_check -o file:expected uniq "$@" - <input
15}
16
17atf_test_case basic
18basic_head() {
19	atf_set descr "basic test without options"
20}
21basic_body() {
22	printf "a\na\nb\nb\na\na\n" >input
23	printf "a\nb\na\n" >expected
24	atf_check_uniq
25}
26
27atf_test_case count
28count_head() {
29	atf_set descr "basic test showing counts"
30}
31count_body() {
32	printf "a\na\nb\nb\nb\na\na\na\na\n" >input
33	printf "   2 a\n   3 b\n   4 a\n" >expected
34	atf_check_uniq -c
35	atf_check_uniq --count
36}
37
38atf_test_case repeated
39repeated_head() {
40	atf_set descr "print repeated lines only"
41}
42repeated_body() {
43	printf "a\na\nb\na\na\n" >input
44	printf "a\na\n" >expected
45	atf_check_uniq -d
46	atf_check_uniq --repeated
47}
48
49atf_test_case count_repeated
50count_repeated_head() {
51	atf_set descr "count and print repeated lines only"
52}
53count_repeated_body() {
54	printf "a\na\nb\nb\na\n" >input
55	printf "   2 a\n   2 b\n" >expected
56	atf_check_uniq --count --repeated
57}
58
59atf_test_case all_repeated
60all_repeated_head() {
61	atf_set descr "print every instance of repeated lines"
62}
63all_repeated_body() {
64	printf "a\na\nb\na\na\n" >input
65	printf "a\na\na\na\n" >expected
66	atf_check_uniq -D
67	atf_check_uniq --all-repeated
68}
69
70atf_test_case skip_fields
71skip_fields_head() {
72	atf_set descr "skip fields"
73}
74skip_fields_body() {
75	printf "1 a\n2 a\n3 b\n4 b\n5 a\n6 a\n" >input
76	printf "1 a\n3 b\n5 a\n" >expected
77	atf_check_uniq -1
78	atf_check_uniq -f 1
79	atf_check_uniq --skip-fields 1
80}
81
82atf_test_case skip_fields_tab
83skip_fields_tab_head() {
84	atf_set descr "skip fields (with tabs)"
85}
86skip_fields_tab_body() {
87	printf "1\ta\n2\ta\n3\tb\n4\tb\n5\ta\n6\ta\n" >input
88	printf "1\ta\n3\tb\n5\ta\n" >expected
89	atf_check_uniq -1
90	atf_check_uniq -f 1
91	atf_check_uniq --skip-fields 1
92}
93
94atf_test_case ignore_case
95ignore_case_head() {
96	atf_set descr "ignore case"
97}
98ignore_case_body() {
99	printf "a\nA\nb\nB\na\nA\n" >input
100	printf "a\nb\na\n" >expected
101	atf_check_uniq -i
102	atf_check_uniq --ignore-case
103}
104
105atf_test_case skip_chars
106skip_chars_head() {
107	atf_set descr "skip chars"
108}
109skip_chars_body() {
110	printf "1 a\n2 a\n3 b\n4 b\n5 a\n6 a\n" >input
111	printf "1 a\n3 b\n5 a\n" >expected
112	atf_check_uniq +2
113	atf_check_uniq -s 2
114	atf_check_uniq --skip-chars 2
115}
116
117atf_test_case unique
118unique_head() {
119	atf_set descr "print non-repeated lines only"
120}
121unique_body() {
122	printf "a\na\nb\na\na\n" >input
123	printf "b\n" >expected
124	atf_check_uniq -u
125	atf_check_uniq --unique
126}
127
128atf_test_case count_unique
129count_unique_head() {
130	atf_set descr "print non-repeated lines with count"
131}
132count_unique_body() {
133	printf "a\na\nb\n" >input
134	printf "   1 b\n" >expected
135	atf_check_uniq --unique --count
136	atf_check_uniq --count --unique
137}
138
139atf_test_case interactive
140interactive_head() {
141	atf_set descr "test interactive use"
142}
143interactive_body() {
144	sh -c 'yes | stdbuf -oL uniq >actual' &
145	pid=$!
146	sleep 1
147	kill $!
148	atf_check -o inline:"y\n" cat actual
149}
150
151atf_test_case interactive_repeated
152interactive_repeated_head() {
153	atf_set descr "test interactive use with -d"
154}
155interactive_repeated_body() {
156	sh -c 'yes | stdbuf -oL uniq -d >actual' &
157	pid=$!
158	sleep 1
159	kill $!
160	atf_check -o inline:"y\n" cat actual
161}
162
163atf_test_case stdout
164stdout_head() {
165	atf_set descr "error writing to stdout"
166}
167stdout_body() {
168	(
169		trap "" PIPE
170		# Give true(1) some time to exit.
171		sleep 1
172		echo a | uniq 2>stderr
173		echo $? >result
174	) | true
175	atf_check -o inline:"1\n" cat result
176	atf_check -o match:"stdout" cat stderr
177}
178
179atf_init_test_cases()
180{
181	atf_add_test_case basic
182	atf_add_test_case count
183	atf_add_test_case repeated
184	atf_add_test_case count_repeated
185	atf_add_test_case all_repeated
186	atf_add_test_case skip_fields
187	atf_add_test_case skip_fields_tab
188	atf_add_test_case ignore_case
189	atf_add_test_case skip_chars
190	atf_add_test_case unique
191	atf_add_test_case count_unique
192	atf_add_test_case interactive
193	atf_add_test_case interactive_repeated
194	atf_add_test_case stdout
195}
196