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 ppc64asm
6
7import (
8	"encoding/binary"
9	"encoding/hex"
10	"io/ioutil"
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", 3)
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		syntax, asm := f[1], f[2]
45		inst, err := Decode(code, binary.BigEndian)
46		var out string
47		if err != nil {
48			out = "error: " + err.Error()
49		} else {
50			switch syntax {
51			case "gnu":
52				out = GNUSyntax(inst)
53			//case "plan9":
54			//	out = GoSyntax(inst, 0, nil, nil)
55			default:
56				t.Errorf("unknown syntax %q", syntax)
57				continue
58			}
59		}
60		if out != asm || inst.Len != size {
61			t.Errorf("Decode(%s) [%s] = %s want %s", f[0], syntax, out, asm)
62		}
63	}
64}
65