gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/util/slice/slice_test.go (about)

     1  package slice_test
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"gitlab.com/picnic-app/backend/role-api/internal/util/slice"
     9  )
    10  
    11  func ExampleBatch() {
    12  	s := []int{1, 2, 3, 4, 5}
    13  	_ = slice.Batch(s, 2, func(batch []int) error {
    14  		fmt.Println(batch)
    15  		return nil
    16  	})
    17  
    18  	// Output:
    19  	// [1 2]
    20  	// [3 4]
    21  	// [5]
    22  }
    23  
    24  func ExampleFilter() {
    25  	s := []int{1, 2, 3, 4, 5}
    26  	s = slice.Filter(s, func(v int) bool { return v&1 == 1 })
    27  	fmt.Println(s)
    28  
    29  	// Output:
    30  	// [1 3 5]
    31  }
    32  
    33  func ExampleConvert() {
    34  	ints := []int{1, 2, 3}
    35  	strings := slice.Convert(strconv.Itoa, ints...)
    36  	fmt.Println(strings)
    37  
    38  	// Output:
    39  	// [1 2 3]
    40  }
    41  
    42  func ExampleConvertPointers() {
    43  	ints := []int{1, 2, 3}
    44  	strings := slice.ConvertPointers(strconv.Itoa, ints...)
    45  
    46  	for _, p := range strings {
    47  		fmt.Println(*p)
    48  	}
    49  
    50  	// Output:
    51  	// 1
    52  	// 2
    53  	// 3
    54  }
    55  
    56  func ExampleReorder() {
    57  	keys := []string{"1", "2", "3"}
    58  	vals := []int{3, 2, 3, 2, 0}
    59  	s := slice.Reorder(keys, vals, strconv.Itoa)
    60  	fmt.Println(s)
    61  
    62  	// Output:
    63  	// [2 2 3 3]
    64  }
    65  
    66  func BenchmarkReorder_Unique(b *testing.B) {
    67  	for i := 0; i < b.N; i++ {
    68  		slice.Reorder(
    69  			[]string{"1", "2", "3", "4"},
    70  			[]int{3, 2, 1, 4},
    71  			strconv.Itoa,
    72  		)
    73  	}
    74  }
    75  
    76  func ExampleUnique() {
    77  	s := slice.Unique([]int{3, 2, 3, 2, 0})
    78  	fmt.Println(s)
    79  
    80  	// Output:
    81  	// [3 2 0]
    82  }