@@ -50,6 +50,20 @@ def isfloat(s):
5050 return False
5151
5252
53+ def compare_numeric (file1 , file2 ):
54+ """Assert that two files have (approximately) the same numerical
55+ values."""
56+ for f1_row , f2_row in zip (file1 , file2 ):
57+ f1_arr = f1_row .split ()
58+ f2_arr = f2_row .split ()
59+ assert len (f1_arr ) == len (f2_arr )
60+ for idx , _ in enumerate (f1_arr ):
61+ if isfloat (f1_arr [idx ]) and isfloat (f2_arr [idx ]):
62+ assert np .isclose (float (f1_arr [idx ]), float (f2_arr [idx ]))
63+ else :
64+ assert f1_arr [idx ] == f2_arr [idx ]
65+
66+
5367class TestApp :
5468 @pytest .fixture
5569 def setup (self ):
@@ -108,7 +122,7 @@ def test_morph_outputs(self, setup, tmp_path):
108122 with open (test_saving_succinct .joinpath (file )) as tf :
109123 generated = filter (ignore_path , gf )
110124 target = filter (ignore_path , tf )
111- assert all ( x == y for x , y in zip ( generated , target ) )
125+ compare_numeric ( generated , target )
112126
113127 # Save multiple verbose morphs
114128 tmp_verbose = tmp_path .joinpath ("verbose" )
@@ -149,7 +163,7 @@ def test_morph_outputs(self, setup, tmp_path):
149163 with open (test_saving_verbose .joinpath (file )) as tf :
150164 generated = filter (ignore_path , gf )
151165 target = filter (ignore_path , tf )
152- assert all ( x == y for x , y in zip ( generated , target ) )
166+ compare_numeric ( generated , target )
153167
154168 def test_morphsqueeze_outputs (self , setup , tmp_path ):
155169 # The file squeeze_morph has a squeeze and stretch applied
@@ -184,17 +198,7 @@ def test_morphsqueeze_outputs(self, setup, tmp_path):
184198 with open (target_file ) as tf :
185199 morphed = filter (ignore_path , mf )
186200 target = filter (ignore_path , tf )
187- for m , t in zip (morphed , target ):
188- m_row = m .split ()
189- t_row = t .split ()
190- assert len (m_row ) == len (t_row )
191- for idx , _ in enumerate (m_row ):
192- if isfloat (m_row [idx ]) and isfloat (t_row [idx ]):
193- assert np .isclose (
194- float (m_row [idx ]), float (t_row [idx ])
195- )
196- else :
197- assert m_row [idx ] == t_row [idx ]
201+ compare_numeric (morphed , target )
198202
199203 def test_morphfuncy_outputs (self , tmp_path ):
200204 def quadratic (x , y , a0 , a1 , a2 ):
@@ -217,14 +221,4 @@ def quadratic(x, y, a0, a1, a2):
217221 with open (tmp_path .joinpath ("funcy_target.cgr" )) as gf :
218222 generated = filter (ignore_path , gf )
219223 target = filter (ignore_path , tf )
220- for m , t in zip (generated , target ):
221- m_row = m .split ()
222- t_row = t .split ()
223- assert len (m_row ) == len (t_row )
224- for idx , _ in enumerate (m_row ):
225- if isfloat (m_row [idx ]) and isfloat (t_row [idx ]):
226- assert np .isclose (
227- float (m_row [idx ]), float (t_row [idx ])
228- )
229- else :
230- assert m_row [idx ] == t_row [idx ]
224+ compare_numeric (generated , target )
0 commit comments