| 1 |
/* |
| 2 |
Copyright 2018 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 output_tests |
| 18 |
|
| 19 |
import ( |
| 20 |
"github.com/google/gofuzz" |
| 21 |
|
| 22 |
"k8s.io/gengo/examples/deepcopy-gen/output_tests/aliases" |
| 23 |
"k8s.io/gengo/examples/deepcopy-gen/output_tests/interfaces" |
| 24 |
) |
| 25 |
|
| 26 |
// interfaceFuzzers contains fuzzer that set all interface to nil because our |
| 27 |
// JSON deepcopy does not work with it. |
| 28 |
// TODO: test also interface deepcopy |
| 29 |
var interfaceFuzzers = []interface{}{ |
| 30 |
func(s *aliases.AliasAliasInterface, c fuzz.Continue) { |
| 31 |
if c.RandBool() { |
| 32 |
*s = nil |
| 33 |
} else { |
| 34 |
*s = &aliasAliasInterfaceInstance{X: c.Int()} |
| 35 |
} |
| 36 |
}, |
| 37 |
func(s *aliases.AliasInterface, c fuzz.Continue) { |
| 38 |
if c.RandBool() { |
| 39 |
*s = nil |
| 40 |
} else { |
| 41 |
*s = &aliasAliasInterfaceInstance{X: c.Int()} |
| 42 |
} |
| 43 |
}, |
| 44 |
func(s *aliases.Interface, c fuzz.Continue) { |
| 45 |
if c.RandBool() { |
| 46 |
*s = nil |
| 47 |
} else { |
| 48 |
*s = &aliasAliasInterfaceInstance{X: c.Int()} |
| 49 |
} |
| 50 |
}, |
| 51 |
func(s *aliases.AliasInterfaceMap, c fuzz.Continue) { |
| 52 |
if c.RandBool() { |
| 53 |
*s = nil |
| 54 |
} else { |
| 55 |
*s = make(aliases.AliasInterfaceMap) |
| 56 |
for i := 0; i < c.Intn(3); i++ { |
| 57 |
if c.RandBool() { |
| 58 |
(*s)[c.RandString()] = nil |
| 59 |
} else { |
| 60 |
(*s)[c.RandString()] = &aliasAliasInterfaceInstance{X: c.Int()} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
}, |
| 66 |
func(s *aliases.AliasInterfaceSlice, c fuzz.Continue) { |
| 67 |
if c.RandBool() { |
| 68 |
*s = nil |
| 69 |
} else { |
| 70 |
*s = make(aliases.AliasInterfaceSlice, 0, 0) |
| 71 |
for i := 0; i < c.Intn(3); i++ { |
| 72 |
if c.RandBool() { |
| 73 |
*s = append(*s, nil) |
| 74 |
} else { |
| 75 |
*s = append(*s, &aliasAliasInterfaceInstance{X: c.Int()}) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
}, |
| 80 |
func(s *interfaces.Inner, c fuzz.Continue) { |
| 81 |
if c.RandBool() { |
| 82 |
*s = nil |
| 83 |
} else { |
| 84 |
*s = &interfacesInnerInstance{X: c.Float64()} |
| 85 |
} |
| 86 |
}, |
| 87 |
} |
| 88 |
|
| 89 |
type aliasAliasInterfaceInstance struct { |
| 90 |
X int |
| 91 |
} |
| 92 |
|
| 93 |
func (i *aliasAliasInterfaceInstance) DeepCopyInterface() aliases.Interface { |
| 94 |
if i == nil { |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
return &aliasAliasInterfaceInstance{X: i.X} |
| 99 |
} |
| 100 |
|
| 101 |
func (i *aliasAliasInterfaceInstance) DeepCopyAliasInterface() aliases.AliasInterface { |
| 102 |
if i == nil { |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
return &aliasAliasInterfaceInstance{X: i.X} |
| 107 |
} |
| 108 |
|
| 109 |
func (i *aliasAliasInterfaceInstance) DeepCopyAliasAliasInterface() aliases.AliasAliasInterface { |
| 110 |
if i == nil { |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
return &aliasAliasInterfaceInstance{X: i.X} |
| 115 |
} |
| 116 |
|
| 117 |
type interfacesInnerInstance struct { |
| 118 |
X float64 |
| 119 |
} |
| 120 |
|
| 121 |
func (i *interfacesInnerInstance) DeepCopyInner() interfaces.Inner { |
| 122 |
if i == nil { |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
return &interfacesInnerInstance{X: i.X} |
| 127 |
} |
| 128 |
|
| 129 |
func (i *interfacesInnerInstance) Function() float64 { |
| 130 |
return i.X |
| 131 |
} |