|
| 1 | +package acc |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/mmcloughlin/addchain/acc/eval" |
| 7 | + "github.com/mmcloughlin/addchain/acc/pass" |
| 8 | + "github.com/mmcloughlin/addchain/alg/ensemble" |
| 9 | + "github.com/mmcloughlin/addchain/alg/exec" |
| 10 | + "github.com/mmcloughlin/addchain/internal/bigint" |
| 11 | + "github.com/mmcloughlin/addchain/internal/results" |
| 12 | + "github.com/mmcloughlin/addchain/internal/test" |
| 13 | +) |
| 14 | + |
| 15 | +// TestResults is an integration test designed to confirm we can produce correct |
| 16 | +// code for all the target exponents in the results list. Specifically, for |
| 17 | +// every target exponent and every algorithm in the ensemble, we confirm the |
| 18 | +// resulting chain can be converted to an addition chain program, allocated |
| 19 | +// variables, and interpreted to give the expected result. |
| 20 | +func TestResults(t *testing.T) { |
| 21 | + timer := test.Start() |
| 22 | + as := ensemble.Ensemble() |
| 23 | + for _, c := range results.Results { |
| 24 | + c := c // scopelint |
| 25 | + t.Run(c.Slug, func(t *testing.T) { |
| 26 | + timer.Check(t) |
| 27 | + |
| 28 | + // Execute. |
| 29 | + ex := exec.NewParallel() |
| 30 | + rs := ex.Execute(c.Target(), as) |
| 31 | + |
| 32 | + // Check each result. |
| 33 | + for _, r := range rs { |
| 34 | + if r.Err != nil { |
| 35 | + t.Fatalf("error with %s: %v", r.Algorithm, r.Err) |
| 36 | + } |
| 37 | + |
| 38 | + // Decompile into IR. |
| 39 | + p, err := Decompile(r.Program) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + // Allocate. |
| 45 | + a := pass.Allocator{ |
| 46 | + Input: "x", |
| 47 | + Output: "z", |
| 48 | + Format: "t%d", |
| 49 | + } |
| 50 | + if err := a.Execute(p); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + // Evaluate. |
| 55 | + i := eval.NewInterpreter() |
| 56 | + x := bigint.One() |
| 57 | + i.Store(a.Input, x) |
| 58 | + i.Store(a.Output, x) |
| 59 | + |
| 60 | + if err := i.Execute(p); err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + // Verify output. |
| 65 | + output, ok := i.Load(a.Output) |
| 66 | + if !ok { |
| 67 | + t.Fatalf("missing output variable %q", a.Output) |
| 68 | + } |
| 69 | + |
| 70 | + expect := r.Chain.End() |
| 71 | + |
| 72 | + if !bigint.Equal(output, expect) { |
| 73 | + t.FailNow() |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments