def test_executer(): f = Feuille() o = f.objets f.executer("A = (1, 2)") f.executer("A.x += 1") assert(o.A.x == 2) f.executer("A' = 3, 4") f.executer("s = [A A']") f.executer("I = Milieu(s)") assertAlmostEqual(o.I.xy, (2.5, 3)) f.executer("del") assert("I" not in o.noms) assert("A_prime" in o.noms) f.executer("del") f.executer("del") assert("A_prime" not in o.noms) f.executer("= (1, 2)") assert(o.M1.coordonnees == (1, 2)) f.executer("txt0 = `Bonjour !`") f.executer(r"txt1 = `$P\`ere et m\`ere ont un accent grave.$`") f.executer("chaine_vide = ``") assert(o.txt0.texte == "Bonjour !") assert(o.txt1.texte == r"$P\`ere et m\`ere ont un accent grave.$") assert(o.chaine_vide.texte == "") f.executer("M = (5, 7)") f.executer("C = _") assert(o.C.x == 5) f.executer("=((i,sqrt(i)) for i in (3,4,5,6))") assert(o.M2.xy == (3, sqrt(3))) assert(o.M3.xy == (4, sqrt(4))) assert(o.M4.xy == (5, sqrt(5))) assert(o.M5.xy == (6, sqrt(6)))
def test_Droite_equation(): a = randint(50) - randint(50) + 0.1 # afin que a ne soit pas nul b = randint(50) - randint(50) + random() c = randint(50) - randint(50) + random() d, e, f = Droite_equation(a, b, c).equation assertAlmostEqual((e / d, f / d), (b / a, c / a)) assertEqual(Droite_equation(a, 0, 0).equation[1:], (0, 0)) assertEqual((Droite_equation(0, a, 0).equation[0], Droite_equation( 0, a, 0).equation[2]), (0, 0)) assert (not Droite_equation(0, 0, 0).existe) d = Droite_equation("y=-5/2x-3/2") assert (Point(0, -1.5) in d) assert (Point(-1, 1) in d) d = Droite_equation("x=2*10**2") assert (Point(200, -1000) in d) assert (Point(100, -1000) not in d) d = Droite_equation("2*x+2*y=1") assert (Point(0.5, 0) in d) assert (Point(1, -0.5) in d) d = Droite_equation("x+y=1") assert (Point(0, 1) in d) assert (Point(1, 0) in d) d = Droite_equation("x+2y=-2") assert (Point(0, -1) in d) assert (Point(-2, 0) in d)
def test_Reflexion(): d = Droite_equation(1, 6, -2) r = Reflexion(d) M = Point(random()+randint(50)-randint(50), random()+randint(50)-randint(50)) m = Mediatrice(M, r(M)) assertAlmostEqual(d.equation_reduite, m.equation_reduite) assert(r(d) is d)
def test_Parallele(): d0 = Droite_equation(2, 1, 7) A = Point(-2, 3) d = Parallele(d0, A) assert(d.parallele(d0)) assert(d.droite is d0 and d.point is A) assertAlmostEqual(d0.equation[:1], d.equation[:1])
def test_session(): i = Interprete(verbose = VERBOSE) i.evaluer("1+7") i.evaluer("x-3") i.evaluer("ans()+ans(1)") assertDernier(i, "x + 5") i.evaluer("f(x, y, z)=2x+3y-z") i.evaluer("f(-1, 5, a)") assertDernier(i, "-a + 13") i.evaluer("f(x)=x^2-7x+3") i.evaluer("f'(x)") assertDernier(i, "2*x - 7") # Noms réservés assertRaises(NameError, i.evaluer, "e=3") assertRaises(NameError, i.evaluer, "pi=3") assertRaises(NameError, i.evaluer, "i=3") assertRaises(NameError, i.evaluer, "oo=3") assertRaises(NameError, i.evaluer, "factorise=3") # Etc. # Test des générateurs i.evaluer('f(x)=x+3') i.evaluer('[f(j) for j in range(1, 11)]') assertDernier(i, '[4, 5, 6, 7, 8, 9, 10, 11, 12, 13]') i.evaluer('tuple(i for i in range(7))') assertDernier(i, '(0, 1, 2, 3, 4, 5, 6)') i.evaluer('[j for j in range(7)]') assertDernier(i, '[0, 1, 2, 3, 4, 5, 6]') # _11 is an alias for ans(11) i.evaluer('_11 == _') assertDernier(i, 'True') i.evaluer('_7') assertDernier(i, "2*x - 7") # _ is an alias for ans(-1), __ is an alias for ans(-2), and so on. i.evaluer('_ == -7 + 2*x') assertDernier(i, 'True') i.evaluer('__') assertDernier(i, "2*x - 7") i.evaluer('______') # ans(-6) assertDernier(i, '(0, 1, 2, 3, 4, 5, 6)') # Affichage des chaînes en mode text (et non math) i.evaluer('"Bonjour !"') assert i.latex_dernier_resultat == u'\u201CBonjour !\u201D' # Virgule comme séparateur décimal resultat, latex = i.evaluer('1,2') assert resultat == '1,2' assertAlmostEqual(i.derniers_resultats[-1], 1.2) # Avec un espace, c'est une liste (tuple) par contre resultat, latex = i.evaluer('1, 2') assertEqual(resultat, '(1 ; 2)') resultat, latex = i.evaluer('"1.2"') assert resultat == '"1.2"' i.evaluer('?aide') i.evaluer('aide?') i.evaluer('aide(aide)') msg_aide = u"\n== Aide sur aide ==\nRetourne (si possible) de l'aide sur la fonction saisie." resultats = i.derniers_resultats assert resultats[-3:] == [msg_aide, msg_aide, msg_aide] # LaTeX latex = i.evaluer("gamma(x)")[1] assertEqual(latex, r'$\mathrm{\Gamma}\left(x\right)$')
def test_Carre_centre(): O = rand_pt() M = rand_pt() p = Carre_centre(O, M) assert(p.centre.existe and p.centre is O) assert(len(p.cotes) == 4) assertAlmostEqual(p.aire, p.cotes[0].longueur**2)
def test_Quadrilatere(): A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() p = Quadrilatere(A, B, C, D) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D).coordonnees)
def test_Triangle(): A = rand_pt() B = rand_pt() C = rand_pt() t = Triangle(A, B, C) O = t.centre_cercle_circonscrit assertAlmostEqual(Segment(O, A).longueur, Segment(O, C).longueur)
def test_reecriture(): f = Feuille() H = f.objets.H = Fonction("2x^2+3x(1+x)", 'R') def h(x): return 2*x**2+3*x*(1+x) assertAlmostEqual(H(17), h(17)) assertAlmostEqual(H(_VAL0), h(_VAL0))
def test_Secteur_angulaire(): u = Vecteur_libre(5.458, -2.546) v = Vecteur_libre(-5.75, 12.6) P = Point(2.54, -5.68) a = Secteur_angulaire(P, u, v) assert(isinstance(a.etiquette, Label_angle)) assertAlmostEqual(a.val, 2.43538435941) assertAlmostEqual(a.degre, 139.537245287)
def test_Pentagone(): A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() E = rand_pt() p = Pentagone(A, B, C, D, E) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D, E).coordonnees)
def test_variables(): f = Feuille() o = f.objets a = o.a = 3 b = o.b = 5 g = o.g = Fonction("a*x+b") assertAlmostEqual(g(4), a*4+b) assertAlmostEqual(g(_VAL0), a*_VAL0+b)
def test_Point_equidistant(): A = rand_pt() B = rand_pt() C = rand_pt() P = Point_equidistant(A, B, C) assertAlmostEqual(Segment(A, P).longueur, Segment(B, P).longueur) assertAlmostEqual(Segment(A, P).longueur, Segment(C, P).longueur) assert(P in Mediatrice(A, B))
def test_Droite_vectorielle(): v = Vecteur_libre(1, 7) A = Point(-2, 3) d = Droite_vectorielle(A, v) assert(d.vecteur is v and d.point is A) assertAlmostEqual(v.y/v.x, -d.equation[0]/d.equation[1]) B = rand_pt() d1 = Droite_vectorielle(B, v) assert(d.parallele(d1))
def test_Bissectrice(): A = Point(1, -5) B = Point(1.5, -5.3) C = Point(3, -4) d = Bissectrice(A, B, C) a, b, c = d.equation d, e = (0.0870545184921, -1.03861105199) assertAlmostEqual(b/a, d) assertAlmostEqual(c/a, e)
def test_Hexagone(): A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() E = rand_pt() F = rand_pt() p = Hexagone(A, B, C, D, E, F) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D, E, F).coordonnees)
def test_Perpendiculaire(): d = Droite_equation(-1, 2, 0) M = Point() d0 = Perpendiculaire(d, M) a, b, c = d.equation a0, b0, c0 = d0.equation assert(d.perpendiculaire(d0)) assertAlmostEqual(a*a0 + b*b0, 0) assert(M in d0)
def test_Extremite(): v=Vecteur_libre(-4.2, -6.7) w = Representant(v) A = w.origine B = w.extremite assertAlmostEqual((B.x - A.x, B.y - A.y), (v.x, v.y)) C = Extremite(w, couleur = 'purple') assert C is B assert C.style('couleur') == 'purple'
def test_Arc_points(): A = Point(-1.2561, 45.236) B = Point(251.2561, 41.256) C = Point(-42.25, 2.351) a = Arc_points(A, B, C) assert(A in a) assert(B in a) assert(C in a) assertAlmostEqual(a.longueur, 1208.25931027) assertAlmostEqual(a.centre.coordonnees, (122.51970100911581, -114.11725498641933))
def test_Point_translation(): u = Vecteur_libre(1.56, 2.78) v = Vecteur(Point(1.1457, 2.7895), Point(2.458, -8.25)) A = Point(5.256, -7.231) tu = Translation(u) tv = Translation(v) Au = Point_translation(A, tu) Av = Point_translation(A, tv) assertAlmostEqual(Au.coordonnees, (6.816, -4.451)) assertAlmostEqual(Av.coordonnees, (6.5683, -18.2705))
def test_modification_variable(): f = Feuille() o = f.objets o.a = Variable(1) o.fa = "5*sin(4/(a+.5))-1.5" o.A = Point(o.a, o.fa) o.fa = "-5*sin(4/(a+.5))+5.5" o.A = Point(o.a, o.fa) assertAlmostEqual(o.A.x, o.a) assertAlmostEqual(o.A.y, o.fa)
def test_Representant(): A=Point(1,2) B=Point(2,4) v=Vecteur(A, B) w = Representant(v, B) assert(w.x == v.x and w.y == v.y and w.z == v.z) assert(w.origine == v.point2) assertEqual(type(w.coordonnees), tuple) assertAlmostEqual(w.extremite.coordonnees, (3, 6)) assert(w._hierarchie < w.extremite._hierarchie < w._hierarchie + 1)
def test_Point_final(): A = Point(random(),random()) C = Point(random(),random()) u = Vecteur_libre(random(),random()) v = Vecteur(Point(random(), random()), Point(random(), random())) w = Representant(Vecteur(Point(random(), random()), Point(random(), random())), A) F = Point_final(C, (u, v, w), (-2, 1.5, 4)) assertEqual(type(F.coordonnees), tuple) assertAlmostEqual(F.x, C.x -2*u.x+1.5*v.x+4*w.x) assertAlmostEqual(F.y, C.y -2*u.y+1.5*v.y+4*w.y)
def test_Rectangle(): A = rand_pt() B = rand_pt() r = Rectangle(A, B) M, N, O, P = r.sommets diagonale1 = Segment(M, O) diagonale2 = Segment(N, P) assertAlmostEqual(diagonale1.longueur, diagonale2.longueur) cote = Droite(M, N) cote_oppose = Droite(O, P) assert (cote.parallele(cote_oppose))
def test_Arc_points(): A = Point(-1.2561, 45.236) B = Point(251.2561, 41.256) C = Point(-42.25, 2.351) a = Arc_points(A, B, C) assert (A in a) assert (B in a) assert (C in a) assertAlmostEqual(a.longueur, 1208.25931027) assertAlmostEqual(a.centre.coordonnees, (122.51970100911581, -114.11725498641933))
def test_Heptagone(): A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() E = rand_pt() F = rand_pt() G = rand_pt() p = Heptagone(A, B, C, D, E, F, G) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D, E, F, G).coordonnees)
def test_Octogone(): A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() E = rand_pt() F = rand_pt() G = rand_pt() H = rand_pt() p = Octogone(A, B, C, D, E, F, G, H) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D, E, F, G, H).coordonnees)
def test_Rectangle(): A = rand_pt() B = rand_pt() r = Rectangle(A, B) M, N, O, P = r.sommets diagonale1 = Segment(M, O) diagonale2 = Segment(N, P) assertAlmostEqual(diagonale1.longueur, diagonale2.longueur) cote = Droite(M, N) cote_oppose = Droite(O, P) assert(cote.parallele(cote_oppose))
def test_Point_rotation(): A = Point(1, -4) r = Rotation(Point(-2, 7.56), 4.25) B = Point_rotation(A, r) assertAlmostEqual(r(A).coordonnees, B.coordonnees) assertAlmostEqual(B.coordonnees, (-13.684339450863803, 10.031803308717693)) A = Point('1', '0') r = Rotation(('0', '0'), 'pi/4') B = Point_rotation(A, r) C = Point('sqrt(2)/2', 'sqrt(2)/2') assert(C.x == B.x and C.y == B.y)
def test_Polygone(): # cas général : polygone à 11 côtés : A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() E = rand_pt() F = rand_pt() G = rand_pt() H = rand_pt() I = rand_pt() J = rand_pt() K = rand_pt() p = p0 = Polygone(A, B, C, D, E, F, G, H, I, J, K) assert (isinstance(p.etiquette, Label_polygone)) assert (p.sommets[0] == A and p.sommets[10] == K) assert (Milieu(B, C) in p.cotes[1]) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D, E, F, G, H, I, J, K).coordonnees) # cas particuliers : t = Polygone(A, B, C) O = t.centre_cercle_circonscrit assertAlmostEqual(Segment(O, A).longueur, Segment(O, C).longueur) assert (Droite(t.orthocentre, C).perpendiculaire(Droite(A, B))) assert (t.__class__ is Triangle) p = Polygone(A, B, C, D) assert (p.__class__ is Quadrilatere) p = Polygone(A, B, C, D, E) assert (p.__class__ is Pentagone) p = Polygone(A, B, C, D, E, F) assert (p.__class__ is Hexagone) p = Polygone(A, B, C, D, E, F, G) assert (p.__class__ is Heptagone) p = Polygone(A, B, C, D, E, F, G, H) assert (p.__class__ is Octogone) assert (p._hierarchie < p.sommets[0]._hierarchie < p.sommets[1]._hierarchie < p.sommets[7]._hierarchie < p._hierarchie + 1) # Test keyword 'points' p = Polygone(points=(A, B, C, D, E, F, G, H, I, J, K)) assert (p.centre.coordonnees == p0.centre.coordonnees) assert ("points" not in p.style()) # Syntaxe spéciale : Polygone créé sans arguments, ou avec un entier comme argument. p = Polygone() p = Polygone(2) assert (isinstance(p, Segment)) p = Polygone(3) assert (isinstance(p, Triangle)) p = Polygone(n=5) assert (isinstance(p, Pentagone)) p = Polygone(n=12) assert (len(p.points) == 12) p = Polygone(23) assert (len(p.points) == 23) assert (len(str(p.points)) < 30000)
def test_Cercle_equation(): c = Cercle_equation() assertAlmostEqual(c.centre.coordonnees, (0, 0)) assert(c.rayon == 1) c.a = -2 c.b = 4 c.c = -4 assertAlmostEqual(c.centre.coordonnees, (1, -2)) assert(c.rayon == 3) assert(c.existe) c.c = 6 assert(not c.existe)
def test_abreviations(): f = Feuille(titre = u"Feuille de travail n°1") o = f.objets assert(o.has_key("Point")) assert(o.has_key("Variable")) assert(o.has_key("Texte")) assert(o.has_key("point")) assert(o.has_key("variable")) assert(o.has_key("texte")) o.txt = ["salut"] assert(isinstance(o.txt, Texte)) assert(o.txt.texte == "salut") o.s = [u"Hé, ça marche !"] assert(isinstance(o.s, Texte)) assert(o.s.texte == u"Hé, ça marche !") o.A = (1, 2) o.k = 7 assert(isinstance(o.A, Point)) assert(isinstance(o.k, Variable)) o.h = 'A.x' o.A.x = 15 assert(o.h == 15) o.h = "A.x-10" assert(o.h == 5) assert(o.h - 3 == 2) o.h = pi/3 assertAlmostEqual(cos(o.h), 1/2) o.B = (-1, 3) o.u = o.A>o.B assert(isinstance(o.u, Vecteur)) assert(o.u.coordonnees == (o.B.x - o.A.x, o.B.y - o.A.y)) o.C = 2-3j assert(isinstance(o.C, Point)) assert(o.C.z == 2-3j) o.C.x = "A.x" #print 'o.C.x.val:', o.C.x.val, type(o.C.x.val) assert(isinstance(o.C.x.val, (float, int, long))) assert(o.C.x == o.A.x) o.A.coordonnees = -11, 3 assert(o.C.coordonnees[0] == -11) o.B.x = "A.x + 1" assert(isinstance(o.B.x.val, (float, int, long))) assert(o.B.x == o.A.x + 1) o.A.coordonnees = 30, -5 assert(o.B.coordonnees[0] == 31) o.A(-3.6, 0.4) assert(o.C.coordonnees[0] ==-3.6) # 'o.EFG = Triangle' doit être accepté comme alias de 'o.EFG = Triangle()' o.EFG = Triangle assert(isinstance(o.EFG, Triangle))
def test_Cercle_equation(): c = Cercle_equation() assertAlmostEqual(c.centre.coordonnees, (0, 0)) assert (c.rayon == 1) c.a = -2 c.b = 4 c.c = -4 assertAlmostEqual(c.centre.coordonnees, (1, -2)) assert (c.rayon == 3) assert (c.existe) c.c = 6 assert (not c.existe)
def test_sympy(): x = Symbol('x', real=True) assert -oo < oo assert not (-1.5 < -oo) assert (1 - exp(x)).is_negative is None assert Matrix([[1, 2], [3, 4]])**Integer(2) == Matrix([[7, 10], [15, 22]]) assertAlmostEqual(E._evalf(50), math.e) assert solve(1 / x, x) == [] # issue 1694 assert solve(-(1 + x) / (2 + x)**2 + 1 / (2 + x), x) == [] # issue 1694 assert limit(1 + 1 / x, x, 0, dir='-') == -oo assert limit(1 / x**2, x, 0, dir='-') == oo assert sympify(u'45') == 45 # issue 2508
def test_sympy(): x = Symbol('x', real = True) assert -oo < oo assert not(-1.5 < -oo) assert (1 - exp(x)).is_negative is None assert Matrix([[1, 2], [3, 4]])**Integer(2) == Matrix([[7, 10], [15, 22]]) assertAlmostEqual(E._evalf(50), math.e) assert solve(1/x, x) == [] # issue 1694 assert solve(-(1 + x)/(2 + x)**2 + 1/(2 + x), x) == [] # issue 1694 assert limit(1 + 1/x, x, 0, dir='-') == -oo assert limit(1/x**2, x, 0, dir='-') == oo assert sympify(u'45') == 45 # issue 2508
def test_Polygone(): # cas général : polygone à 11 côtés : A = rand_pt() B = rand_pt() C = rand_pt() D = rand_pt() E = rand_pt() F = rand_pt() G = rand_pt() H = rand_pt() I = rand_pt() J = rand_pt() K = rand_pt() p = p0 = Polygone(A, B, C, D, E, F, G, H, I, J, K) assert(isinstance(p.etiquette, Label_polygone)) assert(p.sommets[0] == A and p.sommets[10] == K) assert(Milieu(B, C) in p.cotes[1]) assertAlmostEqual(p.centre.coordonnees, Barycentre(A, B, C, D, E, F, G, H, I, J, K).coordonnees) # cas particuliers : t = Polygone(A, B, C) O = t.centre_cercle_circonscrit assertAlmostEqual(Segment(O, A).longueur, Segment(O, C).longueur) assert(Droite(t.orthocentre, C).perpendiculaire(Droite(A, B))) assert(t.__class__ is Triangle) p = Polygone(A, B, C, D) assert(p.__class__ is Quadrilatere) p = Polygone(A, B, C, D, E) assert(p.__class__ is Pentagone) p = Polygone(A, B, C, D, E, F) assert(p.__class__ is Hexagone) p = Polygone(A, B, C, D, E, F, G) assert(p.__class__ is Heptagone) p = Polygone(A, B, C, D, E, F, G, H) assert(p.__class__ is Octogone) assert(p._hierarchie < p.sommets[0]._hierarchie < p.sommets[1]._hierarchie < p.sommets[7]._hierarchie < p._hierarchie + 1) # Test keyword 'points' p = Polygone(points = (A, B, C, D, E, F, G, H, I, J, K)) assert(p.centre.coordonnees == p0.centre.coordonnees) assert("points" not in p.style()) # Syntaxe spéciale : Polygone créé sans arguments, ou avec un entier comme argument. p = Polygone() p = Polygone(2) assert(isinstance(p, Segment)) p = Polygone(3) assert(isinstance(p, Triangle)) p = Polygone(n = 5) assert(isinstance(p, Pentagone)) p = Polygone(n = 12) assert(len(p.points) == 12) p = Polygone(23) assert(len(p.points) == 23) assert(len(str(p.points)) < 30000)
def test_Cercle(): A = Point(2.78841, -5.25) O = Point(27.8841, -0.525) c = Cercle(O, A) assert (c.centre is O) assert (c.point is A) assertAlmostEqual(c.rayon, 25.5366262763) # Test du typage dynamique c = Cercle(Point(1, 1), 1) assert (Point(1, 2) in c) c = Cercle(Point(1, 1), Point(0, 2), Point(-1, 1)) assert (Point(0, 0) in c) assertAlmostEqual(c.centre.coordonnees, (0, 1))
def test_Disque(): A = Point(2.78841, -5.25) B = Point(27.8841, -0.525) C = Point(-42.25, 2.351) c = Cercle_points(A, B, C) d = Disque(c) assertAlmostEqual(d.centre.coordonnees, c.centre.coordonnees) assertAlmostEqual(d.rayon, c.rayon) assert (A in d) assert (B in d) assert (C in d) assert (d.centre in d) assert (Point(-500, -500) not in d)
def test_Mediatrice(): A = Point(4.5, 7.3) B = Point(-4.147, 2.1) s = Segment(A, B) d0 = Mediatrice(s) d1 = Mediatrice(A, B) I = Milieu(A, B) assert (I in d0) assert (I in d1) a, b, c = s.equation a0, b0, c0 = d0.equation assertAlmostEqual(a * a0 + b * b0, 0) assertAlmostEqual(d0.equation, d1.equation)
def test_Projete_arc_cercle(): O = Point(23.15, -12.75) A = Point(-12.5, 7.14) B = Point(7.15, 8.64) a = Arc_cercle(O, A, B) M = rand_pt() P = Projete_arc_cercle(M, a) assert (P in a) M.coordonnees = a.centre.coordonnees assert (not P.existe) M.coordonnees = -17.826266675199999, 11.760911186 assert (type(P.coordonnees) is tuple) assertAlmostEqual(A.coordonnees, P.coordonnees) assertEqual(A, P)
def test_Glisseur_segment(): A = rand_pt() B = rand_pt() s = Segment(A, B) M = Glisseur_segment(s) assert (M in s) M.k = 0 assertEqual(M.k, 0) assertAlmostEqual(M.coordonnees, A.coordonnees) P = Point(*M.coordonnees) M.k = 1 assertEqual(M.k, 1) assertAlmostEqual(M.coordonnees, B.coordonnees) M.k = 2 assertEqual(M.k, 1) # 0<=k<=1 pour un segment assert (M in s) M.k = -1 assertEqual(M.k, 0) # 0<=k<=1 pour un segment assert (M in s) M.k = 1 Q = Point(*M.coordonnees) assertAlmostEqual(Droite(P, Q).equation_reduite, s.equation_reduite) M.k = 1.7 M(*M.coordonnees) assertAlmostEqual(M.k, 1)
def test_operations(): for i in xrange(10): a = randint(50) - randint(50) b = randint(50) - randint(50) if i >= 5: a += random() b += random() u = Variable(a) v = Variable(b) # Test opérations : if i < 5: assertEqual(u + v, a + b) assertEqual(u - v, a - b) assertEqual(u * v, a * b) if a != 0: assertEqual(u**v, a**b) if b != 0: assertEqual(u / v, a / b) assertEqual(u // v, a // b) assertEqual(u % v, a % b) else: assertAlmostEqual(u + v, a + b) assertAlmostEqual(u - v, a - b) assertAlmostEqual(u * v, a * b) if a != 0: assertAlmostEqual(abs(u)**v, abs(a)**b) # Test assignations : u += v a += b assertEqual(u, a) u *= v a *= b assertEqual(u, a)
def test_Segment(): A = Point(4.5, 7.3) B = Point(4, 2.1) s = Segment(A, B) assert (isinstance(s.etiquette, Label_segment)) assertAlmostEqual(s.longueur, sqrt((B.x - A.x)**2 + (B.y - A.y)**2)) I = Milieu(s.point1, s.point2) assertEqual(I.coordonnees, ((A.x + B.x) / 2, (A.y + B.y) / 2)) M = Barycentre((A, 1), (B, -2)) N = Barycentre((A, -2), (B, 1)) assert (I in s) assert (M not in s) assert (N not in s) assert (s.style("legende") == RIEN)
def test_Polygone_regulier(): O = rand_pt() M = rand_pt() p = Polygone_regulier(O, M, 15) assert(len(p.cotes) == 15) assert(p.centre in Mediatrice(O, M)) for i in xrange(15): assertAlmostEqual(Segment(p.sommets[i%15], p.sommets[(i+1)%15]).longueur, Segment(p.sommets[(i+2)%15], p.sommets[(i+3)%15]).longueur) p = Polygone_regulier(O, M, 3) assert(isinstance(p, Triangle)) p = Polygone_regulier(O, M, 4) assert(isinstance(p, Quadrilatere)) # Test de régression : # la taille de str(p.points) croissait exponentiellement. assert(len(str(p.points)) < 30000)
def test_Cercle_rayon(): A = Point(-2.586, 7.541) c_1 = Cercle_rayon(A, -1) c0 = Cercle_rayon(A, 0) c2 = Cercle_rayon(A, 2) assert (isinstance(c_1.etiquette, Label_cercle)) assert (not c_1.existe) assert (c0.existe) assert (c2.existe) assertAlmostEqual(A.coordonnees, c0.centre.coordonnees) assert (A not in c_1) assert (A in c0) assert (A not in c2) assertEqual(c_1.rayon, -1) assertEqual(c0.rayon, 0) assertEqual(c2.rayon, 2) k = random() B = Point(A.x + 2 * sin(k), A.y + 2 * cos(k)) assert (B in c2)
def test_Triangle_isocele(): A = rand_pt() B = rand_pt() tri = Triangle_isocele(A, B, 2*pi/13) C = tri.point3 a = Angle(B, A, C) assertAlmostEqual(a.radian, 2*pi/13) assertAlmostEqual(Segment(A, B).longueur, Segment(A, C).longueur) t1 = Triangle_isocele((0, 0), (1, 1), u'90°') assertAlmostEqual(t1.point3.xy, (-1, 1)) t2 = Triangle_isocele((0, 0), (2, 0), pi/3) assertAlmostEqual(t2.point3.xy, (2*cos(pi/3), 2*sin(pi/3)))
def test_contexte_degre(): with contexte(unite_angle='d'): a = Angle_libre('pi rad') assertAlmostEqual(a.deg, 180) a = Angle_libre('100 grad') assertAlmostEqual(a.deg, 90) b = Angle_libre(30) assertAlmostEqual(b.rad, math.pi / 6) # En interne, tout doit être stocké en radians assert float(b.val) == float(b.rad) # On doit avoir eval(repr(b)) == b assertAlmostEqual(eval(repr(b)).deg, 30)
def test_Polygone_regulier_centre(): O = rand_pt() M = rand_pt() p = Polygone_regulier_centre(O, M, 15) assert(len(p.cotes) == 15) assert(p.centre is O) for Mi in p.sommets: assertAlmostEqual(Segment(O, Mi).longueur, Segment(O, M).longueur) for i in xrange(10): coeffs = tuple(random() for i in xrange(15)) G = Barycentre(*zip(p.sommets, coeffs)) assert(G in p) G.points_ponderes[randint(11)].coefficient = -5 assert(G not in p) # cas particuliers : p = Polygone_regulier_centre(O, M, 3) assert(isinstance(p, Triangle)) p = Polygone_regulier_centre(O, M, 4) assert(isinstance(p, Quadrilatere)) assert(len(str(p.points)) < 30000)
def test_Demicercle(): A = Point(-1.2561, 45.236) B = Point(251.2561, 41.256) c = Demicercle(A, B) assertAlmostEqual(Milieu(A, B).coordonnees, c.centre.coordonnees) assertAlmostEqual(c.diametre, Segment(A, B).longueur) assertAlmostEqual(c.rayon, Segment(A, B).longueur / 2)
def test_Cercle_diametre(): A = Point(2.78841, -5.25) B = Point(27.8841, -0.525) c = Cercle_diametre(A, B) c1 = Cercle(A, B) assertAlmostEqual(c.diametre, c1.rayon) assertAlmostEqual(c.diametre, Segment(A, B).longueur) assertAlmostEqual(c.centre.coordonnees, Milieu(A, B).coordonnees)
def test_Projete_demidroite(): d = Demidroite(rand_pt(), rand_pt()) M = rand_pt() assert (Projete_demidroite(M, d) in d) A = Point(0, 1) B = Point(2, 1) s = Demidroite(A, B) M = Point(1, 7.15) P = Projete_demidroite(M, s) assertAlmostEqual(Milieu(A, B).coordonnees, P.coordonnees) M.x = .5 assertAlmostEqual(Barycentre((A, 3), (B, 1)).coordonnees, P.coordonnees) M.x = -1 assertAlmostEqual(A.coordonnees, P.coordonnees) M.x = 3 assertAlmostEqual(Barycentre((A, -1), (B, 3)).coordonnees, P.coordonnees)
def test_Projete_segment(): s = Segment(rand_pt(), rand_pt()) M = rand_pt() assert (Projete_segment(M, s) in s) A = Point(0, 1) B = Point(2, 1) s = Segment(A, B) M = Point(1, 7.15) P = Projete_segment(M, s) assertAlmostEqual(Milieu(A, B).coordonnees, P.coordonnees) M.x = .5 assertAlmostEqual(Barycentre((A, 3), (B, 1)).coordonnees, P.coordonnees) M.x = -1 assertAlmostEqual(A.coordonnees, P.coordonnees) M.x = 3 assertAlmostEqual(B.coordonnees, P.coordonnees)
def test_Rotation(): A = Point(1.523, 45.35211) r = Rotation(A, pi/4) M = Point(1.4452, -1.2545) assertAlmostEqual(r(M).coordonnees, (34.423837071540447, 12.341247113306926)) assertAlmostEqual(r(A).coordonnees, A.coordonnees) d = Droite(A, M) d1 = Droite(A, r(M)) assertAlmostEqual(r(d).equation_reduite, d1.equation_reduite) assert(r(A) is A)
def test_Centre_gravite(): A = rand_pt() B = rand_pt() C = rand_pt() I = Milieu(B, C) J = Milieu(A, C) K = Milieu(A, B) G = Centre_gravite(Triangle(A, B, C)) assertAlmostEqual(Segment(A, G).longueur, 2 * Segment(I, G).longueur) assertAlmostEqual(Segment(B, G).longueur, 2 * Segment(J, G).longueur) assertAlmostEqual(Segment(C, G).longueur, 2 * Segment(K, G).longueur)
def test_Glisseur_droite(): A = rand_pt() B = rand_pt() d = Droite(A, B) M = Glisseur_droite(d) assert (M in d) M.k = 0 assertEqual(M.k, 0) assertAlmostEqual(M.coordonnees, A.coordonnees) P = Point(*M.coordonnees) M.k = 1 assertEqual(M.k, 1) assertAlmostEqual(M.coordonnees, B.coordonnees) M.k = 2 assertEqual(M.k, 2) M.k = -1 assertEqual(M.k, -1) Q = Point(*M.coordonnees) assertAlmostEqual(Droite(P, Q).equation_reduite, d.equation_reduite) M.k = 1.7 M(*M.coordonnees) assertAlmostEqual(M.k, 1.7)
def test_Carre(): O = rand_pt() M = rand_pt() p = Carre(O, M) A, B, C, D = p.sommets assert(p.centre == Milieu(A, C) == Milieu(B, D)) assert(A == O and B == M) assert(len(p.cotes) == 4) assertAlmostEqual(p.aire, p.cotes[0].longueur**2) # Test redéfinition d'un sommet c = Carre((3, 2), (7, 2)) M = Point(0, 2) c.point1 = M assert(c.point1 is M) assert(c.regulier) assertAlmostEqual(c.aire, 49) assertAlmostEqual(c.point4.coordonnees, (0, 9))
def test_Point_sympy(): A = Point('1/2', '2/3') assertEqual(A.x, symp('1/2')) assertAlmostEqual(A.coordonnees, (1 / 2, 2 / 3))
def test_Centre(): c = rand_cercle() O = Centre(c) M = Glisseur_cercle(c) assertAlmostEqual(Segment(O, M).longueur, c.rayon)