1// Copyright 2014 The Go Authors.  All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package armasm
6
7import (
8	"encoding/hex"
9	"io/ioutil"
10	"strconv"
11	"strings"
12	"testing"
13)
14
15func TestDecode(t *testing.T) {
16	data, err := ioutil.ReadFile("testdata/decode.txt")
17	if err != nil {
18		t.Fatal(err)
19	}
20	all := string(data)
21	for strings.Contains(all, "\t\t") {
22		all = strings.Replace(all, "\t\t", "\t", -1)
23	}
24	for _, line := range strings.Split(all, "\n") {
25		line = strings.TrimSpace(line)
26		if line == "" || strings.HasPrefix(line, "#") {
27			continue
28		}
29		f := strings.SplitN(line, "\t", 4)
30		i := strings.Index(f[0], "|")
31		if i < 0 {
32			t.Errorf("parsing %q: missing | separator", f[0])
33			continue
34		}
35		if i%2 != 0 {
36			t.Errorf("parsing %q: misaligned | separator", f[0])
37		}
38		size := i / 2
39		code, err := hex.DecodeString(f[0][:i] + f[0][i+1:])
40		if err != nil {
41			t.Errorf("parsing %q: %v", f[0], err)
42			continue
43		}
44		mode, err := strconv.Atoi(f[1])
45		if err != nil {
46			t.Errorf("invalid mode %q in: %s", f[1], line)
47			continue
48		}
49		syntax, asm := f[2], f[3]
50		inst, err := Decode(code, Mode(mode))
51		var out string
52		if err != nil {
53			out = "error: " + err.Error()
54		} else {
55			switch syntax {
56			case "gnu":
57				out = GNUSyntax(inst)
58			case "plan9": // [sic]
59				out = GoSyntax(inst, 0, nil, nil)
60			default:
61				t.Errorf("unknown syntax %q", syntax)
62				continue
63			}
64		}
65		if out != asm || inst.Len != size {
66			t.Errorf("Decode(%s) [%s] = %s, %d, want %s, %d", f[0], syntax, out, inst.Len, asm, size)
67		}
68	}
69}
70