|
| 1 | +(* Self-contained test for testing mathicscript <file> |
| 2 | +
|
| 3 | + Recursive GCD from https://mathematica.stackexchange.com/questions/156990/gcd-using-euclidean-algorithm |
| 4 | + Test harness is the same used in Gries Schnieder testing. |
| 5 | + *) |
| 6 | + |
| 7 | +ClearAll[expect, totalRight, totalWrong, totalTests]; |
| 8 | +SetAttributes[ expect, HoldAllComplete ]; |
| 9 | +totalRight = totalWrong = totalTests = 0; |
| 10 | +expect[expected_, actual_] := (* <~~~ Here's the API *) |
| 11 | + Module[{evalActualOnce = actual, |
| 12 | + evalExpectedOnce = expected}, |
| 13 | + totalTests += 1; |
| 14 | + Print[ {"Test[" <> ToString[totalTests] <> "]:=\n", |
| 15 | + HoldForm[actual], |
| 16 | + "\nexpected", HoldForm[expected], |
| 17 | + "\neval'd expected", evalExpectedOnce, |
| 18 | + "\neval'd actual ", evalActualOnce, |
| 19 | + "\nright?", evalExpectedOnce === evalActualOnce} ]; |
| 20 | + Print[ "" ]; (* newline *) |
| 21 | + If[ evalExpectedOnce === evalActualOnce, |
| 22 | + totalRight += 1, |
| 23 | + totalWrong += 1 ]; |
| 24 | + {"total right", totalRight, "total wrong", totalWrong} |
| 25 | + ]; |
| 26 | + |
| 27 | +RecursiveGCD[a_, 0] := a; |
| 28 | +RecursiveGCD[a_, b_] := RecursiveGCD[b, Mod[a, b]]; |
| 29 | + |
| 30 | +expect[6, RecursiveGCD[24, 18]] |
| 31 | +expect[1, RecursiveGCD[3, 5]] |
| 32 | + |
| 33 | +x = RandomInteger[{-100, 100}] |
| 34 | +expect[x, RecursiveGCD[x, 0]] |
| 35 | +expect[x, RecursiveGCD[2 x, x]] |
| 36 | +expect[x, RecursiveGCD[x 2, x]] |
| 37 | +(** uncomment to see a failure **) |
| 38 | +(*** expect[3 x, RecursiveGCD[x 2, x]] ***) |
| 39 | +Print["Total right: ", totalRight, ". Total wrong: ", totalWrong, ". Total tests: ", totalTests] |
| 40 | +If[ Or[totalTests <= 0, totalTests != totalRight], Quit[1], Quit[0]] |
0 commit comments