2019-09-26 22:18:28 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-03-03 14:29:32 -08:00
|
|
|
|
2013-08-29 16:45:31 -07:00
|
|
|
import pytest
|
|
|
|
import capnp
|
|
|
|
import os
|
2013-08-31 16:44:54 -07:00
|
|
|
import math
|
2016-03-03 14:29:32 -08:00
|
|
|
import sys
|
2013-08-29 16:45:31 -07:00
|
|
|
|
|
|
|
this_dir = os.path.dirname(__file__)
|
|
|
|
|
2016-03-03 14:29:32 -08:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
EXPECT_BYTES = True
|
|
|
|
else:
|
|
|
|
EXPECT_BYTES = False
|
|
|
|
|
|
|
|
|
2013-08-29 16:45:31 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def addressbook():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "addressbook.capnp"))
|
2013-08-29 16:45:31 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-09-01 02:13:19 -07:00
|
|
|
def test_addressbook_message_classes(addressbook):
|
2013-08-29 16:45:31 -07:00
|
|
|
def writeAddressBook(fd):
|
2013-09-01 20:10:57 -07:00
|
|
|
message = capnp._MallocMessageBuilder()
|
|
|
|
addressBook = message.init_root(addressbook.AddressBook)
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addressBook.init("people", 2)
|
2013-08-29 16:45:31 -07:00
|
|
|
|
|
|
|
alice = people[0]
|
|
|
|
alice.id = 123
|
2021-10-01 11:00:22 -07:00
|
|
|
alice.name = "Alice"
|
|
|
|
alice.email = "alice@example.com"
|
|
|
|
alicePhones = alice.init("phones", 1)
|
2013-08-29 16:45:31 -07:00
|
|
|
alicePhones[0].number = "555-1212"
|
2021-10-01 11:00:22 -07:00
|
|
|
alicePhones[0].type = "mobile"
|
2013-08-29 16:45:31 -07:00
|
|
|
alice.employment.school = "MIT"
|
|
|
|
|
|
|
|
bob = people[1]
|
|
|
|
bob.id = 456
|
2021-10-01 11:00:22 -07:00
|
|
|
bob.name = "Bob"
|
|
|
|
bob.email = "bob@example.com"
|
|
|
|
bobPhones = bob.init("phones", 2)
|
2013-08-29 16:45:31 -07:00
|
|
|
bobPhones[0].number = "555-4567"
|
2021-10-01 11:00:22 -07:00
|
|
|
bobPhones[0].type = "home"
|
2013-08-29 16:45:31 -07:00
|
|
|
bobPhones[1].number = "555-7654"
|
2021-10-01 11:00:22 -07:00
|
|
|
bobPhones[1].type = "work"
|
2013-08-29 16:45:31 -07:00
|
|
|
bob.employment.unemployed = None
|
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
capnp._write_packed_message_to_fd(fd, message)
|
2013-08-29 16:45:31 -07:00
|
|
|
|
|
|
|
def printAddressBook(fd):
|
2015-12-04 16:01:28 -08:00
|
|
|
message = capnp._PackedFdMessageReader(f)
|
2013-09-01 20:10:57 -07:00
|
|
|
addressBook = message.get_root(addressbook.AddressBook)
|
2013-08-29 16:45:31 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
people = addressBook.people
|
2013-08-29 16:45:31 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
alice = people[0]
|
|
|
|
assert alice.id == 123
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alice.name == "Alice"
|
|
|
|
assert alice.email == "alice@example.com"
|
2013-09-01 20:10:57 -07:00
|
|
|
alicePhones = alice.phones
|
|
|
|
assert alicePhones[0].number == "555-1212"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alicePhones[0].type == "mobile"
|
2013-09-01 20:10:57 -07:00
|
|
|
assert alice.employment.school == "MIT"
|
2013-08-29 16:45:31 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
bob = people[1]
|
|
|
|
assert bob.id == 456
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bob.name == "Bob"
|
|
|
|
assert bob.email == "bob@example.com"
|
2013-09-01 20:10:57 -07:00
|
|
|
bobPhones = bob.phones
|
|
|
|
assert bobPhones[0].number == "555-4567"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bobPhones[0].type == "home"
|
2013-09-01 20:10:57 -07:00
|
|
|
assert bobPhones[1].number == "555-7654"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bobPhones[1].type == "work"
|
2019-09-26 22:18:28 -07:00
|
|
|
assert bob.employment.unemployed is None
|
2013-08-29 16:45:31 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "w")
|
2013-08-29 16:45:31 -07:00
|
|
|
writeAddressBook(f.fileno())
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "r")
|
2013-08-29 16:45:31 -07:00
|
|
|
printAddressBook(f.fileno())
|
2013-08-31 16:44:54 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-09-01 02:13:19 -07:00
|
|
|
def test_addressbook(addressbook):
|
|
|
|
def writeAddressBook(file):
|
2013-09-01 20:10:57 -07:00
|
|
|
addresses = addressbook.AddressBook.new_message()
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addresses.init("people", 2)
|
2013-09-01 02:13:19 -07:00
|
|
|
|
|
|
|
alice = people[0]
|
|
|
|
alice.id = 123
|
2021-10-01 11:00:22 -07:00
|
|
|
alice.name = "Alice"
|
|
|
|
alice.email = "alice@example.com"
|
|
|
|
alicePhones = alice.init("phones", 1)
|
2013-09-01 02:13:19 -07:00
|
|
|
alicePhones[0].number = "555-1212"
|
2021-10-01 11:00:22 -07:00
|
|
|
alicePhones[0].type = "mobile"
|
2013-09-01 02:13:19 -07:00
|
|
|
alice.employment.school = "MIT"
|
|
|
|
|
|
|
|
bob = people[1]
|
|
|
|
bob.id = 456
|
2021-10-01 11:00:22 -07:00
|
|
|
bob.name = "Bob"
|
|
|
|
bob.email = "bob@example.com"
|
|
|
|
bobPhones = bob.init("phones", 2)
|
2013-09-01 02:13:19 -07:00
|
|
|
bobPhones[0].number = "555-4567"
|
2021-10-01 11:00:22 -07:00
|
|
|
bobPhones[0].type = "home"
|
2013-09-01 02:13:19 -07:00
|
|
|
bobPhones[1].number = "555-7654"
|
2021-10-01 11:00:22 -07:00
|
|
|
bobPhones[1].type = "work"
|
2013-09-01 02:13:19 -07:00
|
|
|
bob.employment.unemployed = None
|
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
addresses.write(file)
|
2013-09-01 02:13:19 -07:00
|
|
|
|
|
|
|
def printAddressBook(file):
|
2013-09-01 20:10:57 -07:00
|
|
|
addresses = addressbook.AddressBook.read(file)
|
2013-09-01 02:13:19 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
people = addresses.people
|
2013-09-01 02:13:19 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
alice = people[0]
|
|
|
|
assert alice.id == 123
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alice.name == "Alice"
|
|
|
|
assert alice.email == "alice@example.com"
|
2013-09-01 20:10:57 -07:00
|
|
|
alicePhones = alice.phones
|
|
|
|
assert alicePhones[0].number == "555-1212"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alicePhones[0].type == "mobile"
|
2013-09-01 20:10:57 -07:00
|
|
|
assert alice.employment.school == "MIT"
|
|
|
|
|
|
|
|
bob = people[1]
|
|
|
|
assert bob.id == 456
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bob.name == "Bob"
|
|
|
|
assert bob.email == "bob@example.com"
|
2013-09-01 20:10:57 -07:00
|
|
|
bobPhones = bob.phones
|
|
|
|
assert bobPhones[0].number == "555-4567"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bobPhones[0].type == "home"
|
2013-09-01 20:10:57 -07:00
|
|
|
assert bobPhones[1].number == "555-7654"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bobPhones[1].type == "work"
|
2019-09-26 22:18:28 -07:00
|
|
|
assert bob.employment.unemployed is None
|
2013-09-01 02:13:19 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "w")
|
2013-09-01 20:10:57 -07:00
|
|
|
writeAddressBook(f)
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "r")
|
2013-09-01 20:10:57 -07:00
|
|
|
printAddressBook(f)
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
def test_addressbook_resizable(addressbook):
|
|
|
|
def writeAddressBook(file):
|
|
|
|
addresses = addressbook.AddressBook.new_message()
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addresses.init_resizable_list("people")
|
2013-09-01 20:10:57 -07:00
|
|
|
|
|
|
|
alice = people.add()
|
|
|
|
alice.id = 123
|
2021-10-01 11:00:22 -07:00
|
|
|
alice.name = "Alice"
|
|
|
|
alice.email = "alice@example.com"
|
|
|
|
alicePhones = alice.init("phones", 1)
|
2013-09-01 20:10:57 -07:00
|
|
|
alicePhones[0].number = "555-1212"
|
2021-10-01 11:00:22 -07:00
|
|
|
alicePhones[0].type = "mobile"
|
2013-09-01 20:10:57 -07:00
|
|
|
alice.employment.school = "MIT"
|
|
|
|
|
|
|
|
bob = people.add()
|
|
|
|
bob.id = 456
|
2021-10-01 11:00:22 -07:00
|
|
|
bob.name = "Bob"
|
|
|
|
bob.email = "bob@example.com"
|
|
|
|
bobPhones = bob.init("phones", 2)
|
2013-09-01 20:10:57 -07:00
|
|
|
bobPhones[0].number = "555-4567"
|
2021-10-01 11:00:22 -07:00
|
|
|
bobPhones[0].type = "home"
|
2013-09-01 20:10:57 -07:00
|
|
|
bobPhones[1].number = "555-7654"
|
2021-10-01 11:00:22 -07:00
|
|
|
bobPhones[1].type = "work"
|
2013-09-01 20:10:57 -07:00
|
|
|
bob.employment.unemployed = None
|
|
|
|
|
|
|
|
people.finish()
|
2014-09-03 15:20:26 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
addresses.write(file)
|
|
|
|
|
|
|
|
def printAddressBook(file):
|
|
|
|
addresses = addressbook.AddressBook.read(file)
|
|
|
|
|
|
|
|
people = addresses.people
|
|
|
|
|
|
|
|
alice = people[0]
|
|
|
|
assert alice.id == 123
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alice.name == "Alice"
|
|
|
|
assert alice.email == "alice@example.com"
|
2013-09-01 20:10:57 -07:00
|
|
|
alicePhones = alice.phones
|
|
|
|
assert alicePhones[0].number == "555-1212"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alicePhones[0].type == "mobile"
|
2013-09-01 20:10:57 -07:00
|
|
|
assert alice.employment.school == "MIT"
|
|
|
|
|
|
|
|
bob = people[1]
|
|
|
|
assert bob.id == 456
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bob.name == "Bob"
|
|
|
|
assert bob.email == "bob@example.com"
|
2013-09-01 20:10:57 -07:00
|
|
|
bobPhones = bob.phones
|
|
|
|
assert bobPhones[0].number == "555-4567"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bobPhones[0].type == "home"
|
2013-09-01 20:10:57 -07:00
|
|
|
assert bobPhones[1].number == "555-7654"
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bobPhones[1].type == "work"
|
2019-09-26 22:18:28 -07:00
|
|
|
assert bob.employment.unemployed is None
|
2013-09-01 02:13:19 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "w")
|
2013-09-01 02:13:19 -07:00
|
|
|
writeAddressBook(f)
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "r")
|
2013-09-01 02:13:19 -07:00
|
|
|
printAddressBook(f)
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2014-09-03 15:20:26 -07:00
|
|
|
def test_addressbook_explicit_fields(addressbook):
|
|
|
|
def writeAddressBook(file):
|
|
|
|
addresses = addressbook.AddressBook.new_message()
|
|
|
|
address_fields = addressbook.AddressBook.schema.fields
|
|
|
|
person_fields = addressbook.Person.schema.fields
|
|
|
|
phone_fields = addressbook.Person.PhoneNumber.schema.fields
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addresses._init_by_field(address_fields["people"], 2)
|
2014-09-03 15:20:26 -07:00
|
|
|
|
|
|
|
alice = people[0]
|
2021-10-01 11:00:22 -07:00
|
|
|
alice._set_by_field(person_fields["id"], 123)
|
|
|
|
alice._set_by_field(person_fields["name"], "Alice")
|
|
|
|
alice._set_by_field(person_fields["email"], "alice@example.com")
|
|
|
|
alicePhones = alice._init_by_field(person_fields["phones"], 1)
|
|
|
|
alicePhones[0]._set_by_field(phone_fields["number"], "555-1212")
|
|
|
|
alicePhones[0]._set_by_field(phone_fields["type"], "mobile")
|
|
|
|
employment = alice._get_by_field(person_fields["employment"])
|
|
|
|
employment._set_by_field(
|
|
|
|
addressbook.Person.Employment.schema.fields["school"], "MIT"
|
|
|
|
)
|
2014-09-03 15:20:26 -07:00
|
|
|
|
|
|
|
bob = people[1]
|
2021-10-01 11:00:22 -07:00
|
|
|
bob._set_by_field(person_fields["id"], 456)
|
|
|
|
bob._set_by_field(person_fields["name"], "Bob")
|
|
|
|
bob._set_by_field(person_fields["email"], "bob@example.com")
|
|
|
|
bobPhones = bob._init_by_field(person_fields["phones"], 2)
|
|
|
|
bobPhones[0]._set_by_field(phone_fields["number"], "555-4567")
|
|
|
|
bobPhones[0]._set_by_field(phone_fields["type"], "home")
|
|
|
|
bobPhones[1]._set_by_field(phone_fields["number"], "555-7654")
|
|
|
|
bobPhones[1]._set_by_field(phone_fields["type"], "work")
|
|
|
|
employment = bob._get_by_field(person_fields["employment"])
|
|
|
|
employment._set_by_field(
|
|
|
|
addressbook.Person.Employment.schema.fields["unemployed"], None
|
|
|
|
)
|
2014-09-03 15:20:26 -07:00
|
|
|
|
|
|
|
addresses.write(file)
|
|
|
|
|
|
|
|
def printAddressBook(file):
|
|
|
|
addresses = addressbook.AddressBook.read(file)
|
|
|
|
address_fields = addressbook.AddressBook.schema.fields
|
|
|
|
person_fields = addressbook.Person.schema.fields
|
|
|
|
phone_fields = addressbook.Person.PhoneNumber.schema.fields
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addresses._get_by_field(address_fields["people"])
|
2014-09-03 15:20:26 -07:00
|
|
|
|
|
|
|
alice = people[0]
|
2021-10-01 11:00:22 -07:00
|
|
|
assert alice._get_by_field(person_fields["id"]) == 123
|
|
|
|
assert alice._get_by_field(person_fields["name"]) == "Alice"
|
|
|
|
assert alice._get_by_field(person_fields["email"]) == "alice@example.com"
|
|
|
|
alicePhones = alice._get_by_field(person_fields["phones"])
|
|
|
|
assert alicePhones[0]._get_by_field(phone_fields["number"]) == "555-1212"
|
|
|
|
assert alicePhones[0]._get_by_field(phone_fields["type"]) == "mobile"
|
|
|
|
employment = alice._get_by_field(person_fields["employment"])
|
|
|
|
employment._get_by_field(
|
|
|
|
addressbook.Person.Employment.schema.fields["school"]
|
|
|
|
) == "MIT"
|
2014-09-03 15:20:26 -07:00
|
|
|
|
|
|
|
bob = people[1]
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bob._get_by_field(person_fields["id"]) == 456
|
|
|
|
assert bob._get_by_field(person_fields["name"]) == "Bob"
|
|
|
|
assert bob._get_by_field(person_fields["email"]) == "bob@example.com"
|
|
|
|
bobPhones = bob._get_by_field(person_fields["phones"])
|
|
|
|
assert bobPhones[0]._get_by_field(phone_fields["number"]) == "555-4567"
|
|
|
|
assert bobPhones[0]._get_by_field(phone_fields["type"]) == "home"
|
|
|
|
assert bobPhones[1]._get_by_field(phone_fields["number"]) == "555-7654"
|
|
|
|
assert bobPhones[1]._get_by_field(phone_fields["type"]) == "work"
|
|
|
|
employment = bob._get_by_field(person_fields["employment"])
|
|
|
|
employment._get_by_field(
|
|
|
|
addressbook.Person.Employment.schema.fields["unemployed"]
|
|
|
|
) is None
|
|
|
|
|
|
|
|
f = open("example", "w")
|
2014-09-03 15:20:26 -07:00
|
|
|
writeAddressBook(f)
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open("example", "r")
|
2014-09-03 15:20:26 -07:00
|
|
|
printAddressBook(f)
|
2013-09-01 02:13:19 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def all_types():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "all_types.capnp"))
|
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
# TODO: These tests should be extended to:
|
|
|
|
# - Read each field in Python and assert that it is equal to the expected value.
|
|
|
|
# - Build an identical message using Python code and compare it to the golden.
|
|
|
|
#
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def init_all_types(builder):
|
|
|
|
builder.voidField = None
|
|
|
|
builder.boolField = True
|
|
|
|
builder.int8Field = -123
|
|
|
|
builder.int16Field = -12345
|
|
|
|
builder.int32Field = -12345678
|
|
|
|
builder.int64Field = -123456789012345
|
|
|
|
builder.uInt8Field = 234
|
|
|
|
builder.uInt16Field = 45678
|
|
|
|
builder.uInt32Field = 3456789012
|
|
|
|
builder.uInt64Field = 12345678901234567890
|
|
|
|
builder.float32Field = 1234.5
|
|
|
|
builder.float64Field = -123e45
|
|
|
|
builder.textField = "foo"
|
2013-12-18 12:39:56 -08:00
|
|
|
builder.dataField = b"bar"
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
subBuilder = builder.structField
|
|
|
|
subBuilder.voidField = None
|
|
|
|
subBuilder.boolField = True
|
|
|
|
subBuilder.int8Field = -12
|
|
|
|
subBuilder.int16Field = 3456
|
|
|
|
subBuilder.int32Field = -78901234
|
|
|
|
subBuilder.int64Field = 56789012345678
|
|
|
|
subBuilder.uInt8Field = 90
|
|
|
|
subBuilder.uInt16Field = 1234
|
|
|
|
subBuilder.uInt32Field = 56789012
|
|
|
|
subBuilder.uInt64Field = 345678901234567890
|
|
|
|
subBuilder.float32Field = -1.25e-10
|
|
|
|
subBuilder.float64Field = 345
|
2019-10-19 00:22:59 -07:00
|
|
|
subBuilder.textField = "☃"
|
2013-12-18 12:39:56 -08:00
|
|
|
subBuilder.dataField = b"qux"
|
2013-08-31 16:44:54 -07:00
|
|
|
subSubBuilder = subBuilder.structField
|
|
|
|
subSubBuilder.textField = "nested"
|
|
|
|
subSubBuilder.structField.textField = "really nested"
|
|
|
|
subBuilder.enumField = "baz"
|
|
|
|
|
|
|
|
subBuilder.voidList = [None, None, None]
|
|
|
|
subBuilder.boolList = [False, True, False, True, True]
|
2021-10-01 11:00:22 -07:00
|
|
|
subBuilder.int8List = [12, -34, -0x80, 0x7F]
|
|
|
|
subBuilder.int16List = [1234, -5678, -0x8000, 0x7FFF]
|
|
|
|
subBuilder.int32List = [12345678, -90123456, -0x80000000, 0x7FFFFFFF]
|
|
|
|
subBuilder.int64List = [
|
|
|
|
123456789012345,
|
|
|
|
-678901234567890,
|
|
|
|
-0x8000000000000000,
|
|
|
|
0x7FFFFFFFFFFFFFFF,
|
|
|
|
]
|
|
|
|
subBuilder.uInt8List = [12, 34, 0, 0xFF]
|
|
|
|
subBuilder.uInt16List = [1234, 5678, 0, 0xFFFF]
|
|
|
|
subBuilder.uInt32List = [12345678, 90123456, 0, 0xFFFFFFFF]
|
|
|
|
subBuilder.uInt64List = [123456789012345, 678901234567890, 0, 0xFFFFFFFFFFFFFFFF]
|
2013-08-31 16:44:54 -07:00
|
|
|
subBuilder.float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37]
|
|
|
|
subBuilder.float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306]
|
|
|
|
subBuilder.textList = ["quux", "corge", "grault"]
|
2013-12-18 12:39:56 -08:00
|
|
|
subBuilder.dataList = [b"garply", b"waldo", b"fred"]
|
2021-10-01 11:00:22 -07:00
|
|
|
listBuilder = subBuilder.init("structList", 3)
|
2013-08-31 16:44:54 -07:00
|
|
|
listBuilder[0].textField = "x structlist 1"
|
|
|
|
listBuilder[1].textField = "x structlist 2"
|
|
|
|
listBuilder[2].textField = "x structlist 3"
|
|
|
|
subBuilder.enumList = ["qux", "bar", "grault"]
|
|
|
|
|
|
|
|
builder.enumField = "corge"
|
|
|
|
|
|
|
|
builder.init("voidList", 6)
|
|
|
|
builder.boolList = [True, False, False, True]
|
|
|
|
builder.int8List = [111, -111]
|
|
|
|
builder.int16List = [11111, -11111]
|
|
|
|
builder.int32List = [111111111, -111111111]
|
|
|
|
builder.int64List = [1111111111111111111, -1111111111111111111]
|
|
|
|
builder.uInt8List = [111, 222]
|
|
|
|
builder.uInt16List = [33333, 44444]
|
|
|
|
builder.uInt32List = [3333333333]
|
|
|
|
builder.uInt64List = [11111111111111111111]
|
|
|
|
builder.float32List = [5555.5, float("inf"), float("-inf"), float("nan")]
|
|
|
|
builder.float64List = [7777.75, float("inf"), float("-inf"), float("nan")]
|
|
|
|
builder.textList = ["plugh", "xyzzy", "thud"]
|
2013-12-18 12:39:56 -08:00
|
|
|
builder.dataList = [b"oops", b"exhausted", b"rfc3092"]
|
2021-10-01 11:00:22 -07:00
|
|
|
listBuilder = builder.init("structList", 3)
|
2013-08-31 16:44:54 -07:00
|
|
|
listBuilder[0].textField = "structlist 1"
|
|
|
|
listBuilder[1].textField = "structlist 2"
|
|
|
|
listBuilder[2].textField = "structlist 3"
|
|
|
|
builder.enumList = ["foo", "garply"]
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def assert_almost(float1, float2):
|
|
|
|
if float1 != float2:
|
|
|
|
assert abs((float1 - float2) / float1) < 0.00001
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def check_list(reader, expected):
|
|
|
|
assert len(reader) == len(expected)
|
2023-03-12 11:33:41 -07:00
|
|
|
for i, v in enumerate(expected):
|
2013-08-31 16:44:54 -07:00
|
|
|
if type(v) is float:
|
|
|
|
assert_almost(reader[i], v)
|
|
|
|
else:
|
|
|
|
assert reader[i] == v
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def check_all_types(reader):
|
2019-09-26 22:18:28 -07:00
|
|
|
assert reader.voidField is None
|
|
|
|
assert reader.boolField
|
2013-08-31 16:44:54 -07:00
|
|
|
assert reader.int8Field == -123
|
|
|
|
assert reader.int16Field == -12345
|
|
|
|
assert reader.int32Field == -12345678
|
|
|
|
assert reader.int64Field == -123456789012345
|
|
|
|
assert reader.uInt8Field == 234
|
|
|
|
assert reader.uInt16Field == 45678
|
|
|
|
assert reader.uInt32Field == 3456789012
|
|
|
|
assert reader.uInt64Field == 12345678901234567890
|
|
|
|
assert reader.float32Field == 1234.5
|
|
|
|
assert_almost(reader.float64Field, -123e45)
|
|
|
|
assert reader.textField == "foo"
|
2013-12-18 12:39:56 -08:00
|
|
|
assert reader.dataField == b"bar"
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
subReader = reader.structField
|
2019-09-26 22:18:28 -07:00
|
|
|
assert subReader.voidField is None
|
|
|
|
assert subReader.boolField
|
2013-08-31 16:44:54 -07:00
|
|
|
assert subReader.int8Field == -12
|
|
|
|
assert subReader.int16Field == 3456
|
|
|
|
assert subReader.int32Field == -78901234
|
|
|
|
assert subReader.int64Field == 56789012345678
|
|
|
|
assert subReader.uInt8Field == 90
|
|
|
|
assert subReader.uInt16Field == 1234
|
|
|
|
assert subReader.uInt32Field == 56789012
|
|
|
|
assert subReader.uInt64Field == 345678901234567890
|
|
|
|
assert_almost(subReader.float32Field, -1.25e-10)
|
|
|
|
assert subReader.float64Field == 345
|
2016-03-03 14:29:32 -08:00
|
|
|
|
|
|
|
assert subReader.textField == "☃"
|
|
|
|
# This assertion highlights the encoding we expect to see here, since
|
|
|
|
# otherwise this appears a bit magical...
|
|
|
|
if EXPECT_BYTES:
|
|
|
|
assert len(subReader.textField) == 3
|
|
|
|
else:
|
|
|
|
assert len(subReader.textField) == 1
|
|
|
|
|
2013-12-18 12:39:56 -08:00
|
|
|
assert subReader.dataField == b"qux"
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
subSubReader = subReader.structField
|
|
|
|
assert subSubReader.textField == "nested"
|
|
|
|
assert subSubReader.structField.textField == "really nested"
|
|
|
|
|
|
|
|
assert subReader.enumField == "baz"
|
2016-12-05 22:27:40 -05:00
|
|
|
# Check that enums are hashable and can be used as keys in dicts
|
|
|
|
# interchangably with their string version.
|
2021-10-01 11:00:22 -07:00
|
|
|
assert hash(subReader.enumField) == hash("baz")
|
2016-12-05 22:27:40 -05:00
|
|
|
assert {subReader.enumField: 17}.get(subReader.enumField) == 17
|
2021-10-01 11:00:22 -07:00
|
|
|
assert {subReader.enumField: 17}.get("baz") == 17
|
|
|
|
assert {"baz": 17}.get(subReader.enumField) == 17
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
check_list(subReader.voidList, [None, None, None])
|
|
|
|
check_list(subReader.boolList, [False, True, False, True, True])
|
2021-10-01 11:00:22 -07:00
|
|
|
check_list(subReader.int8List, [12, -34, -0x80, 0x7F])
|
|
|
|
check_list(subReader.int16List, [1234, -5678, -0x8000, 0x7FFF])
|
|
|
|
check_list(subReader.int32List, [12345678, -90123456, -0x80000000, 0x7FFFFFFF])
|
|
|
|
check_list(
|
|
|
|
subReader.int64List,
|
|
|
|
[123456789012345, -678901234567890, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF],
|
|
|
|
)
|
|
|
|
check_list(subReader.uInt8List, [12, 34, 0, 0xFF])
|
|
|
|
check_list(subReader.uInt16List, [1234, 5678, 0, 0xFFFF])
|
|
|
|
check_list(subReader.uInt32List, [12345678, 90123456, 0, 0xFFFFFFFF])
|
|
|
|
check_list(
|
|
|
|
subReader.uInt64List, [123456789012345, 678901234567890, 0, 0xFFFFFFFFFFFFFFFF]
|
|
|
|
)
|
2013-08-31 16:44:54 -07:00
|
|
|
check_list(subReader.float32List, [0.0, 1234567.0, 1e37, -1e37, 1e-37, -1e-37])
|
2021-10-01 11:00:22 -07:00
|
|
|
check_list(
|
|
|
|
subReader.float64List, [0.0, 123456789012345.0, 1e306, -1e306, 1e-306, -1e-306]
|
|
|
|
)
|
2013-08-31 16:44:54 -07:00
|
|
|
check_list(subReader.textList, ["quux", "corge", "grault"])
|
2013-12-18 12:39:56 -08:00
|
|
|
check_list(subReader.dataList, [b"garply", b"waldo", b"fred"])
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
listReader = subReader.structList
|
|
|
|
assert len(listReader) == 3
|
|
|
|
assert listReader[0].textField == "x structlist 1"
|
|
|
|
assert listReader[1].textField == "x structlist 2"
|
|
|
|
assert listReader[2].textField == "x structlist 3"
|
|
|
|
|
|
|
|
check_list(subReader.enumList, ["qux", "bar", "grault"])
|
|
|
|
|
|
|
|
assert reader.enumField == "corge"
|
|
|
|
|
|
|
|
assert len(reader.voidList) == 6
|
|
|
|
check_list(reader.boolList, [True, False, False, True])
|
|
|
|
check_list(reader.int8List, [111, -111])
|
|
|
|
check_list(reader.int16List, [11111, -11111])
|
|
|
|
check_list(reader.int32List, [111111111, -111111111])
|
|
|
|
check_list(reader.int64List, [1111111111111111111, -1111111111111111111])
|
|
|
|
check_list(reader.uInt8List, [111, 222])
|
|
|
|
check_list(reader.uInt16List, [33333, 44444])
|
|
|
|
check_list(reader.uInt32List, [3333333333])
|
|
|
|
check_list(reader.uInt64List, [11111111111111111111])
|
|
|
|
|
|
|
|
listReader = reader.float32List
|
|
|
|
assert len(listReader) == 4
|
|
|
|
assert listReader[0] == 5555.5
|
|
|
|
assert listReader[1] == float("inf")
|
|
|
|
assert listReader[2] == -float("inf")
|
|
|
|
assert math.isnan(listReader[3])
|
|
|
|
|
|
|
|
listReader = reader.float64List
|
|
|
|
len(listReader) == 4
|
|
|
|
assert listReader[0] == 7777.75
|
|
|
|
assert listReader[1] == float("inf")
|
|
|
|
assert listReader[2] == -float("inf")
|
|
|
|
assert math.isnan(listReader[3])
|
|
|
|
|
|
|
|
check_list(reader.textList, ["plugh", "xyzzy", "thud"])
|
2013-12-18 12:39:56 -08:00
|
|
|
check_list(reader.dataList, [b"oops", b"exhausted", b"rfc3092"])
|
2013-08-31 16:44:54 -07:00
|
|
|
|
|
|
|
listReader = reader.structList
|
|
|
|
len(listReader) == 3
|
|
|
|
assert listReader[0].textField == "structlist 1"
|
|
|
|
assert listReader[1].textField == "structlist 2"
|
|
|
|
assert listReader[2].textField == "structlist 3"
|
|
|
|
|
|
|
|
check_list(reader.enumList, ["foo", "garply"])
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def test_build(all_types):
|
2013-09-01 20:10:57 -07:00
|
|
|
root = all_types.TestAllTypes.new_message()
|
2013-08-31 16:44:54 -07:00
|
|
|
init_all_types(root)
|
2021-10-01 11:00:22 -07:00
|
|
|
expectedText = open(
|
|
|
|
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
|
|
|
|
).read()
|
|
|
|
assert str(root) + "\n" == expectedText
|
2013-08-31 16:44:54 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2014-02-28 13:36:38 -08:00
|
|
|
def test_build_first_segment_size(all_types):
|
|
|
|
root = all_types.TestAllTypes.new_message(1)
|
|
|
|
init_all_types(root)
|
2021-10-01 11:00:22 -07:00
|
|
|
expectedText = open(
|
|
|
|
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
|
|
|
|
).read()
|
|
|
|
assert str(root) + "\n" == expectedText
|
2014-02-28 13:36:38 -08:00
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
root = all_types.TestAllTypes.new_message(1024 * 1024)
|
2014-02-28 13:36:38 -08:00
|
|
|
init_all_types(root)
|
2021-10-01 11:00:22 -07:00
|
|
|
expectedText = open(
|
|
|
|
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
|
|
|
|
).read()
|
|
|
|
assert str(root) + "\n" == expectedText
|
2014-02-28 13:36:38 -08:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def test_binary_read(all_types):
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open(os.path.join(this_dir, "all-types.binary"), "r", encoding="utf8")
|
2013-09-01 20:10:57 -07:00
|
|
|
root = all_types.TestAllTypes.read(f)
|
2013-08-31 16:44:54 -07:00
|
|
|
check_all_types(root)
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
expectedText = open(
|
|
|
|
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
|
|
|
|
).read()
|
|
|
|
assert str(root) + "\n" == expectedText
|
2013-08-31 16:44:54 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
# Test set_root().
|
|
|
|
builder = capnp._MallocMessageBuilder()
|
|
|
|
builder.set_root(root)
|
|
|
|
check_all_types(builder.get_root(all_types.TestAllTypes))
|
2013-08-31 17:07:44 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
builder2 = capnp._MallocMessageBuilder()
|
|
|
|
builder2.set_root(builder.get_root(all_types.TestAllTypes))
|
|
|
|
check_all_types(builder2.get_root(all_types.TestAllTypes))
|
2013-08-31 17:07:44 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 16:44:54 -07:00
|
|
|
def test_packed_read(all_types):
|
2021-10-01 11:00:22 -07:00
|
|
|
f = open(os.path.join(this_dir, "all-types.packed"), "r", encoding="utf8")
|
2013-09-01 20:10:57 -07:00
|
|
|
root = all_types.TestAllTypes.read_packed(f)
|
2013-08-31 16:44:54 -07:00
|
|
|
check_all_types(root)
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
expectedText = open(
|
|
|
|
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
|
|
|
|
).read()
|
|
|
|
assert str(root) + "\n" == expectedText
|
2013-08-31 18:19:02 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 18:19:02 -07:00
|
|
|
def test_binary_write(all_types):
|
2013-09-01 20:10:57 -07:00
|
|
|
root = all_types.TestAllTypes.new_message()
|
2013-08-31 18:19:02 -07:00
|
|
|
init_all_types(root)
|
2021-10-01 11:00:22 -07:00
|
|
|
root.write(open("example", "w"))
|
2013-08-31 18:19:02 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
check_all_types(all_types.TestAllTypes.read(open("example", "r")))
|
2013-08-31 18:19:02 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-31 18:19:02 -07:00
|
|
|
def test_packed_write(all_types):
|
2013-09-01 20:10:57 -07:00
|
|
|
root = all_types.TestAllTypes.new_message()
|
2013-08-31 18:19:02 -07:00
|
|
|
init_all_types(root)
|
2021-10-01 11:00:22 -07:00
|
|
|
root.write_packed(open("example", "w"))
|
2013-08-31 18:19:02 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
check_all_types(all_types.TestAllTypes.read_packed(open("example", "r")))
|