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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
package storage
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestCopy(t *testing.T) {
// A longer than B
a := headerFromSlice([]int{0, 1, 2, 3, 4})
b := headerFromSlice([]int{10, 11})
copied := Copy(reflect.TypeOf(1), &a, &b)
assert.Equal(t, 2, copied)
assert.Equal(t, []int{10, 11, 2, 3, 4}, a.Ints())
// B longer than A
a = headerFromSlice([]int{10, 11})
b = headerFromSlice([]int{0, 1, 2, 3, 4})
copied = Copy(reflect.TypeOf(1), &a, &b)
assert.Equal(t, 2, copied)
assert.Equal(t, []int{0, 1}, a.Ints())
// A is empty
a = headerFromSlice([]int{})
b = headerFromSlice([]int{0, 1, 2, 3, 4})
copied = Copy(reflect.TypeOf(1), &a, &b)
assert.Equal(t, 0, copied)
// B is empty
a = headerFromSlice([]int{0, 1, 2, 3, 4})
b = headerFromSlice([]int{})
copied = Copy(reflect.TypeOf(1), &a, &b)
assert.Equal(t, 0, copied)
assert.Equal(t, []int{0, 1, 2, 3, 4}, a.Ints())
}
func TestFill(t *testing.T) {
// A longer than B
a := headerFromSlice([]int{0, 1, 2, 3, 4})
b := headerFromSlice([]int{10, 11})
copied := Fill(reflect.TypeOf(1), &a, &b)
assert.Equal(t, 5, copied)
assert.Equal(t, []int{10, 11, 10, 11, 10}, a.Ints())
// B longer than A
a = headerFromSlice([]int{10, 11})
b = headerFromSlice([]int{0, 1, 2, 3, 4})
copied = Fill(reflect.TypeOf(1), &a, &b)
assert.Equal(t, 2, copied)
assert.Equal(t, []int{0, 1}, a.Ints())
}
func headerFromSlice(x interface{}) Header {
xT := reflect.TypeOf(x)
if xT.Kind() != reflect.Slice {
panic("Expected a slice")
}
xV := reflect.ValueOf(x)
size := uintptr(xV.Len()) * xT.Elem().Size()
return Header{
Raw: FromMemory(xV.Pointer(), size),
}
}
|