go.temporal.io/server@v1.23.0/common/util/util_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package util
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestRepeatSlice(t *testing.T) {
    34  	t.Run("when input slice is nil should return nil", func(t *testing.T) {
    35  		got := RepeatSlice[int](nil, 5)
    36  		require.Nil(t, got, "RepeatSlice produced non-nil slice from nil input")
    37  	})
    38  	t.Run("when input slice is empty should return empty", func(t *testing.T) {
    39  		empty := []int{}
    40  		got := RepeatSlice(empty, 5)
    41  		require.Len(t, got, 0, "RepeatSlice filled empty slice")
    42  	})
    43  	t.Run("when requested repeat number equal 0 should return empty slice", func(t *testing.T) {
    44  		xs := []int{1, 2, 3, 4, 5}
    45  		got := RepeatSlice(xs, 0)
    46  		require.Len(t, got, 0, "RepeatSlice with repeat count 0 returned non-empty slice")
    47  	})
    48  	t.Run("when requested repeat number is less than 0 should return empty slice", func(t *testing.T) {
    49  		xs := []int{1, 2, 3, 4, 5}
    50  		got := RepeatSlice(xs, -1)
    51  		require.Len(t, got, 0, "RepeatSlice with repeat count -1 returned non-empty slice")
    52  	})
    53  	t.Run("when requested repeat number is 3 should return slice three times the input", func(t *testing.T) {
    54  		xs := []int{1, 2, 3, 4, 5}
    55  		got := RepeatSlice(xs, 3)
    56  		require.Len(t, got, len(xs)*3, "RepeatSlice produced slice of wrong length: expected %d got %d", len(xs)*3, len(got))
    57  		for i, v := range got {
    58  			require.Equal(t, xs[i%len(xs)], v, "RepeatSlice wrong value in result: expected %d at index %d but got %d", xs[i%len(xs)], i, v)
    59  		}
    60  	})
    61  	t.Run("should not change the input slice when truncating", func(t *testing.T) {
    62  		xs := []int{1, 2, 3, 4, 5}
    63  		_ = RepeatSlice(xs, 0)
    64  		require.Len(t, xs, 5, "Repeat slice truncated the original slice: expected {1, 2, 3, 4, 5}, got %v", xs)
    65  	})
    66  	t.Run("should not change the input slice when replicating", func(t *testing.T) {
    67  		xs := []int{1, 2, 3, 4, 5}
    68  		_ = RepeatSlice(xs, 5)
    69  		require.Len(t, xs, 5, "Repeat slice changed the original slice: expected {1, 2, 3, 4, 5}, got %v", xs)
    70  	})
    71  }
    72  
    73  func TestMapSlice(t *testing.T) {
    74  	t.Run("when given nil as slice should return nil", func(t *testing.T) {
    75  		ys := MapSlice(nil, func(x int) uint32 { return uint32(x) })
    76  		require.Nil(t, ys, "mapping over nil produced non nil got %v", ys)
    77  	})
    78  	t.Run("when given an empty slice should return empty slice", func(t *testing.T) {
    79  		xs := []int{}
    80  		var ys []uint32
    81  		ys = MapSlice(xs, func(x int) uint32 { return uint32(x) })
    82  		require.Len(t, ys, 0, "mapping over empty slice produced non empty slice got %v", ys)
    83  	})
    84  	t.Run("when given a slice and a function should apply function to every element of the original slice", func(t *testing.T) {
    85  		xs := []int{1, 2, 3, 4, 5}
    86  		ys := MapSlice(xs, func(x int) int { return x + 1 })
    87  		for i, y := range ys {
    88  			require.Equal(t, xs[i]+1, y, "mapping over slice did not apply function expected {2, 3, 4, 5} got %v", ys)
    89  		}
    90  	})
    91  }