Best Python code snippet using Testify_python
WorldTestCases.py
Source:WorldTestCases.py  
...115        ship = AIShip_SetList("Free", start, self.game, [])116        time.sleep(2.0)117        self.game.world.causeExplosion(self.game.world.mid_point(0, 0), 200, 1000)118        time.sleep(3.0)119        self.failIfAlmostEqual(ship.body.position, start, None, "Free Ship in same place", 2)120    """121    def test_explosion_force_amount(self):122        123        #Tests that different radii have impact on ship movement.124        125        start = self.game.world.mid_point(-40, -100)126        ship = AIShip_SetList("Far", start, self.game, [])127        start2 = self.game.world.mid_point(40, -50)128        ship2 = AIShip_SetList("Close", start2, self.game, [])129        time.sleep(2.0)130        self.game.world.causeExplosion(self.game.world.mid_point(0, 0), 150, 50)131        time.sleep(3.0)132        self.failIfAlmostEqual(ship.body.position, start, None, "Far Ship in same place", 2)133        self.failIfAlmostEqual(ship2.body.position, start2, None, "Close Ship in same place", 2)134        self.assertGreater(ship2.body.velocity.length, ship.body.velocity.length, "Ship Close should be traveling faster than Ship Far by being closer to blast radius.")135        self.assertGreater(abs(ship.body.position[0] - ship2.body.position[0]), abs(start[0] - start2[0]), "Ships Horizontal Positions have not diverged.")136        self.assertGreater(abs(ship2.body.position - start2), abs(ship.body.position - start), "Ship Close Should be Farther From its Start than Ship Far.")137    """138class WorldVisualShipRespawnTestCase(SBAGUITestCase):139    def test_ship_ram_planet(self):140        """141        Tests a ship raming into a planet should take damange.142        """143        planet = Planet(self.game.world.mid_point(0,0))144        self.game.world.append(planet)145        start = self.game.world.mid_point(300, 0)146        ship = AIShip_SetList("Doomed", start, self.game, [147                "RotateCommand(self, 180)",148                "ThrustCommand(self, 'B', 6.0, 1.0)"149            ])150        health = ship.health.value151        start2 = self.game.world.mid_point(-50, 250)152        ship2 = AIShip_SetList("Free", start2, self.game, [])153        time.sleep(15.0)154        self.failIfAlmostEqual(ship.body.position, start, None, "Doomed ship should have moved", 5)155        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)156        self.failIfAlmostEqual(ship.health.value, health, None, "Doomed ship should have taken damage", 5)157        #self.assertAlmostEqual(planet.body.position, self.game.world.mid_point(0, 0), None, "Planet shouldn't move when hit", 1)158    def test_ship_ram_planet_destroyed(self):159        """160        Tests a ship raming into a planet should take damange.161        """162        planet = Planet(self.game.world.mid_point(0,0))163        self.game.world.append(planet)164        start = self.game.world.mid_point(300, 0)165        ship = AIShip_SetList("Doomed", start, self.game, [166                "RotateCommand(self, 180)",167                "ThrustCommand(self, 'B', 6.0, 1.0)"168            ])169        ship.health /= 2170        health = ship.health.value171        start2 = self.game.world.mid_point(-50, 250)172        ship2 = AIShip_SetList("Free", start2, self.game, [])173        time.sleep(15.0)174        self.failIfAlmostEqual(ship.body.position, start, None, "Doomed ship should have moved", 5)175        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)176        self.failIfAlmostEqual(ship.health.value, health, None, "Doomed ship should have taken damage", 5)177        self.assertEqual(ship.killedby, planet, "Planet not set as killer")178        #self.assertTrue(ship.destroyed, "Doomed not marked destroyed") # Issue with _game_add_ship_for_player dealing with AI_Ships, moving it and resetting... boo179    def test_planet_gravity(self):180        """181        Tests a ship inside a gravity well vs outside.182        """183        planet = BlackHole(self.game.world.mid_point(0,0), 100, 100)184        self.game.world.append(planet)185        start = self.game.world.mid_point(50, -50)186        ship = AIShip_SetList("Doomed", start, self.game, [])        187        start2 = self.game.world.mid_point(-50, 250)188        ship2 = AIShip_SetList("Free", start2, self.game, [])        189        time.sleep(5.0)190        self.failIfAlmostEqual(ship.body.position, start, None, "Doomed ship should have moved", 5)191        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)192    def test_planet_no_gravity(self):193        """194        Tests a ship inside a gravity well vs outside.195        """196        planet = BlackHole(self.game.world.mid_point(0,0), 100, 0)197        self.game.world.append(planet)198        start = self.game.world.mid_point(50, -50)199        ship = AIShip_SetList("Not Doomed", start, self.game, [])        200        time.sleep(5.0)201        self.assertAlmostEqual(ship.body.position, start, None, "Doomed ship shouldn't have moved", 2)202    def test_planet_torpedo_no_gravity(self):203        """204        Tests torpedos aren't effected by planet's gravity.205        """206        planet = Planet(self.game.world.mid_point(0,0), 256, 100)207        self.game.world.append(planet)208        start = self.game.world.mid_point(0, -100)209        ship = AIShip_SetList("Bully", start, self.game, [210                "RotateCommand(self, 170)",211                "FireTorpedoCommand(self, 'F')",212                "IdleCommand(self, 1.0)",213                "FireTorpedoCommand(self, 'F')",214                "IdleCommand(self, 1.0)",215                "FireTorpedoCommand(self, 'F')",216                "IdleCommand(self, 1.0)",217                "FireTorpedoCommand(self, 'F')",218                "IdleCommand(self, 1.0)",219                "FireTorpedoCommand(self, 'F')",220                "IdleCommand(self, 1.0)",221            ])222        start2 = self.game.world.mid_point(-100, 50)223        ship2 = AIShip_SetList("Doomed", start2, self.game, [])224        self.assertEqual(ship.health.value, ship.health.maximum, "Ship Bully not at full health.")225        self.assertEqual(ship2.health.value, ship2.health.maximum, "Ship Doomed not at full health.")226        time.sleep(12.0)227        self.assertTrue(ship in self.game.world, "Bully Ship missing")228        self.assertTrue(ship2 in self.game.world, "Doomed Ship missing")229        self.assertGreater(ship.health.value, ship.health.maximum / 2, "Ship Bully not at 'full' health.")230        self.assertGreater(ship2.health.value, ship2.health.maximum / 3, "Ship Doomed not at 'full' health.")231        self.assertEqual(len(self.game.world), 3, "Not all objects still in world")232    233    def test_planet_torpedo_gravity(self):234        """235        Tests torpedos are effected by planet's gravity.236        """237        planet = Planet(self.game.world.mid_point(0,0), 256, 100, torpedo = True)238        self.game.world.append(planet)239        start = self.game.world.mid_point(0, -100)240        ship = AIShip_SetList("Bully", start, self.game, [241                "RotateCommand(self, 170)",242                "FireTorpedoCommand(self, 'F')",243                "IdleCommand(self, 1.0)",244                "FireTorpedoCommand(self, 'F')",245                "IdleCommand(self, 1.0)",246                "FireTorpedoCommand(self, 'F')",247                "IdleCommand(self, 1.0)",248                "FireTorpedoCommand(self, 'F')",249                "IdleCommand(self, 1.0)",250                "FireTorpedoCommand(self, 'F')",251                "IdleCommand(self, 1.0)",252            ])253        start2 = self.game.world.mid_point(-100, 50)254        ship2 = AIShip_SetList("Doomed", start2, self.game, [])255        self.assertEqual(ship.health.value, ship.health.maximum, "Ship Bully not at full health.")256        self.assertEqual(ship2.health.value, ship2.health.maximum, "Ship Doomed not at full health.")257        time.sleep(6.0)258        self.assertGreater(ship.health.value, ship.health.maximum / 2, "Ship Bully not at 'full' health.")259        #self.assertLess(ship2.health.value, ship2.health.maximum / 3, "Ship Doomed at 'full' health.")260        self.assertTrue(ship in self.game.world, "Bully Ship missing")261        #self.assertFalse(ship2 in self.game.world, "Doomed Ship not destroyed") # BUGBUG: AI setup overwriting this, should redo how AI-Ships 'respawn'?262        print ship2.killedby263        self.assertIsNotNone(ship2.killedby, "Ship Killed By Not Set.")264        self.assertTrue(isinstance(ship2.killedby, Torpedo), "Ship not marked as destroyed by Torpedo.")265        self.assertEqual(ship2.killedby.owner, ship, "Torpedo was not marked as belonging to Ship Bully.")266        time.sleep(6.0)267        # should be 2, but other AI-ship sticks around...?268        self.assertEqual(len(self.game.world), 3, "Not all objects still in world")269    def test_nebula_drag(self):270        """271        Test which has two ships thrust in the same direction, but one slowed by Nebula.272        """273        neb = Nebula(self.game.world.mid_point(100, -160), (384,256))274        self.game.world.append(neb)275        ship = AIShip_SetList("Nebula", self.game.world.mid_point(-100, -100), self.game, [276            "ThrustCommand(self, 'B', 7.0)",277        ])        278        ship2 = AIShip_SetList("Free", self.game.world.mid_point(-100, 100), self.game, [279            "ThrustCommand(self, 'B', 7.0)",280        ])        281        time.sleep(8.0)282        print ship2.body.position[0], " vs ", ship.body.position[0]283        self.failIfAlmostEqual(ship2.body.position[0], ship.body.position[0], None, "Ship Didn't get Slowed Down By Nebula", 15)284    def test_energy_scoop(self):285        """286        Test two ships, one in Nebula and using Energy Scoop.287        """288        neb = Nebula(self.game.world.mid_point(100, -160), (384,256))289        self.game.world.append(neb)290        ship = AIShip_SetList("Nebula", self.game.world.mid_point(-100, -100), self.game, [291            "ThrustCommand(self, 'B', 7.0)",292            "IdleCommand(self, 2.0)",293            "LowerEnergyScoopCommand(self, 1)",294        ])        295        ship2 = AIShip_SetList("Free", self.game.world.mid_point(-100, 100), self.game, [296            "ThrustCommand(self, 'B', 7.0)",297        ])298        299        ship.energy -= 50300        ship2.energy -= 50301        time.sleep(9.0)302        print ship2.body.position[0], " vs ", ship.body.position[0]303        self.failIfAlmostEqual(ship2.body.position[0], ship.body.position[0], None, "Ship Didn't get Slowed Down By Nebula", 15)304        self.assertGreater(ship.energy.value, ship2.energy.value, "Ship didn't get extra energy")305        self.assertAlmostEqual(ship.energy.value, 100, None, "Ship didn't regain all energy", 5)306    def test_ship_shoot_dragon(self):307        """308        Tests that a dragon can take damage from torpedos309        """310        ship = AIShip_SetList("Shooter", self.game.world.mid_point(-100), self.game, [311                "IdleCommand(self, 2)",312                "FireTorpedoCommand(self, 'F')",313                "IdleCommand(self, 0.1)",314                "FireTorpedoCommand(self, 'F')",315                "IdleCommand(self, 0.1)",316                "FireTorpedoCommand(self, 'F')",317                "IdleCommand(self, 0.1)",318                "FireTorpedoCommand(self, 'F')",319                "IdleCommand(self, 0.1)",320                "FireTorpedoCommand(self, 'F')",321                "IdleCommand(self, 0.1)",322                "FireTorpedoCommand(self, 'F')",323            ])324        dragon = Dragon(self.game.world.mid_point(100), 16, 0)325        dragon.body.velocity = Vec2d(0, 0)326        h = dragon.health.value327        self.game.world.append(dragon)328        time.sleep(0.2)329        self.assertEqual(len(self.game.world), 2, "Found more than two objects in world")330        time.sleep(4)331        self.assertLess(dragon.health.value, h, "Dragon didn't take damage")332        #ensure ship is still there for testing ship timeout function doesn't effect AI ships333        self.assertTrue(ship in self.game.world, "Shooter Ship disappeared")334        time.sleep(2)335        self.assertTrue(ship in self.game.world, "Shooter Ship disappeared")336        self.assertFalse(dragon in self.game.world, "Dragon not destroyed")337class WorldVisualShipDestroyedTestCase(SBAGUITestCase):338    def get_config_filename(self):339        return "test_destroyed.cfg"340    def test_star_damage(self):341        """342        Tests a ship inside a star takes damage.343        """344        planet = Star(self.game.world.mid_point(0,0), 100, 100) # high pull = faster test for Star345        self.game.world.append(planet)346        start = self.game.world.mid_point(20, -20)347        ship = AIShip_SetList("Doomed", start, self.game, [])348        start2 = self.game.world.mid_point(-50, 250)349        ship2 = AIShip_SetList("Free", start2, self.game, [])350        time.sleep(1.0)351        self.failIfAlmostEqual(ship.body.position, start, None, "Doomed ship should have moved", 5)352        self.failIfEqual(ship.health, 100, "Doomed ship did not take damage")353        self.assertEqual(ship2.health, 100, "Free ship took damage")354        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)355        time.sleep(2.5)356        self.assertFalse(ship in self.game.world, "Doomed Ship not destroyed")357        print ship.killedby358        self.assertIsNotNone(ship.killedby, "Ship Killed By Not Set.")359        self.assertTrue(isinstance(ship.killedby, Star), "Ship not marked as destroyed by Star.")360    def test_star_damage_ship_destory_ship(self):361        """362        Tests a ship inside a star takes damage.363        """364        planet = Star(self.game.world.mid_point(0,0), 100, 100) # high pull = faster test for Star365        self.game.world.append(planet)366        start = self.game.world.mid_point(30, -20)367        ship = AIShip_SetList("Doomed", start, self.game, [])368        start2 = self.game.world.mid_point(-50, 250)369        ship2 = AIShip_SetList("Free", start2, self.game, [370                "RotateCommand(self, 70)",371                "FireTorpedoCommand(self, 'F')",372                "FireTorpedoCommand(self, 'F')",373                "FireTorpedoCommand(self, 'F')",374                "FireTorpedoCommand(self, 'F')",375        ])376        time.sleep(1.0)377        self.failIfAlmostEqual(ship.body.position, start, None, "Doomed ship should have moved", 5)378        self.failIfEqual(ship.health, 100, "Doomed ship did not take damage")379        self.assertEqual(ship2.health, 100, "Free ship took damage")380        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)381        time.sleep(2.5)382        self.assertFalse(ship in self.game.world, "Doomed Ship not destroyed")383        print ship.killedby384        self.assertIsNotNone(ship.killedby, "Ship Killed By Not Set.")385        self.assertTrue(isinstance(ship.killedby, Torpedo), "Ship not marked as destroyed by Torpedo.")386        self.assertEqual(ship.killedby.owner, ship2, "Torpedo was not marked as belonging to Ship.")387    def test_blackhole_crush(self):388        """389        Tests a ship being destroyed by a blackhole.390        """391        planet = BlackHole(self.game.world.mid_point(0,0), 100, 100)392        self.game.world.append(planet)393        start = self.game.world.mid_point(0, 0)394        ship = AIShip_SetList("Doomed", start, self.game, [])        395        start2 = self.game.world.mid_point(-50, 250)396        ship2 = AIShip_SetList("Free", start2, self.game, [])        397        time.sleep(1)398        #ensure ship is still there for testing ship timeout function doesn't effect AI ships399        self.assertTrue(ship in self.game.world, "Doomed Ship disappeared early")400        time.sleep(8.0)401        #ensure ship is still there for testing ship timeout function doesn't effect AI ships402        self.assertFalse(ship in self.game.world, "Doomed Ship not destroyed")403        #ensure ship is still there for testing ship timeout function doesn't effect AI ships404        self.assertTrue(ship2 in self.game.world, "Free Ship disappeared")405        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)406    def test_blackhole_crush_vs_shield(self):407        """408        Tests a ship not being destroyed by a blackhole if shields raised.409        """410        planet = BlackHole(self.game.world.mid_point(0,0), 100, 100)411        self.game.world.append(planet)412        start = self.game.world.mid_point(0, 0)413        ship = AIShip_SetList("Doomed", start, self.game, [414                "IdleCommand(self, 4.0)",415                "RaiseShieldsCommand(self, 3.0)",416            ])417        start2 = self.game.world.mid_point(-50, 250)418        ship2 = AIShip_SetList("Free", start2, self.game, [])        419        time.sleep(1)420        #ensure ship is still there for testing ship timeout function doesn't effect AI ships421        self.assertTrue(ship in self.game.world, "Doomed Ship disappeared early")422        time.sleep(6.0)423        # should be safe as shield up424        self.assertTrue(ship in self.game.world, "Doomed Ship disappeared to early destroyed")425        time.sleep(2.5) # wait for shield to expire, should be crushed immediately426        self.assertFalse(ship in self.game.world, "Doomed Ship not destroyed")427        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)428    def test_dragon_eats_ship(self):429        """430        Tests a dragon431        """432        start = self.game.world.mid_point(0,80)433        dragon = Dragon(start, 100, 20)434        dragon.body.velocity = Vec2d(0, -5)435        self.game.world.append(dragon)436        speed = dragon.body.velocity.length437        time.sleep(2.5)438        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 2)439        starts = self.game.world.mid_point(50, 0)440        ship = AIShip_SetList("Doomed", starts, self.game, [])        441        start2 = self.game.world.mid_point(-150, 250)442        ship2 = AIShip_SetList("Free", start2, self.game, [])        443        time.sleep(2.5)444        self.assertGreater(dragon.body.velocity.length, speed, "Dragon not moving faster")445        print dragon.body.velocity.angle_degrees446        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 5)447        self.assertAlmostEqual(dragon.body.velocity.angle_degrees, -50, None, "Dragon should be facing ship", 15)448        time.sleep(3.5)449        self.failIfEqual(ship.health, 100, "Doomed ship did not take damage")450        self.assertEqual(ship2.health, 100, "Free ship took damage")451        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)452        time.sleep(6.5)453        self.assertFalse(ship in self.game.world, "Doomed Ship not destroyed")454    def test_dragon_stopped(self):455        """456        Tests a dragon457        """458        start = self.game.world.mid_point(0,80)459        dragon = Dragon(start, 100, 20)460        dragon.body.velocity = Vec2d(0, 0)461        self.game.world.append(dragon)462        speed = dragon.body.velocity.length463        time.sleep(1.5)464        starts = self.game.world.mid_point(50, 0)465        ship = AIShip_SetList("Doomed", starts, self.game, [])        466        start2 = self.game.world.mid_point(-150, 250)467        ship2 = AIShip_SetList("Free", start2, self.game, [])        468        time.sleep(2.5)469        self.assertGreater(dragon.body.velocity.length, speed, "Dragon not moving faster")470        print dragon.body.velocity.angle_degrees471        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 5)472        self.assertAlmostEqual(dragon.body.velocity.angle_degrees, -50, None, "Dragon should be facing ship", 15)473        time.sleep(0.5)474    def test_dragon_vs_cloak(self):475        """476        Tests a dragon and his ability (or lack of) to see cloaked ships477        """478        start = self.game.world.mid_point(0,100)479        dragon = Dragon(start, 100, 20)480        dragon.body.velocity = Vec2d(0, -5)481        self.game.world.append(dragon)482        speed = dragon.body.velocity.length483        direction = dragon.body.velocity.angle_degrees484        time.sleep(2.5)485        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 2)486        starts = self.game.world.mid_point(120, 0)487        ship = AIShip_SetList("Cloaked", starts, self.game, [488                "RotateCommand(self, 180)",489                "ThrustCommand(self, 'B', 3.0, 1.0)",490                "CloakCommand(self, 5.0)",491            ])492        start2 = self.game.world.mid_point(-150, 250)493        ship2 = AIShip_SetList("Free", start2, self.game, [])494        time.sleep(2.5)495        self.assertAlmostEqual(dragon.body.velocity.length, speed, None, "Dragon increased speed", 5)496        self.assertAlmostEqual(dragon.body.velocity.angle_degrees, direction, None, "Dragon changed direction", 5)497        print dragon.body.velocity.angle_degrees498        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 5)499        time.sleep(5)500        print dragon.body.velocity.angle_degrees501        self.assertGreater(dragon.body.velocity.length, speed, "Dragon should have increased speed")502        self.assertAlmostEqual(dragon.body.velocity.angle_degrees, -60, None, "Dragon should be facing ship", 15)503        time.sleep(2.5)504        self.failIfEqual(ship.health, 100, "Doomed ship did not take damage")505        self.assertEqual(ship2.health, 100, "Free ship took damage")506        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)507        time.sleep(7)508        self.assertFalse(ship in self.game.world, "Doomed Ship not destroyed")509    def test_dragon_vs_cloak_eating(self):510        """511        Tests a dragon and that he can't eat a cloaked ship512        """513        start = self.game.world.mid_point()514        dragon = Dragon(start, 100, 0)515        dragon.body.velocity = Vec2d(0, -0.05)516        self.game.world.append(dragon)517        time.sleep(0.5)518        #self.assertAlmostEqual(dragon.body.position[, start, None, "Dragon shouldn't have moved", 3)519        ship = AIShip_SetList("Cloaked", start, self.game, [520            "IdleCommand(self, 0.5)", # TODO: Looks like get callback before registration so player doesn't exist to play cloak sound?521            "CloakCommand(self, 5.0)",522            ])523        health = ship.health.value524        time.sleep(0.5)525        self.assertEqual(ship.health, health, "Ship lost health")526        time.sleep(3.5)527        self.assertEqual(ship.health, health, "Ship lost health")528        time.sleep(4.5) # wait for decloak529        self.assertNotEqual(ship.health, health, "Ship didn't lose health")530    def test_dragon_ship_escape(self):531        """532        Tests a dragon not catching a ship at full speed.533        """534        start = self.game.world.mid_point(0,80)535        dragon = Dragon(start, 100, 20)536        dragon.body.velocity = Vec2d(0, -5)537        self.game.world.append(dragon)538        speed = dragon.body.velocity.length539        time.sleep(2.5)540        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 2)541        starts = self.game.world.mid_point(50, 0)542        ship = AIShip_SetList("Doomed", starts, self.game, [543                "RotateCommand(self, 150)",544                "ThrustCommand(self, 'B', 7.0, 1.0)",545            ])        546        start2 = self.game.world.mid_point(-150, 250)547        ship2 = AIShip_SetList("Free", start2, self.game, [])        548        time.sleep(2.5)549        self.assertGreater(dragon.body.velocity.length, speed, "Dragon not moving faster")550        print dragon.body.velocity.angle_degrees551        self.failIfAlmostEqual(dragon.body.position, start, None, "Dragon should have moved", 5)552        self.assertAlmostEqual(dragon.body.velocity.angle_degrees, -50, None, "Dragon should be facing ship", 15)553        time.sleep(3.5)554        self.failIfEqual(ship.health, 100, "Doomed ship did not take damage")555        self.assertEqual(ship2.health, 100, "Free ship took damage")556        self.assertAlmostEqual(ship2.body.position, start2, None, "Free Ship not in same place", 2)557        time.sleep(5.5)558        self.assertTrue(ship in self.game.world, "Doomed Ship destroyed")559        self.assertAlmostEqual(dragon.body.velocity.angle_degrees, ship.body.velocity.angle_degrees, None, "Dragon should be headed in about same direction as ship.", 10)560        time.sleep(3.5)561        self.assertAlmostEqual(dragon.body.velocity.length, speed, None, "Dragon should have lost interest", 4)562    def test_all_stop(self):563        start = self.game.world.mid_point(0, 0)564        ship = AIShip_SetListLoop("Doomed", start, self.game, [565                "RotateCommand(self, 15)",...unittest_IBO.py
Source:unittest_IBO.py  
...173        mopt2, _ = maximizePI(GP2, S5.bounds, xi=0.01, maxiter=10)174        self.failUnlessAlmostEqual(dopt2, copt2, 4)175        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)176        self.failUnlessAlmostEqual(-copt2, mopt2, 4)177        self.failIfAlmostEqual(dopt1, dopt2, 4)178        self.failIfAlmostEqual(copt1, copt2, 4)179        self.failIfAlmostEqual(mopt1, mopt2, 4)180        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)181        pif3 = PI(GP3, xi=0.1)    182        dopt3, _ = direct(pif3.negf, S5.bounds, maxiter=10)183        copt3, _ = cdirect(pif3.negf, S5.bounds, maxiter=10)184        mopt3, _ = maximizePI(GP3, S5.bounds, xi=0.1, maxiter=10)185        self.failUnlessAlmostEqual(dopt3, copt3, 4)186        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)187        self.failUnlessAlmostEqual(-copt3, mopt3, 4)188        self.failIfAlmostEqual(dopt1, dopt3, 4)189        self.failIfAlmostEqual(copt1, copt3, 4)190        self.failIfAlmostEqual(mopt1, mopt3, 4)191        self.failIfAlmostEqual(dopt2, dopt3, 4)192        self.failIfAlmostEqual(copt2, copt3, 4)193        self.failIfAlmostEqual(mopt2, mopt3, 4)194    195    196class TestMaximizeUCB(unittest.TestCase):197    198    def test1DcUCB(self):199        200        f = lambda x: float(sin(x*5.))201        X = lhcSample([[0., 1.]], 5, seed=22)202        Y = [f(x) for x in X]203        kernel = GaussianKernel_ard(array([1.0]))204        GP = GaussianProcess(kernel)205        GP.addData(X, Y)206        207        # should use optimizeGP.cpp208        ucbf = UCB(GP, 1)209        dopt, doptx = direct(ucbf.negf, [[0., 1.]], maxiter=10)210        copt, coptx = cdirect(ucbf.negf, [[0., 1.]], maxiter=10)211        mopt, moptx = maximizeUCB(GP, [[0., 1.]], maxiter=10)212        213        self.failUnlessAlmostEqual(dopt, copt, 4)214        self.failUnlessAlmostEqual(-dopt, mopt, 4)215        self.failUnlessAlmostEqual(-copt, mopt, 4)216    217        self.failUnless(sum(abs(doptx-coptx)) < .01)218        self.failUnless(sum(abs(moptx-coptx)) < .01)219        self.failUnless(sum(abs(moptx-doptx)) < .01)220    221    222    def testXi(self):223        224        S5 = Shekel5()225        226        GP1 = GaussianProcess(GaussianKernel_iso([.2]))227        # self.failUnlessEqual(GP1.xi, 0.0)228        X = lhcSample(S5.bounds, 10, seed=0)229        Y = [S5.f(x) for x in X]230        GP1.addData(X, Y)231        ucbf1 = UCB(GP1, len(S5.bounds), scale=0.5)232        dopt1, _ = direct(ucbf1.negf, S5.bounds, maxiter=10)233        copt1, _ = cdirect(ucbf1.negf, S5.bounds, maxiter=10)234        mopt1, _ = maximizeUCB(GP1, S5.bounds, scale=0.5, maxiter=10)235        self.failUnlessAlmostEqual(dopt1, copt1, 4)236        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)237        self.failUnlessAlmostEqual(-copt1, mopt1, 4)238        GP2 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)239        ucbf2 = UCB(GP2, len(S5.bounds), scale=0.01)    240        dopt2, _ = direct(ucbf2.negf, S5.bounds, maxiter=10)241        copt2, _ = cdirect(ucbf2.negf, S5.bounds, maxiter=10)242        mopt2, _ = maximizeUCB(GP2, S5.bounds, scale=.01, maxiter=10)243        self.failUnlessAlmostEqual(dopt2, copt2, 4)244        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)245        self.failUnlessAlmostEqual(-copt2, mopt2, 4)246        self.failIfAlmostEqual(dopt1, dopt2, 4)247        self.failIfAlmostEqual(copt1, copt2, 4)248        self.failIfAlmostEqual(mopt1, mopt2, 4)249        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)250        ucbf3 = UCB(GP3, len(S5.bounds), scale=.9)    251        dopt3, _ = direct(ucbf3.negf, S5.bounds, maxiter=10)252        copt3, _ = cdirect(ucbf3.negf, S5.bounds, maxiter=10)253        mopt3, _ = maximizeUCB(GP3, S5.bounds, scale=0.9, maxiter=10)254        self.failUnlessAlmostEqual(dopt3, copt3, 4)255        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)256        self.failUnlessAlmostEqual(-copt3, mopt3, 4)257        self.failIfAlmostEqual(dopt1, dopt3, 4)258        self.failIfAlmostEqual(copt1, copt3, 4)259        self.failIfAlmostEqual(mopt1, mopt3, 4)260        self.failIfAlmostEqual(dopt2, dopt3, 4)261        self.failIfAlmostEqual(copt2, copt3, 4)262        self.failIfAlmostEqual(mopt2, mopt3, 4)263        264        265class TestMaximizeEI(unittest.TestCase):266    267    def test1DcEI(self):268        269        f = lambda x: float(sin(x*5.))270        X = lhcSample([[0., 1.]], 5, seed=22)271        Y = [f(x) for x in X]272        kernel = GaussianKernel_ard(array([1.0]))273        GP = GaussianProcess(kernel)274        GP.addData(X, Y)275        276        # should use optimizeGP.cpp277        maxei = maximizeEI(GP, [[0., 1.]])278        279        if False:280            figure(1)281            plot(X, Y, 'ro')282            plot([x/100 for x in xrange(100)], [GP.ei(x/100) for x in xrange(100)])283            plot(maxei[1][0], maxei[0], 'ko')284            show()285        286    def test2DcEI(self):287        288        f = lambda x: sum(sin(x))289        bounds = [[0., 5.], [0., 5.]]290        X = lhcSample(bounds, 5, seed=23)291        Y = [f(x) for x in X]292        kernel = GaussianKernel_iso(array([1.0]))293        GP = GaussianProcess(kernel, X, Y)294        maxei = maximizeEI(GP, bounds)295        296        if False:297            figure(1)298            c0 = [(i/100.)*(bounds[0][1]-bounds[0][0])+bounds[0][0] for i in xrange(101)]299            c1 = [(i/100.)*(bounds[1][1]-bounds[1][0])+bounds[1][0] for i in xrange(101)]300            z = array([[GP.ei(array([i, j])) for i in c0] for j in c1])301            ax = plt.subplot(111)302            ax.contour(c0, c1, z, 10, alpha=0.5, cmap=cm.Blues_r)303            plot([x[0] for x in X], [x[1] for x in X], 'ro')304            for i in xrange(len(X)):305                annotate('%2f'%Y[i], X[i])306            plot(maxei[1][0], maxei[1][1], 'ko')307            show()308    def test2DpyEI(self):309        310        f = lambda x: sum(sin(x))311        bounds = [[0., 5.], [0., 5.]]312        X = lhcSample(bounds, 5, seed=24)313        Y = [f(x) for x in X]314        kernel = GaussianKernel_ard(array([1.0, 1.0]))315        GP = GaussianProcess(kernel, X, Y)316        maxei = maximizeEI(GP, bounds)317        318        if False:319            figure(1)320            c0 = [(i/50.)*(bounds[0][1]-bounds[0][0])+bounds[0][0] for i in xrange(51)]321            c1 = [(i/50.)*(bounds[1][1]-bounds[1][0])+bounds[1][0] for i in xrange(51)]322            z = array([[GP.ei(array([i, j])) for i in c0] for j in c1])323            ax = plt.subplot(111)324            cs = ax.contour(c0, c1, z, 10, alpha=0.5, cmap=cm.Blues_r)325            plot([x[0] for x in X], [x[1] for x in X], 'ro')326            for i in xrange(len(X)):327                annotate('%2f'%Y[i], X[i])328            plot(maxei[1][0], maxei[1][1], 'ko')329            show()330            331            332    # deactivated for being slow to run333    def _testKernelMaxEI(self):334        335        # test different methods of optimizing kernel336        S5 = Shekel5()337        338        hv = 0.1339        testkernels = [GaussianKernel_iso([hv]), 340                   GaussianKernel_ard([hv, hv, hv, hv]),341                   MaternKernel3([hv, 1.0])]342                   # MaternKernel5([hv, 1.0])]343        for kernel in testkernels:344            # print345            # print kernel.__class__346            347        348            # train GPs349            X = lhcSample(S5.bounds, 10, seed=0)350            Y = [S5.f(x) for x in X]351        352            GP = GaussianProcess(kernel, X, Y)353        354            eif = EI(GP)355            dopt, doptx = direct(eif.negf, S5.bounds, maxiter=10)356            copt, coptx = cdirect(eif.negf, S5.bounds, maxiter=10)357            mopt, moptx = maximizeEI(GP, S5.bounds, maxiter=10)358            # print dopt, doptx359            # print copt, coptx360            # print mopt, moptx361        362            self.failUnlessAlmostEqual(dopt, copt, 4)363            self.failUnlessAlmostEqual(-dopt, mopt, 4)364            self.failUnlessAlmostEqual(-copt, mopt, 4)365        366            self.failUnless(sum(abs(doptx-coptx)) < .01)367            self.failUnless(sum(abs(moptx-coptx)) < .01)368            self.failUnless(sum(abs(moptx-doptx)) < .01)369        370            # train GP w/prior371            pX = lhcSample(S5.bounds, 100, seed=101)372            pY = [S5.f(x) for x in pX]373            prior = RBFNMeanPrior()374            prior.train(pX, pY, bounds=S5.bounds, k=10, seed=102)375        376            GP = GaussianProcess(kernel, X, Y, prior=prior)        377        378            eif = EI(GP)379            pdopt, pdoptx = direct(eif.negf, S5.bounds, maxiter=10)380            pcopt, pcoptx = cdirect(eif.negf, S5.bounds, maxiter=10)381            pmopt, pmoptx = maximizeEI(GP, S5.bounds, maxiter=10)382        383            self.failIfAlmostEqual(pdopt, dopt, 3)384            self.failUnlessAlmostEqual(pdopt, pcopt, 4)385            self.failUnlessAlmostEqual(-pdopt, pmopt, 4)386            self.failUnlessAlmostEqual(-pcopt, pmopt, 4)387        388            self.failUnless(sum(abs(pdoptx-pcoptx)) < .01)389            self.failUnless(sum(abs(pmoptx-pcoptx)) < .01)390            self.failUnless(sum(abs(pmoptx-pdoptx)) < .01)391        392        393    def testXi(self):394        395        S5 = Shekel5()396        397        GP1 = GaussianProcess(GaussianKernel_iso([.2]))398        # self.failUnlessEqual(GP1.xi, 0.0)399        X = lhcSample(S5.bounds, 10, seed=0)400        Y = [S5.f(x) for x in X]401        GP1.addData(X, Y)402        eif1 = EI(GP1, xi=0.0)403        dopt1, _ = direct(eif1.negf, S5.bounds, maxiter=10)404        copt1, _ = cdirect(eif1.negf, S5.bounds, maxiter=10)405        mopt1, _ = maximizeEI(GP1, S5.bounds, xi=0.0, maxiter=10)406        self.failUnlessAlmostEqual(dopt1, copt1, 4)407        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)408        self.failUnlessAlmostEqual(-copt1, mopt1, 4)409        GP2 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)410        eif2 = EI(GP2, xi=0.01)    411        self.failUnlessEqual(eif2.xi, 0.01)    412        dopt2, _ = direct(eif2.negf, S5.bounds, maxiter=10)413        copt2, _ = cdirect(eif2.negf, S5.bounds, maxiter=10)414        mopt2, _ = maximizeEI(GP2, S5.bounds, xi=0.01, maxiter=10)415        self.failUnlessAlmostEqual(dopt2, copt2, 4)416        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)417        self.failUnlessAlmostEqual(-copt2, mopt2, 4)418        self.failIfAlmostEqual(dopt1, dopt2, 4)419        self.failIfAlmostEqual(copt1, copt2, 4)420        self.failIfAlmostEqual(mopt1, mopt2, 4)421        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)422        eif3 = EI(GP3, xi=0.1)    423        dopt3, _ = direct(eif3.negf, S5.bounds, maxiter=10)424        copt3, _ = cdirect(eif3.negf, S5.bounds, maxiter=10)425        mopt3, _ = maximizeEI(GP3, S5.bounds, xi=0.1, maxiter=10)426        self.failUnlessAlmostEqual(dopt3, copt3, 4)427        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)428        self.failUnlessAlmostEqual(-copt3, mopt3, 4)429        self.failIfAlmostEqual(dopt1, dopt3, 4)430        self.failIfAlmostEqual(copt1, copt3, 4)431        self.failIfAlmostEqual(mopt1, mopt3, 4)432        self.failIfAlmostEqual(dopt2, dopt3, 4)433        self.failIfAlmostEqual(copt2, copt3, 4)434        self.failIfAlmostEqual(mopt2, mopt3, 4)435    436    437    def testNoise(self):438        439        tf = Branin()440        441        X = lhcSample(tf.bounds, 10, seed=0)442        Y = [tf.f(x) for x in X]443        GP1 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=1e-4)444        self.failUnlessEqual(GP1.noise, 1e-4)445        eif1 = EI(GP1)446        dopt1, _ = direct(eif1.negf, tf.bounds, maxiter=10)447        copt1, _ = cdirect(eif1.negf, tf.bounds, maxiter=10)448        mopt1, _ = maximizeEI(GP1, tf.bounds, maxiter=10)449        self.failUnlessAlmostEqual(dopt1, copt1, 4)450        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)451        self.failUnlessAlmostEqual(-copt1, mopt1, 4)452        GP2 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=0.01)453        self.failUnlessEqual(GP2.noise, 0.01)454        455        eif2 = EI(GP2)456        dopt2, _ = direct(eif2.negf, tf.bounds, maxiter=10)457        copt2, _ = cdirect(eif2.negf, tf.bounds, maxiter=10)458        mopt2, _ = maximizeEI(GP2, tf.bounds, maxiter=10)459        self.failUnlessAlmostEqual(dopt2, copt2, 4)460        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)461        self.failUnlessAlmostEqual(-copt2, mopt2, 4)462        self.failIfAlmostEqual(dopt1, dopt2, 4)463        self.failIfAlmostEqual(copt1, copt2, 4)464        self.failIfAlmostEqual(mopt1, mopt2, 4)465        GP3 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=0.1)466        self.failUnlessEqual(GP3.noise, 0.1)467        eif3 = EI(GP3)468        dopt3, _ = direct(eif3.negf, tf.bounds, maxiter=10)469        copt3, _ = cdirect(eif3.negf, tf.bounds, maxiter=10)470        mopt3, _ = maximizeEI(GP3, tf.bounds, maxiter=10)471        self.failUnlessAlmostEqual(dopt3, copt3, 4)472        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)473        self.failUnlessAlmostEqual(-copt3, mopt3, 4)474        self.failIfAlmostEqual(dopt1, dopt3, 4)475        self.failIfAlmostEqual(copt1, copt3, 4)476        self.failIfAlmostEqual(mopt1, mopt3, 4)477        self.failIfAlmostEqual(dopt2, dopt3, 4)478        self.failIfAlmostEqual(copt2, copt3, 4)479        self.failIfAlmostEqual(mopt2, mopt3, 4)480        481        # for GP in [GP1, GP2, GP3]:482        #     mu, sigma2 = GP.posterior(X[0])483        #     print '\n%f, %f' % (mu, sigma2)484            # self.failUnlessEqual(mu, Y[0])485            # self.failUnlessEqual(sigma2, GP.noise)486        487    def testMaxEIPrior(self):488        # make sure that the prior works with the different methods of EI489        # maximization490        491        S5 = Shekel5()492        pX = lhcSample(S5.bounds, 100, seed=511)493        pY = [S5.f(x) for x in pX]..._07_almost_equal.py
Source:_07_almost_equal.py  
1"""2In addition to strict equality, it is possible to test for near equality of floating-point3numbers using failIfAlmostEqual() and failUnlessAlmostEqual() .4The arguments are the values to be compared and the number of decimal places to5use for the test.6"""7import unittest8class AlmostEqualTest(unittest.TestCase):9    def testEqual(self):10        self.failUnlessEqual(1.1, 3.3-2.2)11    def testAlmostEqual(self):12        self.failUnlessAlmostEqual(1.1, 3.3-2.2, places=1)13    def testNotAlmostEqual(self):14        self.failIfAlmostEqual(1.1, 3.3-2.0, places=1)15if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
