func TestDataTextCopyOptimization(t *testing.T) {
	t.Parallel()
	_, seg1, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatal(err)
	}
	root, err := air.NewRootNester1Capn(seg1)
	if err != nil {
		t.Fatal(err)
	}
	_, seg2, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatal(err)
	}
	strsl, err := capnp.NewTextList(seg2, 256)
	if err != nil {
		t.Fatal(err)
	}
	for i := 0; i < strsl.Len(); i++ {
		strsl.Set(i, "testess")
	}

	err = root.SetStrs(strsl)

	if err != nil {
		t.Fatal(err)
	}
	strsl, err = root.Strs()
	if err != nil {
		t.Fatal(err)
	}
	if strsl.Len() != 256 {
		t.Errorf("strsl.Len() = %d; want 256", strsl.Len())
	}
	for i := 0; i < strsl.Len(); i++ {
		s, err := strsl.At(i)
		if err != nil {
			t.Errorf("strsl.At(%d) error: %v", i, err)
			continue
		}
		if s != "testess" {
			t.Errorf("strsl.At(%d) = %q; want \"testess\"", i, s)
		}
	}
}
func makeMarshalTests(t *testing.T) []marshalTest {
	tests := []marshalTest{
		{
			name: "zdateFilledMessage(1)",
			msg:  zdateFilledMessage(t, 1),
			typ:  "Z",
			text: "(zdatevec = [(year = 2004, month = 12, day = 7)])\n",
		},
		{
			name: "zdateFilledMessage(10)",
			msg:  zdateFilledMessage(t, 10),
			typ:  "Z",
			text: "(zdatevec = [(year = 2004, month = 12, day = 7), (year = 2005, month = 12, day = 7), (year = 2006, month = 12, day = 7), (year = 2007, month = 12, day = 7), (year = 2008, month = 12, day = 7), (year = 2009, month = 12, day = 7), (year = 2010, month = 12, day = 7), (year = 2011, month = 12, day = 7), (year = 2012, month = 12, day = 7), (year = 2013, month = 12, day = 7)])\n",
		},
		{
			name: "zdataFilledMessage(20)",
			msg:  zdataFilledMessage(t, 20),
			typ:  "Z",
			text: `(zdata = (data = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13"))` + "\n",
			data: []byte{
				0, 0, 0, 0, 8, 0, 0, 0,
				0, 0, 0, 0, 2, 0, 1, 0,
				28, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 162, 0, 0, 0,
				0, 1, 2, 3, 4, 5, 6, 7,
				8, 9, 10, 11, 12, 13, 14, 15,
				16, 17, 18, 19, 0, 0, 0, 0,
			},
		},
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		if _, err := air.NewRootZjob(seg); err != nil {
			t.Fatal(err)
		}
		tests = append(tests, marshalTest{
			name: "empty Zjob",
			msg:  msg,
			typ:  "Zjob",
			text: "()\n",
			data: []byte{
				0, 0, 0, 0, 3, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 2, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		zjob, err := air.NewRootZjob(seg)
		if err != nil {
			t.Fatal(err)
		}
		if err := zjob.SetCmd("abc"); err != nil {
			t.Fatal(err)
		}
		tests = append(tests, marshalTest{
			name: "Zjob with text",
			msg:  msg,
			typ:  "Zjob",
			text: "(cmd = \"abc\")\n",
			data: []byte{
				0, 0, 0, 0, 4, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 2, 0,
				5, 0, 0, 0, 34, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				97, 98, 99, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		zjob, err := air.NewRootZjob(seg)
		if err != nil {
			t.Fatal(err)
		}
		tl, err := capnp.NewTextList(seg, 1)
		if err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(0, "xyz"); err != nil {
			t.Fatal(err)
		}
		if err := zjob.SetArgs(tl); err != nil {
			t.Fatal(err)
		}
		tests = append(tests, marshalTest{
			name: "Zjob with text list",
			msg:  msg,
			typ:  "Zjob",
			text: "(args = [\"xyz\"])\n",
			data: []byte{
				0, 0, 0, 0, 5, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 2, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				1, 0, 0, 0, 14, 0, 0, 0,
				1, 0, 0, 0, 34, 0, 0, 0,
				120, 121, 122, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		zjob, err := air.NewRootZjob(seg)
		if err != nil {
			t.Fatal(err)
		}
		if err := zjob.SetCmd("abc"); err != nil {
			t.Fatal(err)
		}
		tl, err := capnp.NewTextList(seg, 1)
		if err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(0, "xyz"); err != nil {
			t.Fatal(err)
		}
		if err := zjob.SetArgs(tl); err != nil {
			t.Fatal(err)
		}
		tests = append(tests, marshalTest{
			name: "Zjob with text and text list",
			msg:  msg,
			typ:  "Zjob",
			text: "(cmd = \"abc\", args = [\"xyz\"])\n",
			data: []byte{
				0, 0, 0, 0, 6, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 2, 0,
				5, 0, 0, 0, 34, 0, 0, 0,
				5, 0, 0, 0, 14, 0, 0, 0,
				97, 98, 99, 0, 0, 0, 0, 0,
				1, 0, 0, 0, 34, 0, 0, 0,
				120, 121, 122, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		server, err := air.NewRootZserver(seg)
		if err != nil {
			t.Fatal(err)
		}
		joblist, err := air.NewZjob_List(seg, 1)
		if err != nil {
			t.Fatal(err)
		}
		if err := server.SetWaitingjobs(joblist); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "Zserver with one empty job",
			msg:  msg,
			typ:  "Zserver",
			text: "(waitingjobs = [()])\n",
			data: []byte{
				0, 0, 0, 0, 5, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 23, 0, 0, 0,
				4, 0, 0, 0, 0, 0, 2, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		server, err := air.NewRootZserver(seg)
		if err != nil {
			t.Fatal(err)
		}
		joblist, err := air.NewZjob_List(seg, 1)
		if err != nil {
			t.Fatal(err)
		}
		server.SetWaitingjobs(joblist)
		if err := joblist.At(0).SetCmd("abc"); err != nil {
			t.Fatal(err)
		}
		tl, err := capnp.NewTextList(seg, 1)
		if err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(0, "xyz"); err != nil {
			t.Fatal(err)
		}
		if err := joblist.At(0).SetArgs(tl); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "Zserver with one full job",
			msg:  msg,
			typ:  "Zserver",
			text: "(waitingjobs = [(cmd = \"abc\", args = [\"xyz\"])])\n",
			data: []byte{
				0, 0, 0, 0, 8, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 23, 0, 0, 0,
				4, 0, 0, 0, 0, 0, 2, 0,
				5, 0, 0, 0, 34, 0, 0, 0,
				5, 0, 0, 0, 14, 0, 0, 0,
				97, 98, 99, 0, 0, 0, 0, 0,
				1, 0, 0, 0, 34, 0, 0, 0,
				120, 121, 122, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		server, err := air.NewRootZserver(seg)
		if err != nil {
			t.Fatal(err)
		}
		joblist, err := air.NewZjob_List(seg, 2)
		if err != nil {
			t.Fatal(err)
		}
		server.SetWaitingjobs(joblist)
		if err := joblist.At(0).SetCmd("abc"); err != nil {
			t.Fatal(err)
		}
		if err := joblist.At(1).SetCmd("xyz"); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "Zserver with two jobs",
			msg:  msg,
			typ:  "Zserver",
			text: "(waitingjobs = [(cmd = \"abc\"), (cmd = \"xyz\")])\n",
			data: []byte{
				0, 0, 0, 0, 9, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 39, 0, 0, 0,
				8, 0, 0, 0, 0, 0, 2, 0,
				13, 0, 0, 0, 34, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				9, 0, 0, 0, 34, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				97, 98, 99, 0, 0, 0, 0, 0,
				120, 121, 122, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		_, scratch, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}

		// in seg
		segbag, err := air.NewRootBag(seg)
		if err != nil {
			t.Fatal(err)
		}

		// in scratch
		xc, err := air.NewRootCounter(scratch)
		if err != nil {
			t.Fatal(err)
		}
		xc.SetSize(9)

		// copy from scratch to seg
		if err = segbag.SetCounter(xc); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "copy struct between messages",
			msg:  msg,
			typ:  "Bag",
			text: "(counter = (size = 9))\n",
			data: []byte{
				0, 0, 0, 0, 5, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				0, 0, 0, 0, 1, 0, 2, 0,
				9, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		_, scratch, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}

		// in seg
		segbag, err := air.NewRootBag(seg)
		if err != nil {
			t.Fatal(err)
		}

		// in scratch
		xc, err := air.NewRootCounter(scratch)
		if err != nil {
			t.Fatal(err)
		}
		xc.SetSize(9)
		if err := xc.SetWords("hello"); err != nil {
			t.Fatal(err)
		}

		// copy from scratch to seg
		if err = segbag.SetCounter(xc); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "copy struct with text between messages",
			msg:  msg,
			typ:  "Bag",
			text: "(counter = (size = 9, words = \"hello\"))\n",
			data: []byte{
				0, 0, 0, 0, 6, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				0, 0, 0, 0, 1, 0, 2, 0,
				9, 0, 0, 0, 0, 0, 0, 0,
				5, 0, 0, 0, 50, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				104, 101, 108, 108, 111, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		_, scratch, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}

		// in seg
		segbag, err := air.NewRootBag(seg)
		if err != nil {
			t.Fatal(err)
		}

		// in scratch
		xc, err := air.NewRootCounter(scratch)
		if err != nil {
			t.Fatal(err)
		}
		xc.SetSize(9)
		tl, err := capnp.NewTextList(scratch, 2)
		if err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(0, "hello"); err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(1, "bye"); err != nil {
			t.Fatal(err)
		}
		if err := xc.SetWordlist(tl); err != nil {
			t.Fatal(err)
		}

		// copy from scratch to seg
		if err = segbag.SetCounter(xc); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "copy struct with list of text between messages",
			msg:  msg,
			typ:  "Bag",
			text: "(counter = (size = 9, wordlist = [\"hello\", \"bye\"]))\n",
			data: []byte{
				0, 0, 0, 0, 9, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				0, 0, 0, 0, 1, 0, 2, 0,
				9, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0,
				1, 0, 0, 0, 22, 0, 0, 0,
				5, 0, 0, 0, 50, 0, 0, 0,
				5, 0, 0, 0, 34, 0, 0, 0,
				104, 101, 108, 108, 111, 0, 0, 0,
				98, 121, 101, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		_, scratch, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}

		// in seg
		segbag, err := air.NewRootBag(seg)
		if err != nil {
			t.Fatal(err)
		}

		// in scratch
		xc, err := air.NewRootCounter(scratch)
		if err != nil {
			t.Fatal(err)
		}
		xc.SetSize(9)
		if err := xc.SetWords("abc"); err != nil {
			t.Fatal(err)
		}
		tl, err := capnp.NewTextList(scratch, 2)
		if err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(0, "hello"); err != nil {
			t.Fatal(err)
		}
		if err := tl.Set(1, "byenow"); err != nil {
			t.Fatal(err)
		}
		if err := xc.SetWordlist(tl); err != nil {
			t.Fatal(err)
		}

		// copy from scratch to seg
		if err = segbag.SetCounter(xc); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "copy struct with data, text, and list of text between messages",
			msg:  msg,
			typ:  "Bag",
			text: "(counter = (size = 9, words = \"abc\", wordlist = [\"hello\", \"byenow\"]))\n",
			data: []byte{
				0, 0, 0, 0, 10, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				0, 0, 0, 0, 1, 0, 2, 0,
				9, 0, 0, 0, 0, 0, 0, 0,
				5, 0, 0, 0, 34, 0, 0, 0,
				5, 0, 0, 0, 22, 0, 0, 0,
				97, 98, 99, 0, 0, 0, 0, 0,
				5, 0, 0, 0, 50, 0, 0, 0,
				5, 0, 0, 0, 58, 0, 0, 0,
				104, 101, 108, 108, 111, 0, 0, 0,
				98, 121, 101, 110, 111, 119, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		holder, err := air.NewRootHoldsVerEmptyList(seg)
		if err != nil {
			t.Fatal(err)
		}
		elist, err := air.NewVerEmpty_List(seg, 2)
		if err != nil {
			t.Fatal(err)
		}
		if err := holder.SetMylist(elist); err != nil {
			t.Fatal(err)
		}

		tests = append(tests, marshalTest{
			name: "V0 list of empty",
			msg:  msg,
			typ:  "HoldsVerEmptyList",
			text: "(mylist = [(), ()])\n",
			data: []byte{
				0, 0, 0, 0, 3, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 7, 0, 0, 0,
				8, 0, 0, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		holder, err := air.NewRootNester1Capn(seg)
		if err != nil {
			t.Fatal(err)
		}
		initNester(t, holder, "furiosa", "max")

		tests = append(tests, marshalTest{
			name: "list inside a struct",
			msg:  msg,
			typ:  "Nester1Capn",
			text: "(strs = [\"furiosa\", \"max\"])\n",
			data: []byte{
				0, 0, 0, 0, 6, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 22, 0, 0, 0,
				5, 0, 0, 0, 66, 0, 0, 0,
				5, 0, 0, 0, 34, 0, 0, 0,
				102, 117, 114, 105, 111, 115, 97, 0,
				109, 97, 120, 0, 0, 0, 0, 0,
			},
		})
	}

	{
		msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Fatal(err)
		}
		holder, err := air.NewRootRWTestCapn(seg)
		if err != nil {
			t.Fatal(err)
		}
		mat, err := capnp.NewPointerList(seg, 2)
		if err != nil {
			t.Fatal(err)
		}
		if err := holder.SetNestMatrix(mat); err != nil {
			t.Fatal(err)
		}

		row0, err := air.NewNester1Capn_List(seg, 2)
		if err != nil {
			t.Fatal(err)
		}
		if err := mat.Set(0, row0); err != nil {
			t.Fatal(err)
		}
		initNester(t, row0.At(0), "z", "w")
		initNester(t, row0.At(1), "q", "r")

		row1, err := air.NewNester1Capn_List(seg, 2)
		if err != nil {
			t.Fatal(err)
		}
		if err := mat.Set(1, row1); err != nil {
			t.Fatal(err)
		}
		initNester(t, row1.At(0), "zebra", "wally")
		initNester(t, row1.At(1), "qubert", "rocks")

		tests = append(tests, marshalTest{
			name: "doubly-nested list of struct that has a list field",
			msg:  msg,
			typ:  "RWTestCapn",
			text: `(nestMatrix = [[(strs = ["z", "w"]), (strs = ["q", "r"])], [(strs = ["zebra", "wally"]), (strs = ["qubert", "rocks"])]])` + "\n",
			data: []byte{
				0, 0, 0, 0, 26, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 1, 0,
				1, 0, 0, 0, 22, 0, 0, 0,
				5, 0, 0, 0, 23, 0, 0, 0,
				45, 0, 0, 0, 23, 0, 0, 0,
				8, 0, 0, 0, 0, 0, 1, 0,
				5, 0, 0, 0, 22, 0, 0, 0,
				17, 0, 0, 0, 22, 0, 0, 0,
				5, 0, 0, 0, 18, 0, 0, 0,
				5, 0, 0, 0, 18, 0, 0, 0,
				122, 0, 0, 0, 0, 0, 0, 0,
				119, 0, 0, 0, 0, 0, 0, 0,
				5, 0, 0, 0, 18, 0, 0, 0,
				5, 0, 0, 0, 18, 0, 0, 0,
				113, 0, 0, 0, 0, 0, 0, 0,
				114, 0, 0, 0, 0, 0, 0, 0,
				8, 0, 0, 0, 0, 0, 1, 0,
				5, 0, 0, 0, 22, 0, 0, 0,
				17, 0, 0, 0, 22, 0, 0, 0,
				5, 0, 0, 0, 50, 0, 0, 0,
				5, 0, 0, 0, 50, 0, 0, 0,
				122, 101, 98, 114, 97, 0, 0, 0,
				119, 97, 108, 108, 121, 0, 0, 0,
				5, 0, 0, 0, 58, 0, 0, 0,
				5, 0, 0, 0, 50, 0, 0, 0,
				113, 117, 98, 101, 114, 116, 0, 0,
				114, 111, 99, 107, 115, 0, 0, 0,
			},
		})
	}

	return tests
}