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-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program 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 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports. 
 24  from unittest import TestCase 
 25  from float import * 
 26  from copy import copy 
 27   
 28   
 29  # Some constants for the tests. 
 30  FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 0]) 
 31  NEG_FLOAT_EPSILON = packBytesAsPyFloat([1, 0, 0, 0, 0, 0, 0, 128]) 
 32  FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 65]) 
 33  NEG_FLOAT_NORMAL = packBytesAsPyFloat([0, 0, 0, 0, 128, 132, 46, 193]) 
 34  ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 0]) 
 35  NEG_ZERO = packBytesAsPyFloat([0, 0, 0, 0, 0, 0, 0, 128]) 
 36   
 37   
38 -def make_dict_by_id(elements):
39 """Convert the list into a dictionary of pointer:value pairs.""" 40 41 # Convert. 42 result = {} 43 for element in elements: 44 result[id(element)] = element 45 46 # Return the dictionary. 47 return result
48 49
50 -def winnow_dist_to_list_by_id(dict, exclude):
51 """Generate a list of values in dict excluding the values given.""" 52 53 # Generate a new dictionary with the excluded values missing. 54 resultDict = copy(dict) 55 for val in exclude: 56 del(resultDict[id(val)]) 57 58 # Return as a list. 59 return list(resultDict.values())
60 61
62 -class Test_float(TestCase):
63 """Unit tests for the functions of the 'float' module.""" 64 65 # A dictionary of all numerical types (the key is the memory address, i.e. this is like a pointer). 66 num_types = make_dict_by_id([pos_inf, neg_inf, FLOAT_NORMAL, NEG_FLOAT_NORMAL, FLOAT_EPSILON, NEG_FLOAT_EPSILON, nan, ZERO, NEG_ZERO]) 67
68 - def do_test_sets(self, function, true_class=[], false_class=[]):
69 """Method for checking all the values against the given function.""" 70 71 # The numbers that should return true. 72 for val in true_class: 73 self.assertEqual(function(val), True) 74 75 # The numbers that should return false. 76 for val in false_class: 77 self.assertEqual(function(val), False)
78 79
80 - def test_getFloatClass(self):
81 """Test the float.getFloatClass() function.""" 82 83 tests = (CLASS_POS_INF, pos_inf, 84 CLASS_NEG_INF, neg_inf, 85 CLASS_POS_NORMAL, FLOAT_NORMAL, 86 CLASS_NEG_NORMAL, -FLOAT_NORMAL, 87 CLASS_POS_DENORMAL, FLOAT_EPSILON, 88 CLASS_NEG_DENORMAL, -FLOAT_EPSILON, 89 CLASS_QUIET_NAN, nan, 90 # WE DON'T USE SIGNAL NANS CLASS_SIGNAL_NAN, 91 CLASS_POS_ZERO, ZERO, 92 CLASS_NEG_ZERO, -ZERO 93 ) 94 95 i = iter(tests) 96 for (fpClass, value) in zip(i, i): 97 self.assertEqual(fpClass, getFloatClass(value))
98 99
100 - def test_isPositive(self):
101 """Test the float.isZero() function.""" 102 103 # Negative values. 104 negatives = (neg_inf, NEG_FLOAT_NORMAL, NEG_FLOAT_EPSILON, NEG_ZERO) 105 106 # Positive values. 107 positives = winnow_dist_to_list_by_id(self.num_types, negatives) 108 109 # Run the tests. 110 self.do_test_sets(isPositive, true_class=positives, false_class=negatives)
111 112
113 - def test_isZero(self):
114 """Test the float.isZero() function.""" 115 116 # The zeros. 117 zeros = (ZERO, NEG_ZERO) 118 119 # All other numbers. 120 non_zeros = winnow_dist_to_list_by_id(self.num_types, zeros) 121 122 # Run the tests. 123 self.do_test_sets(isZero, true_class=zeros, false_class=non_zeros)
124