gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/state/tests/map_test.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tests 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 var allMapPrimitives = []any{ 23 bool(true), 24 int(1), 25 int8(1), 26 int16(1), 27 int32(1), 28 int64(1), 29 uint(1), 30 uintptr(1), 31 uint8(1), 32 uint16(1), 33 uint32(1), 34 uint64(1), 35 string(""), 36 registeredMapStruct{}, 37 } 38 39 var allMapKeys = flatten(allMapPrimitives, pointersTo(allMapPrimitives)) 40 41 var allMapValues = flatten(allMapPrimitives, pointersTo(allMapPrimitives), interfacesTo(allMapPrimitives)) 42 43 var emptyMaps = combine(allMapKeys, allMapValues, func(v1, v2 any) any { 44 m := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(v1), reflect.TypeOf(v2))) 45 return m.Interface() 46 }) 47 48 var fullMaps = combine(allMapKeys, allMapValues, func(v1, v2 any) any { 49 m := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(v1), reflect.TypeOf(v2))) 50 m.SetMapIndex(reflect.Zero(reflect.TypeOf(v1)), reflect.Zero(reflect.TypeOf(v2))) 51 return m.Interface() 52 }) 53 54 func TestMapAliasing(t *testing.T) { 55 v := make(map[int]int) 56 ptrToV := &v 57 aliases := []map[int]int{v, v} 58 runTestCases(t, false, "", []any{ptrToV, aliases}) 59 } 60 61 func TestMapsEmpty(t *testing.T) { 62 runTestCases(t, false, "plain", emptyMaps) 63 runTestCases(t, false, "pointers", pointersTo(emptyMaps)) 64 runTestCases(t, false, "interfaces", interfacesTo(emptyMaps)) 65 runTestCases(t, false, "interfacesToPointers", interfacesTo(pointersTo(emptyMaps))) 66 } 67 68 func TestMapsFull(t *testing.T) { 69 runTestCases(t, false, "plain", fullMaps) 70 runTestCases(t, false, "pointers", pointersTo(fullMaps)) 71 runTestCases(t, false, "interfaces", interfacesTo(fullMaps)) 72 runTestCases(t, false, "interfacesToPointer", interfacesTo(pointersTo(fullMaps))) 73 } 74 75 func TestMapContainers(t *testing.T) { 76 var ( 77 nilMap map[int]any 78 emptyMap = make(map[int]any) 79 fullMap = map[int]any{0: nil} 80 ) 81 runTestCases(t, false, "", []any{ 82 mapContainer{v: nilMap}, 83 mapContainer{v: emptyMap}, 84 mapContainer{v: fullMap}, 85 mapPtrContainer{v: nil}, 86 mapPtrContainer{v: &nilMap}, 87 mapPtrContainer{v: &emptyMap}, 88 mapPtrContainer{v: &fullMap}, 89 }) 90 }