sigs.k8s.io/kueue@v0.6.2/pkg/util/maps/maps_test.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes 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 maps
    18  
    19  import (
    20  	"maps"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestToRefMap(t *testing.T) {
    27  	cases := map[string]struct {
    28  		orig       map[int]int
    29  		updatesMap map[int]int
    30  	}{
    31  		"preserve nil": {
    32  			orig: nil,
    33  		},
    34  		"preserve empty": {
    35  			orig: map[int]int{},
    36  		},
    37  		"slice": {
    38  			orig: map[int]int{
    39  				1: 0xa,
    40  				2: 0xb,
    41  				3: 0xc,
    42  			},
    43  			updatesMap: map[int]int{
    44  				1: 0xd,
    45  			},
    46  		},
    47  	}
    48  	for name, tc := range cases {
    49  		t.Run(name, func(t *testing.T) {
    50  			result := maps.Clone(tc.orig)
    51  			if diff := cmp.Diff(tc.orig, result); diff != "" {
    52  				t.Errorf("Unexpected result (-want,+got):\n%s", diff)
    53  			}
    54  
    55  			if len(tc.updatesMap) > 0 {
    56  				for k, v := range tc.updatesMap {
    57  					result[k] = v
    58  				}
    59  				if diff := cmp.Diff(tc.orig, result); diff == "" {
    60  					t.Errorf("changing the result should not alter the original")
    61  				}
    62  			}
    63  		})
    64  
    65  	}
    66  }
    67  
    68  func TestContains(t *testing.T) {
    69  	cases := map[string]struct {
    70  		a          map[string]int
    71  		b          map[string]int
    72  		wantResult bool
    73  	}{
    74  		"nil a": {
    75  			a: nil,
    76  			b: map[string]int{
    77  				"v1": 1,
    78  			},
    79  			wantResult: false,
    80  		},
    81  		"nil b": {
    82  			a: map[string]int{
    83  				"v1": 1,
    84  			},
    85  			b:          nil,
    86  			wantResult: true,
    87  		},
    88  		"extra in b": {
    89  			a: map[string]int{
    90  				"v1": 1,
    91  			},
    92  			b: map[string]int{
    93  				"v1": 1,
    94  				"v2": 2,
    95  			},
    96  			wantResult: false,
    97  		},
    98  		"extra in a": {
    99  			a: map[string]int{
   100  				"v1": 1,
   101  				"v2": 2,
   102  			},
   103  			b: map[string]int{
   104  				"v1": 1,
   105  			},
   106  			wantResult: true,
   107  		},
   108  		"missmatch": {
   109  			a: map[string]int{
   110  				"v1": 1,
   111  				"v2": 3,
   112  			},
   113  			b: map[string]int{
   114  				"v1": 1,
   115  				"v2": 2,
   116  			},
   117  			wantResult: false,
   118  		},
   119  	}
   120  
   121  	for name, tc := range cases {
   122  		t.Run(name, func(t *testing.T) {
   123  			got := Contains(tc.a, tc.b)
   124  			if got != tc.wantResult {
   125  				t.Errorf("Unexpected result, expecting %v", tc.wantResult)
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestMergeIntersect(t *testing.T) {
   132  	cases := map[string]struct {
   133  		a             map[string]int
   134  		b             map[string]int
   135  		wantMerge     map[string]int
   136  		wantIntersect map[string]int
   137  	}{
   138  		"nil a": {
   139  			a: nil,
   140  			b: map[string]int{
   141  				"v1": 1,
   142  			},
   143  			wantMerge: map[string]int{
   144  				"v1": 1,
   145  			},
   146  			wantIntersect: nil,
   147  		},
   148  		"nil b": {
   149  			a: map[string]int{
   150  				"v1": 1,
   151  			},
   152  			b: nil,
   153  			wantMerge: map[string]int{
   154  				"v1": 1,
   155  			},
   156  			wantIntersect: nil,
   157  		},
   158  		"extra in b": {
   159  			a: map[string]int{
   160  				"v1": 1,
   161  			},
   162  			b: map[string]int{
   163  				"v1": 1,
   164  				"v2": 2,
   165  			},
   166  			wantMerge: map[string]int{
   167  				"v1": 2,
   168  				"v2": 2,
   169  			},
   170  			wantIntersect: map[string]int{
   171  				"v1": 2,
   172  			},
   173  		},
   174  		"extra in a": {
   175  			a: map[string]int{
   176  				"v1": 1,
   177  				"v2": 2,
   178  			},
   179  			b: map[string]int{
   180  				"v1": 1,
   181  			},
   182  			wantMerge: map[string]int{
   183  				"v1": 2,
   184  				"v2": 2,
   185  			},
   186  			wantIntersect: map[string]int{
   187  				"v1": 2,
   188  			},
   189  		},
   190  	}
   191  
   192  	for name, tc := range cases {
   193  		t.Run(name, func(t *testing.T) {
   194  			gotMerge := Merge(tc.a, tc.b, func(a, b int) int { return a + b })
   195  			if diff := cmp.Diff(tc.wantMerge, gotMerge); diff != "" {
   196  				t.Errorf("Unexpected Merge result(-want/+got): %s", diff)
   197  			}
   198  
   199  			gotIntersect := Intersect(tc.a, tc.b, func(a, b int) int { return a + b })
   200  			if diff := cmp.Diff(tc.wantIntersect, gotIntersect); diff != "" {
   201  				t.Errorf("Unexpected Intersect result(-want/+got): %s", diff)
   202  			}
   203  		})
   204  	}
   205  }