def test_bad_multi_output(self): with self.assertRaises(AssertionError) as cm: MultiOutput[None] self.assertEqual("MultiOutput must be of non-zero length.", str(cm.exception)) def bad_num_outputs() -> MultiOutput['s':str]: return 1, 2 system = FunctionSystem(bad_num_outputs) context = system.CreateDefaultContext() with self.assertRaises(AssertionError) as cm: system.GetOutputPort("s").Eval(context) self.assertEqual( "Output must be tuple of length 1. Got length 2 instead.", str(cm.exception)) def bad_output_type() -> MultiOutput['s':str]: return [1, 2] system = FunctionSystem(bad_output_type) context = system.CreateDefaultContext() with self.assertRaises(AssertionError) as cm: system.GetOutputPort("s").Eval(context) self.assertEqual( "Output type must be a tuple. Got <class 'list'> instead.", str(cm.exception))
def test_bad_multi_func(self): funcs = (times_two, times_two) with self.assertRaises(RuntimeError) as cm: FunctionSystem(funcs) self.assertIn( "System FunctionSystem[times_two, times_two] already has an " "input port named x", str(cm.exception))
def test_bad_function(self): def bad_function(good: int, bad, *worse, **worst): pass with self.assertRaises(RuntimeError) as cm: FunctionSystem(bad_function) self.assertIn("bad_function", str(cm.exception)) self.assertIn("Needs annotation: bad", str(cm.exception)) self.assertIn("Invalid kind: *worse", str(cm.exception)) self.assertIn("Invalid kind: **worst", str(cm.exception))
def test_bad_inferred_output(self): def bad_inferred_output(): nonlocal called called = True called = False with self.assertRaises(AssertionError) as cm: FunctionSystem(bad_inferred_output) self.assertIn( "For function 'bad_inferred_output': 'None' is not a valid", str(cm.exception)) self.assertTrue(called)
def add_systems(builder, dt, x_port, s_port, z_port): system = FunctionSystem(print_more) self.assertEqual(system.num_input_ports(), 1) self.assertEqual(system.num_output_ports(), 0) system.DeclarePeriodicPublish(dt) builder.AddSystem(system) builder.Connect(s_port, system.GetInputPort("s"))
def test_scalar_as_vector_disabled(self): def expects_scalar(z: float) -> float: print(z) return z + 3 builder = DiagramBuilder() system = builder.AddSystem( FunctionSystem(expects_scalar, scalar_as_vector=False)) value = builder.AddSystem(ConstantSource([1.])) with self.assertRaises(RuntimeError) as cm: builder.Connect(value.get_output_port(0), system.GetInputPort("z")) self.assertIn("Cannot mix vector-valued and abstract-valued", str(cm.exception))
def add_systems(builder, dt, x_port, s_port, z_port): funcs = (scalar_times_three, print_only) system = FunctionSystem(funcs) system.DeclarePeriodicPublish(dt) self.assertEqual(system.num_input_ports(), 1) self.assertEqual(system.num_output_ports(), 1) builder.AddSystem(system) builder.Connect(z_port, system.GetInputPort("z")) printer = builder.AddSystem(make_custom_printer(float, dt)) builder.Connect(system.GetOutputPort("output"), printer.get_input_port(0))
def add_systems(builder, dt, x_port, s_port, z_port): system = FunctionSystem(times_three) self.assertEqual(system.num_input_ports(), 1) self.assertEqual(system.num_output_ports(), 1) builder.AddSystem(system) builder.Connect(z_port, system.GetInputPort("z")) printer = builder.AddSystem(make_custom_printer(float, dt)) builder.Connect(system.get_output_port(0), printer.get_input_port(0))
def add_systems(builder, dt, x_port, s_port, z_port): system = FunctionSystem(multi_output) self.assertEqual(system.num_input_ports(), 2) self.assertEqual(system.num_output_ports(), 2) builder.AddSystem(system) builder.Connect(s_port, system.GetInputPort("s_in")) builder.Connect(x_port, system.GetInputPort("x_in")) printer = builder.AddSystem(make_custom_printer(str, dt)) builder.Connect(system.GetOutputPort("s_out"), printer.get_input_port(0)) printer = builder.AddSystem(make_custom_printer(VectorArg(3), dt)) builder.Connect(system.GetOutputPort("x_out"), printer.get_input_port(0))
def check(func, u): system = FunctionSystem(func) context = system.CreateDefaultContext() system.get_input_port(0).FixValue(context, u) return system.get_output_port(0).Eval(context)
def add_systems(builder, dt, x_port, s_port, z_port): system = FunctionSystem(print_time) self.assertEqual(system.num_input_ports(), 0) self.assertEqual(system.num_output_ports(), 0) system.DeclarePeriodicPublish(dt) builder.AddSystem(system)