Salome HOME
Updating tests to raise exceptions on differences
authorJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Mon, 30 Jan 2017 12:36:27 +0000 (13:36 +0100)
committerJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Mon, 30 Jan 2017 12:36:27 +0000 (13:36 +0100)
test/test6701/Doc_TUI_Exemple_01.py
test/test6701/utExtend.py [new file with mode: 0644]
test/test6702/Doc_TUI_Exemple_02.py
test/test6702/utExtend.py [new file with mode: 0644]
test/test6703/Doc_TUI_Exemple_03.py
test/test6703/utExtend.py [new file with mode: 0644]

index c94ad92c80ffe884230988dc302d9fc6c22450ca..f2a7f91cd24a70149de8a9f7c4308508d8a7edd2 100644 (file)
 # 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 (file)
index 0000000..ec9aba5
--- /dev/null
@@ -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)))
index 8cca45ea729cd6562c613f64a3845326656c8f21..19c2a2a562dc0c615b701d9d0b98f381b2436a8b 100644 (file)
 # 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 (file)
index 0000000..ec9aba5
--- /dev/null
@@ -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)))
index 29e3fe905576f77a8ae12b30b552f185f608357a..44d4af6bfbd4df3cc03c56ab0031b8fe1424826a 100644 (file)
@@ -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 (file)
index 0000000..ec9aba5
--- /dev/null
@@ -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)))