# Copyright (c) 2007 Tim Freeman # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # (This is the standard MIT License, copied from # http://www.opensource.org/licenses/mit-license.php on 24 Apr 2007.) from plan_generators import one_action_plan, dict_to_plan, exhaustive, \ list_to_plan from test_util import assert_equals, expect_exception # Testing one_action_plan. act = (1,1) oap = one_action_plan(act) oap2 = oap.step((3,7)) assert_equals(oap2[0], act) oap = oap2[1] oap2 = oap.step((17,19)) assert_equals(oap2[0], act) # Testing dict_to_plan # ...for the empty plan. empty = dict_to_plan(None) chatter = str(expect_exception(lambda:empty.step("hi"))) assert "hi" in chatter, "invalid chatter %r" % (chatter,) assert "react" in chatter, "invalid chatter %r" % (chatter,) # ...for a more interesting plan. plan = dict_to_plan({"hungry":("eat",{"sleepy":("sleep", None), "hungry":("suffer", None)}), "sleepy":("suffer", None)}) s = str(plan) assert "hungry" in s assert "sleepy" in s assert "dict_to_plan" in s assert s == repr(plan) p = plan.step("hungry") assert_equals(p[0], "eat") p2 = p[1].step("sleepy") assert_equals(p2[0], "sleep") assert "react" in str(expect_exception(lambda:p2[1].step("hungry"))) p2 = p[1].step("hungry") assert_equals(p2[0], "suffer") gripe = str(expect_exception(lambda:plan.step("blue"))) assert "blue" in gripe assert "No plan" in gripe # Testing exhaustive plan_generator def lentest(perceptions_length, behaviors_length, horizon): return len(exhaustive(perceptions=range(perceptions_length), behaviors=range(behaviors_length), horizon=horizon)) assert_equals(lentest(4,2,1), 16) assert_equals(lentest(2,2,1), 4) assert_equals(lentest(2,2,2), 64) behaviors = ["b1","b2"] l = exhaustive(perceptions=["p1","p2"], behaviors=behaviors, horizon=2) randomplan = l[0] randomplan.assert_has_depth(2) expect_exception(lambda:randomplan.assert_has_depth(1)) step1 = randomplan.step("p1") assert step1[0] in behaviors step2 = step1[1].step("p2") assert step2[0] in behaviors lastplan = step2[1] lastplan.assert_has_depth(0) expect_exception(lambda:lastplan.assert_has_depth(1)) gripe = str(expect_exception(lambda:lastplan.step("p1"))) assert "too short" in gripe assert "p1" in gripe # Testing list_to_plan p = list_to_plan(1,2,3) assert_equals(str(p), "list_to_plan(1,2,3)") p.assert_has_depth(3) assert_equals(p.step("doesn't matter")[0], 1) p = list_to_plan(3) assert_equals(p, list_to_plan(3)) assert p != list_to_plan(3, 4) p = p.step("x")[1] assert "Walked off the end" in str(expect_exception(lambda:p.step("y")))