-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
There are cases we cannot deepcopy #28
Comments
Here is a live demo on the Go playground with a simplified test case: type |
`
` |
The author treats the array behavior wrongly, which relies on its deep copy entirely. Any reference type array will be affected by this case. Here are more test cases. func TestArrayOfSomething(t *testing.T) {
verifyCases := map[string]struct {
modifyAndVerify func(t *testing.T)
}{
// failed because the map(underlying is a pointer) will be copied directly
"array of map": {
modifyAndVerify: func(t *testing.T) {
origin := [1]map[string]int{
{
"1": 1,
},
}
copied := deepcopy.Copy(&origin)
assert.NotSame(t, origin, copied)
assert.NotSame(t, origin[0], copied.(*[1]map[string]int)[0])
origin[0]["1"] = 999
assert.Equal(t, 1, copied.(*[1]map[string]int)[0]["1"])
},
},
// failed because the pointer of map will be copied directly
"array of *map": {
modifyAndVerify: func(t *testing.T) {
origin := [1]*map[string]int{
{
"1": 1,
},
}
copied := deepcopy.Copy(&origin)
assert.NotSame(t, origin, copied)
assert.NotSame(t, origin[0], copied.(*[1]*map[string]int)[0])
(*origin[0])["1"] = 999
assert.Equal(t, 1, (*copied.(*[1]*map[string]int)[0])["1"])
},
},
// failed because the pointer of int will be copied directly
"array of *int": {
modifyAndVerify: func(t *testing.T) {
intp := func(i int) *int {
return &i
}
arrayOfInt := [3]*int{intp(1), intp(2)}
copied := deepcopy.Copy(&arrayOfInt)
assert.NotSame(t, &arrayOfInt, copied)
assert.NotSame(t, arrayOfInt[0], copied.(*[3]*int)[0])
arrayOfInt[0] = intp(999)
assert.Equal(t, 1, *copied.(*[3]*int)[0])
},
},
// succeed because int will be copied by value
"array of int": {
modifyAndVerify: func(t *testing.T) {
arrayOfInt := [3]int{1, 2}
copied := deepcopy.Copy(&arrayOfInt)
assert.NotSame(t, &arrayOfInt, copied)
assert.NotSame(t, &arrayOfInt[0], copied.(*[3]int)[0])
arrayOfInt[0] = 999
assert.Equal(t, 1, copied.(*[3]int)[0])
},
},
}
for key, tt := range verifyCases {
t.Run(key, tt.modifyAndVerify)
}
} |
Hello @xieyuschen . |
@xieyuschen, it's been a while. |
@HarutakaMatsumoto restored:) |
@xieyuschen |
Hello!
Somehow Copy function cannot deepcopy an array of list of list.
A example test is following.
Please make it can deepcopy.
Thank you.
The text was updated successfully, but these errors were encountered: