xref: /freebsd/usr.bin/dc/tests/bcode.sh (revision 0957b409)
1# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2#
3# Copyright (c) 2017 Alan Somers
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28
29atf_test_case bmod
30bmod_head()
31{
32	atf_set "descr" "Tests the remainder % operator"
33}
34bmod_body()
35{
36	cat > input.dc << EOF
370 3 % p		# basic usage
381 3 % p
392 3 % p
403 3 % p
414 3 % p
42_1 3 % p	# negative dividends work like a remainder, not a modulo
431 _3 % p	# negative divisors use the divisor's absolute value
441k		# fractional remainders
455 3 % p
466 5 % p
475.4 3 % p
48_.1 3 % p
491.1 _3 % p
501 .3 % p
51EOF
52	dc input.dc > output.txt
53	cat > expect.txt << EOF
540
551
562
570
581
59-1
601
612
621
632.4
64-.1
651.1
66.1
67EOF
68	atf_check cmp expect.txt output.txt
69}
70
71atf_test_case bmod_by_zero
72bmod_by_zero_head()
73{
74	atf_set "descr" "remaindering by zero should print a warning"
75}
76bmod_by_zero_body()
77{
78	atf_check -e match:"remainder by zero" dc -e '1 0 %'
79}
80
81atf_test_case bdivmod
82bdivmod_head()
83{
84	atf_set "descr" "Tests the divide and modulo ~ operator"
85}
86bdivmod_body()
87{
88	cat > input.dc << EOF
890 3 ~ n32Pp	# basic usage
901 3 ~ n32Pp
912 3 ~ n32Pp
923 3 ~ n32Pp
934 3 ~ n32Pp
94_1 3 ~ n32Pp	# negative dividends work like a remainder, not a modulo
95_4 3 ~ n32Pp	# sign of quotient and divisor must agree
961 _3 ~ n32Pp	# negative divisors use the divisor's absolute value
971k		# fractional remainders
985 3 ~ n32Pp
996 5 ~ n32Pp
1005.4 3 ~ n32Pp
101_.1 3 ~ n32Pp
1021.1 _3 ~ n32Pp
1031 .3 ~ n32Pp
1044k
105.01 .003 ~ n32Pp	# divmod quotient always has scale=0
106EOF
107	dc input.dc > output.txt
108	cat > expect.txt << EOF
1090 0
1101 0
1112 0
1120 1
1131 1
114-1 0
115-1 -1
1161 0
1172 1.6
1181 1.2
1192.4 1.8
120-.1 0.0
1211.1 -.3
122.1 3.3
123.001 3.3333
124EOF
125	atf_check cmp expect.txt output.txt
126}
127
128atf_test_case bdivmod_by_zero
129bdivmod_by_zero_head()
130{
131	atf_set "descr" "divmodding by zero should print a warning"
132}
133bdivmod_by_zero_body()
134{
135	atf_check -e match:"divide by zero" dc -e '1 0 ~'
136}
137
138atf_init_test_cases()
139{
140	atf_add_test_case bmod
141	atf_add_test_case bmod_by_zero
142	atf_add_test_case bdivmod
143	atf_add_test_case bdivmod_by_zero
144}
145