Debian Codegraph @

wxgeometrie/geolib/tests/test_feuille_divers.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 import os, sys
3 TOPDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),"../.."))
4 sys.path.insert(0, TOPDIR)
5
6 import re
7 from math import cos, pi, e, sqrt
8
9 from wxgeometrie.geolib.tests.geotestlib import rand_pt
10 from wxgeometrie.geolib import (
11 Triangle_rectangle, DescripteurFeuille, Point, Segment,
12 Vecteur, Fonction, Variable, Feuille, Angle, contexte, Arc_cercle,
13 Texte, Droite, Carre, Triangle, Polygone, Cercle, Parallelogramme,
14 Droite_equation, Cercle_equation, Courbe, Formule
15 )
16 from wxgeometrie.geolib.routines import nice_display
17 from wxgeometrie.geolib.feuille import parse_equation, is_equation
18
•19 import tools.unittests
20
•21 class GeolibTest(tools.unittests.TestCase):
22
23 def test_variables_composees_1(self):
24 f = Feuille()
25 A = f.objets.A = Point()
26 B = f.objets.B = Point()
27 A(-5, 13)
28 B.x = A.x
29 self.assertTrue(B.x == B.coordonnees[0] == A.x == -5)
30 A(1, 9)
31 self.assertTrue(B.x == B.coordonnees[0] == -5 and A.x == 1)
32 B.x="A.x"
33 A(17, 5)
34 self.assertTrue(B.coordonnees[0] == B.x == 17)
35
36 def test_variables_composees_2(self):
37 f = Feuille()
38 f.objets.M1 = Point(-1.27482678984, 1.69976905312)
39 f.objets.M2 = Point(2.42032332564, 1.25635103926)
40 f.objets.s1 = Segment(f.objets.M1,f.objets.M2)
41 f.objets.M1(-2.77136258661, 2.91916859122)
42 f.objets.M1(4.74826789838, -1.07159353349)
43 f.objets.M5 = Point(-5.11778290993, 2.30946882217)
44 f.objets.M6 = Point(-1.86605080831, 3.25173210162)
45 f.objets.s4 = Segment(f.objets.M5,f.objets.M6)
46 f.objets.M5(-5.59815242494, 2.34642032333)
47 f.objets.M1(-2.42032332564, -1.60739030023)
48 f.objets.M6(-1.86605080831, 3.25173210162)
49 f.objets.M6.renommer('B', afficher_nom=True)
50 f.objets.M6 = Point(2.91916859122, 3.5103926097)
51 f.objets.M6.label('B.x', mode='formule')
52 f.objets.B(-1.18244803695, 1.25635103926)
53 f.objets.M6.supprimer()
54 f.objets.B(-2.21709006928, 2.64203233256)
55 f.objets.M6 = Point(-6.6143187067, 0.443418013857)
56 f.objets.M6.renommer('C', afficher_nom=True)
57 f.objets.C.x=f.objets.B.x
58 f.objets.B(-3.17782909931, 3.36258660508)
59 f.objets.C.x="B.x"
60 f.objets.B(-4.74826789838, 3.47344110855)
61 f.objets.B(-1.99538106236, 3.63972286374)
62 self.assertTrue(f.objets.C.coordonnees[0] == f.objets.C.x == \
63 f.objets.B.coordonnees[0] == f.objets.B.x)
64
65 def test_polygones_et_representants_de_vecteurs(self):
66 f = Feuille()
67 f.objets.A = A = rand_pt()
68 f.objets.B = B = rand_pt()
69 f.objets.C = C = rand_pt()
70 f.objets.p = Parallelogramme(A, B, C)
71 f.objets.S1.renommer("D")
72 s = 'p=%s\nD=%s' % (repr(f.objets.p), repr(f.objets.D))
73 del f.objets.p
74 self.assertNotIn("D", f.objets)
75 exec(s, f.objets)
76 self.assertIn("D", f.objets)
77 self.assertIs(f.objets.D, f.objets.p.sommets[3])
78
79 def test_relier_point_axes(self):
80 f = Feuille()
81 M1 = f.objets.M1 = Point(-1.27482678984, 1.69976905312)
82 M2 = f.objets.M2 = Point(2.42032332564, 1.25635103926)
83 Mx = M1.relier_axe_x()
84 self.assertEqual(Mx.x, M1.x)
85 self.assertEqual(Mx.y, 0)
86 self.assertEqual(Mx.label(), '$%s$' % nice_display(M1.x))
87 self.assertEqual(Mx.label(), '$%s$' % nice_display(-1.27482678984))
88 My = M2.relier_axe_y()
89 self.assertEqual(My.x, 0)
90 self.assertEqual(My.y, M2.y)
91 self.assertEqual(My.label(), '$%s$' % nice_display(M2.y))
92
93
94 def test_formules(self):
95 f = Feuille()
96 o = f.objets
97 o.A = Point(e, 3)
98 o.M = Point()
99 o.M.label('{1/ln(A.x) + A.y}', mode='formule')
100
101 # Détails d'implémentation (peut être modifié par la suite)
102 self.assertEqual(o.M.mode_affichage, 'formule')
103 self.assertEqual(o.M.etiquette.texte, '{1/ln(A.x)+A.y}')
104 self.assertIsInstance(o.M.etiquette.formule, Formule)
105
106 # Par contre, ceci doit rester valable quelle que soit l'implémentation !
107 self.assertAlmostEqual(float(o.M.label()), 4.)
108 o.A.x = e**2
109 with contexte(separateur_decimal='.'):
110 self.assertEqual(o.M.label(), '3.5')
111 with contexte(separateur_decimal=','):
112 self.assertEqual(o.M.label(), '3,5')
113
114
115 def test_constantes(self):
116 f = Feuille()
117 self.assertAlmostEqual(f.objets.pi, 3.1415926535897931)
118 self.assertAlmostEqual(f.objets.e, 2.7182818284590451)
119
120 def test_modification_variable(self):
121 f = Feuille()
122 o = f.objets
123 o.a = Variable(1)
124 o.fa = "5*sin(4/(a+.5))-1.5"
125 o.A = Point(o.a, o.fa)
126 o.fa = "-5*sin(4/(a+.5))+5.5"
127 o.A = Point(2, o.fa)
128 self.assertAlmostEqual(o.A.x, 2)
129 self.assertAlmostEqual(o.A.y, o.fa)
130
131 def test_fenetre(self):
132 f = Feuille()
133 f.fenetre = -4, 8, -2, 10
134 self.assertEqual(f.objets.xmin, -4)
135 self.assertEqual(f.objets.xmax, 8)
136 self.assertEqual(f.objets.ymin, -2)
137 self.assertEqual(f.objets.ymax, 10)
138 # Bornes inversées
139 f.fenetre = 7, -8, 3, -1
140 self.assertEqual(f.objets.xmin, -8)
141 self.assertEqual(f.objets.xmax, 7)
142 self.assertEqual(f.objets.ymin, -1)
143 self.assertEqual(f.objets.ymax, 3)
144 # Accès isolé à une borne
145 f.objets.xmin += 2
146 self.assertEqual(f.objets.xmin, -6)
147 self.assertEqual(f.fenetre, (-6, 7, -1, 3))
148 # Objet dépendant d'une borne
149 f.objets.A = Point("xmin - 1", "ymax + 1")
150 self.assertEqual(f.objets.A.xy, (-7, 4))
151 f.objets.xmin = 3
152 f.objets.ymax = f.objets.ymin + 10
153 self.assertEqual(f.objets.A.xy, (2, 10))
154
155
156 def test_dependances(self):
157 f = Feuille()
158 A = f.objets.A = Point(1, 2)
159 B = f.objets.B = Point("A.x+1", "A.y-1")
160 self.assertEqual(B.xy, (2, 1))
161 f.objets.A = (8, 9)
162 self.assertEqual(B.xy, (9, 8))
163 f.objets.B = ("A.x+1", "A.y-1")
164 self.assertEqual( B.xy, (9, 8))
165 f.objets.A = Point(0, 2)
166 self.assertEqual(B.xy, (1, 1))
167 f.objets.B = Point("A.x+1", "A.y-1")
168 self.assertIs(f.objets.B, B)
169 self.assertIs(f.objets.A, A)
170 self.assertEqual(B.xy, (1, 1))
171 f.objets.A = (5, 7)
172 self.assertEqual(B.xy, (6, 6)) # FAIL
173
174
175 def test_is_equation(self):
176 self.assertTrue(is_equation("2*x+3*y=5"))
177 self.assertTrue(is_equation("x=5"))
178 self.assertTrue(is_equation("y=a*x**2+c*x-2*y"))
179 self.assertTrue(is_equation("y*(x-2)=3"))
180
181 # Affectations
182 self.assertFalse(is_equation("a = 5"))
183 self.assertFalse(is_equation("a += 3"))
184 self.assertFalse(is_equation("B.x = 5"))
185 self.assertFalse(is_equation("B.y -= 3"))
186 self.assertFalse(is_equation("B.style(x=2)"))
187
188
189 def test_parse_equation(self):
190 d = dict(globals())
191 s = parse_equation("x=2*y-3")
192 exec(s, d)
193 self.assertEqual(d['_'].equation, (1, -2, 3))
194 s = parse_equation("(x-2)**2+(y-3)**2=49")
195 exec(s, d)
196 self.assertEqual(d['_'].rayon, 7)
197 self.assertEqual(d['_'].centre.xy, (2, 3))
198 s = parse_equation("y=ln(x)-1")
199 exec(s, d)
200 self.assertIsInstance(d['_'], Courbe)
201