Package test_suite :: Package unit_tests :: Module test_float
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests.test_float

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2006 Gary Thompson                                            # 
  4  # Copyright (C) 2009 Edward d'Auvergne                                        # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Python module imports. 
 25  from unittest import TestCase 
 26  from float import * 
 27  from copy import copy 
 28   
 29   
 30  # Some constants for the tests. 
 31  FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 0]) 
 32  NEG_FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 128]) 
 33  FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 65]) 
 34  NEG_FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 193]) 
 35  ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 0]) 
 36  NEG_ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 128]) 
 37   
 38   
39 -def make_dict_by_id(elements):
40 """Convert the list into a dictionary of pointer:value pairs.""" 41 42 # Convert. 43 result = {} 44 for element in elements: 45 result[id(element)] = element 46 47 # Return the dictionary. 48 return result
49 50
51 -def winnow_dist_to_list_by_id(dict, exclude):
52 """Generate a list of values in dict excluding the values given.""" 53 54 # Generate a new dictionary with the excluded values missing. 55 resultDict = copy(dict) 56 for val in exclude: 57 del(resultDict[id(val)]) 58 59 # Return as a list. 60 return list(resultDict.values())
61 62
63 -class Test_float(TestCase):
64 """Unit tests for the functions of the 'float' module.""" 65 66 # A dictionary of all numerical types (the key is the memory address, i.e. this is like a pointer). 67 tests = make_dict_by_id([pos_inf, neg_inf, FLOAT_NORMAL, NEG_FLOAT_NORMAL, FLOAT_EPSILON, NEG_FLOAT_EPSILON, nan, ZERO, NEG_ZERO]) 68
69 - def do_test_sets(self, function, true_class=[], false_class=[]):
70 """Method for checking all the values against the given function.""" 71 72 # The numbers that should return true. 73 for val in true_class: 74 self.assertEqual(function(val), True) 75 76 # The numbers that should return false. 77 for val in false_class: 78 self.assertEqual(function(val), False)
79 80
81 - def test_getFloatClass(self):
82 """Test the float.getFloatClass() function.""" 83 84 tests = (CLASS_POS_INF, pos_inf, 85 CLASS_NEG_INF, neg_inf, 86 CLASS_POS_NORMAL, FLOAT_NORMAL, 87 CLASS_NEG_NORMAL, -FLOAT_NORMAL, 88 CLASS_POS_DENORMAL, FLOAT_EPSILON, 89 CLASS_NEG_DENORMAL, -FLOAT_EPSILON, 90 CLASS_QUIET_NAN, nan, 91 # WE DON'T USE SIGNAL NANS CLASS_SIGNAL_NAN, 92 CLASS_POS_ZERO, ZERO, 93 CLASS_NEG_ZERO, -ZERO 94 ) 95 96 i = iter(tests) 97 for (fpClass, value) in zip(i, i): 98 self.assertEqual(fpClass, getFloatClass(value))
99 100
101 - def test_isPositive(self):
102 """Test the float.isZero() function.""" 103 104 # Negative values. 105 negatives = (neg_inf, NEG_FLOAT_NORMAL, NEG_FLOAT_EPSILON, NEG_ZERO) 106 107 # Positive values. 108 positives = winnow_dist_to_list_by_id(self.tests, negatives) 109 110 # Run the tests. 111 self.do_test_sets(isPositive, true_class=positives, false_class=negatives)
112 113
114 - def test_isZero(self):
115 """Test the float.isZero() function.""" 116 117 # The zeros. 118 zeros = (ZERO, NEG_ZERO) 119 120 # All other numbers. 121 non_zeros = winnow_dist_to_list_by_id(self.tests, zeros) 122 123 # Run the tests. 124 self.do_test_sets(isZero, true_class=zeros, false_class=non_zeros)
125