v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/slice/map_test.go (about)

     1  package slice
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"testing"
     7  )
     8  
     9  func TestMap(t *testing.T) {
    10  	type args struct {
    11  		p  []uint64
    12  		fn func(v uint64, i int) string
    13  	}
    14  	tests := []struct {
    15  		name string
    16  		args args
    17  		want []string
    18  	}{
    19  		{
    20  			name: "itoa",
    21  			args: args{
    22  				p: []uint64{1, 2, 3, 4, 5},
    23  				fn: func(v uint64, i int) string {
    24  					return strconv.FormatUint(v, 10)
    25  				},
    26  			},
    27  			want: []string{"1", "2", "3", "4", "5"},
    28  		},
    29  
    30  		{
    31  			name: "+1",
    32  			args: args{
    33  				p: []uint64{1, 2, 3, 4, 5},
    34  				fn: func(v uint64, i int) string {
    35  					return strconv.FormatUint(v+1, 10)
    36  				},
    37  			},
    38  			want: []string{"2", "3", "4", "5", "6"},
    39  		},
    40  	}
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			if got := Map(tt.args.p, tt.args.fn); !reflect.DeepEqual(got, tt.want) {
    44  				t.Errorf("Map() = %v, want %v", got, tt.want)
    45  			}
    46  		})
    47  	}
    48  }