-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuteme
More file actions
executable file
·285 lines (249 loc) · 9.1 KB
/
Copy pathexecuteme
File metadata and controls
executable file
·285 lines (249 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/sh
set -eu
start_dir=$(pwd)
if [ -n "${CENTL_BIN:-}" ]; then
case "$CENTL_BIN" in
/*) centl_bin=$CENTL_BIN ;;
*) centl_bin="$start_dir/$CENTL_BIN" ;;
esac
else
centl_bin='./centl'
fi
project_root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "$project_root"
fail() {
echo "centl gauntlet: $*" >&2
exit 1
}
rounds=${CENTL_STRESS_ROUNDS:-192}
case "$rounds" in
''|*[!0-9]*) fail "CENTL_STRESS_ROUNDS must be a positive integer" ;;
esac
[ "$rounds" -gt 0 ] || fail "CENTL_STRESS_ROUNDS must be greater than zero"
[ "$rounds" -le 5000 ] || fail "CENTL_STRESS_ROUNDS is capped at 5000"
seed_input=${CENTL_STRESS_SEED:-}
if [ -z "$seed_input" ]; then
clock_seed=$(date +%s 2>/dev/null || printf '1')
seed_input=$((clock_seed + $$))
fi
case "$seed_input" in
''|*[!0-9]*) fail "CENTL_STRESS_SEED must be a non-negative integer" ;;
esac
seed=$seed_input
rng_state=$((seed_input % 2147483647))
[ "$rng_state" -gt 0 ] || rng_state=1
rand_between() {
rb_lo=$1
rb_hi=$2
rng_state=$(( (rng_state * 48271) % 2147483647 ))
RAND_VALUE=$(( rb_lo + (rng_state % (rb_hi - rb_lo + 1)) ))
}
rand_signed() {
rs_max=$1
rand_between 1 "$rs_max"
rs_mag=$RAND_VALUE
rand_between 0 1
if [ "$RAND_VALUE" -eq 0 ]; then
RAND_VALUE=$rs_mag
else
RAND_VALUE=$((-rs_mag))
fi
}
contracts=$(mktemp "${TMPDIR:-/tmp}/centl-gauntlet.XXXXXX") || exit 1
log=$(mktemp "${TMPDIR:-/tmp}/centl-gauntlet-log.XXXXXX") || {
rm -f "$contracts"
exit 1
}
cleanup() {
rm -f "$contracts" "$log"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
claims=0
claim() {
printf '%s | %s | %s\n' "$1" "$2" "$3" >> "$contracts"
claims=$((claims + 1))
}
cat >> "$contracts" <<'EOF_HEADER'
# CENTL randomized math + physics gauntlet.
# Generated by ./executeme. Every non-comment line must verify.
EOF_HEADER
# Deterministic exact arithmetic and algebra anchors.
claim equal '0.1 + 0.2' '3/10'
claim equal '1/3 + 1/6' '1/2'
claim equal '(10^80 + 1) - 10^80' '1'
claim equal '((123456789/97) * 97)' '123456789'
claim equal '((999999937 + 1)^3)' '(999999937^3 + 3*999999937^2 + 3*999999937 + 1)'
claim equal 'sum(k, k = 1, 1000)' '500500'
claim equal 'sum(k^2, k = 1, 1000)' '333833500'
claim equal 'sum(k^3, k = 1, 100)' '25502500'
claim equal 'product((k + 1)/k, k = 1, 250)' '251'
claim equal 'factorial(30)' '265252859812191058636308480000000'
claim equal 'choose(100, 3)' '161700'
claim equal 'fibonacci(50)' '12586269025'
claim equal 'gcd(123456, 7890)' '6'
claim equal 'substitute(diff(x^5 - 3*x^3 + 7*x - 11, x), x = 2)' '51'
claim equal 'integrate(x^4 - 2*x^2 + 1, x = 0, 3)' '168/5'
claim equal 'integrate(3*x^2 + 2*x + 1, x = 0, 10)' '1110'
claim equal 'distance(0, 0, 300, 400)' '500'
claim equal 'hypot(3000, 4000)' '5000'
# Certified real-enclosure anchors. Equality is intentionally avoided here.
claim greater_than 'sqrt(2)' '1414/1000'
claim less_than 'sqrt(2)' '1415/1000'
claim greater_than 'pi' '31415/10000'
claim less_than 'pi' '31416/10000'
claim greater_than 'sin(pi / 6)' '4999/10000'
claim less_than 'sin(pi / 6)' '5001/10000'
claim greater_than 'exp(1)' '2718/1000'
claim less_than 'exp(1)' '2719/1000'
claim greater_than 'log(e)' '999/1000'
claim less_than 'log(e)' '1001/1000'
# Deterministic mechanics anchors expressed only through shipped CENTL math.
claim equal '(3 * 14)' '42'
claim equal '((1/2) * 3 * 14^2)' '294'
claim equal '(5 * (981/100) * 12)' '2943/5'
claim equal '((1/2) * (981/100) * 10^2)' '981/2'
claim equal '((40 * 8) - (1/2) * 10 * 8^2)' '0'
claim equal '(30 * 8)' '240'
claim equal '(40^2 / (2 * 10))' '80'
claim equal '((2 * 9^2 / 3) * 3)' '(2 * 9^2)'
claim equal '(1200 * (981/100) * 15)' '(1200 * (981/100) * 20 - 1200 * (981/100) * 5)'
claim equal '(50 * (3/5))' '30'
claim equal '(12 * (5/4))' '15'
claim equal '(1 / (10*20/(10+20)))' '(1/10 + 1/20)'
iteration=1
while [ "$iteration" -le "$rounds" ]; do
rand_signed 97; a=$RAND_VALUE
rand_between 1 97; b=$RAND_VALUE
rand_signed 97; c=$RAND_VALUE
rand_between 1 97; d=$RAND_VALUE
rand_signed 31; e=$RAND_VALUE
rand_between 2 40; n=$RAND_VALUE
rand_between 0 $((n - 1)); k=$RAND_VALUE
rand_between 1 12; p=$RAND_VALUE
rand_between 2 20; q=$RAND_VALUE
# Exact rational arithmetic, distributivity, and polynomial evaluation.
claim equal "(($a)/$b + ($c)/$d)" "((($a)*$d + ($c)*$b)/($b*$d))"
claim equal "((($a)/$b) * ($b/($a)))" '1'
claim equal "(($a)*(($c)+$e))" "(($a)*($c) + ($a)*$e)"
claim equal "((($a)+($c))^3)" "(($a)^3 + 3*($a)^2*($c) + 3*($a)*($c)^2 + ($c)^3)"
# Finite sums, combinatorics, and integer recurrences.
claim equal "sum(j, j = 1, $n)" "($n*($n+1)/2)"
claim equal "sum(j^2, j = 1, $n)" "($n*($n+1)*(2*$n+1)/6)"
claim equal "factorial($((p + 1)))" "($((p + 1)) * factorial($p))"
claim equal "choose($n, $k) + choose($n, $((k + 1)))" "choose($((n + 1)), $((k + 1)))"
claim equal "fibonacci($((q + 2)))" "fibonacci($((q + 1))) + fibonacci($q)"
# Calculus: compare production differentiation/integration with independent formulas.
claim equal \
"substitute(diff(($a)*x^4 + ($c)*x^3 + ($e)*x^2 + $b*x + $d, x), x = $p)" \
"(4*($a)*$p^3 + 3*($c)*$p^2 + 2*($e)*$p + $b)"
claim equal \
"integrate(($a)*x^4 + ($c)*x^2 + $b*x + $d, x = 0, $p)" \
"(($a)*$p^5/5 + ($c)*$p^3/3 + $b*$p^2/2 + $d*$p)"
# Pythagorean geometry, useful as an exact spatial mechanics primitive.
claim equal "distance(0, 0, $((3 * p)), $((4 * p)))" "$((5 * p))"
# Newtonian work-energy under constant acceleration.
rand_between 1 50; mass=$RAND_VALUE
rand_signed 40; u=$RAND_VALUE
rand_signed 20; acc=$RAND_VALUE
rand_between 1 20; t=$RAND_VALUE
claim equal \
"((1/2)*$mass*((($u)+($acc)*$t)^2-($u)^2))" \
"(($mass*($acc))*(($u)*$t+(1/2)*($acc)*$t^2))"
# Impulse-momentum consistency.
claim equal \
"($mass*(($u)+($acc)*$t)-$mass*($u))" \
"(($mass*($acc))*$t)"
# Uniform-gravity mechanical energy conservation in one dimension.
rand_between 1 30; g=$RAND_VALUE
claim equal \
"((1/2)*$mass*(($u)-$g*$t)^2 + $mass*$g*(($u)*$t-(1/2)*$g*$t^2))" \
"((1/2)*$mass*($u)^2)"
# Hooke/spring work checked through exact integration.
rand_between 1 60; spring_k=$RAND_VALUE
rand_between 1 15; x1=$RAND_VALUE
rand_between 1 15; x2=$RAND_VALUE
claim equal \
"integrate($spring_k*x, x = $x1, $x2)" \
"((1/2)*$spring_k*($x2^2-$x1^2))"
# Inverse-square force scaling (G and masses cancel exactly).
rand_between 1 100; m1=$RAND_VALUE
rand_between 1 100; m2=$RAND_VALUE
rand_between 1 30; r=$RAND_VALUE
claim equal \
"((($m1*$m2)/($r^2))/(($m1*$m2)/((2*$r)^2)))" \
'4'
# Centripetal-force invariant F*r = m*v^2.
rand_between 1 80; speed=$RAND_VALUE
claim equal \
"((($mass*$speed^2)/$r)*$r)" \
"($mass*$speed^2)"
# Wave relation v = f*lambda with exact rational wavelength.
rand_between 1 1000; wave_v=$RAND_VALUE
rand_between 1 200; freq=$RAND_VALUE
claim equal "($freq*($wave_v/$freq))" "$wave_v"
# Thin-lens reciprocal identity with an exact constructed focal length.
rand_between 2 100; obj=$RAND_VALUE
rand_between 2 100; img=$RAND_VALUE
claim equal \
"(1/(($obj*$img)/($obj+$img)))" \
"(1/$obj + 1/$img)"
# Hydrostatic pressure difference.
rand_between 1 2000; rho=$RAND_VALUE
rand_between 1 100; h1=$RAND_VALUE
rand_between "$h1" $((h1 + 100)); h2=$RAND_VALUE
claim equal \
"($rho*$g*($h2-$h1))" \
"($rho*$g*$h2-$rho*$g*$h1)"
# Ohmic power equivalence P = VI = I^2 R.
rand_between 1 100; current=$RAND_VALUE
rand_between 1 100; resistance=$RAND_VALUE
claim equal \
"(($current*$resistance)*$current)" \
"($current^2*$resistance)"
# Rotational work-energy for constant angular acceleration.
rand_between 1 40; inertia=$RAND_VALUE
rand_signed 20; omega=$RAND_VALUE
rand_between 1 20; alpha=$RAND_VALUE
rand_between 1 20; theta=$RAND_VALUE
claim equal \
"(($inertia*$alpha)*$theta)" \
"((1/2)*$inertia*((($omega)^2+2*($alpha)*$theta)-($omega)^2))"
# General 1D elastic collision: independently check momentum and kinetic energy.
rand_between 1 30; cm1=$RAND_VALUE
rand_between 1 30; cm2=$RAND_VALUE
rand_signed 30; cu1=$RAND_VALUE
rand_signed 30; cu2=$RAND_VALUE
v1="((($cm1-$cm2)*($cu1)+2*$cm2*($cu2))/($cm1+$cm2))"
v2="((2*$cm1*($cu1)+($cm2-$cm1)*($cu2))/($cm1+$cm2))"
claim equal \
"($cm1*($cu1)+$cm2*($cu2))" \
"($cm1*$v1+$cm2*$v2)"
claim equal \
"($cm1*($cu1)^2+$cm2*($cu2)^2)" \
"($cm1*($v1)^2+$cm2*($v2)^2)"
# Periodically force very large arbitrary-precision polynomial cancellation.
if [ $((iteration % 16)) -eq 0 ]; then
rand_between 100000 999999; big1=$RAND_VALUE
rand_between 100000 999999; big2=$RAND_VALUE
big="${big1}${big2}${big1}${big2}"
claim equal \
"(($big+1)^3)" \
"($big^3+3*$big^2+3*$big+1)"
fi
iteration=$((iteration + 1))
done
if [ "${CENTL_STRESS_VERBOSE:-0}" = 1 ]; then
echo "centl gauntlet seed=$seed rounds=$rounds claims=$claims" >&2
fi
if "$centl_bin" check "$contracts" >"$log" 2>&1; then
printf '1\n'
else
status=$?
echo "centl gauntlet: FAIL (seed=$seed rounds=$rounds claims=$claims exit=$status)" >&2
echo "replay: CENTL_STRESS_SEED=$seed CENTL_STRESS_ROUNDS=$rounds ./executeme" >&2
cat "$log" >&2
exit "$status"
fi