golang.org/x/arch@v0.17.0/loong64/loong64asm/decode_test.go (about)

     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 loong64asm
     6  
     7  import (
     8  	"encoding/hex"
     9  	"io/ioutil"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func testDecode(t *testing.T, syntax string) {
    16  	input := filepath.Join("testdata", syntax+"cases.txt")
    17  	data, err := ioutil.ReadFile(input)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	all := string(data)
    22  	for strings.Contains(all, "\t\t") {
    23  		all = strings.Replace(all, "\t\t", "\t", -1)
    24  	}
    25  	for _, line := range strings.Split(all, "\n") {
    26  		line = strings.TrimSpace(line)
    27  		if line == "" || strings.HasPrefix(line, "#") {
    28  			continue
    29  		}
    30  		f := strings.SplitN(line, "\t", 2)
    31  		i := strings.Index(f[0], "|")
    32  		if i < 0 {
    33  			t.Errorf("parsing %q: missing | separator", f[0])
    34  			continue
    35  		}
    36  		if i%2 != 0 {
    37  			t.Errorf("parsing %q: misaligned | separator", f[0])
    38  		}
    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  		asm := f[1]
    45  		inst, decodeErr := Decode(code)
    46  		if decodeErr != nil && decodeErr != errUnknown {
    47  			// Some rarely used system instructions are not supported
    48  			// Following logicals will filter such unknown instructions
    49  			t.Errorf("parsing %x: %s", code, decodeErr)
    50  			continue
    51  		}
    52  		var out string
    53  		switch syntax {
    54  		case "gnu":
    55  			out = GNUSyntax(inst)
    56  		case "plan9":
    57  			out = GoSyntax(inst, 0, nil)
    58  		default:
    59  			t.Errorf("unknown syntax %q", syntax)
    60  			continue
    61  		}
    62  
    63  		// var out string
    64  		if asm != out || len(asm) != len(out) {
    65  			t.Errorf("Decode(%s) [%s] = %s want %s", f[0], syntax, out, asm)
    66  		}
    67  	}
    68  }
    69  
    70  func TestDecodeGNUSyntax(t *testing.T) {
    71  	testDecode(t, "gnu")
    72  }
    73  
    74  func TestDecodeGoSyntax(t *testing.T) {
    75  	testDecode(t, "plan9")
    76  }