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

Source Code for Module test_suite.unit_tests._lib.test_float

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