fixed-length arrays cleanup

This commit is contained in:
Jae Kwon
2015-07-18 17:19:47 -07:00
parent dc7b912881
commit 8d1e176180
6 changed files with 39 additions and 52 deletions

View File

@ -16,40 +16,6 @@ type SimpleStruct struct {
Time time.Time
}
type SimpleArray [5]byte
func TestSimpleArray(t *testing.T) {
var foo SimpleArray
// Type of pointer to array
rt := reflect.TypeOf(&foo)
fmt.Printf("rt: %v\n", rt)
// Type of array itself.
// NOTE: normally this is acquired through other means
// like introspecting on method signatures, or struct fields.
rte := rt.Elem()
fmt.Printf("rte: %v\n", rte)
// Get a new pointer to the array
// NOTE: calling .Interface() is to get the actual value,
// instead of reflection values.
ptr := reflect.New(rte).Interface()
fmt.Printf("ptr: %v", ptr)
// Make a simple int aray
fooArray := SimpleArray([5]byte{1, 10, 50, 100, 200})
fooBytes := BinaryBytes(fooArray)
fooReader := bytes.NewReader(fooBytes)
// Now you can read it.
n, err := new(int64), new(error)
it := ReadBinary(foo, fooReader, n, err)
fmt.Println(it, reflect.TypeOf(it))
}
//-------------------------------------
type Animal interface{}
const (
@ -502,3 +468,40 @@ func TestBadAlloc(t *testing.T) {
res := ReadBinary(instance, b, n, err)
fmt.Println(res, *err)
}
//------------------------------------------------------------------------------
type SimpleArray [5]byte
func TestSimpleArray(t *testing.T) {
var foo SimpleArray
// Type of pointer to array
rt := reflect.TypeOf(&foo)
fmt.Printf("rt: %v\n", rt) // *binary.SimpleArray
// Type of array itself.
// NOTE: normally this is acquired through other means
// like introspecting on method signatures, or struct fields.
rte := rt.Elem()
fmt.Printf("rte: %v\n", rte) // binary.SimpleArray
// Get a new pointer to the array
// NOTE: calling .Interface() is to get the actual value,
// instead of reflection values.
ptr := reflect.New(rte).Interface()
fmt.Printf("ptr: %v\n", ptr) // &[0 0 0 0 0]
// Make a simple int aray
fooArray := SimpleArray([5]byte{1, 10, 50, 100, 200})
fooBytes := BinaryBytes(fooArray)
fooReader := bytes.NewReader(fooBytes)
// Now you can read it.
n, err := new(int64), new(error)
it := ReadBinary(foo, fooReader, n, err).(SimpleArray)
if !bytes.Equal(it[:], fooArray[:]) {
t.Errorf("Expected %v but got %v", fooArray, it)
}
}