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
|
package acc
import (
"reflect"
"testing"
"github.com/mmcloughlin/addchain"
"github.com/mmcloughlin/addchain/acc/ir"
"github.com/mmcloughlin/addchain/acc/pass"
"github.com/mmcloughlin/addchain/internal/assert"
)
func TestDecompileExample(t *testing.T) {
p := addchain.Program{}
_, err := p.Double(0)
assert.NoError(t, err)
_, err = p.Add(0, 1)
assert.NoError(t, err)
_, err = p.Shift(1, 3)
assert.NoError(t, err)
_, err = p.Add(0, 5)
assert.NoError(t, err)
t.Log(p)
expect := &ir.Program{
Instructions: []*ir.Instruction{
{
Output: ir.Index(1),
Op: ir.Double{
X: ir.Index(0),
},
},
{
Output: ir.Index(2),
Op: ir.Add{
X: ir.Index(0),
Y: ir.Index(1),
},
},
{
Output: ir.Index(5),
Op: ir.Shift{
X: ir.Index(1),
S: 3,
},
},
{
Output: ir.Index(6),
Op: ir.Add{
X: ir.Index(0),
Y: ir.Index(5),
},
},
},
}
got, err := Decompile(p)
if err != nil {
t.Fatal()
}
t.Logf("got:\n%s", got)
if !reflect.DeepEqual(got, expect) {
t.Logf("expect:\n%s", expect)
t.Fatal("mismatch")
}
}
func TestDecompileRandom(t *testing.T) {
CheckRandom(t, func(t *testing.T, p addchain.Program) {
r, err := Decompile(p)
if err != nil {
t.Fatal(err)
}
if err := pass.Validate(r); err != nil {
t.Fatal(err)
}
})
}
|