LEFT | RIGHT |
(no file at all) | |
| 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 |
| 5 package x86asm |
| 6 |
| 7 import ( |
| 8 "encoding/hex" |
| 9 "io/ioutil" |
| 10 "strings" |
| 11 "testing" |
| 12 ) |
| 13 |
| 14 func TestDecode(t *testing.T) { |
| 15 data, err := ioutil.ReadFile("testdata/decode.txt") |
| 16 if err != nil { |
| 17 t.Fatal(err) |
| 18 } |
| 19 all := string(data) |
| 20 for strings.Contains(all, "\t\t") { |
| 21 all = strings.Replace(all, "\t\t", "\t", -1) |
| 22 } |
| 23 for _, line := range strings.Split(all, "\n") { |
| 24 line = strings.TrimSpace(line) |
| 25 if line == "" || strings.HasPrefix(line, "#") { |
| 26 continue |
| 27 } |
| 28 f := strings.SplitN(line, "\t", 3) |
| 29 i := strings.Index(f[0], "|") |
| 30 if i < 0 { |
| 31 t.Errorf("parsing %q: missing | separator", f[0]) |
| 32 continue |
| 33 } |
| 34 if i%2 != 0 { |
| 35 t.Errorf("parsing %q: misaligned | separator", f[0]) |
| 36 } |
| 37 size := i / 2 |
| 38 code, err := hex.DecodeString(f[0][:i] + f[0][i+1:]) |
| 39 if err != nil { |
| 40 t.Errorf("parsing %q: %v", f[0], err) |
| 41 continue |
| 42 } |
| 43 syntax, asm := f[1], f[2] |
| 44 inst, instSize, err := Decode(code, 32) |
| 45 var out string |
| 46 if err != nil { |
| 47 out = "error: " + err.Error() |
| 48 } else { |
| 49 switch syntax { |
| 50 case "gnu": |
| 51 out = GNUSyntax(inst) |
| 52 case "intel": |
| 53 out = IntelSyntax(inst) |
| 54 default: |
| 55 t.Errorf("unknown syntax %q", syntax) |
| 56 continue |
| 57 } |
| 58 } |
| 59 if out != asm || instSize != size { |
| 60 t.Errorf("Decode(%s) [%s] = %s, %d, want %s, %d", f[0],
syntax, out, instSize, asm, size) |
| 61 } |
| 62 } |
| 63 } |
LEFT | RIGHT |