import QuantityPredicate, DirectionalTestPredicate, LogicPredicate, Predicate import unittest, Direction class TruePredicate(Predicate.Predicate): def holds(self, dummy1, dummy2): return True class FalsePredicate(Predicate.Predicate): def holds(self, dummy1, dummy2): return False class d(object): def __init__(self, val): self.state = val class andunittest(unittest.TestCase): def testAND(self): d = [None, None] #d holds none for the dummy parameter t = TruePredicate() f = FalsePredicate() self.assertTrue(LogicPredicate.AndPredicate(t, t, t).holds(*d)) #true self.assertFalse(LogicPredicate.AndPredicate(f, f, f).holds(*d)) #false self.assertFalse(LogicPredicate.AndPredicate(f, t, f).holds(*d)) #false self.assertFalse(LogicPredicate.AndPredicate(t, t, t, t, t, f).holds(*d)) #false self.assertTrue(LogicPredicate.AndPredicate().holds(*d)) # true class orunittest(unittest.TestCase): def testOR(self): t = TruePredicate() f = FalsePredicate() d = [None, None] #another dummy list of None self.assertTrue( LogicPredicate.OrPredicate(t, t, t).holds(*d)) #true self.assertFalse(LogicPredicate.OrPredicate(f, f, f).holds(*d)) #false self.assertTrue(LogicPredicate.OrPredicate(f, t, f).holds(*d)) #true self.assertTrue(LogicPredicate.OrPredicate(t, t, t, t, t, f).holds(*d)) #true self.assertFalse(LogicPredicate.OrPredicate().holds(*d)) #false class notunittest(unittest.TestCase): def testNOT(self): t = TruePredicate() f = FalsePredicate() d = [None, None] #another dummy list of None self.assertTrue(LogicPredicate.NotPredicate(f).holds(*d)) self.assertFalse(LogicPredicate.NotPredicate(t).holds(*d)) self.assertRaises(Exception, \ LogicPredicate.NotPredicate(None).holds,*d) class otherPredicateTest(unittest.TestCase): def setUp(self): d1 = d(1) d2 = d(2) d3 = d(3) self.board1 = [[d1, d2, d3], [d1, d2, d3], [d1, d2, d3]] self.board2 = [[d3, None, None], [None, d3, d2], [d2, None, d2]] self.ds1 = {1:3, 2:2, 3:3} self.ds2 = {1:0, 2:3, 3:1} self.dsa = {Direction.NW:1, Direction.N:1, Direction.NE:1, \ Direction.E:2, Direction.SE:3, Direction.S:3, \ Direction.SW:3, Direction.W:2 \ } self.dsb = {Direction.NW:3, Direction.N:None, Direction.NE:None, \ Direction.E:2, Direction.SE:2, Direction.S:None, \ Direction.SW:None, Direction.W:3 \ } self.coords = (1, 1) class quantitypredicatetest(otherPredicateTest): def testboards(self): p = QuantityPredicate.QuantityPredicate(self.ds1) q = QuantityPredicate.QuantityPredicate(self.ds2) self.assertTrue(p.holds(self.board1, self.coords)) self.assertFalse(p.holds(self.board2, self.coords)) self.assertTrue(q.holds(self.board2, self.coords)) self.assertFalse(q.holds(self.board1, self.coords)) class directionalpredicatetest(otherPredicateTest): def setUp(self): super(directionalpredicatetest,self).setUp() self.p = DirectionalTestPredicate.DirectionalTestPredicate(self.dsa) self.q = DirectionalTestPredicate.DirectionalTestPredicate(self.dsb) def testboards(self): self.assertTrue(self.p.holds(self.board1, self.coords)) self.assertFalse(self.p.holds(self.board2, self.coords)) self.assertTrue(self.q.holds(self.board2, self.coords)) self.assertFalse(self.q.holds(self.board1, self.coords)) if __name__=="__main__": unittest.main() #!/glimmer/bin/python3 from zerolife import enum from zerolife.predicate import DirectionalTestPredicate, QuantityPredicate from zerolife.predicate import LogicPredicate def testToString(): board = None dsdirection = {enum.Direction.NW:"blue", enum.Direction.N:"green", enum.Direction.S:"no"} dsquantity = {"blue":2, "green":3} dtp1 = DirectionalTestPredicate.DirectionalTestPredicate(dsdirection) qp1 = QuantityPredicate.QuantityPredicate(dsquantity) print(dtp1) print(qp1) andPredicate = LogicPredicate.AndPredicate(dtp1, qp1) orPredicate = LogicPredicate.OrPredicate(dtp1, qp1) notPredicate = LogicPredicate.NotPredicate(orPredicate) print(andPredicate) print(orPredicate) print(notPredicate) if __name__=='__main__': testToString() ## Creature Project ## CSC323: Group 1: Nora, Terian, Josh, and Jordan ## 4/7/10 ## testclass.py: Contains general unit tests. from localworld import * class WorldTests: """Unit Tests for LocalWorld""" def __init__(self): pass def initTests(self): """Initialization tests for LocalWorld""" #Smoke Tests and Tests of various default creatures test1 = LocalWorld(5,7, "Nora") test2 = LocalWorld(1,1,"Dead") test3 = LocalWorld(4,4,"Simple") test4 = LocalWorld(3,2,"Bob") print (test1, test2, test3, test4) print("Expected weird stuff:") #Test of Negative and Boundary and Large Values test5 = LocalWorld(0,0,"Simple") test6 = LocalWorld(-3,4, "Dead") test7 = LocalWorld(-3,-3, "SamR") test8 = LocalWorld(10, 20000, "C") print (test5, test6, test7) #smoke test- square, rectangle, default, simple, none, dead #null values, negative/boundary valuest def creTests(self): """Tests get and set creature methods of LocalWorld""" world = LocalWorld(10, 24,"T") #please check the exception in setCreature bounds checking. print("Expected bounds error:") world.setCreature(489474,450594, "F") world.setCreature(4,5,"A") world.setCreature(0,0,"B") world.setCreature(9,23,"C") world.setCreature(9,5,"D") world.setCreature(3,23,"E") print("Expect to see: A B C D E") print(world.getCreature(4,5)) print(world.getCreature(0,0)) print(world.getCreature(9,23)) print(world.getCreature(9,5)) print(world.getCreature(3,23)) ## CHECK BOUNDS ERROR PRINTING PROBLEM print("Expected bounds error:") print(world.getCreature(43907,3848)) #setCreature:off grid on grid- edge, middle default, simple #creature getCreature: off grid, on gri default and changed def runTests(self): """Runs unit tests for all of LocalWorld's methods""" print("Running initalization tests...") WorldTests().initTests() print("Initalization test finished") print("Running creatures tests...") WorldTests().creTests() print("All Tests finished.") w = WorldTests() w.runTests() """Test plan: Unit Tests for Local World: toString and fromString: has correct form- can be recreated with to/from methods __expandworld: expand by negative, 0, square, rectangle. getCreature from these points Unit Tests for each method: Unit Tests for Creature: Unit Tests for Species: """