github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/internal/obj/x86/asm_test.go (about)

     1  // Copyright 2018 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 x86
     6  
     7  import (
     8  	"cmd/internal/obj"
     9  	"testing"
    10  )
    11  
    12  func init() {
    13  	// Required for tests that access any of
    14  	// opindex/ycover/reg/regrex global tables.
    15  	var ctxt obj.Link
    16  	instinit(&ctxt)
    17  }
    18  
    19  func TestRegisterListEncDec(t *testing.T) {
    20  	tests := []struct {
    21  		printed string
    22  		reg0    int16
    23  		reg1    int16
    24  	}{
    25  		{"[R10-R13]", REG_R10, REG_R13},
    26  		{"[X0-AX]", REG_X0, REG_AX},
    27  
    28  		{"[X0-X3]", REG_X0, REG_X3},
    29  		{"[X21-X24]", REG_X21, REG_X24},
    30  
    31  		{"[Y0-Y3]", REG_Y0, REG_Y3},
    32  		{"[Y21-Y24]", REG_Y21, REG_Y24},
    33  
    34  		{"[Z0-Z3]", REG_Z0, REG_Z3},
    35  		{"[Z21-Z24]", REG_Z21, REG_Z24},
    36  	}
    37  
    38  	for _, test := range tests {
    39  		enc := EncodeRegisterRange(test.reg0, test.reg1)
    40  		reg0, reg1 := decodeRegisterRange(enc)
    41  
    42  		if int16(reg0) != test.reg0 {
    43  			t.Errorf("%s reg0 mismatch: have %d, want %d",
    44  				test.printed, reg0, test.reg0)
    45  		}
    46  		if int16(reg1) != test.reg1 {
    47  			t.Errorf("%s reg1 mismatch: have %d, want %d",
    48  				test.printed, reg1, test.reg1)
    49  		}
    50  		wantPrinted := test.printed
    51  		if rlconv(enc) != wantPrinted {
    52  			t.Errorf("%s string mismatch: have %s, want %s",
    53  				test.printed, rlconv(enc), wantPrinted)
    54  		}
    55  	}
    56  }
    57  
    58  func TestRegIndex(t *testing.T) {
    59  	tests := []struct {
    60  		regFrom int
    61  		regTo   int
    62  	}{
    63  		{REG_AL, REG_R15B},
    64  		{REG_AX, REG_R15},
    65  		{REG_M0, REG_M7},
    66  		{REG_K0, REG_K7},
    67  		{REG_X0, REG_X31},
    68  		{REG_Y0, REG_Y31},
    69  		{REG_Z0, REG_Z31},
    70  	}
    71  
    72  	for _, test := range tests {
    73  		for index, reg := 0, test.regFrom; reg <= test.regTo; index, reg = index+1, reg+1 {
    74  			have := regIndex(int16(reg))
    75  			want := index
    76  			if have != want {
    77  				regName := rconv(int(reg))
    78  				t.Errorf("regIndex(%s):\nhave: %d\nwant: %d",
    79  					regName, have, want)
    80  			}
    81  		}
    82  	}
    83  }