def test_display_tree(): tree = behavior.BehaviorTree('Consumption') assert tree._display_tree() == "\nConsumption: 0.000000 " assert tree._display_tree(1) == "\n Consumption: 0.000000 " tree = behavior.BehaviorTree('Consumption', units={'Consumption': 'g'}) assert tree._display_tree() == "\nConsumption: 0.000000 g" assert tree._display_tree(1) == "\n Consumption: 0.000000 g"
def test_tree_merge(): structure = ['Consumption', ['AS Prob', 'Intensity']] tree1 = behavior.BehaviorTree(structure, { 'AS Prob': 1, 'Consumption': 5, 'Intensity': 2 }) tree2 = behavior.BehaviorTree(structure, { 'AS Prob': 1, 'Consumption': -5, 'Intensity': 1.0 }) merged = behavior.BehaviorTree.merge(tree1, tree2) assert merged.structure == structure # convert to tuple (hashable) so dicts_equal doesn't error out for k in merged: merged[k] = tuple(merged[k]) assert dicts_equal(merged.contents, { 'AS Prob': (1, 1), 'Consumption': (5, -5), 'Intensity': (2, 1.0) }) with pytest.raises(TypeError) as excinfo: behavior.BehaviorTree.merge() assert excinfo.value.args[0] == 'Expected at least one argument'
def test_print_tree(): tree_no_structure = behavior.BehaviorTree(contents={'Consumption': 100}) assert str(tree_no_structure) == str({'Consumption': 100}) tree = behavior.BehaviorTree(['Consumption', ['AS Prob']], { 'Consumption': 100, 'AS Prob': 50.0 }) assert str(tree) == '\nConsumption: 100.000000 \n AS Prob: 50.000000 '
def test_tree_setitem(): tree_empty = behavior.BehaviorTree() tree_empty['a'] = 10 assert tree_empty['a'] == 10 tree_empty['b'] += 5 assert tree_empty['b'] == 5 tree_list = behavior.BehaviorTree(contents=defaultdict(list)) tree_list['a'].append(5) assert tree_list['a'] == [5]
def test_tree_getitem(): tree_empty = behavior.BehaviorTree() assert tree_empty['a'] == 0.0 tree2 = behavior.BehaviorTree(contents={'a': 5}) with pytest.raises(KeyError) as excinfo: tree2['b'] assert excinfo.value.args[0] == 'b' tree_list = behavior.BehaviorTree(contents=defaultdict(list)) assert tree_list['a'] == []
def test_tree_summarize(): structure = ['Consumption', ['AS Prob', 'Intensity']] tree1 = behavior.BehaviorTree(structure, { 'AS Prob': 1, 'Consumption': 5, 'Intensity': 2 }) tree2 = behavior.BehaviorTree(structure, { 'AS Prob': 1, 'Consumption': -5, 'Intensity': 1.0 }) merged = behavior.BehaviorTree.merge(tree1, tree2) summarized = merged.summarize(sum) assert summarized.structure == structure assert dicts_equal(summarized.contents, { 'AS Prob': 2, 'Consumption': 0, 'Intensity': 3.0 })
def test_init_tree(): tree = behavior.BehaviorTree(['Consumption', ['AS Prob', 'Intensity']]) tree['Consumption'] = 5 tree['AS Prob'] = 1 tree['Intensity'] = 2 assert dicts_equal( tree.contents, defaultdict(float, { 'AS Prob': 1, 'Consumption': 5, 'Intensity': 2 })) assert tree.structure == ['Consumption', ['AS Prob', 'Intensity']]
def test_tree_iter(): tree = behavior.BehaviorTree(contents={'Consumption': 100}) # should give back same set of keys assert set(iter(tree)) == set(iter(tree.contents))
def test_copy_tree(): tree = behavior.BehaviorTree(['Consumption'], {'Consumption': 100}) tree2 = tree.copy() assert tree.structure == tree2.structure assert dicts_equal(tree.contents, tree2.contents)