github.com/uber-go/tally/v4@v4.1.17/key_gen_test.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package tally
    22  
    23  import (
    24  	"math/rand"
    25  	"sort"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestKeyForPrefixedStringMaps(t *testing.T) {
    32  	tests := []struct {
    33  		desc   string
    34  		prefix string
    35  		maps   []map[string]string
    36  		want   string
    37  	}{
    38  		{
    39  			desc:   "no maps",
    40  			prefix: "foo",
    41  			want:   "foo+",
    42  		},
    43  		{
    44  			desc:   "disjoint maps",
    45  			prefix: "foo",
    46  			maps: []map[string]string{
    47  				{
    48  					"a": "foo",
    49  					"b": "bar",
    50  				},
    51  				{
    52  					"c": "baz",
    53  					"d": "qux",
    54  				},
    55  			},
    56  			want: "foo+a=foo,b=bar,c=baz,d=qux",
    57  		},
    58  		{
    59  			desc:   "map overlap",
    60  			prefix: "foo",
    61  			maps: []map[string]string{
    62  				{
    63  					"a": "1",
    64  					"b": "1",
    65  					"c": "1",
    66  					"d": "1",
    67  				},
    68  				{"b": "2"},
    69  				{"c": "3"},
    70  				{"d": "4"},
    71  			},
    72  			want: "foo+a=1,b=2,c=3,d=4",
    73  		},
    74  	}
    75  
    76  	for _, tt := range tests {
    77  		t.Run(tt.desc, func(t *testing.T) {
    78  			got := keyForPrefixedStringMaps(tt.prefix, tt.maps...)
    79  			assert.Equal(t, tt.want, got)
    80  		})
    81  	}
    82  }
    83  
    84  func TestInsertionSort(t *testing.T) {
    85  	chars := []byte("abcdefghijklmnopqrstuvwxyz")
    86  	n := len(chars)
    87  	var expected []string
    88  	var actual []string
    89  	for i := 0; i < 12; i++ {
    90  		s := rand.Intn(20) + 5
    91  		var key []byte
    92  		for j := 0; j < s; j++ {
    93  			key = append(key, chars[rand.Intn(n)])
    94  		}
    95  		expected = append(expected, string(key))
    96  		actual = append(actual, string(key))
    97  	}
    98  	sort.Strings(expected)
    99  	insertionSort(actual)
   100  	assert.Equal(t, expected, actual)
   101  }