Created
January 15, 2011 13:12
-
-
Save fsouza/780896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Retangulo(object): | |
def __init__(self, xmin, xmax, ymin, ymax): | |
if xmax <= xmin or ymax <= ymin: | |
raise ValueError("max tem que ser maior que min") | |
self.xmin = xmin | |
self.xmax = xmax | |
self.ymin = ymin | |
self.ymax = ymax | |
self.base = self.xmax - self.xmin | |
self.altura = self.ymax - self.ymin | |
@property | |
def area(self): | |
return self.base * self.altura | |
@property | |
def perimetro(self): | |
return (self.base + self.altura) * 2 | |
def verifica_no_range(self, x, y): | |
return x in range(self.xmin, self.xmax + 1) and y in range(self.ymin, self.ymax + 1) | |
def verifica_intersecao(self, retangulo): | |
return self.verifica_no_range(retangulo.xmin, retangulo.ymin) or \ | |
self.verifica_no_range(retangulo.xmax, retangulo.ymax) | |
def __contains__(self, retangulo): | |
return self.verifica_intersecao(retangulo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from nose.tools import * | |
from retangulo import Retangulo | |
class RetanguloTestCase(unittest.TestCase): | |
@raises(ValueError) | |
def test_lanca_exception_com_valores_invalidos_para_x(self): | |
retangulo = Retangulo(xmin = 5, xmax = 2, ymin = 8, ymax = 10) | |
@raises(ValueError) | |
def test_lanca_exception_com_valores_invalidos_para_y(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 8, ymax = 3) | |
def test_deve_guardar_os_valores_dos_pontos(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 7, ymax = 10) | |
assert_equals(retangulo.xmin, 3) | |
assert_equals(retangulo.xmax, 8) | |
assert_equals(retangulo.ymin, 7) | |
assert_equals(retangulo.ymax, 10) | |
def test_calcula_area_do_retangulo(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 7, ymax = 10) | |
assert_equals(retangulo.area, 15) | |
def test_calcula_perimetro(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 7, ymax = 10) | |
assert_equals(retangulo.perimetro, 16) | |
def test_verifica_se_retangulo_esta_dentro_do_outro_pelo_ponto_minimo(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 7, ymax = 10) | |
retangulo_dentro = Retangulo(xmin = 4, xmax = 9, ymin = 9, ymax = 15) | |
retangulo_fora = Retangulo(xmin = 4, xmax = 9, ymin = 4, ymax = 6) | |
assert_true(retangulo.verifica_intersecao(retangulo_dentro)) | |
assert_false(retangulo.verifica_intersecao(retangulo_fora)) | |
def test_verifica_se_retangulo_esta_dentro_do_outro_pelo_ponto_max(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 7, ymax = 10) | |
retangulo_dentro = Retangulo(xmin = 2, xmax = 7, ymin = 3, ymax = 9) | |
assert_true(retangulo.verifica_intersecao(retangulo_dentro)) | |
def test_verifica_se_retangulo_esta_dentro_usando_in(self): | |
retangulo = Retangulo(xmin = 3, xmax = 8, ymin = 7, ymax = 10) | |
retangulo_dentro = Retangulo(xmin = 4, xmax = 9, ymin = 9, ymax = 15) | |
retangulo_fora = Retangulo(xmin = 4, xmax = 9, ymin = 4, ymax = 6) | |
assert_true(retangulo_dentro in retangulo) | |
assert_false(retangulo_fora in retangulo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment