1
2
3
4
5 package x86csv
6
7 import (
8 "reflect"
9 "strings"
10 "testing"
11 )
12
13
14
15
16
17 func TestReader(t *testing.T) {
18 input := `# x86.csv v0.2
19 "ADDSUBPD xmm1, xmm2/m128","ADDSUBPD xmm2/m128, xmm1","addsubpd xmm2/m128, xmm1","66 0F D0 /r","V","V","SSE3","","rw,r","",""
20 "VPEXTRQ r/m64, xmm1, imm8","VPEXTRQ imm8, xmm1, r/m64","vpextrq imm8, xmm1, r/m64","VEX.128.66.0F3A.W1 16 /r ib","I","V","AVX","","w,r,r","",""
21 "XOR r8, r/m8","XORB r/m8, r8","xorb r/m8, r8","REX 32 /r","N.E.","V","","pseudo64","rw,r","Y","8"
22 `
23 want := []Inst{
24 {
25 Intel: "ADDSUBPD xmm1, xmm2/m128",
26 Go: "ADDSUBPD xmm2/m128, xmm1",
27 GNU: "addsubpd xmm2/m128, xmm1",
28 Encoding: "66 0F D0 /r",
29 Mode32: "V",
30 Mode64: "V",
31 CPUID: "SSE3",
32 Action: "rw,r",
33 },
34 {
35 Intel: "VPEXTRQ r/m64, xmm1, imm8",
36 Go: "VPEXTRQ imm8, xmm1, r/m64",
37 GNU: "vpextrq imm8, xmm1, r/m64",
38 Encoding: "VEX.128.66.0F3A.W1 16 /r ib",
39 Mode32: "I",
40 Mode64: "V",
41 CPUID: "AVX",
42 Action: "w,r,r",
43 },
44 {
45 Intel: "XOR r8, r/m8",
46 Go: "XORB r/m8, r8",
47 GNU: "xorb r/m8, r8",
48 Encoding: "REX 32 /r",
49 Mode32: "N.E.",
50 Mode64: "V",
51 Tags: "pseudo64",
52 Action: "rw,r",
53 Multisize: "Y",
54 DataSize: "8",
55 },
56 }
57
58 r := NewReader(strings.NewReader(input))
59 inst, err := r.Read()
60 if err != nil {
61 t.Fatalf("Read(): %v", err)
62 }
63 restInsts, err := r.ReadAll()
64 if err != nil {
65 t.Fatalf("ReadAll(): %v", err)
66 }
67 if remainder, err := r.ReadAll(); remainder != nil || err != nil {
68 t.Errorf("ReadAll() on exhausted r failed")
69 }
70 have := append([]*Inst{inst}, restInsts...)
71
72 if len(want) != len(have) {
73 t.Fatalf("len(have) is %d, want %d\n", len(have), len(want))
74 }
75 lines := strings.Split(input, "\n")
76 lines = lines[1:]
77 for i := range want {
78 if want[i] != *have[i] {
79 t.Errorf("%s:\nhave: %v\nwant: %v", lines[i], have[i], want[i])
80 }
81 }
82 }
83
84 func TestSyntaxSplit(t *testing.T) {
85 tests := []struct {
86 syntax string
87 opcode string
88 args []string
89 }{
90 {"RET", "RET", nil},
91 {"CALLW* r/m16", "CALLW*", []string{"r/m16"}},
92 {"JMP_FAR m16:16", "JMP_FAR", []string{"m16:16"}},
93 {"movl CR0-CR7, rmr32", "movl", []string{"CR0-CR7", "rmr32"}},
94 {"VFMSUBADD132PD xmm1, xmmV, xmm2/m128", "VFMSUBADD132PD", []string{"xmm1", "xmmV", "xmm2/m128"}},
95 }
96
97 for _, tt := range tests {
98 op, args := instOpcode(tt.syntax), instArgs(tt.syntax)
99 if op != tt.opcode {
100 t.Errorf("%s: opcode mismatch (have `%s`, want `%s`)",
101 tt.syntax, op, tt.opcode)
102 }
103 if !reflect.DeepEqual(args, tt.args) {
104 t.Errorf("%s: args mismatch (have %v, want %s)",
105 tt.syntax, args, tt.args)
106 }
107 }
108 }
109
View as plain text