def test_load_field(): """Should fail if the saved fields are not the same as the loaded fields. """ lt = Layout() gssn_1 = Gaussian(channels=1, width=[5.0]) field_saver_1 = SaveField() gssn_2 = Gaussian(channels=1, width=[10.0]) field_saver_2 = SaveField() lt.add_links((gssn_1[0], field_saver_1[0]), (gssn_2[0], field_saver_2[0])) lt.run(gssn_1, gssn_2) fields = field_saver_1.fields + field_saver_2.fields lt_ = Layout() load_field = LoadField(fields=fields) lt.run(load_field) fields = load_field[0].fields assert (fields[0] == gssn_1[0].fields[0]) assert (fields[1] == gssn_2[0].fields[0]) assert_array_equal(fields[0].channels, gssn_1[0].fields[0].channels) assert_array_equal(fields[1].channels, gssn_2[0].fields[0].channels) assert_array_equal(fields[0].noise, gssn_1[0].fields[0].noise) assert_array_equal(fields[1].noise, gssn_2[0].fields[0].noise) assert_array_equal(fields[0].delays, gssn_1[0].fields[0].delays) assert_array_equal(fields[1].delays, gssn_2[0].fields[0].delays)
def test_non_existant_link_del(): """Should raise error if trying to delete a non-existing link.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') layout = Layout() layout.add_links((a[1], b[0]), (a[2], b[1])) layout.add_unidir_links((b[2], c[2]), (a[0], c[1])) layout.del_link(c[2], b[2]) # Valid (even if unidir in other dir.) # Testing pytest.raises(DelError, layout.del_links, (c[2], b[2])) pytest.raises(DelError, layout.del_links, (a[1], b[1])) pytest.raises(DelError, layout.del_links, (a[2], c[2]))
def test_leafs(): """Should be specific leafs for specific tree.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') d = IdealCoupler(name='d') e = IdealCoupler(name='e') f = IdealCoupler(name='f') layout = Layout() layout.add_links((a[0], b[0]), (a[1], c[0]), (b[1], d[0]), (b[2], e[0])) # Testing assert layout.get_leafs_of_comps([a, b, c, d, e, f]) == [c, d, e] assert layout.leafs == [c, d, e]
def test_degree(): """Should be of a specific degree for specific tree.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') d = IdealCoupler(name='d') e = IdealCoupler(name='e') f = IdealCoupler(name='f') layout = Layout() layout.add_links((a[0], b[0]), (a[1], c[0]), (b[1], d[0]), (b[2], e[0]), (c[1], a[2])) # Testing assert layout.get_degree(a) == 3 assert layout.get_degree(b) == 3 assert layout.get_degree(c) == 2 assert layout.get_degree(d) == 1 assert layout.get_degree(e) == 1 assert layout.get_degree(f) == 0
def test_wrong_pass_comp_ports(): """Should fail if the right error is not raised.""" # Environment creations class DummyPassComp(AbstractPassComp): def __init__(self): super().__init__('', '', [cst.OPTI_ALL, cst.OPTI_ALL], True) def __call__(self, domain, ports, fields): return ([5 for i in range(len(fields))], fields) start = Gaussian() pass_comp = DummyPassComp() a = IdealCoupler() layout = Layout() layout.add_links((start[0], pass_comp[0]), (pass_comp[1], a[0])) # Testing pytest.warns(WrongPortWarning, layout.run, start)
def test_save_field_to_file(): """Should fail if the saved fields are not the same as the loaded fields. """ file_name = 'temp_file_for_save_field_to_file_test.pk1' if (os.path.isfile(file_name)): print("Can not perfom test because a file named '{}' already exist." .format(file_name)) assert False else: lt = Layout() gssn_1 = Gaussian(channels=1, width=[5.0], save=True) field_saver_1 = SaveFieldToFile(file_name=file_name, add_fields=False) gssn_2 = Gaussian(channels=1, width=[10.0], save=True) field_saver_2 = SaveFieldToFile(file_name=file_name, add_fields=True) lt.add_links((gssn_1[0], field_saver_1[0]), (gssn_2[0], field_saver_2[0])) lt.run(gssn_1, gssn_2) lt_ = Layout() load_field = LoadFieldFromFile(file_name=file_name) lt_.run(load_field) fields = load_field[0].fields # Removing created file os.remove(file_name) # Tests assert (fields[0] == gssn_1[0].fields[0]) assert (fields[1] == gssn_2[0].fields[0]) assert_array_equal(fields[0].channels, gssn_1[0].fields[0].channels) assert_array_equal(fields[1].channels, gssn_2[0].fields[0].channels) assert_array_equal(fields[0].noise, gssn_1[0].fields[0].noise) assert_array_equal(fields[1].noise, gssn_2[0].fields[0].noise) assert_array_equal(fields[0].delays, gssn_1[0].fields[0].delays) assert_array_equal(fields[1].delays, gssn_2[0].fields[0].delays)