"testing";
"unsafe";
)
+import "fmt" // TODO DELETE
// Guarantee encoding format by comparing some encodings to hand-written values
type EncodeT struct {
// Do not run the machine yet; instead do individual instructions crafted by hand.
func TestScalarEncInstructions(t *testing.T) {
var b = new(bytes.Buffer);
- var state EncState;
+ state := new(EncState);
// bool
{
instr := &encInstr{ encBool, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(boolResult, b.Data()) {
t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Data())
}
instr := &encInstr{ encInt, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Data())
}
instr := &encInstr{ encUint, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Data())
}
instr := &encInstr{ encInt, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Data())
}
instr := &encInstr{ encUint, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
instr := &encInstr{ encInt16, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Data())
}
instr := &encInstr{ encUint16, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
instr := &encInstr{ encInt32, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Data())
}
instr := &encInstr{ encUint32, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
instr := &encInstr{ encInt64, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Data())
}
instr := &encInstr{ encUint, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
instr := &encInstr{ encFloat, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float enc instructions: expected % x got % x", floatResult, b.Data())
}
instr := &encInstr{ encFloat32, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Data())
}
instr := &encInstr{ encFloat64, 0, 0, 0 };
state.w = b;
state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 1;
instr.indir = 1;
instr.offset = uintptr(unsafe.Offsetof(data.b));
- instr.op(instr, &state);
+ instr.op(instr, state);
instr.field = 2;
instr.indir = 2;
instr.offset = uintptr(unsafe.Offsetof(data.c));
- instr.op(instr, &state);
+ instr.op(instr, state);
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Data())
}
}
}
+
+func expectField(n int, state *DecState, t *testing.T) {
+ v := int(DecodeUint(state));
+ if state.err != nil {
+ t.Fatalf("decoding field number %d: %v", n, state.err);
+ }
+ if v != n {
+ t.Fatalf("decoding field number %d, got %d", n, v);
+ }
+}
+
+// Test instruction execution for decoding.
+// Do not run the machine yet; instead do individual instructions crafted by hand.
+func TestScalarDecInstructions(t *testing.T) {
+ state := new(DecState);
+
+ // bool
+ {
+ b := bytes.NewBuffer(boolResult);
+ var data struct { a bool; b *bool; c **bool };
+ instr := &decInstr{ decBool, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != true {
+ t.Errorf("int a = %v not true", data.a)
+ }
+ if *data.b != true {
+ t.Errorf("int b = %v not true", *data.b)
+ }
+ if **data.c != true {
+ t.Errorf("int c = %v not true", **data.c)
+ }
+ }
+
+ // int
+ {
+ b := bytes.NewBuffer(signedResult);
+ var data struct { a int; b *int; c **int };
+ instr := &decInstr{ decInt, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // uint
+ {
+ b := bytes.NewBuffer(unsignedResult);
+ var data struct { a uint; b *uint; c **uint };
+ instr := &decInstr{ decUint, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // int8
+ {
+ b := bytes.NewBuffer(signedResult);
+ var data struct { a int8; b *int8; c **int8 };
+ instr := &decInstr{ decInt8, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // uint8
+ {
+ b := bytes.NewBuffer(unsignedResult);
+ var data struct { a uint8; b *uint8; c **uint8 };
+ instr := &decInstr{ decUint8, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // int16
+ {
+ b := bytes.NewBuffer(signedResult);
+ var data struct { a int16; b *int16; c **int16 };
+ instr := &decInstr{ decInt16, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // uint16
+ {
+ b := bytes.NewBuffer(unsignedResult);
+ var data struct { a uint16; b *uint16; c **uint16 };
+ instr := &decInstr{ decUint16, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // int32
+ {
+ b := bytes.NewBuffer(signedResult);
+ var data struct { a int32; b *int32; c **int32 };
+ instr := &decInstr{ decInt32, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // uint32
+ {
+ b := bytes.NewBuffer(unsignedResult);
+ var data struct { a uint32; b *uint32; c **uint32 };
+ instr := &decInstr{ decUint32, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // int64
+ {
+ b := bytes.NewBuffer(signedResult);
+ var data struct { a int64; b *int64; c **int64 };
+ instr := &decInstr{ decInt64, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // uint64
+ {
+ b := bytes.NewBuffer(unsignedResult);
+ var data struct { a uint64; b *uint64; c **uint64 };
+ instr := &decInstr{ decUint64, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // float
+ {
+ b := bytes.NewBuffer(floatResult);
+ var data struct { a float; b *float; c **float };
+ instr := &decInstr{ decFloat, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // float32
+ {
+ b := bytes.NewBuffer(floatResult);
+ var data struct { a float32; b *float32; c **float32 };
+ instr := &decInstr{ decFloat32, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+
+ // float64
+ {
+ b := bytes.NewBuffer(floatResult);
+ var data struct { a float64; b *float64; c **float64 };
+ instr := &decInstr{ decFloat64, 0, 0, 0 };
+ state.r = b;
+ state.base = uintptr(unsafe.Pointer(&data));
+ expectField(0, state, t);
+ instr.op(instr, state);
+ instr.field = 1;
+ instr.indir = 1;
+ instr.offset = uintptr(unsafe.Offsetof(data.b));
+ expectField(1, state, t);
+ instr.op(instr, state);
+ instr.field = 2;
+ instr.indir = 2;
+ instr.offset = uintptr(unsafe.Offsetof(data.c));
+ expectField(2, state, t);
+ instr.op(instr, state);
+ if data.a != 17 {
+ t.Errorf("int a = %v not 17", data.a)
+ }
+ if *data.b != 17 {
+ t.Errorf("int b = %v not 17", *data.b)
+ }
+ if **data.c != 17 {
+ t.Errorf("int c = %v not 17", **data.c)
+ }
+ }
+}
import (
"io";
+ "math";
"os";
"unsafe";
)
}
return int64(x >> 1)
}
+
+// The 'instructions' of the decoding machine
+type decInstr struct {
+ op func(i *decInstr, state *DecState);
+ field int; // field number
+ indir int; // how many pointer indirections to reach the value in the struct
+ offset uintptr; // offset in the structure of the field to encode
+}
+
+// Since the encoder writes no zeros, if we arrive at a decoder we have
+// a value to extract and store. The field number has already been read
+// (it's how we knew to call this decoder).
+// Each decoder is responsible for handling any indirections associated
+// with the data structure. If any pointer so reached is nil, allocation must
+// be done.
+
+// Walk the pointer hierarchy, allocating if we find a nil. Stop one before the end.
+func decIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
+ for ; indir > 1; indir-- {
+ if *(*unsafe.Pointer)(p) == nil {
+ // Allocation required
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(unsafe.Pointer));
+ }
+ p = *(*unsafe.Pointer)(p);
+ }
+ return p
+}
+
+func decBool(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := int(DecodeInt(state));
+ if state.err == nil {
+ *(*bool)(p) = v != 0;
+ }
+}
+
+func decInt(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := int(DecodeInt(state));
+ if state.err == nil {
+ *(*int)(p) = v;
+ }
+}
+
+func decUint(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := uint(DecodeUint(state));
+ if state.err == nil {
+ *(*uint)(p) = v;
+ }
+}
+
+func decInt8(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := int8(DecodeInt(state));
+ if state.err == nil {
+ *(*int8)(p) = v;
+ }
+}
+
+func decUint8(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := uint8(DecodeUint(state));
+ if state.err == nil {
+ *(*uint8)(p) = v;
+ }
+}
+
+func decInt16(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := int16(DecodeInt(state));
+ if state.err == nil {
+ *(*int16)(p) = v;
+ }
+}
+
+func decUint16(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := uint16(DecodeUint(state));
+ if state.err == nil {
+ *(*uint16)(p) = v;
+ }
+}
+
+func decInt32(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := int32(DecodeInt(state));
+ if state.err == nil {
+ *(*int32)(p) = v;
+ }
+}
+
+func decUint32(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := uint32(DecodeUint(state));
+ if state.err == nil {
+ *(*uint32)(p) = v;
+ }
+}
+
+func decInt64(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := int64(DecodeInt(state));
+ if state.err == nil {
+ *(*int64)(p) = v;
+ }
+}
+
+func decUint64(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := uint64(DecodeUint(state));
+ if state.err == nil {
+ *(*uint64)(p) = v;
+ }
+}
+
+// Floating-point numbers are transmitted as uint64s holding the bits
+// of the underlying representation. They are sent byte-reversed, with
+// the exponent end coming out first, so integer floating point numbers
+// (for example) transmit more compactly. This routine does the
+// unswizzling.
+func floatFromBits(u uint64) float64 {
+ var v uint64;
+ for i := 0; i < 8; i++ {
+ v <<= 8;
+ v |= u & 0xFF;
+ u >>= 8;
+ }
+ return math.Float64frombits(v);
+}
+
+func decFloat(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := float(floatFromBits(uint64(DecodeUint(state))));
+ if state.err == nil {
+ *(*float)(p) = v;
+ }
+}
+
+func decFloat32(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := float32(floatFromBits(uint64(DecodeUint(state))));
+ if state.err == nil {
+ *(*float32)(p) = v;
+ }
+}
+
+func decFloat64(i *decInstr, state *DecState) {
+ p := unsafe.Pointer(state.base+i.offset);
+ if i.indir > 0 {
+ if i.indir > 1 {
+ p = decIndirect(p, i.indir);
+ }
+ if *(*unsafe.Pointer)(p) == nil {
+ *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int));
+ p = *(*unsafe.Pointer)(p);
+ }
+ }
+ v := floatFromBits(uint64(DecodeUint(state)));
+ if state.err == nil {
+ *(*float64)(p) = v;
+ }
+}