github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/test/associate_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/isyscore/isc-gobase/isc"
     7  )
     8  
     9  type AssociateStruct struct {
    10  	Key  string
    11  	Name string
    12  	Age  int
    13  }
    14  
    15  func initList() []AssociateStruct {
    16  	return []AssociateStruct{
    17  		{"K", "库陈胜", 20},
    18  		{"K", "库陈胜", 30},
    19  		{"K1", "库陈胜", 30},
    20  		{"K1", "库陈胜", 40},
    21  		{"K1", "库陈胜", 50},
    22  		{"K2", "库陈胜", 60},
    23  		{"K2", "库陈胜", 70},
    24  		{"K2", "库陈胜", 80},
    25  		{"K4", "库陈胜", 90},
    26  	}
    27  }
    28  
    29  var transformFun = func(a AssociateStruct) isc.Pair[string, AssociateStruct] {
    30  	return isc.NewPair(a.Key, a)
    31  }
    32  
    33  var transformFun1 = func(a AssociateStruct) int {
    34  	return a.Age
    35  }
    36  
    37  var keySelector = func(a AssociateStruct) string {
    38  	return a.Key
    39  }
    40  
    41  func TestAssociate(t *testing.T) {
    42  	list := initList()
    43  	l := isc.Associate(list, transformFun)
    44  	t.Logf("%v", l)
    45  }
    46  
    47  func TestAssociateTo(t *testing.T) {
    48  	list := initList()
    49  	m := map[string]AssociateStruct{}
    50  	r := isc.AssociateTo(list, &m, transformFun)
    51  	t.Logf("%v", r)
    52  }
    53  
    54  func TestAssociateBy(t *testing.T) {
    55  	list := initList()
    56  	r := isc.AssociateBy(list, keySelector)
    57  	t.Logf("%v", r)
    58  }
    59  
    60  func TestAssociateByAndValue(t *testing.T) {
    61  	list := initList()
    62  	r := isc.AssociateByAndValue(list, keySelector, transformFun)
    63  	t.Logf("%v", r)
    64  }
    65  
    66  func TestAssociateByAndValueTo(t *testing.T) {
    67  	list := initList()
    68  	m := make(map[string]int)
    69  	isc.AssociateByAndValueTo(list, &m, keySelector, transformFun1)
    70  	t.Logf("%v", m)
    71  }
    72  
    73  //
    74  func TestAssociateByTo(t *testing.T) {
    75  	list := initList()
    76  	m := make(map[string]AssociateStruct)
    77  	isc.AssociateByTo(list, &m, keySelector)
    78  	t.Logf("%v", m)
    79  }
    80  
    81  //
    82  
    83  //
    84  func TestAssociateWith(t *testing.T) {
    85  	list := initList()
    86  	m := isc.AssociateWith(list, transformFun1)
    87  	t.Logf("%v", m)
    88  }
    89  
    90  //
    91  func TestAssociateWithTo(t *testing.T) {
    92  	list := initList()
    93  	m := make(map[AssociateStruct]int)
    94  	isc.AssociateWithTo(list, &m, transformFun1)
    95  	t.Logf("%v", m)
    96  }