github.com/searKing/golang/go@v1.2.117/exp/slices/map_test.go (about)

     1  // Copyright 2022 The searKing Author. 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 slices_test
     6  
     7  import (
     8  	"fmt"
     9  	"slices"
    10  	"strconv"
    11  	"testing"
    12  
    13  	slices_ "github.com/searKing/golang/go/exp/slices"
    14  )
    15  
    16  func TestMap(t *testing.T) {
    17  	tests := []struct {
    18  		data []int
    19  		want []string
    20  	}{
    21  		{nil, nil},
    22  		{[]int{}, []string{}},
    23  		{[]int{0}, []string{"0"}},
    24  		{[]int{1, 0}, []string{"1", "0"}},
    25  		{[]int{1, 2}, []string{"1", "2"}},
    26  		{[]int{0, 1, 2}, []string{"0", "1", "2"}},
    27  		{[]int{0, 1, 0, 2}, []string{"0", "1", "0", "2"}},
    28  	}
    29  	for _, tt := range tests {
    30  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    31  			{
    32  				got := slices_.Map(tt.data)
    33  
    34  				if slices.Compare(got, tt.want) != 0 {
    35  					t.Errorf("slices_.Map(%v) = %v, want %v", tt.data, got, tt.want)
    36  				}
    37  			}
    38  		})
    39  	}
    40  }
    41  
    42  func TestMapFunc(t *testing.T) {
    43  	tests := []struct {
    44  		data []int
    45  		want []string
    46  	}{
    47  		{nil, nil},
    48  		{[]int{}, []string{}},
    49  		{[]int{0}, []string{"0"}},
    50  		{[]int{1, 0}, []string{"1", "0"}},
    51  		{[]int{1, 2}, []string{"1", "2"}},
    52  		{[]int{0, 1, 2}, []string{"0", "1", "2"}},
    53  		{[]int{0, 1, 0, 2}, []string{"0", "1", "0", "2"}},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    57  			{
    58  				got := slices_.MapFunc(tt.data, func(e int) string {
    59  					return strconv.Itoa(e)
    60  				})
    61  
    62  				if slices.Compare(got, tt.want) != 0 {
    63  					t.Errorf("slices_.MapFunc(%v, func(e int) string {return strconv.Itoa(e)}) = %v, want %v", tt.data, got, tt.want)
    64  				}
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestMapIndexFunc(t *testing.T) {
    71  	tests := []struct {
    72  		data []int
    73  		want []string
    74  	}{
    75  		{nil, nil},
    76  		{[]int{}, []string{}},
    77  		{[]int{0}, []string{"0:0"}},
    78  		{[]int{1, 0}, []string{"0:1", "1:0"}},
    79  		{[]int{1, 2}, []string{"0:1", "1:2"}},
    80  		{[]int{0, 1, 2}, []string{"0:0", "1:1", "2:2"}},
    81  		{[]int{0, 1, 0, 2}, []string{"0:0", "1:1", "2:0", "3:2"}},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    85  			{
    86  				got := slices_.MapIndexFunc(tt.data, func(i int, e int) string {
    87  					return fmt.Sprintf("%d:%d", i, e)
    88  				})
    89  
    90  				if slices.Compare(got, tt.want) != 0 {
    91  					t.Errorf("slices_.MapIndexFunc(%v, func(i int, e int) string {return fmt.Sprintf(\"%%d:%%d\", i, e)}) = %v, want %v", tt.data, got, tt.want)
    92  				}
    93  			}
    94  		})
    95  	}
    96  }