deep_copy_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package conversion
  14. import (
  15. "math/rand"
  16. "reflect"
  17. "testing"
  18. "github.com/google/gofuzz"
  19. )
  20. func TestDeepCopy(t *testing.T) {
  21. semantic := EqualitiesOrDie()
  22. f := fuzz.New().NilChance(.5).NumElements(0, 100)
  23. table := []interface{}{
  24. map[string]string{},
  25. int(5),
  26. "hello world",
  27. struct {
  28. A, B, C struct {
  29. D map[string]int
  30. }
  31. X []int
  32. Y []byte
  33. }{},
  34. }
  35. for _, obj := range table {
  36. obj2, err := NewCloner().DeepCopy(obj)
  37. if err != nil {
  38. t.Errorf("Error: couldn't copy %#v", obj)
  39. continue
  40. }
  41. if e, a := obj, obj2; !semantic.DeepEqual(e, a) {
  42. t.Errorf("expected %#v\ngot %#v", e, a)
  43. }
  44. obj3 := reflect.New(reflect.TypeOf(obj)).Interface()
  45. f.Fuzz(obj3)
  46. obj4, err := NewCloner().DeepCopy(obj3)
  47. if err != nil {
  48. t.Errorf("Error: couldn't copy %#v", obj)
  49. continue
  50. }
  51. if e, a := obj3, obj4; !semantic.DeepEqual(e, a) {
  52. t.Errorf("expected %#v\ngot %#v", e, a)
  53. }
  54. f.Fuzz(obj3)
  55. }
  56. }
  57. func copyOrDie(t *testing.T, in interface{}) interface{} {
  58. out, err := NewCloner().DeepCopy(in)
  59. if err != nil {
  60. t.Fatalf("DeepCopy failed: %#q: %v", in, err)
  61. }
  62. return out
  63. }
  64. func TestDeepCopySliceSeparate(t *testing.T) {
  65. x := []int{5}
  66. y := copyOrDie(t, x).([]int)
  67. x[0] = 3
  68. if y[0] == 3 {
  69. t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
  70. }
  71. }
  72. func TestDeepCopyArraySeparate(t *testing.T) {
  73. x := [1]int{5}
  74. y := copyOrDie(t, x).([1]int)
  75. x[0] = 3
  76. if y[0] == 3 {
  77. t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
  78. }
  79. }
  80. func TestDeepCopyMapSeparate(t *testing.T) {
  81. x := map[string]int{"foo": 5}
  82. y := copyOrDie(t, x).(map[string]int)
  83. x["foo"] = 3
  84. if y["foo"] == 3 {
  85. t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
  86. }
  87. }
  88. func TestDeepCopyPointerSeparate(t *testing.T) {
  89. z := 5
  90. x := &z
  91. y := copyOrDie(t, x).(*int)
  92. *x = 3
  93. if *y == 3 {
  94. t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
  95. }
  96. }
  97. func TestDeepCopyStruct(t *testing.T) {
  98. type Foo struct {
  99. A int
  100. }
  101. type Bar struct {
  102. Foo
  103. F *Foo
  104. }
  105. a := &Bar{Foo{1}, &Foo{2}}
  106. b := copyOrDie(t, a).(*Bar)
  107. a.A = 3
  108. a.F.A = 4
  109. if b.A != 1 || b.F.A != 2 {
  110. t.Errorf("deep copy wasn't deep: %#v, %#v", a, b)
  111. }
  112. }
  113. var result interface{}
  114. func BenchmarkDeepCopy(b *testing.B) {
  115. table := []interface{}{
  116. map[string]string{},
  117. int(5),
  118. "hello world",
  119. struct {
  120. A, B, C struct {
  121. D map[string]int
  122. }
  123. X []int
  124. Y []byte
  125. }{},
  126. }
  127. f := fuzz.New().RandSource(rand.NewSource(1)).NilChance(.5).NumElements(0, 100)
  128. for i := range table {
  129. out := table[i]
  130. obj := reflect.New(reflect.TypeOf(out)).Interface()
  131. f.Fuzz(obj)
  132. table[i] = obj
  133. }
  134. b.ResetTimer()
  135. var r interface{}
  136. for i := 0; i < b.N; i++ {
  137. for j := range table {
  138. r, _ = NewCloner().DeepCopy(table[j])
  139. }
  140. }
  141. result = r
  142. }