Gavin Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | gen() |
| 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 | |
| 9 | neg() |
| 10 | { |
| 11 | result=$(gen) |
| 12 | result="$((result & 1))" |
| 13 | echo -n "$result" |
| 14 | } |
| 15 | |
| 16 | zero() |
| 17 | { |
| 18 | result=$(gen) |
| 19 | result="$((result & 15))" |
| 20 | echo -n $(expr "$result" = 15) |
| 21 | } |
| 22 | |
| 23 | num() |
| 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 Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 69 | ops=( '+' '-' '*' '/' '%' '^' ) |
| 70 | files=( "add" "subtract" "multiply" "divide" "modulus" "power" "sqrt" ) |
| 71 | |
| 72 | script="$0" |
| 73 | |
| 74 | testdir=$(dirname "$script") |
| 75 | |
| 76 | if [ "$#" -lt 1 ]; then |
| 77 | echo "usage: $0 num_tests [bc test_output1 test_output2]" |
| 78 | exit 1 |
| 79 | fi |
| 80 | |
| 81 | ntests="$1" |
| 82 | shift |
| 83 | |
| 84 | if [ "$#" -gt 0 ]; then |
| 85 | bc="$1" |
| 86 | shift |
| 87 | else |
| 88 | bc="$testdir/../bc" |
| 89 | fi |
| 90 | |
| 91 | if [ "$#" -gt 0 ]; then |
| 92 | out1="$1" |
| 93 | shift |
| 94 | else |
| 95 | out1="$testdir/../log_bc.txt" |
| 96 | fi |
| 97 | |
| 98 | if [ "$#" -gt 0 ]; then |
| 99 | out2="$1" |
| 100 | shift |
| 101 | else |
| 102 | out2="$testdir/../log_test.txt" |
| 103 | fi |
| 104 | |
| 105 | t=0 |
| 106 | |
| 107 | while [ "$t" -lt "$ntests" ]; do |
| 108 | |
| 109 | rm -rf "$out1" |
| 110 | rm -rf "$out2" |
| 111 | |
| 112 | line="" |
| 113 | |
| 114 | operator=$(gen) |
| 115 | |
Gavin Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 116 | op=$(expr "$operator" % 7) |
| 117 | |
Gavin Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 118 | if [ "$op" -ne 7 ]; then |
| 119 | |
Gavin Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 120 | line=$(num 1 1 1) |
| 121 | |
Gavin Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 122 | line="$line ${ops[$op]}" |
| 123 | |
Gavin Howard | ea7bea6 | 2018-02-22 18:55:03 -0700 | [diff] [blame] | 124 | 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 | |
| 159 | done |
| 160 | |