Пример #1
0
func TestWriteBitListTwo64BitWords(t *testing.T) {

	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	list := seg.NewBitList(66)
	list.Set(64, true)
	list.Set(65, true)

	z.SetBoolvec(list)

	buf := bytes.Buffer{}
	seg.WriteTo(&buf)

	cv.Convey("Given a go-capnproto created List(Bool) Z::boolvec with bool values [true (+ 64 more times)]", t, func() {
		cv.Convey("Decoding it with c++ capnp should yield the expected text", func() {
			cv.So(CapnpDecodeSegment(seg, "", "aircraftlib/aircraft.capnp", "Z"), cv.ShouldEqual, `(boolvec = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true])`)
		})
	})

	cv.Convey("And we should be able to read back what we wrote", t, func() {
		z := air.ReadRootZ(seg)
		cv.So(z.Which(), cv.ShouldEqual, air.Z_BOOLVEC)

		var bitlist = z.Boolvec()
		cv.So(bitlist.Len(), cv.ShouldEqual, 66)

		for i := 0; i < 64; i++ {
			cv.So(bitlist.At(i), cv.ShouldEqual, false)
		}
		cv.So(bitlist.At(64), cv.ShouldEqual, true)
		cv.So(bitlist.At(65), cv.ShouldEqual, true)
	})
}
Пример #2
0
// For enums like Airport, their lists should have a set method
func TestEnumListHasSet(t *testing.T) {

	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	airc := air.AutoNewAircraft(seg)
	b737 := air.AutoNewB737(seg)
	base := air.AutoNewPlaneBase(seg)
	base.SetName("helen")
	homes := air.NewAirportList(seg, 2)

	// test is here!!
	// these next two lines should compile, because there should be a Set method.
	homes.Set(0, air.AIRPORT_JFK)
	homes.Set(1, air.AIRPORT_SFO)
	base.SetHomes(homes)
	b737.SetBase(base)
	airc.SetB737(b737)
	z.SetAircraft(airc)
	j, err := z.MarshalCapLit()
	panicOn(err)

	cv.Convey("To confirm that enum lists have a Set() method: Given the aircraftlib schema (and an Aircraft value), we should generate a MarshalCapLit() function that returns a literal representation in bytes for the given Aircraft value ", t, func() {
		cv.So(string(j), cv.ShouldEqual, `(aircraft = (b737 = (base = (name = "helen", homes = [jfk, sfo], rating = 0, canFly = false, capacity = 0, maxSpeed = 0))))`)
	})

}
Пример #3
0
func zdateFilledSegment(n int, packed bool) (*capn.Segment, []byte) {
	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	list := air.NewZdateList(seg, n)
	// hand added a Set() method to messages_test.go, so plist not needed
	plist := capn.PointerList(list)

	for i := 0; i < n; i++ {
		d := air.NewZdate(seg)
		d.SetMonth(12)
		d.SetDay(7)
		d.SetYear(int16(2004 + i))
		plist.Set(i, capn.Object(d))
		//list.Set(i, d)
	}
	z.SetZdatevec(list)

	buf := bytes.Buffer{}
	if packed {
		seg.WriteToPacked(&buf)
	} else {
		seg.WriteTo(&buf)
	}
	return seg, buf.Bytes()
}
Пример #4
0
func TestPrint(t *testing.T) {

	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	airc := air.AutoNewAircraft(seg)
	b737 := air.AutoNewB737(seg)
	base := air.AutoNewPlaneBase(seg)
	base.SetName("helen")
	base.SetMaxSpeed(0.5)
	homes := air.NewAirportList(seg, 2)
	homes.Set(0, air.AIRPORT_JFK)
	homes.Set(1, air.AIRPORT_SFO)
	base.SetHomes(homes)
	b737.SetBase(base)
	airc.SetB737(b737)
	z.SetAircraft(airc)
	lit, err := z.MarshalCapLit()
	panicOn(err)
	json, err := z.MarshalJSON()
	panicOn(err)

	cv.Convey("Given the aircraftlib schema (and an Aircraft value), we should generate a MarshalCapLit() function that returns a literal representation in bytes for the given Aircraft value. And the MarshalJSON() should return the expected format too.", t, func() {
		cv.So(string(lit), cv.ShouldEqual, `(aircraft = (b737 = (base = (name = "helen", homes = [jfk, sfo], rating = 0, canFly = false, capacity = 0, maxSpeed = 0.5))))`)
		cv.So(string(json), cv.ShouldEqual, `{"aircraft":{"b737":{"base":{"name":"helen","homes":["jfk", "sfo"],"rating":0,"canFly":false,"capacity":0,"maxSpeed":0.5}}}}`)
	})

}
Пример #5
0
func zboolvec_value_FilledSegment(value int64, elementCount uint) (*capn.Segment, []byte) {
	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	list := seg.NewBitList(int(elementCount))
	if value > 0 {
		for i := uint(0); i < elementCount; i++ {
			list.Set(int(i), ValAtBit(value, i))
		}
	}
	z.SetBoolvec(list)

	buf := bytes.Buffer{}
	seg.WriteTo(&buf)
	return seg, buf.Bytes()
}
Пример #6
0
func zdataFilledSegment(n int) (*capn.Segment, []byte) {
	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	d := air.NewZdata(seg)

	b := make([]byte, n)
	for i := 0; i < len(b); i++ {
		b[i] = byte(i)
	}
	d.SetData(b)
	z.SetZdata(d)

	buf := bytes.Buffer{}
	seg.WriteTo(&buf)
	return seg, buf.Bytes()
}
Пример #7
0
func ExampleAirplaneWrite() string {

	fname := "out.write_test.airplane.cpz"

	// make a brand new, empty segment (message)
	seg := capn.NewBuffer(nil)

	// If you want runtime-type identification, this is easily obtained. Just
	// wrap everything in a struct that contains a single anoymous union (e.g. struct Z).
	// Then always set a Z as the root object in you message/first segment.
	// The cost of the extra word of storage is usually worth it, as
	// then human readable output is easily obtained via a shell command such as
	//
	// $ cat binary.cpz | capnp decode aircraft.capnp Z
	//
	// If you need to conserve space, and know your content in advance, it
	// isn't necessary to use an anonymous union. Just supply the type name
	// in place of 'Z' in the decode command above.

	z := air.NewRootZ(seg) // root should be allocated first.
	// There must be only one root.

	// then non-root objects:
	aircraft := air.NewAircraft(seg)
	b737 := air.NewB737(seg)
	planebase := air.NewPlaneBase(seg)

	// how to create a list. Requires a cast at the moment.
	homes := air.NewAirportList(seg, 2)
	uint16list := capn.UInt16List(homes) // cast to the underlying type
	uint16list.Set(0, uint16(air.AIRPORT_JFK))
	uint16list.Set(1, uint16(air.AIRPORT_LAX))

	// set the primitive fields
	planebase.SetCanFly(true)
	planebase.SetName("Henrietta")
	planebase.SetRating(100)
	planebase.SetMaxSpeed(876) // km/hr
	// if we don't set capacity, it will get the default value, in this case 0.
	//planebase.SetCapacity(26020) // Liters fuel

	// set a list field
	planebase.SetHomes(homes)

	// wire up the pointers between objects
	b737.SetBase(planebase)
	aircraft.SetB737(b737)
	z.SetAircraft(aircraft)

	// ready to write

	// example of writing to memory
	buf := bytes.Buffer{}
	seg.WriteTo(&buf)

	// example of writing to file. Just use WriteTo().
	// We could have used SegToFile(seg, fname) from
	// util_test.go intead, but this makes it clear how easy it is.
	file, err := os.Create(fname)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	seg.WriteTo(file)

	// readback and view that file in human readable format. Defined in util_test.go
	text, err := CapnFileToText(fname, "aircraftlib/aircraft.capnp", "")
	if err != nil {
		panic(err)
	}
	fmt.Printf("here is our aircraft:\n")
	fmt.Printf("%s\n", text)

	return text
}