xref: /freebsd/usr.bin/dc/tests/bcode.sh (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# Copyright (c) 2017 Alan Somers
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26
27atf_test_case bmod
28bmod_head()
29{
30	atf_set "descr" "Tests the remainder % operator"
31}
32bmod_body()
33{
34	cat > input.dc << EOF
350 3 % p		# basic usage
361 3 % p
372 3 % p
383 3 % p
394 3 % p
40_1 3 % p	# negative dividends work like a remainder, not a modulo
411 _3 % p	# negative divisors use the divisor's absolute value
421k		# fractional remainders
435 3 % p
446 5 % p
455.4 3 % p
46_.1 3 % p
471.1 _3 % p
481 .3 % p
49EOF
50	dc input.dc > output.txt
51	cat > expect.txt << EOF
520
531
542
550
561
57-1
581
592
601
612.4
62-.1
631.1
64.1
65EOF
66	atf_check cmp expect.txt output.txt
67}
68
69atf_test_case bmod_by_zero
70bmod_by_zero_head()
71{
72	atf_set "descr" "remaindering by zero should print a warning"
73}
74bmod_by_zero_body()
75{
76	atf_check -e match:"remainder by zero" dc -e '1 0 %'
77}
78
79atf_test_case bdivmod
80bdivmod_head()
81{
82	atf_set "descr" "Tests the divide and modulo ~ operator"
83}
84bdivmod_body()
85{
86	cat > input.dc << EOF
870 3 ~ n32Pp	# basic usage
881 3 ~ n32Pp
892 3 ~ n32Pp
903 3 ~ n32Pp
914 3 ~ n32Pp
92_1 3 ~ n32Pp	# negative dividends work like a remainder, not a modulo
93_4 3 ~ n32Pp	# sign of quotient and divisor must agree
941 _3 ~ n32Pp	# negative divisors use the divisor's absolute value
951k		# fractional remainders
965 3 ~ n32Pp
976 5 ~ n32Pp
985.4 3 ~ n32Pp
99_.1 3 ~ n32Pp
1001.1 _3 ~ n32Pp
1011 .3 ~ n32Pp
1024k
103.01 .003 ~ n32Pp	# divmod quotient always has scale=0
104EOF
105	dc input.dc > output.txt
106	cat > expect.txt << EOF
1070 0
1081 0
1092 0
1100 1
1111 1
112-1 0
113-1 -1
1141 0
1152 1.6
1161 1.2
1172.4 1.8
118-.1 0.0
1191.1 -.3
120.1 3.3
121.001 3.3333
122EOF
123	atf_check cmp expect.txt output.txt
124}
125
126atf_test_case bdivmod_by_zero
127bdivmod_by_zero_head()
128{
129	atf_set "descr" "divmodding by zero should print a warning"
130}
131bdivmod_by_zero_body()
132{
133	atf_check -e match:"divide by zero" dc -e '1 0 ~'
134}
135
136atf_init_test_cases()
137{
138	atf_add_test_case bmod
139	atf_add_test_case bmod_by_zero
140	atf_add_test_case bdivmod
141	atf_add_test_case bdivmod_by_zero
142}
143