From: Jean-Philippe ARGAUD Date: Mon, 30 Jan 2017 12:36:27 +0000 (+0100) Subject: Updating tests to raise exceptions on differences X-Git-Tag: V8_3_0rc1~32 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d62722f63b1760d3b5419ed440aeb46d8656d88b;p=modules%2Fadao.git Updating tests to raise exceptions on differences --- diff --git a/test/test6701/Doc_TUI_Exemple_01.py b/test/test6701/Doc_TUI_Exemple_01.py index c94ad92..f2a7f91 100644 --- a/test/test6701/Doc_TUI_Exemple_01.py +++ b/test/test6701/Doc_TUI_Exemple_01.py @@ -21,9 +21,11 @@ # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D "Verification d'un exemple de la documentation" +from utExtend import assertAlmostEqualArrays + # ============================================================================== def test1(): - """Exemple""" + """Test""" from numpy import array, matrix import adaoBuilder case = adaoBuilder.New() @@ -36,7 +38,7 @@ def test1(): case.set( 'Observer', Variable="Analysis", Template="ValuePrinter" ) case.execute() # - return 0 + return case.get("Analysis")[-1] # ============================================================================== if __name__ == "__main__": @@ -46,4 +48,5 @@ if __name__ == "__main__": Un exemple simple de creation d'un cas de calcul TUI ADAO +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """ - test1() + xa = test1() + assertAlmostEqualArrays(xa, [0.25, 0.80, 0.95], places = 5) diff --git a/test/test6701/utExtend.py b/test/test6701/utExtend.py new file mode 100644 index 0000000..ec9aba5 --- /dev/null +++ b/test/test6701/utExtend.py @@ -0,0 +1,42 @@ +#-*-coding:iso-8859-1-*- +# +# Copyright (C) 2008-2017 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D + +""" + Unittest extensions for Numpy objects +""" +__author__ = "Jean-Philippe ARGAUD" +__all__ = ["assertAlmostEqualVector"] + +import numpy + +# ============================================================================== +def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None): + "Compare two vectors, like unittest.assertAlmostEqual" + if msg is not None: + print msg + if delta is not None: + if ( (numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any(): + raise AssertionError("%s != %s within %s places"%(first,second,delta)) + else: + if ( (numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any(): + raise AssertionError("%s != %s within %i places"%(first,second,places)) + return max(abs(numpy.asarray(first) - numpy.asarray(second))) diff --git a/test/test6702/Doc_TUI_Exemple_02.py b/test/test6702/Doc_TUI_Exemple_02.py index 8cca45e..19c2a2a 100644 --- a/test/test6702/Doc_TUI_Exemple_02.py +++ b/test/test6702/Doc_TUI_Exemple_02.py @@ -21,9 +21,11 @@ # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D "Verification d'un exemple de la documentation" +from utExtend import assertAlmostEqualArrays + # ============================================================================== def test1(): - """Exemple""" + """Test""" from numpy import array, matrix import adaoBuilder case = adaoBuilder.New() @@ -39,7 +41,7 @@ def test1(): return case.get("Analysis")[-1] def test2(): - """Exemple""" + """Test""" from numpy import array, matrix import adaoBuilder case = adaoBuilder.New() @@ -63,11 +65,6 @@ def test2(): # return case.get("Analysis")[-1] -def almost_equal_vectors(v1, v2, precision = 1.e-15, msg = ""): - """Comparaison de deux vecteurs""" - print " Difference maximale %s: %.2e"%(msg, max(abs(v2 - v1))) - return max(abs(v2 - v1)) < precision - # ============================================================================== if __name__ == "__main__": print '\n AUTODIAGNOSTIC \n' @@ -79,4 +76,5 @@ if __name__ == "__main__": """ xa1 = test1() xa2 = test2() - assert almost_equal_vectors( xa1, xa2, msg = "entre les deux" ) + ecart = assertAlmostEqualArrays(xa1, xa2, places = 15) + print " Difference maximale entre les deux : %.2e"%ecart diff --git a/test/test6702/utExtend.py b/test/test6702/utExtend.py new file mode 100644 index 0000000..ec9aba5 --- /dev/null +++ b/test/test6702/utExtend.py @@ -0,0 +1,42 @@ +#-*-coding:iso-8859-1-*- +# +# Copyright (C) 2008-2017 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D + +""" + Unittest extensions for Numpy objects +""" +__author__ = "Jean-Philippe ARGAUD" +__all__ = ["assertAlmostEqualVector"] + +import numpy + +# ============================================================================== +def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None): + "Compare two vectors, like unittest.assertAlmostEqual" + if msg is not None: + print msg + if delta is not None: + if ( (numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any(): + raise AssertionError("%s != %s within %s places"%(first,second,delta)) + else: + if ( (numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any(): + raise AssertionError("%s != %s within %i places"%(first,second,places)) + return max(abs(numpy.asarray(first) - numpy.asarray(second))) diff --git a/test/test6703/Doc_TUI_Exemple_03.py b/test/test6703/Doc_TUI_Exemple_03.py index 29e3fe9..44d4af6 100644 --- a/test/test6703/Doc_TUI_Exemple_03.py +++ b/test/test6703/Doc_TUI_Exemple_03.py @@ -21,6 +21,8 @@ # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D "Verification d'un exemple de la documentation" +from utExtend import assertAlmostEqualArrays + # ============================================================================== # # Construction artificielle d'un exemple de donnees utilisateur @@ -46,7 +48,7 @@ observations = simulation((2, 3, 4)) # ============================================================================== def test1(): - "Exemple" + "Test" import numpy import adaoBuilder # @@ -99,7 +101,7 @@ def test1(): print "Simulation at optimal state.....:",numpy.ravel(FX_at_optimum) print # - return 0 + return case.get("Analysis")[-1] # ============================================================================== if __name__ == "__main__": @@ -109,4 +111,5 @@ if __name__ == "__main__": Exploitation independante des resultats d'un cas de calcul ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """ - test1() + xa = test1() + assertAlmostEqualArrays(xa, [ 2., 3., 4.]) diff --git a/test/test6703/utExtend.py b/test/test6703/utExtend.py new file mode 100644 index 0000000..ec9aba5 --- /dev/null +++ b/test/test6703/utExtend.py @@ -0,0 +1,42 @@ +#-*-coding:iso-8859-1-*- +# +# Copyright (C) 2008-2017 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D + +""" + Unittest extensions for Numpy objects +""" +__author__ = "Jean-Philippe ARGAUD" +__all__ = ["assertAlmostEqualVector"] + +import numpy + +# ============================================================================== +def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None): + "Compare two vectors, like unittest.assertAlmostEqual" + if msg is not None: + print msg + if delta is not None: + if ( (numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any(): + raise AssertionError("%s != %s within %s places"%(first,second,delta)) + else: + if ( (numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any(): + raise AssertionError("%s != %s within %i places"%(first,second,places)) + return max(abs(numpy.asarray(first) - numpy.asarray(second)))