gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/state/tests/array_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 allArrayPrimitives = []any{ 23 [1]bool{}, 24 [1]bool{true}, 25 [2]bool{false, true}, 26 [1]int{}, 27 [1]int{1}, 28 [2]int{0, 1}, 29 [1]int8{}, 30 [1]int8{1}, 31 [2]int8{0, 1}, 32 [1]int16{}, 33 [1]int16{1}, 34 [2]int16{0, 1}, 35 [1]int32{}, 36 [1]int32{1}, 37 [2]int32{0, 1}, 38 [1]int64{}, 39 [1]int64{1}, 40 [2]int64{0, 1}, 41 [1]uint{}, 42 [1]uint{1}, 43 [2]uint{0, 1}, 44 [1]uintptr{}, 45 [1]uintptr{1}, 46 [2]uintptr{0, 1}, 47 [1]uint8{}, 48 [1]uint8{1}, 49 [2]uint8{0, 1}, 50 [1]uint16{}, 51 [1]uint16{1}, 52 [2]uint16{0, 1}, 53 [1]uint32{}, 54 [1]uint32{1}, 55 [2]uint32{0, 1}, 56 [1]uint64{}, 57 [1]uint64{1}, 58 [2]uint64{0, 1}, 59 [1]string{}, 60 [1]string{""}, 61 [1]string{nonEmptyString}, 62 [2]string{"", nonEmptyString}, 63 } 64 65 func TestArrayPrimitives(t *testing.T) { 66 runTestCases(t, false, "plain", flatten(allArrayPrimitives)) 67 runTestCases(t, false, "pointers", pointersTo(flatten(allArrayPrimitives))) 68 runTestCases(t, false, "interfaces", interfacesTo(flatten(allArrayPrimitives))) 69 runTestCases(t, false, "interfacesToPointers", interfacesTo(pointersTo(flatten(allArrayPrimitives)))) 70 } 71 72 func TestSlices(t *testing.T) { 73 var allSlices = flatten( 74 filter(allArrayPrimitives, func(o any) (any, bool) { 75 v := reflect.New(reflect.TypeOf(o)).Elem() 76 v.Set(reflect.ValueOf(o)) 77 return v.Slice(0, v.Len()).Interface(), true 78 }), 79 filter(allArrayPrimitives, func(o any) (any, bool) { 80 v := reflect.New(reflect.TypeOf(o)).Elem() 81 v.Set(reflect.ValueOf(o)) 82 if v.Len() == 0 { 83 // Return the pure "nil" value for the slice. 84 return reflect.New(v.Slice(0, 0).Type()).Elem().Interface(), true 85 } 86 return v.Slice(1, v.Len()).Interface(), true 87 }), 88 filter(allArrayPrimitives, func(o any) (any, bool) { 89 v := reflect.New(reflect.TypeOf(o)).Elem() 90 v.Set(reflect.ValueOf(o)) 91 if v.Len() == 0 { 92 // Return the zero-valued slice. 93 return reflect.MakeSlice(v.Slice(0, 0).Type(), 0, 0).Interface(), true 94 } 95 return v.Slice(0, v.Len()-1).Interface(), true 96 }), 97 ) 98 runTestCases(t, false, "plain", allSlices) 99 runTestCases(t, false, "pointers", pointersTo(allSlices)) 100 runTestCases(t, false, "interfaces", interfacesTo(allSlices)) 101 runTestCases(t, false, "interfacesToPointers", interfacesTo(pointersTo(allSlices))) 102 } 103 104 func TestArrayContainers(t *testing.T) { 105 var ( 106 emptyArray [1]any 107 fullArray [1]any 108 ) 109 fullArray[0] = &emptyArray 110 runTestCases(t, false, "", []any{ 111 arrayContainer{v: emptyArray}, 112 arrayContainer{v: fullArray}, 113 arrayPtrContainer{v: nil}, 114 arrayPtrContainer{v: &emptyArray}, 115 arrayPtrContainer{v: &fullArray}, 116 }) 117 } 118 119 func TestSliceContainers(t *testing.T) { 120 var ( 121 nilSlice []any 122 emptySlice = make([]any, 0) 123 fullSlice = []any{nil} 124 ) 125 runTestCases(t, false, "", []any{ 126 sliceContainer{v: nilSlice}, 127 sliceContainer{v: emptySlice}, 128 sliceContainer{v: fullSlice}, 129 slicePtrContainer{v: nil}, 130 slicePtrContainer{v: &nilSlice}, 131 slicePtrContainer{v: &emptySlice}, 132 slicePtrContainer{v: &fullSlice}, 133 }) 134 }