xref: /freebsd/usr.bin/uniq/tests/uniq_test.sh (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
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 -cd
57	atf_check_uniq -c -d
58	atf_check_uniq -dc
59	atf_check_uniq -d -c
60	atf_check_uniq --count --repeated
61	atf_check_uniq --repeated --count
62}
63
64atf_test_case all_repeated
65all_repeated_head() {
66	atf_set descr "print every instance of repeated lines"
67}
68all_repeated_body() {
69	printf "a\na\nb\na\na\n" >input
70	printf "a\na\na\na\n" >expected
71	atf_check_uniq -D
72	atf_check_uniq -Dnone
73	atf_check_uniq --all-repeated
74	atf_check_uniq --all-repeated=none
75	printf "\na\na\n\na\na\n" >expected
76	atf_check_uniq -Dprepend
77	atf_check_uniq --all-repeated=prepend
78	printf "a\na\n\na\na\n" >expected
79	atf_check_uniq -Dseparate
80	atf_check_uniq --all-repeated=separate
81}
82
83atf_test_case count_all_repeated
84count_all_repeated_head() {
85	atf_set descr "count and print every instance of repeated lines"
86}
87count_all_repeated_body() {
88	printf "a\na\nb\na\na\n" >input
89	printf "   1 a\n   2 a\n   1 a\n   2 a\n" >expected
90	atf_check_uniq -D -c
91	atf_check_uniq -Dnone -c
92	atf_check_uniq -cD
93	atf_check_uniq -cDnone
94	atf_check_uniq -c -D
95	atf_check_uniq -c -Dnone
96	atf_check_uniq --all-repeated --count
97	atf_check_uniq --all-repeated=none --count
98	atf_check_uniq --count --all-repeated
99	atf_check_uniq --count --all-repeated=none
100}
101
102atf_test_case skip_fields
103skip_fields_head() {
104	atf_set descr "skip fields"
105}
106skip_fields_body() {
107	printf "1 a\n2 a\n3 b\n4 b\n5 a\n6 a\n" >input
108	printf "1 a\n3 b\n5 a\n" >expected
109	atf_check_uniq -1
110	atf_check_uniq -f 1
111	atf_check_uniq --skip-fields 1
112}
113
114atf_test_case skip_fields_tab
115skip_fields_tab_head() {
116	atf_set descr "skip fields (with tabs)"
117}
118skip_fields_tab_body() {
119	printf "1\ta\n2\ta\n3\tb\n4\tb\n5\ta\n6\ta\n" >input
120	printf "1\ta\n3\tb\n5\ta\n" >expected
121	atf_check_uniq -1
122	atf_check_uniq -f 1
123	atf_check_uniq --skip-fields 1
124}
125
126atf_test_case ignore_case
127ignore_case_head() {
128	atf_set descr "ignore case"
129}
130ignore_case_body() {
131	printf "a\nA\nb\nB\na\nA\n" >input
132	printf "a\nb\na\n" >expected
133	atf_check_uniq -i
134	atf_check_uniq --ignore-case
135}
136
137atf_test_case skip_chars
138skip_chars_head() {
139	atf_set descr "skip chars"
140}
141skip_chars_body() {
142	printf "1 a\n2 a\n3 b\n4 b\n5 a\n6 a\n" >input
143	printf "1 a\n3 b\n5 a\n" >expected
144	atf_check_uniq +2
145	atf_check_uniq -s 2
146	atf_check_uniq --skip-chars 2
147}
148
149atf_test_case unique
150unique_head() {
151	atf_set descr "print non-repeated lines only"
152}
153unique_body() {
154	printf "a\na\nb\na\na\n" >input
155	printf "b\n" >expected
156	atf_check_uniq -u
157	atf_check_uniq --unique
158}
159
160atf_test_case count_unique
161count_unique_head() {
162	atf_set descr "print non-repeated lines with count"
163}
164count_unique_body() {
165	printf "a\na\nb\n" >input
166	printf "   1 b\n" >expected
167	atf_check_uniq --unique --count
168	atf_check_uniq --count --unique
169}
170
171atf_test_case interactive
172interactive_head() {
173	atf_set descr "test interactive use"
174}
175interactive_body() {
176	sh -c 'yes | stdbuf -oL uniq >actual' &
177	pid=$!
178	sleep 1
179	kill $!
180	atf_check -o inline:"y\n" cat actual
181}
182
183atf_test_case interactive_repeated
184interactive_repeated_head() {
185	atf_set descr "test interactive use with -d"
186}
187interactive_repeated_body() {
188	sh -c 'yes | stdbuf -oL uniq -d >actual' &
189	pid=$!
190	sleep 1
191	kill $!
192	atf_check -o inline:"y\n" cat actual
193}
194
195atf_test_case stdout
196stdout_head() {
197	atf_set descr "error writing to stdout"
198}
199stdout_body() {
200	(
201		trap "" PIPE
202		# Give true(1) some time to exit.
203		sleep 1
204		echo a | uniq 2>stderr
205		echo $? >result
206	) | true
207	atf_check -o inline:"1\n" cat result
208	atf_check -o match:"stdout" cat stderr
209}
210
211atf_init_test_cases()
212{
213	atf_add_test_case basic
214	atf_add_test_case count
215	atf_add_test_case repeated
216	atf_add_test_case count_repeated
217	atf_add_test_case all_repeated
218	atf_add_test_case count_all_repeated
219	atf_add_test_case skip_fields
220	atf_add_test_case skip_fields_tab
221	atf_add_test_case ignore_case
222	atf_add_test_case skip_chars
223	atf_add_test_case unique
224	atf_add_test_case count_unique
225	atf_add_test_case interactive
226	atf_add_test_case interactive_repeated
227	atf_add_test_case stdout
228}
229