xref: /netbsd-src/tests/games/t_morse.sh (revision 1c66f876fd1533e8602590ac7aceb2a2f941d1f6)
1#	$NetBSD: t_morse.sh,v 1.3 2024/10/12 20:44:11 rillig Exp $
2#
3# Copyright (c) 2024 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
28atf_test_case digits
29digits_head()
30{
31	atf_set 'require.progs' '/usr/games/morse'
32}
33digits_body()
34{
35	trailing_space=' '
36	morse_s_digits="\
37 -----
38 .----
39 ..---
40 ...--
41 ....-
42 .....
43 -....
44 --...
45 ---..
46 ----.
47$trailing_space
48 ...-.-
49"
50	atf_check -o "inline:$morse_s_digits" \
51	    /usr/games/morse -s 0123456789
52
53	morse_digits="\
54 daw daw daw daw daw
55 dit daw daw daw daw
56 dit dit daw daw daw
57 dit dit dit daw daw
58 dit dit dit dit daw
59 dit dit dit dit dit
60 daw dit dit dit dit
61 daw daw dit dit dit
62 daw daw daw dit dit
63 daw daw daw daw dit
64
65 dit dit dit daw dit daw
66"
67	atf_check -o "inline:$morse_digits" \
68	    /usr/games/morse 0123456789
69}
70
71
72# Before 2024-06-16, non-ASCII characters invoked undefined behavior,
73# possibly crashing morse.
74atf_test_case nonascii
75nonascii_head()
76{
77	atf_set 'require.progs' '/usr/games/morse'
78}
79nonascii_body()
80{
81	expected="\
82
83 dit dit dit daw dit daw
84"
85	atf_check -o "inline:$expected" \
86	    /usr/games/morse äöü
87}
88
89
90atf_test_case roundtrip
91roundtrip_head() {
92	atf_set 'require.progs' '/usr/games/morse'
93}
94roundtrip_body()
95{
96	# Most punctuation is ignored during encoding.
97	# Missing are: !#$%&*;<>[\]^{|}~
98
99	input=\
100' !"#$%&'\''()*+,-./'\
101'0123456789:;<=>?'\
102'@ABCDEFGHIJKLMNO'\
103'PQRSTUVWXYZ[\]^_'\
104'`abcdefghijklmno'\
105'pqrstuvwxyz{|}~'
106
107	expected=\
108' "'\''()+,-./'\
109'0123456789:=?'\
110'@ABCDEFGHIJKLMNO'\
111'PQRSTUVWXYZ_'\
112'ABCDEFGHIJKLMNO'\
113'PQRSTUVWXYZ \n'
114
115	atf_check -o 'save:roundtrip.morse' \
116	    /usr/games/morse -s "$input"
117	atf_check -o "inline:$expected" \
118	    /usr/games/morse -d < roundtrip.morse
119}
120
121
122atf_init_test_cases()
123{
124	atf_add_test_case digits
125	atf_add_test_case nonascii
126	atf_add_test_case roundtrip
127}
128