-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcow_test.go
More file actions
48 lines (35 loc) · 1.49 KB
/
cow_test.go
File metadata and controls
48 lines (35 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cow
import (
"reflect"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func TestCOWBuffer(t *testing.T) {
data := []byte{'a', 'b', 'c', 'd'}
buffer := NewCOWBuffer(data)
defer buffer.Close()
copy1 := buffer.Clone()
copy2 := buffer.Clone()
assert.Equal(t, unsafe.SliceData(data), unsafe.SliceData(buffer.data))
assert.Equal(t, unsafe.SliceData(buffer.data), unsafe.SliceData(copy1.data))
assert.Equal(t, unsafe.SliceData(copy1.data), unsafe.SliceData(copy2.data))
assert.True(t, (*byte)(unsafe.SliceData(data)) == unsafe.StringData(buffer.String()))
assert.True(t, (*byte)(unsafe.StringData(buffer.String())) == unsafe.StringData(copy1.String()))
assert.True(t, (*byte)(unsafe.StringData(copy1.String())) == unsafe.StringData(copy2.String()))
assert.True(t, buffer.Update(0, 'g'))
assert.False(t, buffer.Update(-1, 'g'))
assert.False(t, buffer.Update(4, 'g'))
assert.True(t, reflect.DeepEqual([]byte{'g', 'b', 'c', 'd'}, buffer.data))
assert.True(t, reflect.DeepEqual([]byte{'a', 'b', 'c', 'd'}, copy1.data))
assert.True(t, reflect.DeepEqual([]byte{'a', 'b', 'c', 'd'}, copy2.data))
assert.NotEqual(t, unsafe.SliceData(buffer.data), unsafe.SliceData(copy1.data))
assert.Equal(t, unsafe.SliceData(copy1.data), unsafe.SliceData(copy2.data))
copy1.Close()
previous := copy2.data
copy2.Update(0, 'f')
current := copy2.data
// 1 reference - don't need to copy buffer during update
assert.Equal(t, unsafe.SliceData(previous), unsafe.SliceData(current))
copy2.Close()
}