Debian Codegraph @

s390x/s390xasm/decode_test.go

raw symbols SCIP
1 // Copyright 2024 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
5 package s390xasm
6
7 import (
8 "encoding/hex"
9 "io/ioutil"
10 "path"
11 "strings"
12 "testing"
13 )
14
15 func TestDecode(t *testing.T) {
16 files, err := ioutil.ReadDir("testdata")
17 if err != nil {
18 t.Fatal(err)
19 }
20 for _, f := range files {
21 if !strings.HasPrefix(f.Name(), "decode") {
22 continue
23 }
24 filename := path.Join("testdata", f.Name())
25 data, err := ioutil.ReadFile(filename)
26 if err != nil {
27 t.Fatal(err)
28 }
29 decode(data, t, filename)
30 }
31 }
32
33 func decode(data []byte, t *testing.T, filename string) {
34 all := string(data)
35 // Simulate PC based on number of instructions found in the test file.
36 pc := uint64(0)
37 for strings.Contains(all, "\t\t") {
38 all = strings.Replace(all, "\t\t", "\t", -1)
39 }
40 for _, line := range strings.Split(all, "\n") {
41 line = strings.TrimSpace(line)
42 if line == "" || strings.HasPrefix(line, "#") {
43 continue
44 }
45 f := strings.SplitN(line, "\t", 3)
46 i := strings.Index(f[0], "|")
47 if i < 0 {
48 t.Errorf("%s: parsing %q: missing | separator", filename, f[0])
49 continue
50 }
51 if i%2 != 0 {
52 t.Errorf("%s: parsing %q: misaligned | separator", filename, f[0])
53 }
54 size := i / 2
55 code, err := hex.DecodeString(f[0][:i] + f[0][i+1:])
56 if err != nil {
57 t.Errorf("%s: parsing %q: %v", filename, f[0], err)
58 continue
59 }
60 syntax, asm := f[1], f[2]
61 inst, err := Decode(code)
62 var out string
63 if err != nil {
64 out = "error: " + err.Error()
65 } else {
66 switch syntax {
67 case "gnu":
68 out = GNUSyntax(inst, pc)
69 case "plan9":
70 out = GoSyntax(inst, pc, nil)
71 default:
72 t.Errorf("unknown syntax %q", syntax)
73 continue
74 }
75 }
76 pc += uint64(size)
77 if out != asm || inst.Len != size {
78 t.Errorf("%s: Decode(%s) [%s] = %s want %s", filename, f[0], syntax, out, asm)
79 }
80 }
81 }