github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/general/common_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package general
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  )
    25  
    26  func TestMax(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	as := require.New(t)
    30  	as.Equal(2, Max(1, 2))
    31  }
    32  
    33  func TestMaxUInt64(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	as := require.New(t)
    37  	as.Equal(uint64(2), MaxUInt64(1, 2))
    38  }
    39  
    40  func TestMinUInt64(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	as := require.New(t)
    44  	as.Equal(uint64(2), MaxUInt64(1, 2))
    45  }
    46  
    47  func TestMaxInt64(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	as := require.New(t)
    51  	as.Equal(int64(2), MaxInt64(1, 2))
    52  }
    53  
    54  func TestGetValueWithDefault(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	as := require.New(t)
    58  	as.Equal("5", GetValueWithDefault(map[string]string{"2": "2"}, "1", "5"))
    59  	as.Equal("2", GetValueWithDefault(map[string]string{"1": "2"}, "1", "5"))
    60  }
    61  
    62  func TestIsNameEnabled(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	as := require.New(t)
    66  	as.Equal(true, IsNameEnabled("test", sets.NewString(), []string{"test"}))
    67  	as.Equal(true, IsNameEnabled("test", sets.NewString(), []string{"*"}))
    68  	as.Equal(false, IsNameEnabled("test", sets.NewString(), []string{}))
    69  }
    70  
    71  func TestParseUint64PointerToString(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	as := require.New(t)
    75  	var a uint64 = 5
    76  	as.Equal(ParseUint64PointerToString(&a), "5")
    77  }
    78  
    79  func TestParseStringToUint64Pointer(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	as := require.New(t)
    83  	p, err := ParseStringToUint64Pointer("5")
    84  	as.Nil(err)
    85  	as.Equal(uint64(5), *p)
    86  }
    87  
    88  func TestGetInt64PointerFromUint64Pointer(t *testing.T) {
    89  	t.Parallel()
    90  
    91  	as := require.New(t)
    92  	var a uint64 = 5
    93  	p, err := GetInt64PointerFromUint64Pointer(&a)
    94  	as.Nil(err)
    95  	as.Equal(int64(5), *p)
    96  }
    97  
    98  func TestGetStringValueFromMap(t *testing.T) {
    99  	t.Parallel()
   100  
   101  	as := require.New(t)
   102  	as.Equal("a", GetStringValueFromMap(map[string]string{"labelA": "a"}, "labelA"))
   103  	as.Equal("", GetStringValueFromMap(map[string]string{"labelB": "a"}, "labelA"))
   104  }
   105  
   106  func TestGenerateHash(t *testing.T) {
   107  	t.Parallel()
   108  
   109  	as := require.New(t)
   110  	as.Greater(len(GenerateHash([]byte{60, 60}, 5)), 0)
   111  }
   112  
   113  func TestCheckMapEqual(t *testing.T) {
   114  	t.Parallel()
   115  
   116  	as := require.New(t)
   117  	as.Equal(true, CheckMapEqual(map[string]string{"labelA": "a"}, map[string]string{"labelA": "a"}))
   118  	as.Equal(false, CheckMapEqual(map[string]string{"labelB": "a"}, map[string]string{"labelA": "a"}))
   119  }
   120  
   121  func TestUIntPointerToFloat64(t *testing.T) {
   122  	t.Parallel()
   123  
   124  	as := require.New(t)
   125  	var a uint = 5
   126  	as.Equal(5.0, UIntPointerToFloat64(&a))
   127  }
   128  
   129  func TestUInt64PointerToFloat64(t *testing.T) {
   130  	t.Parallel()
   131  
   132  	as := require.New(t)
   133  	var a uint64 = 5
   134  	as.Equal(5.0, UInt64PointerToFloat64(&a))
   135  }
   136  
   137  func TestJsonPathEmpty(t *testing.T) {
   138  	t.Parallel()
   139  
   140  	as := require.New(t)
   141  	as.Equal(true, JsonPathEmpty([]byte("{}")))
   142  	as.Equal(true, JsonPathEmpty([]byte("")))
   143  }
   144  
   145  func TestFormatMemoryQutantity(t *testing.T) {
   146  	t.Parallel()
   147  	as := require.New(t)
   148  	as.Equal("1024[1Ki]", FormatMemoryQuantity(1<<10))
   149  	as.Equal("1.048576e+06[1Mi]", FormatMemoryQuantity(1<<20))
   150  	as.Equal("1.073741824e+09[1Gi]", FormatMemoryQuantity(1<<30))
   151  }