mailr9288 - /1.3/test_suite/unit_tests/test_float.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on August 12, 2009 - 18:57:
Author: bugman
Date: Wed Aug 12 18:57:21 2009
New Revision: 9288

URL: http://svn.gna.org/viewcvs/relax?rev=9288&view=rev
Log:
Massive clean up of the float module unit tests.

As a general clean up, the module has been converted to the relax coding 
conventions.  Importantly
the floating point numbers defined as constants for the tests are now 
properly defined using the
packBytesAsPyFloat() function.  This will allow these tests to pass on all 
machines!


Modified:
    1.3/test_suite/unit_tests/test_float.py

Modified: 1.3/test_suite/unit_tests/test_float.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/test_suite/unit_tests/test_float.py?rev=9288&r1=9287&r2=9288&view=diff
==============================================================================
--- 1.3/test_suite/unit_tests/test_float.py (original)
+++ 1.3/test_suite/unit_tests/test_float.py Wed Aug 12 18:57:21 2009
@@ -1,6 +1,7 @@
 
###############################################################################
 #                                                                            
 #
 # Copyright (C) 2006 Gary Thompson                                           
 #
+# Copyright (C) 2009 Edward d'Auvergne                                       
 #
 #                                                                            
 #
 # This file is part of the program relax.                                    
 #
 #                                                                            
 #
@@ -20,75 +21,104 @@
 #                                                                            
 #
 
###############################################################################
 
-import unittest
-from float  import *
+# Python module imports.
+from unittest import TestCase
+from float import *
 from copy import copy
 
-FLOAT_EPSILON=float(4.94065645841247e-324) # replace a a later date
-FLOAT_NORMAL = float(1e6)
-ZERO = float(+0.0)
-NEG_ZERO = -ZERO
-NEG_FLOAT_EPSILON = -FLOAT_EPSILON
-NEG_FLOAT_NORMAL =  -FLOAT_NORMAL
+
+# Some constants for the tests.
+FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 0])
+NEG_FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 128])
+FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 65])
+NEG_FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 193])
+ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 0])
+NEG_ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 128])
 
 
-def makeDictById(elements):
-    result ={}
+def make_dict_by_id(elements):
+    """Convert the list into a dictionary of pointer:value pairs."""
+
+    # Convert.
+    result = {}
     for element in elements:
-        result[id(element)]=element
+        result[id(element)] = element
 
+    # Return the dictionary.
     return result
 
 
-def winnowDictToListById(dict,elements):
+def winnow_dist_to_list_by_id(dict, exclude):
+    """Generate a list of values in dict excluding the values given."""
+
+    # Generate a new dictionary with the excluded values missing.
     resultDict = copy(dict)
+    for val in exclude:
+        del(resultDict[id(val)])
 
-    for element in elements:
-        del(resultDict[id(element)])
-
+    # Return as a list.
     return resultDict.values()
 
-class Test_float(unittest.TestCase):
-    
-    tests = makeDictById([pos_inf, neg_inf, FLOAT_NORMAL, 
-                          NEG_FLOAT_NORMAL, FLOAT_EPSILON, 
-                          NEG_FLOAT_EPSILON, nan, ZERO, NEG_ZERO])
+
+class Test_float(TestCase):
+    """Unit tests for the functions of the 'float' module."""
+
+    # A dictionary of all numerical types (the key is the memory address, 
i.e. this is like a pointer).
+    tests = make_dict_by_id([pos_inf, neg_inf, FLOAT_NORMAL, 
NEG_FLOAT_NORMAL, FLOAT_EPSILON, NEG_FLOAT_EPSILON, nan, ZERO, NEG_ZERO])
+
+    def do_test_sets(self, function, true_class=[], false_class=[]):
+        """Method for checking all the values against the given function."""
+
+        # The numbers that should return true.
+        for val in true_class:
+            self.assertEqual(function(val), True)
+
+        # The numbers that should return false.
+        for val in false_class:
+            self.assertEqual(function(val), False)
+
 
     def test_getFloatClass(self):
+        """Test the float.getFloatClass() function."""
 
-        tests = ( CLASS_POS_INF, pos_inf,
-                  CLASS_NEG_INF, neg_inf,
-                  CLASS_POS_NORMAL, FLOAT_NORMAL,
-                  CLASS_NEG_NORMAL, -FLOAT_NORMAL,
-                  CLASS_POS_DENORMAL,  FLOAT_EPSILON,
-                  CLASS_NEG_DENORMAL,  -FLOAT_EPSILON,
-                  CLASS_QUIET_NAN,     nan,
-                  # WE DON'T USE SIGNAL NANS CLASS_SIGNAL_NAN,
-                  CLASS_POS_ZERO,    ZERO,
-                  CLASS_NEG_ZERO,    -ZERO)
+        tests = (CLASS_POS_INF,        pos_inf,
+                 CLASS_NEG_INF,        neg_inf,
+                 CLASS_POS_NORMAL,     FLOAT_NORMAL,
+                 CLASS_NEG_NORMAL,     -FLOAT_NORMAL,
+                 CLASS_POS_DENORMAL,   FLOAT_EPSILON,
+                 CLASS_NEG_DENORMAL,   -FLOAT_EPSILON,
+                 CLASS_QUIET_NAN,      nan,
+                 # WE DON'T USE SIGNAL NANS CLASS_SIGNAL_NAN,
+                 CLASS_POS_ZERO,       ZERO,
+                 CLASS_NEG_ZERO,       -ZERO
+        )
 
-        i=iter(tests)
-        for (fpClass, value) in zip(i,i):
+        i = iter(tests)
+        for (fpClass, value) in zip(i, i):
             self.assertEqual(fpClass, getFloatClass(value))
 
 
-    def test_isZero(self):
-        positives = (ZERO,NEG_ZERO)
-        negatives= winnowDictToListById(self.tests,positives)
+    def test_isPositive(self):
+        """Test the float.isZero() function."""
 
-        self.doTestSets(isZero,positives=positives, negatives=negatives)
+        # Negative values.
+        negatives = (neg_inf, NEG_FLOAT_NORMAL, NEG_FLOAT_EPSILON, NEG_ZERO)
+
+        # Positive values.
+        positives = winnow_dist_to_list_by_id(self.tests, negatives)
+
+        # Run the tests.
+        self.do_test_sets(isPositive, true_class=positives, 
false_class=negatives)
 
 
-    def test_isPositive(self):
-        negatives = (neg_inf, NEG_FLOAT_NORMAL,NEG_FLOAT_EPSILON, NEG_ZERO)
-        positives= winnowDictToListById(self.tests,negatives)
+    def test_isZero(self):
+        """Test the float.isZero() function."""
 
-        self.doTestSets(isPositive,positives=positives, negatives=negatives)
+        # The zeros.
+        zeros = (ZERO, NEG_ZERO)
 
-    #todo add reporting of failed number class...
-    def doTestSets(self,function,positives=[],negatives=[]):
-        for positive in positives:
-            self.assertEqual(function(positive),True)
+        # All other numbers.
+        non_zeros = winnow_dist_to_list_by_id(self.tests, zeros)
 
-        for negative in negatives:
-            self.assertEqual(function(negative),False)
+        # Run the tests.
+        self.do_test_sets(isZero, true_class=zeros, false_class=non_zeros)




Related Messages


Powered by MHonArc, Updated Wed Aug 12 20:00:04 2009