github.com/jaypipes/ghw@v0.21.1/pkg/pci/address/address_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package address_test
     8  
     9  import (
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  
    14  	pciaddr "github.com/jaypipes/ghw/pkg/pci/address"
    15  )
    16  
    17  func TestPCIAddressFromString(t *testing.T) {
    18  
    19  	tests := []struct {
    20  		addrStr  string
    21  		expected *pciaddr.Address
    22  		// AddressFromString is more flexible than String() and wants
    23  		// to accept addresses not in full canonical form, as long as
    24  		// it can do the right thing - e.g. a sane default Domain exists.
    25  		// Thus we need to sometimes skip the Address -> string check.
    26  		skipStringTest bool
    27  	}{
    28  		{
    29  			addrStr: "00:00.0",
    30  			expected: &pciaddr.Address{
    31  				Domain:   "0000",
    32  				Bus:      "00",
    33  				Device:   "00",
    34  				Function: "0",
    35  			},
    36  			skipStringTest: true,
    37  		},
    38  		{
    39  			addrStr: "0000:00:00.0",
    40  			expected: &pciaddr.Address{
    41  				Domain:   "0000",
    42  				Bus:      "00",
    43  				Device:   "00",
    44  				Function: "0",
    45  			},
    46  		},
    47  		{
    48  			addrStr: "0000:03:00.0",
    49  			expected: &pciaddr.Address{
    50  				Domain:   "0000",
    51  				Bus:      "03",
    52  				Device:   "00",
    53  				Function: "0",
    54  			},
    55  		},
    56  		{
    57  			addrStr: "0000:03:00.A",
    58  			expected: &pciaddr.Address{
    59  				Domain:   "0000",
    60  				Bus:      "03",
    61  				Device:   "00",
    62  				Function: "a",
    63  			},
    64  		},
    65  		{
    66  			// PCI-X / PCI Express extensions may use 5-digit domain
    67  			addrStr: "10000:03:00.A",
    68  			expected: &pciaddr.Address{
    69  				Domain:   "10000",
    70  				Bus:      "03",
    71  				Device:   "00",
    72  				Function: "a",
    73  			},
    74  		},
    75  	}
    76  	for x, test := range tests {
    77  		got := pciaddr.FromString(test.addrStr)
    78  		if !reflect.DeepEqual(got, test.expected) {
    79  			t.Fatalf("Test #%d failed. Expected %v but got %v", x, test.expected, got)
    80  		}
    81  
    82  		if test.skipStringTest {
    83  			continue
    84  		}
    85  
    86  		addrStr := got.String()
    87  		// addresses are case insensitive
    88  		if !strings.EqualFold(addrStr, test.addrStr) {
    89  			t.Fatalf("Test #%d failed. Expected %q but got %q (case insensitive match)", x, test.addrStr, addrStr)
    90  		}
    91  	}
    92  }