Files
go/list_ptr_test.go
zeekay d9bcc8711f The generator can say what the chains actually put on the wire
A list's encoding was one thing for every element type: a run of
{length, payload} pairs. The Lux chains write four different runs — a
run of numbers, a run of fixed-width byte records, a run of struct
payloads back to back, and a run of relative pointers to payloads
written elsewhere — and none of them was that one. So both chains that
have a C++ port wrote their own accessors by hand, and the generator
could not have replaced them.

The shape is now derived from the element type rather than fixed. A
number is a run of numbers at its own width; bytes_fixed[N] is those
runs back to back; a struct with no tail is its payload back to back at
its own width; a struct WITH a tail cannot sit in a run at all, because
its tail has nowhere to go between two neighbours, so it becomes a
pointer. The schema never spells any of this, which is what stops two
authors spelling one list two ways. bytes and text have no width, so a
run of them is refused rather than invented.

Order is the wire, too. Every payload a struct points at is written
before the struct's own object, because SetList needs an offset that
already exists — the order the reference has always written in, and the
one the emitted builder had backwards. An empty list now writes nothing
at all: the alignment pad that a start-then-discard would leave behind
moves every byte after it.

Three entry points per struct, because there are three places a struct
sits: New for a message of its own, Append for an object inside somebody
else's message, Encode for one element of an inline run. A nested struct
field is the nested type's own Input, not a pre-built buffer the caller
hands over unchecked.

Readers take the stride the schema knows, so a lying length word is
refused once instead of at every element, and a list of records answers
with the record's type instead of an offset the caller has to know.

Both backends emit version 2. That is the header every vector in the
chain corpus opens with, and the one the hardened runtime writes; a
generated builder that wrote version 1 wrote a header no chain has.

The Go runtime gains the pointer list it was missing — AddObjectPtr,
ObjectPtr and the strided read — so both backends can express the same
wire rather than one of them refusing.

Proven: one schema, both backends, byte-identical output over all four
list shapes, a bytes tail and a pointer list.
2026-09-06 09:09:54 -07:00

83 lines
2.2 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause-Eco
// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
package zap
import "testing"
// TestPointerListRoundTrips writes two payloads of different widths, names
// them from one list, and reads both back. A pointer list exists for exactly
// this: an inline run cannot hold records that are not the same width.
func TestPointerListRoundTrips(t *testing.T) {
b := NewBuilderV2(256)
first := b.StartObject(8)
first.SetUint64(0, 0x1122334455667788)
firstAt := first.Finish()
second := b.StartObject(16)
second.SetUint64(0, 9)
second.SetBytes(8, []byte("wide"))
secondAt := second.Finish()
lb := b.StartList(4)
lb.AddObjectPtr(firstAt)
lb.AddObjectPtr(secondAt)
lb.AddObjectPtr(0) // the null element
listAt, count := lb.Finish()
root := b.StartObject(8)
root.SetList(0, listAt, count)
root.FinishAsRoot()
m, err := Parse(b.Finish())
if err != nil {
t.Fatal(err)
}
l := m.Root().ListStride(0, 4)
if l.Len() != 3 {
t.Fatalf("len = %d, want 3", l.Len())
}
if got := l.ObjectPtr(0).Uint64(0); got != 0x1122334455667788 {
t.Errorf("element 0 = %#x", got)
}
if got := l.ObjectPtr(1).Uint64(0); got != 9 {
t.Errorf("element 1 = %d", got)
}
if got := string(l.ObjectPtr(1).Bytes(8)); got != "wide" {
t.Errorf("element 1 tail = %q", got)
}
if !l.ObjectPtr(2).IsNull() {
t.Error("the null element is not null")
}
if !l.ObjectPtr(3).IsNull() {
t.Error("reading past the end answered an object")
}
}
// TestListStrideRefusesALyingLength is the whole reason a reader wants the
// stride: a length word the payload cannot possibly hold is refused once,
// here, rather than at every element.
func TestListStrideRefusesALyingLength(t *testing.T) {
b := NewBuilderV2(256)
lb := b.StartList(8)
lb.AddUint64(1)
lb.AddUint64(2)
at, _ := lb.Finish()
ob := b.StartObject(8)
ob.SetList(0, at, 30) // two elements on the wire, thirty claimed
ob.FinishAsRoot()
m, err := Parse(b.Finish())
if err != nil {
t.Fatal(err)
}
if l := m.Root().ListStride(0, 8); !l.IsNull() {
t.Fatalf("stride-8 read accepted a length of %d", l.Len())
}
if l := m.Root().List(0); l.Len() != 30 {
t.Fatalf("the strideless read is meant to be the permissive one, got %d", l.Len())
}
}