def test_write_subregion_simple(self, ks): """A simple test that ensures the appropriate keyspace data is written out.""" # Create a simple keyspace and some instances of it keyspaces = [ks(x=1, y=1, p=31), ks(x=3, y=7, p=2), ] # Add two field instances, one to get the routing key the other to get # the mask. kf = KeyField(maps={'c': 'c'}) mf = MaskField(tag='routing') # Create the region r = KeyspacesRegion(keyspaces, fields=[kf, mf], prepend_num_keyspaces=True) # Write out the region, then check that the data corresponds to what # we'd expect. fp = tempfile.TemporaryFile() r.write_subregion_to_file(fp, slice(0, 10), c=5) fp.seek(0) assert fp.read(4) == b'\x02\x00\x00\x00' # Number of keyspaces assert fp.read() == struct.pack('4I', keyspaces[0](c=5).get_value(), keyspaces[0].get_mask(tag='routing'), keyspaces[1](c=5).get_value(), keyspaces[1].get_mask(tag='routing'))
def test_sizeof_with_prepends(self): r = KeyspacesRegion( [(Signal(BitField(32)), {})], fields=[], prepend_num_keyspaces=True ) assert r.sizeof(slice(None)) == 4
def test_write_subregion_simple(self, ks): """A simple test that ensures the appropriate keyspace data is written out.""" # Create a simple keyspace and some instances of it keyspaces = [ (Signal(ks), dict(x=1, y=1, p=31)), (Signal(ks(x=3, y=7, p=2)), {}), ] # Add two field instances, one to get the routing key the other to get # the mask. kf = KeyField(maps={'c': 'c'}) mf = MaskField(tag='routing') # Create the region r = KeyspacesRegion(keyspaces, fields=[kf, mf], prepend_num_keyspaces=True) # Write out the region, then check that the data corresponds to what # we'd expect. fp = tempfile.TemporaryFile() r.write_subregion_to_file(fp, slice(0, 10), c=5) expected_ks = [s.keyspace(**kw) for s, kw in keyspaces] fp.seek(0) assert fp.read(4) == b'\x02\x00\x00\x00' # Number of keyspaces assert fp.read() == struct.pack('4I', expected_ks[0](c=5).get_value(), expected_ks[0].get_mask(tag='routing'), expected_ks[1](c=5).get_value(), expected_ks[1].get_mask(tag='routing'))
def test_sizeof_partitioned(self): r = KeyspacesRegion([(Signal(BitField(32)), {})] * 4, fields=[mock.Mock()], partitioned_by_atom=True, prepend_num_keyspaces=False) assert r.sizeof(slice(1, 2)) == 4 assert r.sizeof(slice(2, 4)) == 8
def test_sizeof_partitioned(self): r = KeyspacesRegion( [(Signal(BitField(32)), {})]*4, fields=[mock.Mock()], partitioned_by_atom=True, prepend_num_keyspaces=False ) assert r.sizeof(slice(1, 2)) == 4 assert r.sizeof(slice(2, 4)) == 8
def test_sizeof_no_prepends(self, key_bits, n_keys, n_fields, partitioned, vertex_slice): # Generate the list of keys, prepends and fields keys = [BitField(key_bits) for _ in range(n_keys)] fields = [mock.Mock() for _ in range(n_fields)] # Create the region r = KeyspacesRegion(keys, fields, partitioned) # Determine the size n_atoms = (n_keys if not partitioned else vertex_slice.stop - vertex_slice.start) assert r.sizeof(vertex_slice) == n_atoms * n_fields * 4
def test_sizeof_no_prepends(self, key_bits, n_keys, n_fields, partitioned, vertex_slice): # Generate the list of keys, prepends and fields keys = [(Signal(BitField(key_bits)), {}) for _ in range(n_keys)] fields = [mock.Mock() for _ in range(n_fields)] # Create the region r = KeyspacesRegion(keys, fields, partitioned) # Determine the size n_atoms = (n_keys if not partitioned else vertex_slice.stop - vertex_slice.start) assert r.sizeof(vertex_slice) == n_atoms * n_fields * 4
def test_write_subregion_calls_fields(self): """Check that writing a subregion to file calls the field functions with each key and that any extra arguments are passed along. """ # Create some keyspaces keys = [(Signal(BitField(32)), {}) for _ in range(10)] # Create two fields fields = [mock.Mock() for _ in range(2)] fields[0].return_value = 0 fields[1].return_value = 0 # Create an UNPARTITIONED region and write out a slice, check that # field methods were called with EACH key and the kwargs. r = KeyspacesRegion(keys, fields) fp = tempfile.TemporaryFile() kwargs = {"spam": "and eggs", "green_eggs": "and ham"} r.write_subregion_to_file(fp, slice(0, 1), **kwargs) for f in fields: f.assert_has_calls( [mock.call(k.keyspace, **kwargs) for k, _ in keys]) f.reset_mock() # Create a PARTITIONED region and write out a slice, check that # field methods were called with EACH key IN THE SLICE and the kwargs. r = KeyspacesRegion(keys, fields, partitioned_by_atom=True) for sl in (slice(0, 1), slice(2, 5)): fp = tempfile.TemporaryFile() kwargs = {"spam": "spam spam spam", "in_a_box": "with a fox"} r.write_subregion_to_file(fp, sl, **kwargs) for f in fields: f.assert_has_calls( [mock.call(k.keyspace, **kwargs) for k, _ in keys[sl]]) f.reset_mock()
def test_write_subregion_calls_fields(self): """Check that writing a subregion to file calls the field functions with each key and that any extra arguments are passed along. """ # Create some keyspaces keys = [(Signal(BitField(32)), {}) for _ in range(10)] # Create two fields fields = [mock.Mock() for _ in range(2)] fields[0].return_value = 0 fields[1].return_value = 0 # Create an UNPARTITIONED region and write out a slice, check that # field methods were called with EACH key and the kwargs. r = KeyspacesRegion(keys, fields) fp = tempfile.TemporaryFile() kwargs = {"spam": "and eggs", "green_eggs": "and ham"} r.write_subregion_to_file(fp, slice(0, 1), **kwargs) for f in fields: f.assert_has_calls([mock.call(k.keyspace, **kwargs) for k, _ in keys]) f.reset_mock() # Create a PARTITIONED region and write out a slice, check that # field methods were called with EACH key IN THE SLICE and the kwargs. r = KeyspacesRegion(keys, fields, partitioned_by_atom=True) for sl in (slice(0, 1), slice(2, 5)): fp = tempfile.TemporaryFile() kwargs = {"spam": "spam spam spam", "in_a_box": "with a fox"} r.write_subregion_to_file(fp, sl, **kwargs) for f in fields: f.assert_has_calls([mock.call(k.keyspace, **kwargs) for k, _ in keys[sl]]) f.reset_mock()
def test_sizeof_with_prepends(self): r = KeyspacesRegion([(Signal(BitField(32)), {})], fields=[], prepend_num_keyspaces=True) assert r.sizeof(slice(None)) == 4