@@ -15,6 +15,7 @@ import (
15
15
"testing"
16
16
"unicode"
17
17
"unicode/utf8"
18
+ "unsafe"
18
19
)
19
20
20
21
func eq (a , b []string ) bool {
@@ -2118,3 +2119,35 @@ func BenchmarkIndexPeriodic(b *testing.B) {
2118
2119
})
2119
2120
}
2120
2121
}
2122
+
2123
+ func TestClone (t * testing.T ) {
2124
+ var cloneTests = [][]byte {
2125
+ []byte (nil ),
2126
+ []byte {},
2127
+ Clone ([]byte {}),
2128
+ []byte (strings .Repeat ("a" , 42 ))[:0 ],
2129
+ []byte (strings .Repeat ("a" , 42 ))[:0 :0 ],
2130
+ []byte ("short" ),
2131
+ []byte (strings .Repeat ("a" , 42 )),
2132
+ }
2133
+ for _ , input := range cloneTests {
2134
+ clone := Clone (input )
2135
+ if ! Equal (clone , input ) {
2136
+ t .Errorf ("Clone(%q) = %q; want %q" , input , clone , input )
2137
+ }
2138
+
2139
+ if input == nil && clone != nil {
2140
+ t .Errorf ("Clone(%#v) return value should be equal to nil slice." , input )
2141
+ }
2142
+
2143
+ if input != nil && clone == nil {
2144
+ t .Errorf ("Clone(%#v) return value should not be equal to nil slice." , input )
2145
+ }
2146
+
2147
+ inputHeader := (* reflect .SliceHeader )(unsafe .Pointer (& input ))
2148
+ cloneHeader := (* reflect .SliceHeader )(unsafe .Pointer (& clone ))
2149
+ if cap (input ) != 0 && cloneHeader .Data == inputHeader .Data {
2150
+ t .Errorf ("Clone(%q) return value should not reference inputs backing memory." , input )
2151
+ }
2152
+ }
2153
+ }
0 commit comments