github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/types/sym_test.go (about)

     1  // Copyright 2015 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 types_test
     6  
     7  import (
     8  	"reflect"
     9  	"sort"
    10  	"testing"
    11  
    12  	"github.com/go-asm/go/cmd/compile/types"
    13  )
    14  
    15  func TestSymLess(t *testing.T) {
    16  	var (
    17  		local = types.NewPkg("", "")
    18  		abc   = types.NewPkg("abc", "")
    19  		uvw   = types.NewPkg("uvw", "")
    20  		xyz   = types.NewPkg("xyz", "")
    21  		gr    = types.NewPkg("gr", "")
    22  	)
    23  
    24  	data := []*types.Sym{
    25  		abc.Lookup("b"),
    26  		local.Lookup("B"),
    27  		local.Lookup("C"),
    28  		uvw.Lookup("c"),
    29  		local.Lookup("C"),
    30  		gr.Lookup("φ"),
    31  		local.Lookup("Φ"),
    32  		xyz.Lookup("b"),
    33  		abc.Lookup("a"),
    34  		local.Lookup("B"),
    35  	}
    36  	want := []*types.Sym{
    37  		local.Lookup("B"),
    38  		local.Lookup("B"),
    39  		local.Lookup("C"),
    40  		local.Lookup("C"),
    41  		local.Lookup("Φ"),
    42  		abc.Lookup("a"),
    43  		abc.Lookup("b"),
    44  		xyz.Lookup("b"),
    45  		uvw.Lookup("c"),
    46  		gr.Lookup("φ"),
    47  	}
    48  	if len(data) != len(want) {
    49  		t.Fatal("want and data must match")
    50  	}
    51  	if reflect.DeepEqual(data, want) {
    52  		t.Fatal("data must be shuffled")
    53  	}
    54  	sort.Slice(data, func(i, j int) bool { return data[i].Less(data[j]) })
    55  	if !reflect.DeepEqual(data, want) {
    56  		t.Logf("want: %#v", want)
    57  		t.Logf("data: %#v", data)
    58  		t.Errorf("sorting failed")
    59  	}
    60  }