blob: 308a39c7718853bca4248b28bb3092053811a297 [file] [log] [blame]
Gavin Howardea7bea62018-02-22 18:55:03 -07001#! /bin/bash
2
3gen()
4{
5 result=`dd if=/dev/urandom bs=4 count=1 2>/dev/null | od -t u4 | awk 'NR==1 {print $2}'`
6 echo -n "$result"
7}
8
9neg()
10{
11 result=$(gen)
12 result="$((result & 1))"
13 echo -n "$result"
14}
15
16zero()
17{
18 result=$(gen)
19 result="$((result & 15))"
20 echo -n $(expr "$result" = 15)
21}
22
23num()
24{
25 num=""
26
27 neg=$1
28
29 real=$2
30
31 zero=$3
32
33 if [ "$neg" -ne 0 ]; then
34
35 neg=$(neg)
36
37 if [ "$neg" -ne 0 ]; then
38 num="-"
39 fi
40 fi
41
42 if [ "$zero" -eq 0 ]; then
43
44 z=$(zero)
45
46 if [ "$z" -ne 0 ]; then
47 num="${num}0"
48 fi
49 else
50 num="$num`gen`"
51 fi
52
53 if [ "$real" -ne 0 ]; then
54
55 if [ "$zero" -eq 0 ]; then
56
57 z=$(zero)
58
59 if [ "$z" -ne 0 ]; then
60 num="$num."
61 num="$num`gen`"
62 fi
63 fi
64 fi
65
66 echo -n "$num"
67}
68
Gavin Howardea7bea62018-02-22 18:55:03 -070069ops=( '+' '-' '*' '/' '%' '^' )
70files=( "add" "subtract" "multiply" "divide" "modulus" "power" "sqrt" )
71
72script="$0"
73
74testdir=$(dirname "$script")
75
76if [ "$#" -lt 1 ]; then
77 echo "usage: $0 num_tests [bc test_output1 test_output2]"
78 exit 1
79fi
80
81ntests="$1"
82shift
83
84if [ "$#" -gt 0 ]; then
85 bc="$1"
86 shift
87else
88 bc="$testdir/../bc"
89fi
90
91if [ "$#" -gt 0 ]; then
92 out1="$1"
93 shift
94else
95 out1="$testdir/../log_bc.txt"
96fi
97
98if [ "$#" -gt 0 ]; then
99 out2="$1"
100 shift
101else
102 out2="$testdir/../log_test.txt"
103fi
104
105t=0
106
107while [ "$t" -lt "$ntests" ]; do
108
109 rm -rf "$out1"
110 rm -rf "$out2"
111
112 line=""
113
114 operator=$(gen)
115
Gavin Howardea7bea62018-02-22 18:55:03 -0700116 op=$(expr "$operator" % 7)
117
Gavin Howardea7bea62018-02-22 18:55:03 -0700118 if [ "$op" -ne 7 ]; then
119
Gavin Howardea7bea62018-02-22 18:55:03 -0700120 line=$(num 1 1 1)
121
Gavin Howardea7bea62018-02-22 18:55:03 -0700122 line="$line ${ops[$op]}"
123
Gavin Howardea7bea62018-02-22 18:55:03 -0700124 if [ "$op" -eq 3 ]; then
125 line="$line `num 1 1 0`"
126 elif [ "$op" -eq 5 ]; then
127 line="$line `num 1 0 1`"
128 else
129 line="$line `num 1 1 1`"
130 fi
131
132
133 else
134 line="sqrt(`num 0 1 1`)"
135 fi
136
137 echo "$line"
138
139 echo "$line" | bc -lq > "$out1"
140 echo "$line" | "$bc" -lq > "$out2"
141
142 error="$?"
143
144 if [ "$error" -ne 0 ]; then
145 exit "$error"
146 fi
147
148 diff "$out1" "$out2"
149
150 error="$?"
151
152 if [ "$error" -ne 0 ]; then
153 echo "$line" >> "${files[$op]}.txt"
154 exit "$error"
155 fi
156
157 t=$(expr "$t" + "1")
158
159done
160