func TestDataVersioningZeroPointersToMore(t *testing.T) { expEmpty := CapnpEncode("(mylist = [(),()])", "HoldsVerEmptyList") cv.Convey("Given a struct with 0 ptr fields, and a newer version of the struct with 1-2 pointer fields", t, func() { cv.Convey("then serializing the empty list and reading it back into 1 or 2 pointer fields should default initialize the pointer fields", func() { seg := capn.NewBuffer(nil) scratch := capn.NewBuffer(nil) emptyholder := air.NewRootHoldsVerEmptyList(seg) elist := air.NewVerEmptyList(scratch, 2) emptyholder.SetMylist(elist) actEmpty := ShowSeg(" after NewRootHoldsVerEmptyList(seg) and SetMylist(elist), segment seg is:", seg) actEmptyCap := string(CapnpDecode(actEmpty, "HoldsVerEmptyList")) expEmptyCap := string(CapnpDecode(expEmpty, "HoldsVerEmptyList")) cv.So(actEmptyCap, cv.ShouldResemble, expEmptyCap) fmt.Printf("\n actEmpty is \n") ShowBytes(actEmpty, 10) fmt.Printf("actEmpty decoded by capnp: '%s'\n", string(CapnpDecode(actEmpty, "HoldsVerEmptyList"))) cv.So(actEmpty, cv.ShouldResemble, expEmpty) // seg is set, now read into bigger list buf := bytes.Buffer{} seg.WriteTo(&buf) segbytes := buf.Bytes() reseg, _, err := capn.ReadFromMemoryZeroCopy(segbytes) if err != nil { panic(err) } ShowSeg(" after re-reading segbytes, segment reseg is:", reseg) fmt.Printf("segbytes decoded by capnp as HoldsVerOneDataList: '%s'\n", string(CapnpDecode(segbytes, "HoldsVerOneDataList"))) reHolder := air.ReadRootHoldsVerTwoTwoList(reseg) list22 := reHolder.Mylist() len22 := list22.Len() cv.So(len22, cv.ShouldEqual, 2) for i := 0; i < 2; i++ { ele := list22.At(i) val := ele.Val() cv.So(val, cv.ShouldEqual, 0) duo := ele.Duo() cv.So(duo, cv.ShouldEqual, 0) ptr1 := ele.Ptr1() ptr2 := ele.Ptr2() fmt.Printf("ptr1 = %#v\n", ptr1) cv.So(ptr1.Segment, cv.ShouldEqual, nil) fmt.Printf("ptr2 = %#v\n", ptr2) cv.So(ptr2.Segment, cv.ShouldEqual, nil) } }) }) }
func TestV0ListofEmptyShouldMatchCapnp(t *testing.T) { exp := CapnpEncode("(mylist = [(),()])", "HoldsVerEmptyList") cv.Convey("Given an empty struct with 0 data/0 ptr fields", t, func() { cv.Convey("then a list of 2 empty structs should match the capnp representation", func() { seg := capn.NewBuffer(nil) scratch := capn.NewBuffer(nil) holder := air.NewRootHoldsVerEmptyList(seg) ShowSeg(" after NewRootHoldsVerEmptyList(seg), segment seg is:", seg) elist := air.NewVerEmptyList(seg, 2) plist := capn.PointerList(elist) ShowSeg(" pre NewVerEmpty(scratch), segment seg is:", seg) e0 := air.NewVerEmpty(scratch) e1 := air.NewVerEmpty(scratch) plist.Set(0, capn.Object(e0)) plist.Set(1, capn.Object(e1)) ShowSeg(" pre SetMylist, segment seg is:", seg) fmt.Printf("Then we do the SetMylist():\n") holder.SetMylist(elist) // save buf := bytes.Buffer{} seg.WriteTo(&buf) act := buf.Bytes() save(act, "my.act.holder.elist") // show ShowSeg(" actual:\n", seg) fmt.Printf("act decoded by capnp: '%s'\n", string(CapnpDecode(act, "HoldsVerEmptyList"))) fmt.Printf("expected:\n") ShowBytes(exp, 10) fmt.Printf("exp decoded by capnp: '%s'\n", string(CapnpDecode(exp, "HoldsVerEmptyList"))) cv.So(act, cv.ShouldResemble, exp) }) }) }
func TestV1DataVersioningEmptyToBigger(t *testing.T) { //expOneSet := CapnpEncode("(mylist = [(val = 27),(val = 42)])", "HoldsVerOneDataList") //expOneZeroed := CapnpEncode("(mylist = [(val = 0),(val = 0)])", "HoldsVerOneDataList") //expOneEmpty := CapnpEncode("(mylist = [(),()])", "HoldsVerOneDataList") expEmpty := CapnpEncode("(mylist = [(),()])", "HoldsVerEmptyList") cv.Convey("Given a struct with 0 data/0 ptr fields, and a newer version of the struct with 1 data fields", t, func() { cv.Convey("then reading from serialized form the small list into the bigger (one or two data values) list should work, getting default value 0 for val/duo.", func() { seg := capn.NewBuffer(nil) scratch := capn.NewBuffer(nil) emptyholder := air.NewRootHoldsVerEmptyList(seg) elist := air.NewVerEmptyList(scratch, 2) emptyholder.SetMylist(elist) actEmpty := ShowSeg(" after NewRootHoldsVerEmptyList(seg) and SetMylist(elist), segment seg is:", seg) actEmptyCap := string(CapnpDecode(actEmpty, "HoldsVerEmptyList")) expEmptyCap := string(CapnpDecode(expEmpty, "HoldsVerEmptyList")) cv.So(actEmptyCap, cv.ShouldResemble, expEmptyCap) fmt.Printf("\n actEmpty is \n") ShowBytes(actEmpty, 10) fmt.Printf("actEmpty decoded by capnp: '%s'\n", string(CapnpDecode(actEmpty, "HoldsVerEmptyList"))) cv.So(actEmpty, cv.ShouldResemble, expEmpty) // seg is set, now read into bigger list buf := bytes.Buffer{} seg.WriteTo(&buf) segbytes := buf.Bytes() reseg, _, err := capn.ReadFromMemoryZeroCopy(segbytes) if err != nil { panic(err) } ShowSeg(" after re-reading segbytes, segment reseg is:", reseg) fmt.Printf("segbytes decoded by capnp as HoldsVerOneDataList: '%s'\n", string(CapnpDecode(segbytes, "HoldsVerOneDataList"))) reHolder := air.ReadRootHoldsVerOneDataList(reseg) onelist := reHolder.Mylist() lenone := onelist.Len() cv.So(lenone, cv.ShouldEqual, 2) for i := 0; i < 2; i++ { ele := onelist.At(i) val := ele.Val() cv.So(val, cv.ShouldEqual, 0) } reHolder2 := air.ReadRootHoldsVerTwoDataList(reseg) twolist := reHolder2.Mylist() lentwo := twolist.Len() cv.So(lentwo, cv.ShouldEqual, 2) for i := 0; i < 2; i++ { ele := twolist.At(i) val := ele.Val() cv.So(val, cv.ShouldEqual, 0) duo := ele.Duo() cv.So(duo, cv.ShouldEqual, 0) } }) }) }