@@ -6,21 +6,44 @@ function echo_math_test() (
6
6
local inputs=(' 1 - 1/2' ' 1 - 1/3' ' 1 - 1/4' ' 123456789 / 0.0987654321' )
7
7
local expected_six=$' 0.500000\n 0.666667\n 0.750000\n 1249999988.609375'
8
8
local expected_two=$' 0.50\n 0.67\n 0.75\n 1249999988.61'
9
+ local expected_zero=$' 0\n 1\n 1\n 1249999989'
10
+ local expected_zero_deno=$' 1\n 1\n 1\n 1249999989'
9
11
function __test {
12
+ local expected
13
+
10
14
# this first one may install the tool, use --discard-tty to avoid dumping thousands of lines to the CI terminal, and --ignore-tty to avoid comparisons of the captured TTY data
15
+
16
+ # --precision=6
17
+ # arguments
11
18
eval-tester --discard-tty --ignore-tty --stdout=" $expected_six " -- \
12
19
echo-math " $@ " -- " ${inputs[@]} "
13
-
20
+ # stdin
14
21
__print_lines " ${inputs[@]} " |
15
22
eval-tester --stdout=" $expected_six " -- \
16
23
echo-math " $@ " --stdin
17
24
25
+ # --precision=2
26
+ # arguments
18
27
eval-tester --stdout=" $expected_two " -- \
19
28
echo-math --precision=2 " $@ " -- " ${inputs[@]} "
20
-
29
+ # stdin
21
30
__print_lines " ${inputs[@]} " |
22
31
eval-tester --stdout=" $expected_two " -- \
23
32
echo-math --precision=2 " $@ " --stdin
33
+
34
+ # --precision=0
35
+ if [[ $* == * ' --tool=deno' * ]]; then
36
+ expected=" $expected_zero_deno "
37
+ else
38
+ expected=" $expected_zero "
39
+ fi
40
+ # arguments
41
+ eval-tester --stdout=" $expected " -- \
42
+ echo-math --precision=0 " $@ " -- " ${inputs[@]} "
43
+ # stdin
44
+ __print_lines " ${inputs[@]} " |
45
+ eval-tester --stdout=" $expected " -- \
46
+ echo-math --precision=0 " $@ " --stdin
24
47
}
25
48
26
49
# run test with defaults
@@ -48,7 +71,11 @@ function echo_math_test() (
48
71
)
49
72
function echo_math() (
50
73
source " $DOROTHY /sources/stdinargs.bash"
51
- local all_tools=(awk gawk deno)
74
+ local all_tools=()
75
+ if [[ $BASH_NATIVE_FLOATING_POINT == ' yes' ]]; then
76
+ all_tools+=(bash)
77
+ fi
78
+ all_tools+=(awk gawk deno)
52
79
# [bc] is removed, as it fails silently on debian systems
53
80
54
81
# =====================================
@@ -112,6 +139,7 @@ function echo_math() (
112
139
# =====================================
113
140
# Action
114
141
142
+ # bash: dynamic decimals by default
115
143
# bc: 0 decimals by default
116
144
# bc -l: 20 decimals by default
117
145
# 0. becomes ., hence need for printf
@@ -125,34 +153,41 @@ function echo_math() (
125
153
# echo-escape-command: Failed to compute: awk -v precision=6 BEGIN\ \{\ printf\ \"%.\"\ precision\ \"f\\n\"\,\ /\ \}
126
154
__print_error ' Failed to compute: ' --code=" $( echo-quote --always=no -- " $@ " | echo-join --stdin || :) " --newline \
127
155
" With $1 binary: " --code=" $( type -P " $1 " || :) " --newline \
128
- " With $1 version: " --code=" $( $1 --version 2> /dev/null || :) "
156
+ " With $1 version: " --code=" $( $1 --version 2> /dev/null || :) " || :
129
157
return " $exit_status "
130
158
}
131
159
}
132
160
133
161
# perl implementation (doesn't work on opensuse): https://gist.github.com/balupton/2a8ec0c004cb382895807ae71c6cef83
134
- if [[ $option_tool == ' bc' ]]; then
135
- function on_line {
162
+ if [[ $option_tool == ' bash' ]]; then
163
+ enable fltexpr
164
+ function __on_line {
165
+ local formula=" $1 " fodder_to_respect_exit_status
166
+ fltexpr " fodder_to_respect_exit_status=$formula " || return
167
+ printf " %.${option_precision} f\n" " $fodder_to_respect_exit_status " || return
168
+ }
169
+ elif [[ $option_tool == ' bc' ]]; then
170
+ function __on_line {
136
171
local formula=" $1 " fodder_to_respect_exit_status
137
- fodder_to_respect_exit_status=" $( __wrap bc --mathlib --no-prompt --no-read-prompt --standard --warn --expression=" $formula " ) "
138
- printf " %.${option_precision} f\n" " $fodder_to_respect_exit_status "
172
+ fodder_to_respect_exit_status=" $( __wrap bc --mathlib --no-prompt --no-read-prompt --standard --warn --expression=" $formula " ) " || return
173
+ printf " %.${option_precision} f\n" " $fodder_to_respect_exit_status " || return
139
174
}
140
175
elif [[ $option_tool == ' deno' ]]; then
141
176
local deno_script
142
177
deno_script=" $( type -P echo-math.ts) "
143
- function on_line {
178
+ function __on_line {
144
179
local formula=" $1 "
145
- __wrap " $deno_script " " $option_precision " " $formula "
180
+ __wrap " $deno_script " " $option_precision " " $formula " || return
146
181
}
147
182
elif [[ $option_tool == ' awk' ]]; then
148
- function on_line {
183
+ function __on_line {
149
184
local formula=" $1 "
150
- __wrap awk -v precision=" $option_precision " ' BEGIN { printf "%." precision "f\n", ' " $formula " ' }'
185
+ __wrap awk -v precision=" $option_precision " ' BEGIN { printf "%." precision "f\n", ' " $formula " ' }' || return
151
186
}
152
187
elif [[ $option_tool == ' gawk' ]]; then
153
- function on_line {
188
+ function __on_line {
154
189
local formula=" $1 "
155
- __wrap gawk -v precision=" $option_precision " ' BEGIN { printf "%." precision "f\n", ' " $formula " ' }'
190
+ __wrap gawk -v precision=" $option_precision " ' BEGIN { printf "%." precision "f\n", ' " $formula " ' }' || return
156
191
}
157
192
else
158
193
return 19 # ENODEV 19 Operation not supported by device
0 commit comments