arrays, not slices, and only with non-pointer elements.

(actually slices encode but do not decode yet)

R=rsc
DELTA=221  (82 added, 65 deleted, 74 changed)
OCL=31095
CL=31095
This commit is contained in:
Rob Pike 2009-07-02 16:43:46 -07:00
parent 29e6eb21ec
commit 0c33d4353e
3 changed files with 146 additions and 129 deletions

View file

@ -125,11 +125,6 @@ func newEncState(b *bytes.Buffer) *EncState {
return state;
}
func encAddrOf(state *EncState, instr *encInstr) unsafe.Pointer {
p := unsafe.Pointer(state.base+instr.offset);
return encIndirect(p, instr.indir);
}
// Test instruction execution for encoding.
// Do not run the machine yet; instead do individual instructions crafted by hand.
func TestScalarEncInstructions(t *testing.T) {
@ -140,8 +135,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a bool } { true };
instr := &encInstr{ encBool, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(boolResult, b.Data()) {
t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Data())
}
@ -153,8 +147,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a int } { 17 };
instr := &encInstr{ encInt, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Data())
}
@ -166,8 +159,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a uint } { 17 };
instr := &encInstr{ encUint, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Data())
}
@ -179,8 +171,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a int8 } { 17 };
instr := &encInstr{ encInt, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Data())
}
@ -192,8 +183,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a uint8 } { 17 };
instr := &encInstr{ encUint, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
@ -208,8 +198,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a int16 } { 17 };
instr := &encInstr{ encInt16, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Data())
}
@ -221,8 +210,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a uint16 } { 17 };
instr := &encInstr{ encUint16, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
@ -234,8 +222,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a int32 } { 17 };
instr := &encInstr{ encInt32, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Data())
}
@ -247,8 +234,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a uint32 } { 17 };
instr := &encInstr{ encUint32, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
@ -260,8 +246,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a int64 } { 17 };
instr := &encInstr{ encInt64, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Data())
}
@ -273,8 +258,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a uint64 } { 17 };
instr := &encInstr{ encUint, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
@ -286,8 +270,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a float } { 17 };
instr := &encInstr{ encFloat, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float enc instructions: expected % x got % x", floatResult, b.Data())
}
@ -299,8 +282,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a float32 } { 17 };
instr := &encInstr{ encFloat32, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Data())
}
@ -312,8 +294,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a float64 } { 17 };
instr := &encInstr{ encFloat64, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Data())
}
@ -325,8 +306,7 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a []byte } { strings.Bytes("hello") };
instr := &encInstr{ encUint8Array, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(bytesResult, b.Data()) {
t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Data())
}
@ -338,21 +318,14 @@ func TestScalarEncInstructions(t *testing.T) {
data := struct { a string } { "hello" };
instr := &encInstr{ encString, 6, 0, 0 };
state := newEncState(b);
state.base = uintptr(unsafe.Pointer(&data));
instr.op(instr, state, encAddrOf(state, instr));
instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(bytesResult, b.Data()) {
t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Data())
}
}
}
// derive the address of a field, after indirecting indir times.
func decAddrOf(state *DecState, instr *decInstr) unsafe.Pointer {
p := unsafe.Pointer(state.base+instr.offset);
return decIndirect(p, instr.indir);
}
func execDec(typ string, instr *decInstr, state *DecState, t *testing.T) {
func execDec(typ string, instr *decInstr, state *DecState, t *testing.T, p unsafe.Pointer) {
v := int(DecodeUint(state));
if state.err != nil {
t.Fatalf("decoding %s field: %v", typ, state.err);
@ -360,7 +333,7 @@ func execDec(typ string, instr *decInstr, state *DecState, t *testing.T) {
if v + state.fieldnum != 6 {
t.Fatalf("decoding field number %d, got %d", 6, v + state.fieldnum);
}
instr.op(instr, state, decAddrOf(state, instr));
instr.op(instr, state, decIndirect(p, instr.indir));
state.fieldnum = 6;
}
@ -380,8 +353,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a bool };
instr := &decInstr{ decBool, 6, 0, 0 };
state := newDecState(boolResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("bool", instr, state, t);
execDec("bool", instr, state, t, unsafe.Pointer(&data));
if data.a != true {
t.Errorf("int a = %v not true", data.a)
}
@ -391,8 +363,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a int };
instr := &decInstr{ decInt, 6, 0, 0 };
state := newDecState(signedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("int", instr, state, t);
execDec("int", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -403,8 +374,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a uint };
instr := &decInstr{ decUint, 6, 0, 0 };
state := newDecState(unsignedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("uint", instr, state, t);
execDec("uint", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -415,8 +385,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a int8 };
instr := &decInstr{ decInt8, 6, 0, 0 };
state := newDecState(signedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("int8", instr, state, t);
execDec("int8", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -427,8 +396,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a uint8 };
instr := &decInstr{ decUint8, 6, 0, 0 };
state := newDecState(unsignedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("uint8", instr, state, t);
execDec("uint8", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -439,8 +407,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a int16 };
instr := &decInstr{ decInt16, 6, 0, 0 };
state := newDecState(signedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("int16", instr, state, t);
execDec("int16", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -451,8 +418,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a uint16 };
instr := &decInstr{ decUint16, 6, 0, 0 };
state := newDecState(unsignedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("uint16", instr, state, t);
execDec("uint16", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -463,8 +429,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a int32 };
instr := &decInstr{ decInt32, 6, 0, 0 };
state := newDecState(signedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("int32", instr, state, t);
execDec("int32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -475,8 +440,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a uint32 };
instr := &decInstr{ decUint32, 6, 0, 0 };
state := newDecState(unsignedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("uint32", instr, state, t);
execDec("uint32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -487,8 +451,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a int64 };
instr := &decInstr{ decInt64, 6, 0, 0 };
state := newDecState(signedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("int64", instr, state, t);
execDec("int64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -499,8 +462,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a uint64 };
instr := &decInstr{ decUint64, 6, 0, 0 };
state := newDecState(unsignedResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("uint64", instr, state, t);
execDec("uint64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -511,8 +473,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a float };
instr := &decInstr{ decFloat, 6, 0, 0 };
state := newDecState(floatResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("float", instr, state, t);
execDec("float", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -523,8 +484,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a float32 };
instr := &decInstr{ decFloat32, 6, 0, 0 };
state := newDecState(floatResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("float32", instr, state, t);
execDec("float32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -535,8 +495,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a float64 };
instr := &decInstr{ decFloat64, 6, 0, 0 };
state := newDecState(floatResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("float64", instr, state, t);
execDec("float64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
@ -547,8 +506,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a []byte };
instr := &decInstr{ decUint8Array, 6, 0, 0 };
state := newDecState(bytesResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("bytes", instr, state, t);
execDec("bytes", instr, state, t, unsafe.Pointer(&data));
if string(data.a) != "hello" {
t.Errorf(`bytes a = %q not "hello"`, string(data.a))
}
@ -559,8 +517,7 @@ func TestScalarDecInstructions(t *testing.T) {
var data struct { a string };
instr := &decInstr{ decString, 6, 0, 0 };
state := newDecState(bytesResult);
state.base = uintptr(unsafe.Pointer(&data));
execDec("bytes", instr, state, t);
execDec("bytes", instr, state, t, unsafe.Pointer(&data));
if data.a != "hello" {
t.Errorf(`bytes a = %q not "hello"`, data.a)
}
@ -574,6 +531,7 @@ func TestEncode(t *testing.T) {
}
type T1 struct {
a, b,c int;
n *[3]float;
s string;
y []byte;
t *T2;
@ -582,6 +540,7 @@ func TestEncode(t *testing.T) {
a: 17,
b: 18,
c: -5,
n: &[3]float{1.5, 2.5, 3.5},
s: "Now is the time",
y: strings.Bytes("hello, sailor"),
t: &T2{"this is T2"},